using FHT.Agency.CommonTypes; using FHT.Agency.Queries.ImplementedInterfaces; using FHT.Agency.Services.ImplementedInterfaces; using FHT.Agency.UI.InputModels.Agent; using FHT.Agency.UI.ViewModels; using FHT.Agency.Web.Extensions; using FHT.Agency.Web.Filters; using FHT.Agency.Web.Utility; using FHT.ApiCommon; using FHT.Core.UI.ViewModels; using FHT.DDD; using Microsoft.AspNet.Identity; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web.Http; using System.Web.Http.Cors; using System.Web.Http.Description; namespace FHT.Agency.Web.ApiControllers { [Authorize] [RoutePrefix("api/agencies")] [EnableCors(origins: "*", headers: "*", methods: "*")] public class AgencyApiController : ApiController { private readonly IAgentQuery _agentQuery; private readonly IUserQuery _userQuery; private readonly IAgentClientQuery _agentClientQuery; private readonly IAccountService _accountService; private readonly ICreateAgentService _createAgentService; public AgencyApiController(IAgentQuery agentQuery, IAgentClientQuery agentClientQuery, IUserQuery userQuery, IAccountService accountService, ICreateAgentService createAgentService) { _agentQuery = agentQuery; _userQuery = userQuery; _agentClientQuery = agentClientQuery; _accountService = accountService; _createAgentService = createAgentService; } #region 查询 [HttpGet] [ResponseType(typeof(List))] [Route("_search")] public async Task SearchAsync(string q) { List res = await _agentQuery.SearchAsync(q); return Ok(res); } [HttpGet] [ResponseType(typeof(QueryResult))] [ClaimsAuthorize(FhtUserClaimType.ManageAgentUsers)] [Route("_searchAgentUsers")] public async Task SearchAgentUsersAsync(int? agentOrganizationId = null, AccountStatus? accountStatus = null, int? channelUserId = 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 users = await _userQuery.SearchAgentUsersAsync(authenticatedUserId, agentOrganizationId, accountStatus, channelUserId, offset, limit); return Ok(users); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.AuditAgentUsers)] [ResponseType(typeof(UserViewModel))] [Route("{userId:int:min(1)}")] public async Task GetAsync(int userId) { UserViewModel user = await _userQuery.GetAsync(userId); return Ok(user); } [HttpGet] [ResponseType(typeof(UpdateAgentAccountInputModel))] [Route("_getAgentAccountModel")] public IHttpActionResult GetAgentAccount(int userId) { UpdateAgentAccountInputModel agentAccount = _createAgentService.GetAgentAccount(userId); return Ok(agentAccount); } [HttpGet] [ResponseType(typeof(List))] [Route("_getAgentsByOrganizationId")] public async Task GetAgentsByOrganizationIdAsync(int agentOrganizationId) { List agents = await _agentQuery.GetAgentsAsync(agentOrganizationId); return Ok(agents); } [HttpGet] [ResponseType(typeof(List))] [Route("_getChannelUsersByCurrUser")] public async Task GetChannelUsersByCurrUserAsync(int? channelUserId = null) { int authenticatedUserId = User.Identity.GetUserId(); List allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); List channelUsers = ChannelUserHelper.ChannelUser(allChannelUsers, User.UserRole(), channelUserId); return Ok(channelUsers); } [HttpGet] [ResponseType(typeof(List))] [Route("_getChannelUsersByCurrRole")] public async Task GetChannelUsersByCurrRoleAsync(int? channelUserId = null) { int authenticatedUserId = User.Identity.GetUserId(); var channelUsers = new List(); var allChannelUsers = new List(); if (User.UserRole() == FhtUserRoleType.CustomerServiceStaff) { allChannelUsers = await _userQuery.GetCustomerServiceChannelUsersAsync(authenticatedUserId); channelUsers = ChannelUserHelper.CustomerServiceChannelUsers(allChannelUsers, channelUserId); } else { allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); channelUsers = ChannelUserHelper.ChannelUser(allChannelUsers, User.UserRole(), channelUserId); } return Ok(channelUsers); } [HttpGet] [ResponseType(typeof(UI.ViewModels.Agent.AgentOrderAndClientStatisticsViewModel))] [Route("_getAgentOrderAndClientStatistics")] public async Task GetAgentOrderAndClientStatisticsAsync() { int authenticatedUserId = User.Identity.GetUserId(); UI.ViewModels.Agent.AgentOrderAndClientStatisticsViewModel agentOrderAndClientStatistics = await _agentQuery.GetAgentOrderAndClientStatisticsAsync(authenticatedUserId); return Ok(agentOrderAndClientStatistics); } [HttpGet] [ResponseType(typeof(UI.ViewModels.Agent.AgentFinanceStatusStatisticsViewModel))] [Route("_getAgentFinanceStatusStatistics")] public async Task GetAgentFinanceStatusStatisticsAsync() { int authenticatedUserId = User.Identity.GetUserId(); UI.ViewModels.Agent.AgentFinanceStatusStatisticsViewModel res = await _agentQuery.GetAgentFinanceStatusStatisticsAsync(authenticatedUserId); return Ok(res); } #endregion #region 修改 [HttpPost] [Route("_create", Name = nameof(CreateAgentAsync))] [ResponseType(typeof(CreatedViewModel))] [ValidateModel] public async Task CreateAgentAsync(CreateAgentAccountInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); bool isExist = await _userQuery.CheckOrganizationExistAgentAdminAsync(model.AgentOrganizationId); if (isExist && model.Role == FhtUserRoleType.AgentAdmin) { throw new Exception("已存在管理员!"); } int agentId = await _createAgentService.CreateAgentAsync(authenticatedUserId, model); string url = Url.Link(nameof(CreateAgentAsync), new { agentId }); return Created(url, new CreatedViewModel { Id = agentId, Location = url }); } [HttpPost] [Route("_update")] [ResponseType(typeof(void))] [ValidateModel] public IHttpActionResult UpdateAgent(UpdateAgentAccountInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); _createAgentService.UpdateAgent(model); return Ok(); } [HttpPost] [ClaimsAuthorize(FhtUserClaimType.AuditAgentUsers)] [Route("_audit")] [ResponseType(typeof(void))] public IHttpActionResult AuditAgentUser(int userId, AccountStatus accountStatus) { int authenticatedUserId = User.Identity.GetUserId(); _accountService.AuditAgentUser(authenticatedUserId, userId, accountStatus); return Ok(); } #endregion #region autocomplete [HttpGet] [Route("_autocompletesclient")] [ResponseType(typeof(List))] public async Task AutoCompleteClientAsync(string q = "", int? id = null) { var agentClients = new List(); if (id.HasValue) { SelectListItemViewModel agentClient = await _agentClientQuery.GetAgentClientByIdAsync(id.Value); agentClients.Add(agentClient); } else { int authenticatedUserId = User.Identity.GetUserId(); UserViewModel user = await _userQuery.GetAsync(authenticatedUserId); agentClients = await _agentClientQuery.SearchAgentClientsAsync(user.AgentOrganizationId, authenticatedUserId, User.UserRole(), q); } return Ok(agentClients); } [HttpGet] [Route("_autocomplete")] [ResponseType(typeof(List))] public async Task AutocompleteAsync(string q = "", int? id = null) { int authenticatedUserId = User.Identity.GetUserId(); var agentClients = new List(); if (id.HasValue) { SelectListItemViewModel agentClient = await _agentQuery.GetAgentByIdAsync(id.Value); agentClients.Add(agentClient); } else { agentClients = await _agentQuery.SearchAgentsAsync(authenticatedUserId, q); } return Ok(agentClients); } [HttpGet] [Route("_autocompleteclientcompany")] [ResponseType(typeof(List))] public async Task AutoCompleteClientCompanyAsync(string q) { int currentUserId = User.Identity.GetUserId(); UserViewModel user = await _userQuery.GetAsync(currentUserId); if (!user.AgentOrganizationId.HasValue) { throw new ArgumentException("只有代理商才能购买订单!"); } List data = await _agentClientQuery.SearchCompaniesAsync(user.AgentOrganizationId.Value, q); return Ok(data); } #endregion } }