using System.Linq; using System.Threading.Tasks; 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 Microsoft.AspNet.Identity; using FHT.Agency.UI.InputModels; using FHT.Utilities; using System.Collections.Generic; using WebGrease.Css.Extensions; using System.Net; using FHT.Agency.Web.Utility; using FHT.Agency.Web.Extensions; using FHT.Agency.UI.ViewModels.Agent; using FHT.Agency.UI.ViewModels; using FHT.Agency.Web.Filters; using FHT.Agency.UI.InputModels.Search; using FHT.MvcCommon; using Nito.AsyncEx; namespace FHT.Agency.Web.Controllers { public class ManageAgentController : Controller { private readonly ICreateAgentService _createAgentService; private readonly IAccountService _accountService; private readonly IAgentQuery _agentQuery; private readonly IRegionQuery _regionQuery; private readonly IUserQuery _userQuery; public ManageAgentController(ICreateAgentService createAgentService, IAgentQuery agentQuery, IRegionQuery regionQuery, IUserQuery userQuery, IAccountService accountService) { _createAgentService = createAgentService; _accountService = accountService; _agentQuery = agentQuery; _regionQuery = regionQuery; _userQuery = userQuery; } [HttpGet] public ActionResult Index() { return RedirectToAction(nameof(AgentOrganizations)); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageAgentOrganization)] public async Task AgentOrganizations(SearchAgentInputModel args) { int authenticatedUserId = User.Identity.GetUserId(); args.Role = User.UserRole(); List allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); if (args.ChannelUserId.HasValue && allChannelUsers.Count(u => u.UserId == args.ChannelUserId.Value) == 0)//非法请求 { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ViewBag.ChannelUsers = ChannelUserHelper.ChannelUser(allChannelUsers, User.UserRole(), args.ChannelUserId); ViewBag.AgentOrganizations = await _agentQuery.GetAgentOrganizationsAsync(authenticatedUserId, args.AgentOrganizationId, args.ChannelUserId, args.AgentStatus, args.DateRange.BeginTime, args.DateRange.EndTime, args.Offset(), args.Limit); return View("~/Views/ManageAgent/AgentOrganizations.cshtml", args); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.AuditAgentOrganizations)] public async Task AuditAgentOrganizations(SearchAgentInputModel args) { int authenticatedUserId = User.Identity.GetUserId(); args.Operate = "Audit"; args.Role = User.UserRole(); List allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); if (args.ChannelUserId.HasValue && allChannelUsers.Count(u => u.UserId == args.ChannelUserId.Value) == 0)//非法请求 { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ViewBag.ChannelUsers = ChannelUserHelper.ChannelUser(allChannelUsers, User.UserRole(), args.ChannelUserId); ViewBag.AgentOrganizations = await _agentQuery.GetAgentOrganizationsAsync(authenticatedUserId, args.AgentOrganizationId, args.ChannelUserId, args.AgentStatus, args.DateRange.BeginTime, args.DateRange.EndTime, args.Offset(), args.Limit); return View("~/Views/ManageAgent/AgentOrganizations.cshtml", args); } [HttpGet] public async Task AgentOrganization(int agentOrganizationId) { AgentOrganizationViewModel agentOrganization = await _agentQuery.GetAgentOrganizationAsync(agentOrganizationId); if (agentOrganization == null) return new HttpNotFoundResult(); return View(agentOrganization); } public async Task AgentUsers(SearchAgentUserInputModel args) { int authenticatedUserId = User.Identity.GetUserId(); args.Role = User.UserRole(); List allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); if (args.ChannelUserId.HasValue && allChannelUsers.Count(u => u.UserId == args.ChannelUserId.Value) == 0)//非法请求 { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ViewBag.ChannelUsers = ChannelUserHelper.ChannelUser(allChannelUsers, User.UserRole(), args.ChannelUserId); ViewBag.Users = await _userQuery.SearchAgentUsersAsync(authenticatedUserId, args.AgentOrganizationId, args.AccountStatus, args.ChannelUserId, args.Offset(), args.Limit); return View(args); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageAgentOrganization, FhtUserClaimType.AuditAgentOrganizations)] public async Task AgentOrganizationDetail(int agentOrganizationId) { AgentOrganizationViewModel agentOrganization = await _agentQuery.GetAgentOrganizationAsync(agentOrganizationId); if (agentOrganization == null) return new HttpNotFoundResult(); IOrderedEnumerable> agentStatus = EnumHelpers.Enum2Dictionary().Where(s => s.Key != AgentStatus.Pending && s.Key != AgentStatus.Temporary).OrderBy(m => m.Key); var status = new List(); agentStatus.ForEach(et => { status.Add(new SelectListItem { Value = ((int)et.Key).ToString(), Text = et.Key.GetEnumDescription(), Selected = et.Key == agentOrganization.Status }); }); ViewBag.Status = status; ViewBag.AgentOrganizationViewModel = agentOrganization; var model = new AuditAgentOrganizationInputModel() { AgentStatus = agentOrganization.Status, ProtocolNo = agentOrganization.ProtocolNo, EffectiveDate = agentOrganization.EffectiveDate, ExpiryDate = agentOrganization.ExpiryDate, Id = agentOrganization.Id }; return View(model); } /// /// 分配代理商 /// /// /// [HttpGet] [ClaimsAuthorize(FhtUserClaimType.AllotAgentOrganizations)] public async Task AllotOrganizations(SearchAgentInputModel args) { int authenticatedUserId = User.Identity.GetUserId(); args.Role = User.UserRole(); List allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); if (args.ChannelUserId.HasValue && allChannelUsers.Count(u => u.UserId == args.ChannelUserId.Value) == 0)//非法请求 { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ViewBag.ChannelUsers = ChannelUserHelper.ChannelUser(allChannelUsers, User.UserRole(), args.ChannelUserId); ViewBag.AgentOrganizations = await _agentQuery.GetAgentOrganizationsAsync(authenticatedUserId, args.AgentOrganizationId, args.ChannelUserId, args.AgentStatus, args.DateRange.BeginTime, args.DateRange.EndTime, args.Offset(), args.Limit); return View(args); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.AllotAgentOrganizations)] public async Task AllotAgentOrganization(int agentOrganizationId) { int authenticatedUserId = User.Identity.GetUserId(); bool isViewDetail = Request.QueryString["actions"] == "detail"; AgentOrganizationViewModel agentOrganization = await _agentQuery.GetAgentOrganizationAsync(agentOrganizationId); if (agentOrganization == null) { return new HttpNotFoundResult(); } else if (!isViewDetail && agentOrganization.Status != AgentStatus.Pass)//分配的代理商必须是审核通过的 { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } List allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); ViewBag.ChannelUsers = ChannelUserHelper.ChannelUser(allChannelUsers, User.UserRole(), agentOrganization.ChannelManagerId); ViewBag.Role = User.UserRole(); return View(agentOrganization); } [HttpPost] [ClaimsAuthorize(FhtUserClaimType.AllotAgentOrganizations)] public ActionResult AllotAgentOrganization(int organizationId,int channelManagerId ) { int authenticatedUserId = User.Identity.GetUserId(); _createAgentService.AllotAgentOrganization(authenticatedUserId, organizationId, channelManagerId); return RedirectToAction(nameof(AllotOrganizations), "ManageAgent"); } [HttpPost] [ValidateAntiForgeryToken] [ClaimsAuthorize(FhtUserClaimType.AuditAgentOrganizations)] public ActionResult OrganizationAudit(AuditAgentOrganizationInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); _createAgentService.AuditAgentOrganization(authenticatedUserId, model); return RedirectToAction(nameof(AuditAgentOrganizations)); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.AuditAgentUsers)] public async Task OrganizationUsers(SearchAgentUserInputModel args) { int authenticatedUserId = User.Identity.GetUserId(); args.Role = User.UserRole(); IOrderedEnumerable> accountStatus = EnumHelpers.Enum2Dictionary().OrderBy(m => m.Key); var accountStatusItems = new List(); accountStatus.ForEach(et => { accountStatusItems.Add(new SelectListItem { Value = ((int)et.Key).ToString(), Text = et.Key.GetEnumDescription(), Selected = et.Key == args.AccountStatus }); }); List allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); if (args.ChannelUserId.HasValue && allChannelUsers.Count(u => u.UserId == args.ChannelUserId.Value) == 0)//非法请求 { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } List channelUsers = ChannelUserHelper.ChannelUser(allChannelUsers, User.UserRole(), args.ChannelUserId); SegChannelUsers(channelUsers); ViewBag.AccountStatus = accountStatusItems; ViewBag.Users = await _userQuery.SearchAgentUsersAsync(authenticatedUserId, args.AgentOrganizationId, args.AccountStatus, args.ChannelUserId, args.Offset(), args.Limit); return View(args); } private void SegChannelUsers(List users) { ViewBag.ChannelUsers = users; } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.AuditAgentUsers)] public async Task AgentUserDetail(int userId) { UserViewModel user = await _userQuery.GetAsync(userId); IOrderedEnumerable> accountStatus = EnumHelpers.Enum2Dictionary().Where(s => s.Key != AccountStatus.Pending && s.Key != AccountStatus.Temporary).OrderBy(m => m.Key); var status = new List(); accountStatus.ForEach(et => { status.Add(new SelectListItem { Value = ((int)et.Key).ToString(), Text = et.Key.GetEnumDescription(), Selected = et.Key == user.AccountStatus }); }); ViewBag.Status = status; ViewBag.Role = User.UserRole(); return View(user); } [HttpPost] [ValidateAntiForgeryToken] [ClaimsAuthorize(FhtUserClaimType.AuditAgentUsers)] public ActionResult AgentUserAudit(UserViewModel model) { int authenticatedUserId = User.Identity.GetUserId(); _accountService.AuditAgentUser(authenticatedUserId, model.UserId, model.AccountStatus); return RedirectToAction(nameof(OrganizationUsers), new { userId = model.UserId }); } [HttpGet] [ChildActionOnly] public PartialViewResult Agents(int agentOrganizationId) { List agents =AsyncContext.Run(()=> _agentQuery.GetAgentsAsync(agentOrganizationId)); return PartialView(agents); } [HttpGet] public async Task CreateAgent() { int authenticatedUserId = User.Identity.GetUserId(); List agentOrganizations = await _agentQuery.GetPassAgentOrganizationsAsync(authenticatedUserId); ViewBag.AgentOrganizations = agentOrganizations; return View(new CreateAgentAccountInputModel() { Role = FhtUserRoleType.AgentAdmin }); } [HttpGet] public async Task UpdateAgent(int id) { int authenticatedUserId = User.Identity.GetUserId(); List agentOrganizations = await _agentQuery.GetPassAgentOrganizationsAsync(authenticatedUserId); ViewBag.AgentOrganizations = agentOrganizations; UpdateAgentAccountInputModel agentAccount = _createAgentService.GetAgentAccount(id); return View(agentAccount); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult UpdateAgent(UpdateAgentAccountInputModel model) { if (model != null && ModelState.IsValid) { int authenticatedUserId = User.Identity.GetUserId(); _createAgentService.UpdateAgent(model); return RedirectToAction(nameof(AgentUsers)); } return View(); } [HttpPost] [ValidateAntiForgeryToken] public async Task CreateAgent(CreateAgentAccountInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); bool isExist = await _userQuery.CheckOrganizationExistAgentAdminAsync(model.AgentOrganizationId); if (isExist && model.Role == FhtUserRoleType.AgentAdmin) { ModelState.AddModelError("", "已存在管理员!"); } if (model != null && ModelState.IsValid) { await _createAgentService.CreateAgentAsync(authenticatedUserId, model); return RedirectToAction(nameof(AgentUsers)); } else { List agentOrganizations = await _agentQuery.GetSimpleAgentOrganizationsAsync(authenticatedUserId); ViewBag.AgentOrganizations = agentOrganizations; return View(new CreateAgentAccountInputModel() { Role = FhtUserRoleType.AgentCustomerServiceStaff }); } } /// /// 创建代理商 /// /// [HttpGet] public async Task CreateAgentOrganization(int? organizationId) { int authenticatedUserId = User.Identity.GetUserId(); UserViewModel currUser = await _userQuery.GetAsync(authenticatedUserId); var inputModel = new AgentOrganizationInputModel(); if (organizationId.HasValue) { AgentOrganizationViewModel agentOrgation = await _agentQuery.GetAgentOrganizationAsync(organizationId.Value); if (agentOrgation.Status == AgentStatus.Pass) { return RedirectToAction(nameof(AgentOrganizations)); } inputModel = _createAgentService.GetAgentOrganization(organizationId.Value); RegionViewModel region =await _regionQuery.GetRegionAsync(inputModel.Region.SecondLevelId); if (region != null) { inputModel.Region = new RegionInputModel { FirstLevelId = region.ParentId, SecondLevelId = region.RegionId }; } } List regions =await _regionQuery.GetRegionsAsync(); ViewBag.FirstLevelRegions = regions.Select(s => new SelectListItem { Value = s.RegionId.ToString(), Text = s.Name, Selected = inputModel.Region.FirstLevelId == s.RegionId }); ViewBag.ChannelManagers = await ChannelManagerAsync(currUser); return View(inputModel); } /// /// 创建代理商 /// /// /// [HttpPost] [ValidateAntiForgeryToken] public async Task CreateAgentOrganization(AgentOrganizationInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); if (model != null && ModelState.IsValid) { if (model.Id.HasValue) { _createAgentService.UpdateAgentOrganization(model); } else { _createAgentService.CreateAgentOrganization(authenticatedUserId, model); } return RedirectToAction(nameof(AgentOrganizations)); } UserViewModel currUser = await _userQuery.GetAsync(authenticatedUserId); List regions =await _regionQuery.GetRegionsAsync(); ViewBag.FirstLevelRegions = regions.Select(s => new SelectListItem { Value = s.RegionId.ToString(), Text = s.Name }); ViewBag.ChannelManagers = await ChannelManagerAsync(currUser); TempData["message"] = "创建成功代理商机构成功!"; return View(); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ViewOwnOrganization)] public async Task ViewOrganizationDetial() { int authenticatedUserId = User.Identity.GetUserId(); UserViewModel currUser = await _userQuery.GetAsync(authenticatedUserId); var model = new AgentOrganizationViewModel(); if (currUser.AgentOrganizationId.HasValue) { model = await _agentQuery.GetAgentOrganizationAsync(currUser.AgentOrganizationId.Value); } return View(model); } private async Task> ChannelManagerAsync(UserViewModel currUser) { int authenticatedUserId = User.Identity.GetUserId(); List allChannelManagers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId); return ChannelUserHelper.ChannelUser(allChannelManagers, currUser.Role, null); } [HttpGet] [IsAjaxJsonResult] public async Task SearchAgents(string q, int? id) { if (id.HasValue) { SelectListItemViewModel agentClient = await _agentQuery.GetAgentByIdAsync(id.Value); return Json(agentClient, JsonRequestBehavior.AllowGet); } else { int authenticatedUserId = User.Identity.GetUserId(); List agentClients = await _agentQuery.SearchAgentsAsync(authenticatedUserId, q); return Json(agentClients, JsonRequestBehavior.AllowGet); } } } }