using System; using FHT.Agency.CommonTypes; using FHT.Agency.Queries.ImplementedInterfaces; using FHT.Agency.Services.ImplementedInterfaces; using FHT.Agency.UI.InputModels.Agent; using FHT.Agency.Web.Filters; using Microsoft.AspNet.Identity; using System.Threading.Tasks; using FHT.Utilities; using FHT.Core.CommonTypes; using FHT.Management.Interfaces.DependentInterfaces.Services; using FHT.Management.Interfaces.InputModels; using FHT.Core.Agency.Interfaces; using FHT.DDD; using System.Web.Http; using FHT.ApiCommon; using System.Web.Http.Description; using FHT.Core.UI.ViewModels; using System.Web.Http.Cors; namespace FHT.Agency.Web.ApiControllers { [Authorize] [RoutePrefix("api/agencyclients")] [EnableCors(origins: "*", headers: "*", methods: "*")] public class AgencyClientApiController : ApiController { private readonly IAgentQuery _agentQuery; private readonly IAgentClientService _agentClientService; private readonly IAgentClientQuery _agentClientQuery; private readonly IUserQuery _userQuery; private readonly Core.Queries.ImplementedInterfaces.ICompanyQuery _companyQuery; private readonly IManagementCompanyAuthenticationService _managementCompanyAuthenticationService; private readonly Core.Agency.Interfaces.IAccountService _accountService; private readonly ICompanyService _companyService; private readonly IUserProfileService _userProfileService; public AgencyClientApiController(IAgentClientService agentClientService, IAgentClientQuery agentClientQuery, IUserQuery userQuery, Core.Queries.ImplementedInterfaces.ICompanyQuery companyQuery, IManagementCompanyAuthenticationService managementCompanyAuthenticationService, IAgentQuery agentQuery, Core.Agency.Interfaces.IAccountService accountService, ICompanyService companyService, IUserProfileService userProfileService) { _agentClientService = agentClientService; _agentClientQuery = agentClientQuery; _userQuery = userQuery; _companyQuery = companyQuery; _managementCompanyAuthenticationService = managementCompanyAuthenticationService; _agentQuery = agentQuery; _accountService = accountService; _companyService = companyService; _userProfileService = userProfileService; } #region 查询 [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageClients)] [Route("_search")] [ResponseType(typeof(QueryResult))] public async Task SearchAgentClientsOfAgentOrganizationAsync(int? clientId = null, int? agentOrganizationId = null, int currPage = 0, int limit = 20) { int authenticatedUserId = User.Identity.GetUserId(); int offset = (currPage < 2 ? 0 : currPage - 1) * limit; UI.ViewModels.UserViewModel user = await _userQuery.GetAsync(authenticatedUserId); QueryResult clients = await _agentClientQuery.SearchAgentClientsOfAgentOrganizationAsync(user.AgentOrganizationId, authenticatedUserId, clientId,agentOrganizationId, offset,limit); return Ok(clients); } [HttpPost] [Route("_checkIfAccountExist")] [ResponseType(typeof(bool))] public async Task CheckIfAccountExistsAsync(string mobile) { bool? phoneExisted = await _accountService.CheckIfAccountExistsAsync(mobile); bool isExists = phoneExisted == true; return Ok(!isExists); } [HttpPost] [Route("_checkIsValidRealName")] [ResponseType(typeof(bool))] public async Task IsValidRealNameAsync(string realName) { RealNameStatus status = await _userProfileService.CheckRealNameStatusAsync(realName); bool isValid = status != RealNameStatus.Invalid; return Ok(isValid); } [HttpPost] [Route("_checkCompanyNameCanBeRegistered")] [ResponseType(typeof(bool))] public IHttpActionResult IsCompanyNameCanBeRegistered(string companyName) { bool result = _companyQuery.CheckAllowedToUseCompanyName(companyName, null); return Json(result); } #endregion #region 修改 [HttpPost] [ValidateModel] [Route("_create", Name = nameof(CreateClientAsync))] [ResponseType(typeof(CreatedViewModel))] [ClaimsAuthorize(FhtUserClaimType.CreateClient)] public async Task CreateClientAsync(CreateUserAndCompanyInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); UI.ViewModels.UserViewModel user = await _userQuery.GetAsync(authenticatedUserId); int? companyId = null; if (user.AgentOrganizationId.HasValue) { string ipAddress = IpAddressHelper.GetIpAddress(); int? userId = await _accountService.AgentRegisterClientAccountAsync(model.Mobile, model.RealName, model.Gender.ToInt(), model.Profession, model.Industry.IndustryId, model.Region.SecondLevelId, false, ipAddress, model.Password); if (!userId.HasValue || userId.Value == 0) { throw new Exception("客户创建失败,请检查数据有效性!"); } companyId = _companyService.AgentRegisterClientCompany(userId.Value, model.Industry.IndustryId, model.CompanyName, model.Region.SecondLevelId, user.AgentOrganizationId.Value, RegistrationSourceType.Agency); if (!companyId.HasValue || companyId.Value == 0) { throw new Exception("客户创建失败,请检查数据有效性!"); } else { //同步到中间表 await _agentClientService.CreateAgentClientAsync(authenticatedUserId, new AgentClientInputModel { CompanyId = companyId.Value, CompanyName = model.CompanyName, Created = DateTime.Now, AgentOrganizationId = user.AgentOrganizationId.Value }); //同步企业认证信息 int applyId = await _managementCompanyAuthenticationService.CreateOrUpdateAsync(new DDD.CoreAuthenticatedUserId(userId.Value), companyId.Value, new AuthenticationInputModel { RealName = model.RealName, IdCardNumber = model.IdCardNumber, PhotoIdUrl = model.PhotoIdUrl, LicenseNo = model.LicenseNo, LicenseUrl = model.LicenseUrl, Status = ProcessingStatus.Accepted }); if (applyId > 0) { await _managementCompanyAuthenticationService.AcceptAsync(authenticatedUserId, applyId); _companyService.ChangeCompanyVerified(companyId.Value, true); } } } string url = Url.Link(nameof(CreateClientAsync), new { companyId }); return Created(url, new CreatedViewModel { Id = companyId.Value, Location = url }); } #endregion } }