using System; using System.Linq; using System.Web.Mvc; 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 FHT.Agency.Web.Extensions; using FHT.Agency.UI.InputModels.Search; 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 System.Collections.Generic; namespace FHT.Agency.Web.Controllers { public class AgentController : Controller { private const string Data = "该账号已经注册"; private readonly IAgentQuery _agentQuery; private readonly IRegionQuery _regionQuery; private readonly Core.Queries.ImplementedInterfaces.IIndustryQuery _industryQuery; 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 AgentController(IRegionQuery regionQuery, Core.Queries.ImplementedInterfaces.IIndustryQuery industryQuery, IAgentClientService agentClientService, IAgentClientQuery agentClientQuery, IUserQuery userQuery, Core.Queries.ImplementedInterfaces.ICompanyQuery companyQuery, IManagementCompanyAuthenticationService managementCompanyAuthenticationService, IAgentQuery agentQuery, Core.Agency.Interfaces.IAccountService accountService, ICompanyService companyService, IUserProfileService userProfileService) { _regionQuery = regionQuery; _industryQuery = industryQuery; _agentClientService = agentClientService; _agentClientQuery = agentClientQuery; _userQuery = userQuery; _companyQuery = companyQuery; _managementCompanyAuthenticationService = managementCompanyAuthenticationService; _agentQuery = agentQuery; _accountService = accountService; _companyService = companyService; _userProfileService = userProfileService; } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.CreateClient)] public async Task< ActionResult> CreateClient() { await InitializationAsync(); var model = new CreateUserAndCompanyInputModel(); return View(model); } [HttpPost] [ClaimsAuthorize(FhtUserClaimType.CreateClient)] [ValidateAntiForgeryToken] public async Task CreateClient(CreateUserAndCompanyInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); UI.ViewModels.UserViewModel user =await _userQuery.GetAsync(authenticatedUserId); 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) { InitializationAsync(); ModelState.AddModelError("", "客户创建失败,请检查数据有效性!"); return View("~/Views/Agent/CreateClient.cshtml", new CreateUserAndCompanyInputModel()); } int? companyId = _companyService.AgentRegisterClientCompany(userId.Value, model.Industry.IndustryId, model.CompanyName, model.Region.SecondLevelId, user.AgentOrganizationId.Value, RegistrationSourceType.Agency); if (!companyId.HasValue || companyId.Value == 0) { InitializationAsync(); ModelState.AddModelError("", "客户创建失败,请检查数据有效性!"); return View("~/Views/Agent/CreateClient.cshtml", new CreateUserAndCompanyInputModel()); } 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); } } } else { InitializationAsync(); ModelState.AddModelError("", "非代理商用户无权创建客户!"); return View("~/Views/Agent/CreateClient.cshtml", new CreateUserAndCompanyInputModel()); } return RedirectToAction(nameof(AgentClients)); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageClients)] public async Task AgentClients(SearchAgentClientInputModel args) { int authenticatedUserId = User.Identity.GetUserId(); UI.ViewModels.UserViewModel user =await _userQuery.GetAsync(authenticatedUserId); ViewBag.AgentClients = await _agentClientQuery.SearchAgentClientsOfAgentOrganizationAsync(user.AgentOrganizationId, authenticatedUserId, args.ClientId, args.AgentOrganizationId, args.Offset(), args.Limit); return View(args); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.CreateClient)] public async Task IsMobilePhoneCanBeRegistered(string mobile) { bool? phoneExisted = await _accountService.CheckIfAccountExistsAsync(mobile); if (phoneExisted == true) return Json(Data, JsonRequestBehavior.AllowGet); return Json(true, JsonRequestBehavior.AllowGet); } #pragma warning disable FHTRoslynChecker_MvcControllerActionCheck // MvcControllerAction /// /// 验证姓名是不是合法, 给客户端验证用的 /// /// /// [AllowAnonymous] public async Task IsValidRealName(string realName) { RealNameStatus status = await _userProfileService.CheckRealNameStatusAsync(realName); if (RealNameStatus.Invalid == status) { return Json("请输入合法姓名", JsonRequestBehavior.AllowGet); } return Json(true, JsonRequestBehavior.AllowGet); } #pragma warning restore FHTRoslynChecker_MvcControllerActionCheck // MvcControllerAction /// /// 验证姓名是不是合法, 给客户端验证用的 /// /// /// [AllowAnonymous] public async Task IsValidApplicant(string applicant) { RealNameStatus status =await _userProfileService.CheckRealNameStatusAsync(applicant); if (RealNameStatus.Invalid == status) { return Json("请输入合法姓名", JsonRequestBehavior.AllowGet); } return Json(true, JsonRequestBehavior.AllowGet); } [HttpGet] // [ClaimsAuthorize(FhtUserClaimType.CreateClient)] [AllowAnonymous] public ActionResult IsCompanyNameCanBeRegistered(string companyName) { bool result = _companyQuery.CheckAllowedToUseCompanyName(companyName, null); if (!result) { return Json("该企业名称已经使用", JsonRequestBehavior.AllowGet); } return Json(true, JsonRequestBehavior.AllowGet); } [HttpGet] public async Task SearchAgentClients(string q, int? id) { if (id.HasValue) { UI.ViewModels.SelectListItemViewModel agentClient =await _agentClientQuery.GetAgentClientByIdAsync(id.Value); return Json(agentClient, JsonRequestBehavior.AllowGet); } else { int authenticatedUserId = User.Identity.GetUserId(); UI.ViewModels.UserViewModel user =await _userQuery.GetAsync(authenticatedUserId); List agentClients =await _agentClientQuery.SearchAgentClientsAsync(user.AgentOrganizationId, authenticatedUserId, User.UserRole(), q); return Json(agentClients, JsonRequestBehavior.AllowGet); } } [HttpGet] [AllowAnonymous] public async Task< JsonpResult> SearchAgency(string q) { List res =await _agentQuery.SearchAsync(q); return this.Jsonp(res); } #region 辅助方法 private async Task InitializationAsync() { List industries = _industryQuery.GetIndustries(); ViewBag.industry = industries.Select(s => new SelectListItem { Value = s.IndustryId.ToString(), Text = s.IndustryName }); List regions =await _regionQuery.GetRegionsAsync(); ViewBag.provinces = regions.Select(s => new SelectListItem { Value = s.RegionId.ToString(), Text = s.Name }); } #endregion } }