using FHT.Agency.CommonTypes; using FHT.Agency.Queries.ImplementedInterfaces; using FHT.Agency.UI.ViewModels; using FHT.Agency.UI.ViewModels.Agent; using FHT.Agency.Web.Extensions; using FHT.Agency.Web.Filters; using FHT.Agency.Web.Utility; using FHT.ApiCommon; using Microsoft.AspNet.Identity; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web.Http; using System.Web.Http.Description; using System; using FHT.DDD; using FHT.Agency.UI.InputModels; using FHT.Agency.Services.ImplementedInterfaces; using FHT.Agency.UI.InputModels.Agent; using FHT.Core.UI.ViewModels; using System.Web.Http.Cors; namespace FHT.Agency.Web.ApiControllers { [Authorize] [RoutePrefix("api/agencyorganizations")] [EnableCors(origins: "*", headers: "*", methods: "*")] public class AgencyOrganizationApiController : ApiController { private readonly IAgentQuery _agentQuery; private readonly IUserQuery _userQuery; private readonly ICreateAgentService _createAgentService; public AgencyOrganizationApiController(IAgentQuery agentQuery, IUserQuery userQuery, ICreateAgentService createAgentService) { _agentQuery = agentQuery; _userQuery = userQuery; _createAgentService = createAgentService; } #region 查询 [HttpGet] [Route("_autocomplete")] [ResponseType(typeof(List))] public async Task AutoCompleteAsync(string q = "", int? id = null) { var res = new List(); if (id.HasValue) { AgentOrganizationViewModel org =await _agentQuery.GetAgentOrganizationAsync(id.Value); res.Add(new SelectListItemViewModel { Text = org.Name, Value = org.Id }); } else { List orgs = await _agentQuery.SearchAgentOrganizationAsync(q); res = orgs.Select(c => new SelectListItemViewModel { Text = c.Name, Value = c.Id }).ToList(); } return Ok(res); } [HttpGet] [ResponseType(typeof(AgentOrganizationInputModel))] [Route("_getAgentOrganizationModel")] public IHttpActionResult GetAgentOrganization(int organizationId) { AgentOrganizationInputModel data = _createAgentService.GetAgentOrganization(organizationId); return Ok(data); } [HttpGet] [ResponseType(typeof(AgentOrganizationAuditViewModel))] [Route("_getAuditAgentOrganizationByOrderId")] public async Task GetAuditAgentOrganizationByOrderIdAsync(long orderId) { AgentOrganizationAuditViewModel data = await _agentQuery.GetAgentOrganizationAuditAsync(orderId); return Ok(data); } [HttpGet] [ResponseType(typeof(AgentOrganizationViewModel))] [Route("{id:int:min(1)}")] public async Task GetAgentOrganizationAsync(int id) { AgentOrganizationViewModel data = await _agentQuery.GetAgentOrganizationAsync(id); return Ok(data); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageAgentOrganization)] [ResponseType(typeof(QueryResult))] [Route("_search")] public async Task SearchAsync(int? agentOrganizationId = null, int? channelUserId = null, AgentStatus? agentStatus = null, DateTime? beginTime = null, DateTime? endTime = null, int currPage = 1, int limit = 20) { int authenticatedUserId = User.Identity.GetUserId(); int offset = (currPage < 2 ? 0 : currPage - 1) * limit; List allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); if (channelUserId.HasValue && allChannelUsers.Count(u => u.UserId == channelUserId.Value) == 0)//非法请求 { throw new InvalidOperationException("你无权操作!"); } List channelUsers = ChannelUserHelper.ChannelUser(allChannelUsers, User.UserRole(), channelUserId); QueryResult agentOrganizations = await _agentQuery.GetAgentOrganizationsAsync(authenticatedUserId, agentOrganizationId, channelUserId, agentStatus, beginTime, endTime, offset, limit); return Ok(agentOrganizations); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.AuditAgentOrganizations)] [ResponseType(typeof(QueryResult))] [Route("_searchForAudit")] public async Task SearchAgentOrganizationForAuditAsync(int? agentOrganizationId = null, int? channelUserId = null, AgentStatus? agentStatus = null, DateTime? beginTime = null, DateTime? endTime = null, int currPage = 1, int limit = 20) { int authenticatedUserId = User.Identity.GetUserId(); int offset = (currPage < 2 ? 0 : currPage - 1) * limit; List allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); if (channelUserId.HasValue && allChannelUsers.Count(u => u.UserId == channelUserId.Value) == 0) { throw new InvalidOperationException("你无权操作!"); } List channelUsers = ChannelUserHelper.ChannelUser(allChannelUsers, User.UserRole(), channelUserId); QueryResult result = await _agentQuery.GetAgentOrganizationsAsync(authenticatedUserId, agentOrganizationId, channelUserId, agentStatus, beginTime, endTime, offset, limit); return Ok(result); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageAgentOrganization, FhtUserClaimType.AuditAgentOrganizations)] [ResponseType(typeof(AuditAgentOrganizationInputModel))] [Route("_getAgentOrganizationAuditModel")] public async Task GetAgentOrganizationAuditModelAsync(int agentOrganizationId) { AgentOrganizationViewModel agentOrganization = await _agentQuery.GetAgentOrganizationAsync(agentOrganizationId); var model = new AuditAgentOrganizationInputModel() { AgentStatus = agentOrganization.Status, ProtocolNo = agentOrganization.ProtocolNo, EffectiveDate = agentOrganization.EffectiveDate, ExpiryDate = agentOrganization.ExpiryDate, Id = agentOrganization.Id }; return Ok(model); } /// /// 分配代理商 /// [HttpGet] [ClaimsAuthorize(FhtUserClaimType.AllotAgentOrganizations)] [ResponseType(typeof(QueryResult))] [Route("_searchForAllot")] public async Task SearchOrganizationsForAllotAsync(int? agentOrganizationId = null, int? channelUserId = null, AgentStatus? agentStatus = null, DateTime? beginTime = null, DateTime? endTime = null, int currPage = 1, int limit = 20) { int authenticatedUserId = User.Identity.GetUserId(); int offset = (currPage < 2 ? 0 : currPage - 1) * limit; List allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); if (channelUserId.HasValue && allChannelUsers.Count(u => u.UserId == channelUserId.Value) == 0)//非法请求 { throw new InvalidOperationException("你无权操作!"); } QueryResult agentOrganizations = await _agentQuery.GetAgentOrganizationsAsync(authenticatedUserId, agentOrganizationId, channelUserId, agentStatus, beginTime, endTime, offset, limit); return Ok(agentOrganizations); } [HttpGet] [ResponseType(typeof(List))] [Route("_getPassAgentOrganizations")] public async Task GetPassAgentOrganizationsAsync() { int authenticatedUserId = User.Identity.GetUserId(); List agentOrganizations = await _agentQuery.GetPassAgentOrganizationsAsync(authenticatedUserId); return Ok(agentOrganizations); } [HttpGet] [ResponseType(typeof(List))] [Route("_getSimpleAgentOrganizations")] public async Task GetSimpleAgentOrganizationsAsync() { int authenticatedUserId = User.Identity.GetUserId(); List agentOrganizations = await _agentQuery.GetSimpleAgentOrganizationsAsync(authenticatedUserId); return Ok(agentOrganizations); } [HttpGet] [ResponseType(typeof(List))] [Route("_getAllAgentOrganizations")] public async Task GetAllAgentOrganizationsAsync() { int authenticatedUserId = User.Identity.GetUserId(); List res = await _agentQuery.GetAllAgentOrganizationsAsync(); return Ok(res); } #endregion #region 创建、分配 /// /// 创建代理商 /// /// /// [HttpPost] [ValidateModel] [ClaimsAuthorize(FhtUserClaimType.CreateAgentOrganization)] [Route("_create", Name = nameof(CreateAgentOrganization))] [ResponseType(typeof(CreatedViewModel))] public IHttpActionResult CreateAgentOrganization(AgentOrganizationInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); int organizationId = _createAgentService.CreateAgentOrganization(authenticatedUserId, model); string url = Url.Link(nameof(CreateAgentOrganization), new { organizationId }); return Created(url, new CreatedViewModel { Id = organizationId, Location = url }); } /// /// 创建代理商 /// /// /// [HttpPost] [ValidateModel] [Route("_update")] [ResponseType(typeof(void))] [ClaimsAuthorize(FhtUserClaimType.CreateAgentOrganization)] public IHttpActionResult UpdateAgentOrganization(AgentOrganizationInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); _createAgentService.UpdateAgentOrganization(model); return Ok(); } /// /// 审核 /// /// /// [HttpPost] [Route("_audit")] [ValidateModel] [ResponseType(typeof(void))] [ClaimsAuthorize(FhtUserClaimType.AuditAgentOrganizations)] public IHttpActionResult OrganizationAudit(AuditAgentOrganizationInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); _createAgentService.AuditAgentOrganization(authenticatedUserId, model); return Ok(); } /// /// 分配 /// /// [HttpPost] [ClaimsAuthorize(FhtUserClaimType.AllotAgentOrganizations)] [Route("_allot")] [ResponseType(typeof(void))] public IHttpActionResult AllotAgentOrganization(int organizationId, int channelManagerId) { int authenticatedUserId = User.Identity.GetUserId(); _createAgentService.AllotAgentOrganization(authenticatedUserId, organizationId, channelManagerId); return Ok(); } #endregion } }