using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Core.Extensions; using WalkingTec.Mvvm.Mvc; using wtmProject.ViewModel._Admin.WTM_CUSTOMVMs; using wtmProject.Model; using wtmProject.ViewModel._Admin.WTM_BUSINESSVMs; using wtmProject.ViewModel._Admin.WTM_VISIT_PLANVMs; using wtmProject.ViewModel._Admin.FrameworkOutRecordVMs; using BootstrapBlazor.Components; using wtmProject.ViewModel._Admin.WTM_CONTRACTVMs; namespace wtmProject.Controllers { [Area("_Admin")] [AuthorizeJwtWithCookie] [ActionDescription("客户维护")] [ApiController] [Route("api/WTM_CUSTOM")] [AllRights] public partial class WTM_CUSTOMController : BaseApiController { [ActionDescription("Sys.Search")] [HttpPost("Search")] public IActionResult Search(WTM_CUSTOMSearcher searcher) { if (ModelState.IsValid) { var vm = Wtm.CreateVM(); vm.Searcher = searcher; return Content(vm.GetJson(enumToString: false)); } else { return BadRequest(ModelState.GetErrorJson()); } } [ActionDescription("Sys.Get")] [HttpGet("{id}")] public WTM_CUSTOMVM Get(string id) { var vm = Wtm.CreateVM(id); return vm; } [ActionDescription("Sys.Create")] [HttpPost("Add")] public IActionResult Add(WTM_CUSTOMVM vm) { if (!ModelState.IsValid) { return BadRequest(ModelState.GetErrorJson()); } else { vm.DoAdd(); if (!ModelState.IsValid) { return BadRequest(ModelState.GetErrorJson()); } else { return Ok(vm.Entity); } } } [ActionDescription("Sys.Edit")] [HttpPut("Edit")] public IActionResult Edit(WTM_CUSTOMVM vm) { if (!ModelState.IsValid) { return BadRequest(ModelState.GetErrorJson()); } else { vm.DoEdit(false); if (!ModelState.IsValid) { return BadRequest(ModelState.GetErrorJson()); } else { return Ok(vm.Entity); } } } [HttpPost("BatchDelete")] [ActionDescription("Sys.Delete")] public IActionResult BatchDelete(string[] ids) { var vm = Wtm.CreateVM(); if (ids != null && ids.Count() > 0) { vm.Ids = ids; } else { return Ok(); } if (!ModelState.IsValid || !vm.DoBatchDelete()) { return BadRequest(ModelState.GetErrorJson()); } else { return Ok(ids.Count()); } } [ActionDescription("Sys.Export")] [HttpPost("ExportExcel")] public IActionResult ExportExcel(WTM_CUSTOMSearcher searcher) { var vm = Wtm.CreateVM(); vm.Searcher = searcher; vm.SearcherMode = ListVMSearchModeEnum.Export; return vm.GetExportData(); } [ActionDescription("Sys.CheckExport")] [HttpPost("ExportExcelByIds")] public IActionResult ExportExcelByIds(string[] ids) { var vm = Wtm.CreateVM(); if (ids != null && ids.Count() > 0) { vm.Ids = new List(ids); vm.SearcherMode = ListVMSearchModeEnum.CheckExport; } return vm.GetExportData(); } [ActionDescription("Sys.DownloadTemplate")] [HttpGet("GetExcelTemplate")] public IActionResult GetExcelTemplate() { var vm = Wtm.CreateVM(); var qs = new Dictionary(); foreach (var item in Request.Query.Keys) { qs.Add(item, Request.Query[item]); } vm.SetParms(qs); var data = vm.GenerateTemplate(out string fileName); return File(data, "application/vnd.ms-excel", fileName); } [ActionDescription("Sys.Import")] [HttpPost("Import")] public ActionResult Import(WTM_CUSTOMImportVM vm) { if (vm!=null && (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData())) { return BadRequest(vm.GetErrorJson()); } else { return Ok(vm?.EntityList?.Count ?? 0); } } [HttpGet("GetWTM_AREAs")] public ActionResult GetWTM_AREAs() { var userAreas = DC.Set().Where(t => t.UserCode == Wtm.LoginUserInfo.ITCode).Select(m => m.AreaCode).ToList(); return Ok(DC.Set().Where(x => userAreas.Contains(x.AreaCode)).GetSelectListItems(Wtm, x => x.AreaName, null, false, false)); } [HttpGet("GetCustomInfo")] public ActionResult GetCustomInfo(string name) { var temp = new WTM_CUSTOM_Temp(); if (!string.IsNullOrEmpty(name)) { var id=new Guid(name); var vm = DC.Set().Where(o => o.ID == id).FirstOrDefault(); if (vm != null) { temp.CustomInfo = vm; //客户联系人子表 var persons = DC.Set().Where(o => o.CustomId == vm.ID).ToList(); temp.CustomInfo.CustomPerson = persons; //拜访计划 var plans = DC.Set().Where(o => o.CustomId == vm.ID).ToList(); var planIds = plans.Select(m => m.ID).ToList(); //拜访计划协同资源 var planUsers = DC.Set().Where(o => planIds.Contains((Guid)o.VisitPlanId)).ToList(); var users = DC.Set().ToList(); planUsers.ForEach(item => { item.User = users.FirstOrDefault(o => o.ID == item.UserId); }); //拜访计划 temp.VisitRecord= plans.Select(item => new WTM_VISIT_PLAN_View { Name_view = item.MarkManage?.Name, VisitStartTime = item.VisitStartTime, VisitEndTime = item.VisitEndTime, CustomAllName_view = item.Custom?.CustomAllName, PersonName_view = item.VisitObject?.PersonName, VisitTarget = item.VisitTarget ==null?null:item.VisitTarget.Replace("\n",""), VisitPersons = string.Join(",", planUsers.Where(o => o.VisitPlanId == item.ID).Select(v=>v.User?.Name).ToList()), }).ToList(); //商机维护 var business = DC.Set().Where(o => o.CustomId == vm.ID).ToList(); var projects = DC.Set(); temp.BusinessInfo = business.Select(item => new WTM_BUSINESS_View { ID = item.ID, CustomAllName_view = item.Custom?.CustomAllName, PersonName_view = item.VisitObject?.PersonName, BusinessName = item.BusinessName, ProjectName_view = projects.FirstOrDefault(t=>t.ID == item.ProjectId)?.ProjectName, BusinessStatus = item.BusinessStatus, UpdateTime = item.UpdateTime, UpdateBy = item.UpdateBy, }).ToList(); //合同管理 var contracts = DC.Set().Where(o => o.CustomId == vm.ID).ToList(); var contractIds = contracts.Select(t => t.ID).ToList(); var billList = DC.Set().Where(o => contractIds.Contains((Guid)o.ContractId)).ToList(); temp.ContractInfo = contracts.Select(item => new WTM_CONTRACT_View { ID = item.ID, ContractCode = item.ContractCode, ContractName = item.ContractName, ContractAmount = item.ContractAmount, ProjectName_view = projects.FirstOrDefault(t => t.ID == item.ProjectId)?.ProjectName, RefundAmount = billList.Where(u => u.ContractId == item.ID).Sum(p => p.BillingAmount).ToString(), RefundCount = billList.Where(u => u.ContractId == item.ID).Count().ToString() + "/" + ((int)item.ContractCycle + 1), Name_view2 = item.ProjectManage.Name, Name_view = item.MarkManage.Name, }).ToList(); } else { temp.CustomInfo = new WTM_CUSTOM(); var persons = new List(); temp.CustomInfo.CustomPerson = persons; temp.VisitRecord = new List(); temp.BusinessInfo = new List(); temp.ContractInfo = new List(); } } else { temp.CustomInfo = new WTM_CUSTOM(); var persons = new List(); temp.CustomInfo.CustomPerson = persons; temp.VisitRecord = new List(); temp.BusinessInfo = new List(); temp.ContractInfo = new List(); } return Ok(temp); } } }