| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- using System;
- using System.Collections.Generic;
- using System.Common;
- using System.Common.Helpers;
- using System.Data;
- using System.IRepositories;
- using System.Linq;
- using System.Model;
- using System.Security.Claims;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using SqlSugar;
- using TVShoppingCallCenter_ZLJ.Models.Inputs;
- namespace TVShoppingCallCenter_ZLJ.Controllers.TaskManagement
- {
- [Produces("application/json")]
- [Route("api/[controller]")]
- public class TaskManagementController : BaseController
- {
- private readonly ISys_UserAccountRepository _sys_useraccountRepository;
- private readonly ISys_TaskManagementRepository _sys_taskmanagement_Repository;
- private readonly ISys_DepartmentRepository _sys_departmentRepository;
- public TaskManagementController(ISys_TaskManagementRepository sys_taskmanagement_Repository, ISys_DepartmentRepository sys_departmentRepository,
- ISys_UserAccountRepository sys_useraccountRepository)
- {
- _sys_taskmanagement_Repository = sys_taskmanagement_Repository;
- _sys_departmentRepository = sys_departmentRepository;
- _sys_useraccountRepository = sys_useraccountRepository;
- }
- /// <summary>
- /// 添加任务
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [HttpPost("add")]
- public async Task<IActionResult> AddAsync(T_Sys_TaskManagement input)
- {
- if (string.IsNullOrEmpty(input.F_Name))
- return Error("请输入任务名称");
- if (string.IsNullOrEmpty(input.F_Content))
- return Error("请输入任务内容");
- if (input.F_StartTime == null)
- return Error("请选择任务开始时间");
- if (input.F_EndTime == null)
- return Error("请选择任务结束时间");
- if (input.F_Type < 0)
- return Error("请选择任务类型");
- if (string .IsNullOrEmpty (input .F_Deptid ))
- return Error("请选择任务部门");
- if (input.F_Money < 0)
- return Error("请输入目标金额");
- // string user = "8000";
- if (input.F_Remindertime == null)
- input.F_Remindertime = input.F_EndTime.Value.AddDays(-3);
- if (input.F_Currentamount > 0)
- input.F_Taskprogress = string.Format("{0:f2}", input.F_Currentamount / input.F_Money * 100);
- else
- input.F_Taskprogress = "0";
- string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- input.F_CreateTime = DateTime.Now;
- input.F_CreateUser = user;
- input.F_IsDelete = 0;
- var res = await _sys_taskmanagement_Repository.Add(input);
- if (res > 0)
- {
- return Success("添加成功");
- }
- else
- {
- return Error("添加失败");
- }
- }
- /// <summary>
- /// 修改任务
- /// </summary>
- [HttpPost("update")]
- public async Task<IActionResult> UpdateAsync(T_Sys_TaskManagement input)
- {
- if (input.F_ID <= 0)
- return Error("参数错误");
- if (string.IsNullOrEmpty(input.F_Name))
- return Error("请输入任务名称");
- if (string.IsNullOrEmpty(input.F_Content))
- return Error("请输入任务内容");
- if (input.F_StartTime == null)
- return Error("请选择任务开始时间");
- if (input.F_EndTime == null)
- return Error("请选择任务结束时间");
- if (input.F_Type < 0)
- return Error("请选择任务类型");
- if (string.IsNullOrEmpty(input.F_Deptid))
- return Error("请选择任务部门");
- if (input.F_Money < 0)
- return Error("请输入目标金额");
- var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == input.F_ID);
- if (model == null)
- return Error("操作失败");
- if (input.F_Remindertime == null)
- input.F_Remindertime = input.F_EndTime.Value.AddDays(-3);
- if (input.F_Currentamount > 0)
- input.F_Taskprogress = string.Format("{0:f2}", input.F_Currentamount / input.F_Money * 100);
- else
- input.F_Taskprogress = "0";
- // string user = "8000";
- string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- input.F_IsDelete = 0;
- input.F_CreateUser = model.F_CreateUser;
- input.F_CreateTime = model.F_CreateTime ;
- input.F_UpdateTime = DateTime.Now;
- input.F_UpdateUser = user;
- // model.F_UpdateUser = "8000";
- var b = await _sys_taskmanagement_Repository.Update(input);
- if (b)
- return Success("修改成功");
- return Error("修改失败");
- }
- /// <summary>
- /// 删除任务
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- [HttpPost("delete")]
- public async Task<IActionResult> Remove(int[] ids)
- {
- var res = 0;
- if (ids != null && ids.Length > 0)
- {
- foreach (var item in ids)
- {
- var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == item);
- model.F_IsDelete = (int)EnumUserCountState.Delete;
- model.F_DeleteTime = DateTime.Now.ToLocalTime();
- model.F_DeleteUser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- if (_sys_taskmanagement_Repository.Update(model).Result)
- res += 1;
- }
- if (res == ids.Length)
- return Success("删除成功");
- else if (res > 0 && res < ids.Length)
- return Error("部分删除失败,请查看后重新操作");
- else
- return Error("删除失败,请查看后重新操作");
- }
- else
- return Error("请选择要删除的记录");
- }
- /// <summary>
- /// 操作任务
- /// </summary>
- /// <param name="id">id</param>
- /// <returns></returns>
- [HttpGet("operation")]
- public async Task<IActionResult> GetOperationAsync(int id, int state)
- {
- if (id <= 0)
- return Error("参数错误");
- var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == id);
- if (model == null)
- {
- return Error("获取失败");
- }
- model.F_State = state;
- var obj = await _sys_taskmanagement_Repository.Update(model);
- if (obj)
- return Success("操作成功");
- else
- return Error("操作失败");
- }
- /// <summary>
- /// 获取任务列表
- /// </summary>
- /// <param name="keyword"></param>
- /// <param name="pageindex"></param>
- /// <param name="pagesize"></param>
- /// <returns></returns>
- [HttpGet("getlist")]
- public async Task<IActionResult> GetListMark(string keyword, string name,string parentname, string starttime, string endtime, string tasker,
- string deptid , int parentid=0,int type = -1, int pageindex = 1, int pagesize = 20)
- {
- List<IConditionalModel> conModels = new List<IConditionalModel>();
- #region 条件筛选
- conModels.Add(new ConditionalModel() { FieldName = "F_IsDelete", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumUserCountState.Enabled).ToString() });
- if (!string.IsNullOrEmpty(name))
- {
- conModels.Add(new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = name });
- }
- if (!string.IsNullOrEmpty(tasker))
- {
- if (_sys_useraccountRepository .GetSingle(x => x.F_UserName == tasker).Result != null)
- {
- conModels.Add(new ConditionalModel() { FieldName = "F_Tasker", ConditionalType = ConditionalType.Like, FieldValue = _sys_useraccountRepository.GetSingle(x => x.F_UserName == tasker).Result.F_UserId .ToString () });
- }
- else
- {
- return Error("请输入正确的任务人");
- }
- }
- if (!string.IsNullOrEmpty(parentname))
- {
- if (_sys_taskmanagement_Repository.GetSingle (x=>x .F_Name == parentname).Result!=null )
- {
- conModels.Add(new ConditionalModel() { FieldName = "F_Parentid", ConditionalType = ConditionalType.Like, FieldValue = _sys_taskmanagement_Repository.GetSingle(x => x.F_Name == parentname).Result.F_ID .ToString () });
- }
- else
- {
- return Error("请输入正确的父级任务名称");
- }
- }
- if (type > -1)
- {
- conModels.Add(new ConditionalModel() { FieldName = "F_Type", ConditionalType = ConditionalType.Like, FieldValue = type.ToString() });
- }
- conModels.Add(new ConditionalModel() { FieldName = "F_Parentid", ConditionalType = ConditionalType.Equal , FieldValue = parentid.ToString() });
- if (!string.IsNullOrEmpty(keyword))
- {
- conModels.Add(new ConditionalCollections()
- {
- ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
- {
- new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = keyword }),
- new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Content", ConditionalType = ConditionalType.Like, FieldValue = keyword })
- }
- });
- }
- if (!string.IsNullOrEmpty(starttime))
- {
- conModels.Add(new ConditionalModel() { FieldName = "F_StartTime", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = starttime });
- }
- if (!string.IsNullOrEmpty(endtime))
- {
- conModels.Add(new ConditionalModel() { FieldName = "F_EndTime", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = endtime });
- }
- string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- var dept = _sys_useraccountRepository.GetSingle(x => x.F_UserCode == user).Result != null ? _sys_useraccountRepository.GetSingle(x => x.F_UserCode == user).Result.F_DeptId : 0;
- if (!string .IsNullOrEmpty (deptid))
- {
- conModels.Add(new ConditionalModel() { FieldName = "F_Deptid", ConditionalType = ConditionalType.Equal, FieldValue = deptid });
- }
- else
- {
- conModels.Add(new ConditionalModel() { FieldName = "F_CreateUser", ConditionalType = ConditionalType.Equal , FieldValue = user });
- new KeyValuePair<WhereType, ConditionalModel>(WhereType.Or, new ConditionalModel() { FieldName = "F_Deptid", ConditionalType = ConditionalType.Like, FieldValue = dept.ToString() });
- }
- #endregion
- int recordCount = 0;
- var list = await _sys_taskmanagement_Repository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount });
- var obj = new
- {
- state = "success",
- message = "成功",
- rows = ConvertAsync(list.Rows ),
- total = list.Totals,
- };
- return Content(obj.ToJson());
- }
- /// <summary>
- /// 获取任务详情
- /// </summary>
- /// <param name="id">id</param>
- /// <returns></returns>
- [HttpGet("getdetails")]
- public async Task<IActionResult> GetDetailsAsync(int id)
- {
- if (id <= 0)
- return Error("参数错误");
- var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == id);
- if (model == null)
- {
- return Error("获取失败");
- }
- return Success("获取成功!", Convert(model, 1));
- }
- /// <summary>
- /// 修改任务当前金额
- /// </summary>
- /// <param name="id"></param>
- /// <param name="money"></param>
- /// <returns></returns>
- public int UpdateTaskAmount(T_Bus_Order t_Bus_Order )
- {
- //0成功1任务暂停2任务终止3修改任务金额失败4任务不存在5父级任务不存在6父级任务修改失败7父级任务暂停8父级任务终止
- int n = 0;
-
- var model = _sys_taskmanagement_Repository.GetListALL (x => x.F_Tasker == t_Bus_Order.F_AddUser&& x.F_IsDelete ==0);
- if (model == null)
- return 4;
- foreach (var it in model.Result )
- {
- if (it.F_State == 0)
- {
- it.F_Currentamount +=(float ) t_Bus_Order.F_RealPrice;
- it.F_Taskprogress = string.Format("{0:f2}", it.F_Currentamount / it.F_Money * 100);
- var res = _sys_taskmanagement_Repository.Update(it);
- if (!res.Result)
- return 3;
- if (it.F_Parentid > 0)
- {
- n = TreeRecursion(it.F_Parentid, (float)t_Bus_Order.F_RealPrice);
- }
- }
- else
- n = (int)it .F_State;
- }
-
- return n;
- }
- /// <summary>
- /// 递归算法
- /// </summary>
- /// <param name="data"></param>
- /// <param name="parentId"></param>
- /// <returns></returns>
- private int TreeRecursion( int parentId = 0,float money=0)
- {
- int n = 0;
- var model = _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == parentId).Result;
- if (model == null)
- return 5;
- if (model.F_State == 0)
- {
- model.F_Currentamount += money;
- model.F_Taskprogress = string.Format("{0:f2}", model.F_Currentamount / model.F_Money * 100);
- var res = _sys_taskmanagement_Repository.Update(model);
- if (!res.Result)
- return 6;
- if (model.F_Parentid > 0)
- {
- n = TreeRecursion(model.F_Parentid, money);
- }
- }
- else
- n = (int)model.F_State+6;
- return n;
- }
- /// <summary>
- /// 上传文件并导入数据库
- /// </summary>
- /// <returns></returns>
- [HttpPost("importexcel")]
- public async Task<IActionResult> ImportExcel(int headrow = 0,int parentid=0)
- {
- Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
- if (!_upfile.ContentType.Equals("application/vnd.ms-excel") && !_upfile.ContentType.Equals("application/x-xls") && !_upfile.ContentType.Equals("application/x-xlsx") && !_upfile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && !_upfile.ContentType.Equals("application/octet-stream"))
- return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
- NPOIHelper npoi = new NPOIHelper();
- var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
- int num = dtExcel.Rows.Count;
- var cols = dtExcel.Columns;
- int colnum = cols.Count;
- string errmsg = string.Empty;
- if (num > 0)
- {
- int index = 1;
- foreach (DataRow dr in dtExcel.Rows)
- {
- var model = new T_Sys_TaskManagement();
- string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- model.F_Name = dr["任务名称"].ToString();
- model.F_Content = dr["任务内容"].ToString();
- if (string .IsNullOrEmpty (dr["任务开始时间"].ToString()))
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!请输入任务开始时间";
- continue;
- }
- if (string.IsNullOrEmpty(dr["任务结束时间"].ToString()))
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!请输入任务结束时间";
- continue;
- }
- if (string.IsNullOrEmpty(dr["目标金额"].ToString()))
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!请输入目标金额";
- continue;
- }
- try
- {
- model.F_StartTime = DateTime.Parse(dr["任务开始时间"].ToString());
- }
- catch
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!任务开始时间格式错误";
- continue;
- }
- try
- {
- model.F_EndTime = DateTime.Parse(dr["任务结束时间"].ToString());
- }
- catch
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!任务结束时间格式错误";
- continue;
- }
- if (dr["任务类型"].ToString() == "月")
- model.F_Type = 2;
- else if (dr["任务类型"].ToString() == "周")
- model.F_Type = 1;
- else
- model.F_Type = 0;
- if (_sys_departmentRepository.GetSingle(x => x.F_DeptName == dr["任务部门"].ToString()).Result !=null )
- {
- model.F_Deptid = _sys_departmentRepository.GetSingle(x => x.F_DeptName == dr["任务部门"].ToString()).Result.F_DeptId.ToString ();
- }
- else
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!请检查任务部门";
- continue;
- }
- if (_sys_useraccountRepository.GetSingle(x => x.F_UserName == dr["任务人"].ToString()).Result != null)
- {
- model.F_Tasker = _sys_useraccountRepository.GetSingle(x => x.F_UserName == dr["任务人"].ToString()).Result.F_UserId ;
-
- }
- else
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!请检查任务人";
- continue;
- }
- try
- {
- model.F_Money = float .Parse(dr["目标金额"].ToString());
- }
- catch
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!请输入正确的目标金额";
- continue;
- }
- if (string.IsNullOrEmpty(dr["当前金额"].ToString()))
- model.F_Currentamount = 0;
- try
- {
- model.F_Currentamount = float.Parse(dr["当前金额"].ToString());
- }
- catch
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!请输入正确的当前金额";
- continue;
- }
- if (!string .IsNullOrEmpty (dr["任务提醒时间"].ToString()))
- {
- try
- {
- model.F_Remindertime = DateTime.Parse(dr["任务提醒时间"].ToString());
- }
- catch
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!任务提醒时间格式错误";
- continue;
- }
- }
- else
- {
- model.F_Remindertime = model.F_EndTime.Value.AddDays(-3);
- }
- model.F_Taskprogress = string.Format("{0:f2}", model.F_Currentamount / model.F_Money * 100);
- if (dr["任务类型"].ToString() == "终止")
- model.F_State = 2;
- else if (dr["任务类型"].ToString() == "暂停")
- model.F_State = 1;
- else
- model.F_State = 0;
- model.F_CreateTime = DateTime.Now;
- model.F_CreateUser = user;
- model.F_IsDelete = 0;
- model.F_Parentid = parentid;
- int b = await _sys_taskmanagement_Repository.Add(model);
- if (b <= 0)
- {
- if (!string.IsNullOrEmpty(errmsg))
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!";
- }
- else
- {
- errmsg = "第" + index + "行导入失败!";
- }
- }
- index++;
- }
- }
- else
- {
- return Error("文件中无数据");
- }
- if (!string.IsNullOrEmpty(errmsg))
- {
- return Error(errmsg);
- }
- return Success("导入成功");
- }
- #region 私有方法
- private TaskManagementInput Convert(T_Sys_TaskManagement it, int type = 0)
- {
- TaskManagementInput task = new TaskManagementInput();
- task.F_ID = it.F_ID;
- task.F_Name = it.F_Name;
- task.F_Content = it.F_Content;
- task.F_StartTime = it.F_StartTime;
- task.F_EndTime = it.F_EndTime;
- task.F_Type = it.F_Type;
- task.F_Deptid = it.F_Deptid;
- string deptmsg = "";
- if (!string.IsNullOrEmpty(it.F_Deptid))
- {
- try
- {
- string[] sprit = it.F_Deptid.Split(',');
- foreach (var iv in sprit)
- {
- if (deptmsg == "")
- deptmsg = _sys_departmentRepository.GetSingle(x => x.F_DeptId == int.Parse(iv)).Result != null ? _sys_departmentRepository.GetSingle(x => x.F_DeptId == int.Parse(iv)).Result.F_DeptName : "";
- else
- deptmsg = deptmsg + "," + _sys_departmentRepository.GetSingle(x => x.F_DeptId == int.Parse(iv)).Result != null ? _sys_departmentRepository.GetSingle(x => x.F_DeptId == int.Parse(iv)).Result.F_DeptName : "";
- }
- }
- catch
- {
- }
- }
- task.F_DeptName = deptmsg;// 部门名称
- task.F_Tasker = it.F_Tasker;
- task.F_TaskerName = _sys_useraccountRepository.GetSingle(x => x.F_UserId == it.F_Tasker).Result != null ? _sys_useraccountRepository.GetSingle(x => x.F_UserId == it.F_Tasker).Result .F_UserName : "";
- task.F_Money = it.F_Money;
- task.F_Currentamount = it.F_Currentamount;
- task.F_Taskprogress = it.F_Taskprogress;
- task.F_State = it.F_State;
- task.F_Remindertime = it.F_Remindertime;
- if (type ==0)
- {
- var taskcount = _sys_taskmanagement_Repository.GetListALL(x => x.F_Parentid == it.F_ID&&x.F_IsDelete ==0).Result != null ? _sys_taskmanagement_Repository.GetListALL(x => x.F_Parentid == it.F_ID && x.F_IsDelete == 0).Result: null ;
- if (taskcount!=null )
- {
- task.children = ConvertAsync(taskcount);
- }
- }
-
- task.F_Parentid = it.F_Parentid;
- task.F_ParentName = _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == it.F_Parentid).Result != null ? _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == it.F_Parentid).Result.F_Name : "";// 部门名称
- return task;
- }
- /// model转input
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- private List<TaskManagementInput> ConvertAsync(List<T_Sys_TaskManagement> model)
- {
- List<TaskManagementInput> input = new List<TaskManagementInput>();
- if (model != null)
- {
- foreach (var it in model)
- {
- TaskManagementInput marketing = new TaskManagementInput();
- marketing = Convert(it);
- input.Add(marketing);
- }
- }
- return input;
- }
- #endregion
- }
- }
|