303 lines
12 KiB
C#
303 lines
12 KiB
C#
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_BUSINESSVMs;
|
||
using wtmProject.Model;
|
||
using NetBox.Extensions;
|
||
using Elsa.Activities.Temporal;
|
||
|
||
|
||
namespace wtmProject.Controllers
|
||
{
|
||
[Area("_Admin")]
|
||
[AuthorizeJwtWithCookie]
|
||
[ActionDescription("商机线索维护")]
|
||
[ApiController]
|
||
[Route("api/WTM_BUSINESS")]
|
||
public partial class WTM_BUSINESSController : BaseApiController
|
||
{
|
||
[ActionDescription("Sys.Search")]
|
||
[HttpPost("Search")]
|
||
public IActionResult Search(WTM_BUSINESSSearcher searcher)
|
||
{
|
||
if (ModelState.IsValid)
|
||
{
|
||
var vm = Wtm.CreateVM<WTM_BUSINESSListVM>();
|
||
vm.Searcher = searcher;
|
||
return Content(vm.GetJson(enumToString: false));
|
||
}
|
||
else
|
||
{
|
||
return BadRequest(ModelState.GetErrorJson());
|
||
}
|
||
}
|
||
|
||
[ActionDescription("Sys.Get")]
|
||
[HttpGet("{id}")]
|
||
public WTM_BUSINESSVM Get(string id)
|
||
{
|
||
var vm = Wtm.CreateVM<WTM_BUSINESSVM>(id);
|
||
return vm;
|
||
}
|
||
|
||
[ActionDescription("Sys.Create")]
|
||
[HttpPost("Add")]
|
||
public IActionResult Add(WTM_BUSINESSVM vm)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return BadRequest(ModelState.GetErrorJson());
|
||
}
|
||
else
|
||
{
|
||
if (vm != null && vm.Entity != null)
|
||
{
|
||
if (!string.IsNullOrEmpty(vm.Entity.Year) && !string.IsNullOrEmpty(vm.Entity.Month))
|
||
{
|
||
vm.Entity.GetDate = DateTime.Parse(vm.Entity.Year + "-" + vm.Entity.Month + "-01");
|
||
if (vm.Entity.ActSignDate.HasValue)
|
||
{
|
||
int difference = MonthsBetween(vm.Entity.GetDate.Value, vm.Entity.ActSignDate.Value);
|
||
vm.Entity.DealCycle = difference.ToString();
|
||
}
|
||
}
|
||
if (vm.Entity.CustomId != null)
|
||
{
|
||
var custom = DC.Set<WTM_CUSTOM>().FirstOrDefault(t => t.ID == vm.Entity.CustomId);
|
||
if (custom != null)
|
||
{
|
||
vm.Entity.AreaId = custom.AreaId;
|
||
}
|
||
}
|
||
if (!string.IsNullOrEmpty(vm.Entity.DealRate))
|
||
{
|
||
if (vm.Entity.DealRate.IndexOf("%") == -1)
|
||
{
|
||
vm.Entity.DealRate = vm.Entity.DealRate + "%";
|
||
}
|
||
}
|
||
}
|
||
|
||
vm.DoAdd();
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return BadRequest(ModelState.GetErrorJson());
|
||
}
|
||
else
|
||
{
|
||
return Ok(vm.Entity);
|
||
}
|
||
}
|
||
|
||
}
|
||
public static int MonthsBetween(DateTime start, DateTime end)
|
||
{
|
||
// 获取开始和结束的年份和月份
|
||
int startYear = start.Year;
|
||
int startMonth = start.Month;
|
||
int endYear = end.Year;
|
||
int endMonth = end.Month;
|
||
|
||
// 计算年份差和月份差
|
||
int yearDiff = endYear - startYear;
|
||
int monthDiff = endMonth - startMonth;
|
||
|
||
// 如果结束月份小于开始月份,则需要从年份差中减去1(因为我们已经在年份差中加了1年的时间)
|
||
if (monthDiff < 0)
|
||
{
|
||
yearDiff -= 1; // 减去一年,因为我们已经在年份差中加了1年的时间,现在要调整回去
|
||
monthDiff += 12; // 月份差加上12个月,相当于减去一年,并加上下一年的所有月份
|
||
}
|
||
// 最终的月份差是年份差乘以12加上月份差
|
||
return yearDiff * 12 + monthDiff;
|
||
}
|
||
[ActionDescription("Sys.Edit")]
|
||
[HttpPut("Edit")]
|
||
public IActionResult Edit(WTM_BUSINESSVM vm)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return BadRequest(ModelState.GetErrorJson());
|
||
}
|
||
else
|
||
{
|
||
if (vm != null && vm.Entity != null)
|
||
{
|
||
if (!string.IsNullOrEmpty(vm.Entity.Year) && !string.IsNullOrEmpty(vm.Entity.Month))
|
||
{
|
||
vm.Entity.GetDate = DateTime.Parse(vm.Entity.Year+"-"+ vm.Entity.Month +"-01");
|
||
if (vm.Entity.ActSignDate.HasValue)
|
||
{
|
||
int difference = MonthsBetween(vm.Entity.GetDate.Value, vm.Entity.ActSignDate.Value);
|
||
vm.Entity.DealCycle = difference.ToString();
|
||
}
|
||
}
|
||
if (vm.Entity.CustomId != null)
|
||
{
|
||
var custom = DC.Set<WTM_CUSTOM>().FirstOrDefault(t => t.ID == vm.Entity.CustomId);
|
||
if (custom != null)
|
||
{
|
||
vm.Entity.AreaId = custom.AreaId;
|
||
}
|
||
}
|
||
if (!string.IsNullOrEmpty(vm.Entity.DealRate))
|
||
{
|
||
if (vm.Entity.DealRate.IndexOf("%") == -1)
|
||
{
|
||
vm.Entity.DealRate = vm.Entity.DealRate + "%";
|
||
}
|
||
}
|
||
}
|
||
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<WTM_BUSINESSBatchVM>();
|
||
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_BUSINESSSearcher searcher)
|
||
{
|
||
var vm = Wtm.CreateVM<WTM_BUSINESSListVM>();
|
||
vm.Searcher = searcher;
|
||
vm.SearcherMode = ListVMSearchModeEnum.Export;
|
||
return vm.GetExportData();
|
||
}
|
||
|
||
[ActionDescription("Sys.CheckExport")]
|
||
[HttpPost("ExportExcelByIds")]
|
||
public IActionResult ExportExcelByIds(string[] ids)
|
||
{
|
||
var vm = Wtm.CreateVM<WTM_BUSINESSListVM>();
|
||
if (ids != null && ids.Count() > 0)
|
||
{
|
||
vm.Ids = new List<string>(ids);
|
||
vm.SearcherMode = ListVMSearchModeEnum.CheckExport;
|
||
}
|
||
return vm.GetExportData();
|
||
}
|
||
|
||
[ActionDescription("Sys.DownloadTemplate")]
|
||
[HttpGet("GetExcelTemplate")]
|
||
public IActionResult GetExcelTemplate()
|
||
{
|
||
var vm = Wtm.CreateVM<WTM_BUSINESSImportVM>();
|
||
var qs = new Dictionary<string, string>();
|
||
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_BUSINESSImportVM vm)
|
||
{
|
||
if (vm!=null && (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData()))
|
||
{
|
||
return BadRequest(vm.GetErrorJson());
|
||
}
|
||
else
|
||
{
|
||
var customs = DC.Set<WTM_CUSTOM>().ToList();
|
||
if (vm != null && vm.EntityList.Any())
|
||
{
|
||
foreach (var item in vm.EntityList)
|
||
{
|
||
if (item != null)
|
||
{
|
||
if (!string.IsNullOrEmpty(item.PreSignDateStr))
|
||
{
|
||
item.PreSignDate = DateTime.Parse(item.PreSignDateStr);
|
||
}
|
||
if (!string.IsNullOrEmpty(item.ActSignDateStr))
|
||
{
|
||
item.ActSignDate = DateTime.Parse(item.ActSignDateStr);
|
||
}
|
||
if (!string.IsNullOrEmpty(item.Year) && !string.IsNullOrEmpty(item.Month) && item.ActSignDate.HasValue)
|
||
{
|
||
item.GetDate = DateTime.Parse(item.Year + "-" + item.Month + "-01");
|
||
int difference = MonthsBetween(item.GetDate.Value, item.ActSignDate.Value);
|
||
item.DealCycle = difference.ToString();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return Ok(vm?.EntityList?.Count ?? 0);
|
||
}
|
||
}
|
||
|
||
[ActionDescription("Sys.GetWTM_CUSTOMs")]
|
||
[HttpGet("GetWTM_CUSTOMs")]
|
||
public ActionResult GetWTM_CUSTOMs()
|
||
{
|
||
var userAreas = DC.Set<WTM_USER_AREA>().Where(t => t.UserCode == Wtm.LoginUserInfo.ITCode).Select(m => m.AreaCode).ToList();
|
||
return Ok(DC.Set<WTM_CUSTOM>().Where(t => t.Area != null && userAreas.Contains(t.Area.AreaCode)).GetSelectListItems(Wtm, x => x.CustomAllName));
|
||
}
|
||
[ActionDescription("Sys.GetWTM_CUSTOM_PERSONs")]
|
||
[HttpGet("GetWTM_CUSTOM_PERSONs")]
|
||
public ActionResult GetWTM_CUSTOM_PERSONs()
|
||
{
|
||
return Ok(DC.Set<WTM_CUSTOM_PERSON>().GetSelectListItems(Wtm, x => x.PersonName));
|
||
}
|
||
[ActionDescription("Sys.GetWTM_PROJECTs")]
|
||
[HttpGet("GetWTM_PROJECTs")]
|
||
public ActionResult GetWTM_PROJECTs()
|
||
{
|
||
var userProjects = DC.Set<WTM_USER_PROJECT>().Where(t => t.UserCode == Wtm.LoginUserInfo.ITCode).Select(m => m.ProjectCode).ToList();
|
||
return Ok(DC.Set<WTM_PROJECT>().Where(x => userProjects.Contains(x.ProjectCode)).GetSelectListItems(Wtm, x => x.ProjectName));
|
||
}
|
||
[ActionDescription("Sys.GetFrameworkUsers")]
|
||
[HttpGet("GetFrameworkUsers")]
|
||
public ActionResult GetFrameworkUsers()
|
||
{
|
||
return Ok(DC.Set<FrameworkUser>().OrderBy(m=>m.DisplayOrder).GetSelectListItems(Wtm, x => x.Name,null,false, false));
|
||
}
|
||
[ActionDescription("Sys.GetWTM_AREAs")]
|
||
[HttpGet("GetWTM_AREAs")]
|
||
public ActionResult GetWTM_AREAs()
|
||
{
|
||
var userAreas = DC.Set<WTM_USER_AREA>().Where(t => t.UserCode == Wtm.LoginUserInfo.ITCode).Select(m => m.AreaCode).ToList();
|
||
return Ok(DC.Set<WTM_AREA>().Where(x => userAreas.Contains(x.AreaCode)).GetSelectListItems(Wtm, x => x.AreaName, null, false, false));
|
||
}
|
||
|
||
}
|
||
}
|