足力健后端,使用.netcore版本,合并1个项目使用

TaskManagementController.cs 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Common;
  4. using System.Common.Helpers;
  5. using System.Data;
  6. using System.IRepositories;
  7. using System.Linq;
  8. using System.Model;
  9. using System.Security.Claims;
  10. using System.Threading.Tasks;
  11. using Microsoft.AspNetCore.Mvc;
  12. using SqlSugar;
  13. using TVShoppingCallCenter_ZLJ.Models.Inputs;
  14. namespace TVShoppingCallCenter_ZLJ.Controllers.TaskManagement
  15. {
  16. [Produces("application/json")]
  17. [Route("api/[controller]")]
  18. public class TaskManagementController : BaseController
  19. {
  20. private readonly ISMS_InternalMessagesReposytory _sys_internalMessagesrepository;
  21. private readonly ISys_UserAccountRepository _sys_useraccountRepository;
  22. private readonly ISys_TaskManagementRepository _sys_taskmanagement_Repository;
  23. private readonly ISys_DepartmentRepository _sys_departmentRepository;
  24. public TaskManagementController(ISys_TaskManagementRepository sys_taskmanagement_Repository, ISys_DepartmentRepository sys_departmentRepository,
  25. ISys_UserAccountRepository sys_useraccountRepository, ISMS_InternalMessagesReposytory sys_internalMessagesrepository)
  26. {
  27. _sys_taskmanagement_Repository = sys_taskmanagement_Repository;
  28. _sys_departmentRepository = sys_departmentRepository;
  29. _sys_useraccountRepository = sys_useraccountRepository;
  30. _sys_internalMessagesrepository = sys_internalMessagesrepository;
  31. }
  32. /// <summary>
  33. /// 添加任务
  34. /// </summary>
  35. /// <param name="input"></param>
  36. /// <returns></returns>
  37. [HttpPost("add")]
  38. public async Task<IActionResult> AddAsync(T_Sys_TaskManagement input)
  39. {
  40. if (string.IsNullOrEmpty(input.F_Name))
  41. return Error("请输入任务名称");
  42. if (string.IsNullOrEmpty(input.F_Content))
  43. return Error("请输入任务内容");
  44. if (input.F_StartTime == null)
  45. return Error("请选择任务开始时间");
  46. if (input.F_EndTime == null)
  47. return Error("请选择任务结束时间");
  48. if (input.F_Type < 0)
  49. return Error("请选择任务类型");
  50. if (string .IsNullOrEmpty (input .F_Deptid ))
  51. return Error("请选择任务部门");
  52. if (input.F_Money < 0)
  53. return Error("请输入目标金额");
  54. // string user = "8000";
  55. if (input.F_Remindertime == null)
  56. input.F_Remindertime = input.F_EndTime.Value.AddDays(-3);
  57. if (input.F_Currentamount > 0)
  58. input.F_Taskprogress = string.Format("{0:f2}", input.F_Currentamount / input.F_Money * 100);
  59. else
  60. input.F_Taskprogress = "0";
  61. string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  62. input.F_CreateTime = DateTime.Now;
  63. input.F_CreateUser = user;
  64. input.F_IsDelete = 0;
  65. var res = await _sys_taskmanagement_Repository.Add(input);
  66. if (res > 0)
  67. {
  68. if (input .F_Parentid == res)
  69. {
  70. input.F_Parentid = 0;
  71. var v= _sys_taskmanagement_Repository.Update(input);
  72. }
  73. try
  74. {
  75. DateTime time = DateTime.Parse(input.F_Remindertime.ToString());
  76. if (input .F_Tasker >0)
  77. {
  78. string usercode = _sys_useraccountRepository.GetSingle(x => x.F_UserId == input.F_Tasker).Result.F_UserCode;
  79. bool n = new InternalMessages.InternalMessagesController(_sys_internalMessagesrepository, _sys_useraccountRepository).AddInternalMessagesInfo(res, "任务提醒", input.F_Name + "任务提醒", user, time, 2, 1, 0, usercode, 1);
  80. }
  81. else
  82. {
  83. bool n = new InternalMessages.InternalMessagesController(_sys_internalMessagesrepository, _sys_useraccountRepository).AddInternalMessagesInfo(res, "任务提醒", input.F_Name + "任务提醒", user, time, 2, 1, int.Parse(input.F_Deptid.Split(',')[input.F_Deptid.Split(',').Length - 1]), "", 0);
  84. }
  85. }
  86. catch
  87. {
  88. }
  89. return Success("添加成功");
  90. }
  91. else
  92. {
  93. return Error("添加失败");
  94. }
  95. }
  96. /// <summary>
  97. /// 修改任务
  98. /// </summary>
  99. [HttpPost("update")]
  100. public async Task<IActionResult> UpdateAsync(T_Sys_TaskManagement input)
  101. {
  102. if (input.F_ID <= 0)
  103. return Error("参数错误");
  104. if (input.F_Parentid == input.F_ID)
  105. return Error("父级ID不能和子ID相同");
  106. if (string.IsNullOrEmpty(input.F_Name))
  107. return Error("请输入任务名称");
  108. if (string.IsNullOrEmpty(input.F_Content))
  109. return Error("请输入任务内容");
  110. if (input.F_StartTime == null)
  111. return Error("请选择任务开始时间");
  112. if (input.F_EndTime == null)
  113. return Error("请选择任务结束时间");
  114. if (input.F_Type < 0)
  115. return Error("请选择任务类型");
  116. if (string.IsNullOrEmpty(input.F_Deptid))
  117. return Error("请选择任务部门");
  118. if (input.F_Money < 0)
  119. return Error("请输入目标金额");
  120. var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == input.F_ID);
  121. if (model == null)
  122. return Error("操作失败");
  123. if (input.F_Remindertime == null)
  124. input.F_Remindertime = input.F_EndTime.Value.AddDays(-3);
  125. if (input.F_Currentamount > 0)
  126. input.F_Taskprogress = string.Format("{0:f2}", input.F_Currentamount / input.F_Money * 100);
  127. else
  128. input.F_Taskprogress = "0";
  129. // string user = "8000";
  130. string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  131. input.F_IsDelete = 0;
  132. input.F_CreateUser = model.F_CreateUser;
  133. input.F_CreateTime = model.F_CreateTime ;
  134. input.F_UpdateTime = DateTime.Now;
  135. input.F_UpdateUser = user;
  136. // model.F_UpdateUser = "8000";
  137. var b = await _sys_taskmanagement_Repository.Update(input);
  138. if (b)
  139. {
  140. DateTime time = DateTime.Parse(input.F_Remindertime.ToString());
  141. var InternalMessages = _sys_internalMessagesrepository.GetListALL (x => x.F_ID == model.F_ID&&x.SMS_IsDelete ==0).Result.FirstOrDefault ();
  142. if (InternalMessages!=null )
  143. {
  144. try
  145. {
  146. if (input.F_Tasker > 0)
  147. {
  148. string usercode = _sys_useraccountRepository.GetSingle(x => x.F_UserId == input.F_Tasker).Result.F_UserCode;
  149. InternalMessages.SMS_Content = input.F_Name + "任务提醒";
  150. InternalMessages.SMS_Remindertime = input.F_Remindertime;
  151. InternalMessages.SMS_ReceiveUserCode = usercode;
  152. bool n = await _sys_internalMessagesrepository.Update(InternalMessages);
  153. }
  154. else
  155. {
  156. InternalMessages.SMS_Content = input.F_Name + "任务提醒";
  157. InternalMessages.SMS_Remindertime = input.F_Remindertime;
  158. InternalMessages.SMS_Deptid = int.Parse(input.F_Deptid.Split(',')[input.F_Deptid.Split(',').Length - 1]);
  159. bool n = await _sys_internalMessagesrepository.Update(InternalMessages);
  160. }
  161. }
  162. catch
  163. {
  164. }
  165. }
  166. else
  167. {
  168. try
  169. {
  170. if (input.F_Tasker > 0)
  171. {
  172. string usercode = _sys_useraccountRepository.GetSingle(x => x.F_UserId == input.F_Tasker).Result.F_UserCode;
  173. bool n = new InternalMessages.InternalMessagesController(_sys_internalMessagesrepository, _sys_useraccountRepository).AddInternalMessagesInfo(model.F_ID, "任务提醒", input.F_Name + "任务提醒", user, time, 2, 1, 0, usercode, 1);
  174. }
  175. else
  176. {
  177. bool n = new InternalMessages.InternalMessagesController(_sys_internalMessagesrepository, _sys_useraccountRepository).AddInternalMessagesInfo(model.F_ID, "任务提醒", input.F_Name + "任务提醒", user, time, 2, 1, int.Parse(input.F_Deptid.Split(',')[input.F_Deptid.Split(',').Length - 1]), "", 0);
  178. }
  179. }
  180. catch
  181. {
  182. }
  183. }
  184. return Success("修改成功");
  185. }
  186. return Error("修改失败");
  187. }
  188. /// <summary>
  189. /// 删除任务
  190. /// </summary>
  191. /// <param name="ids"></param>
  192. /// <returns></returns>
  193. [HttpPost("delete")]
  194. public async Task<IActionResult> Remove(int[] ids)
  195. {
  196. var res = 0;
  197. if (ids != null && ids.Length > 0)
  198. {
  199. foreach (var item in ids)
  200. {
  201. var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == item);
  202. var message = await _sys_internalMessagesrepository.GetListALL(x => x.F_ID == item);
  203. if (message != null)
  204. {
  205. foreach (var it in message)
  206. {
  207. it.SMS_IsDelete = (int)EnumUserCountState.Delete;
  208. var n = _sys_internalMessagesrepository.Update(it);
  209. }
  210. }
  211. model.F_IsDelete = (int)EnumUserCountState.Delete;
  212. model.F_DeleteTime = DateTime.Now.ToLocalTime();
  213. model.F_DeleteUser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  214. if (_sys_taskmanagement_Repository.Update(model).Result)
  215. res += 1;
  216. }
  217. if (res == ids.Length)
  218. return Success("删除成功");
  219. else if (res > 0 && res < ids.Length)
  220. return Error("部分删除失败,请查看后重新操作");
  221. else
  222. return Error("删除失败,请查看后重新操作");
  223. }
  224. else
  225. return Error("请选择要删除的记录");
  226. }
  227. /// <summary>
  228. /// 操作任务
  229. /// </summary>
  230. /// <param name="id">id</param>
  231. /// <returns></returns>
  232. [HttpGet("operation")]
  233. public async Task<IActionResult> GetOperationAsync(int id, int state)
  234. {
  235. if (id <= 0)
  236. return Error("参数错误");
  237. var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == id);
  238. if (model == null)
  239. {
  240. return Error("获取失败");
  241. }
  242. model.F_State = state;
  243. var obj = await _sys_taskmanagement_Repository.Update(model);
  244. if (obj)
  245. return Success("操作成功");
  246. else
  247. return Error("操作失败");
  248. }
  249. /// <summary>
  250. /// 获取任务列表
  251. /// </summary>
  252. /// <param name="keyword"></param>
  253. /// <param name="pageindex"></param>
  254. /// <param name="pagesize"></param>
  255. /// <returns></returns>
  256. [HttpGet("getlist")]
  257. public async Task<IActionResult> GetListMark(string keyword, string name, string starttime, string endtime, string tasker,
  258. string deptid , int parentid=0,int type = -1, int pageindex = 1, int pagesize = 20)
  259. {
  260. List<IConditionalModel> conModels = new List<IConditionalModel>();
  261. #region 条件筛选
  262. conModels.Add(new ConditionalModel() { FieldName = "F_IsDelete", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumUserCountState.Enabled).ToString() });
  263. if (!string.IsNullOrEmpty(name))
  264. {
  265. conModels.Add(new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = name });
  266. }
  267. if (!string.IsNullOrEmpty(tasker))
  268. {
  269. if (_sys_useraccountRepository .GetListALL(x => x.F_UserName == tasker&&x.F_DeleteFlag ==0).Result .FirstOrDefault ()!= null)
  270. {
  271. conModels.Add(new ConditionalModel() { FieldName = "F_Tasker", ConditionalType = ConditionalType.Equal , FieldValue = _sys_useraccountRepository.GetListALL (x => x.F_UserName == tasker && x.F_DeleteFlag == 0).Result.FirstOrDefault ().F_UserId .ToString () });
  272. }
  273. else
  274. {
  275. return Error("请输入正确的任务人");
  276. }
  277. }
  278. if (string .IsNullOrEmpty (tasker)&& string.IsNullOrEmpty(keyword))
  279. {
  280. conModels.Add(new ConditionalModel() { FieldName = "F_Parentid", ConditionalType = ConditionalType.Equal, FieldValue = parentid.ToString() });
  281. }
  282. // string user = "8000";
  283. string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  284. var dept = _sys_useraccountRepository.GetSingle(x => x.F_UserCode == user&&x.F_DeleteFlag ==0).Result != null ? _sys_useraccountRepository.GetSingle(x => x.F_UserCode == user&& x.F_DeleteFlag == 0).Result.F_DeptId : 0;
  285. if (!string.IsNullOrEmpty(deptid))
  286. {
  287. conModels.Add(new ConditionalModel() { FieldName = "F_Deptid", ConditionalType = ConditionalType.Equal, FieldValue = deptid });
  288. }
  289. else
  290. {
  291. conModels.Add(new ConditionalModel() { FieldName = "F_CreateUser", ConditionalType = ConditionalType.Equal, FieldValue = user });
  292. new KeyValuePair<WhereType, ConditionalModel>(WhereType.Or, new ConditionalModel() { FieldName = "F_Deptid", ConditionalType = ConditionalType.Like, FieldValue = dept.ToString() });
  293. deptid = dept.ToString();
  294. }
  295. if (type > -1)
  296. {
  297. conModels.Add(new ConditionalModel() { FieldName = "F_Type", ConditionalType = ConditionalType.Like, FieldValue = type.ToString() });
  298. }
  299. if (!string.IsNullOrEmpty(keyword))
  300. {
  301. conModels.Add(new ConditionalCollections()
  302. {
  303. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
  304. {
  305. new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = keyword }),
  306. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Content", ConditionalType = ConditionalType.Like, FieldValue = keyword })
  307. }
  308. });
  309. }
  310. if (!string.IsNullOrEmpty(starttime))
  311. {
  312. conModels.Add(new ConditionalModel() { FieldName = "F_StartTime", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = starttime });
  313. }
  314. if (!string.IsNullOrEmpty(endtime))
  315. {
  316. conModels.Add(new ConditionalModel() { FieldName = "F_EndTime", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = endtime });
  317. }
  318. #endregion
  319. int recordCount = 0;
  320. var list = await _sys_taskmanagement_Repository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount });
  321. var obj = new
  322. {
  323. state = "success",
  324. message = "成功",
  325. rows = ConvertAsync(list.Rows ),
  326. total = list.Totals,
  327. };
  328. return Content(obj.ToJson());
  329. }
  330. /// <summary>
  331. /// 获取任务详情
  332. /// </summary>
  333. /// <param name="id">id</param>
  334. /// <returns></returns>
  335. [HttpGet("getdetails")]
  336. public async Task<IActionResult> GetDetailsAsync(int id)
  337. {
  338. if (id <= 0)
  339. return Error("参数错误");
  340. var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == id);
  341. if (model == null)
  342. {
  343. return Error("获取失败");
  344. }
  345. return Success("获取成功!", Convert(model, 1));
  346. }
  347. /// <summary>
  348. /// 修改任务当前金额
  349. /// </summary>
  350. /// <param name="id"></param>
  351. /// <param name="money"></param>
  352. /// <returns></returns>
  353. public int UpdateTaskAmount(T_Bus_Order t_Bus_Order )
  354. {
  355. //0成功1任务暂停2任务终止3修改任务金额失败4任务不存在5父级任务不存在6父级任务修改失败7父级任务暂停8父级任务终止
  356. int n = 0;
  357. var model = _sys_taskmanagement_Repository.GetListALL (x => x.F_Tasker == t_Bus_Order.F_AddUser&& x.F_IsDelete ==0);
  358. if (model == null)
  359. return 4;
  360. foreach (var it in model.Result )
  361. {
  362. if (it.F_State == 0)
  363. {
  364. it.F_Currentamount +=(float ) t_Bus_Order.F_RealPrice;
  365. it.F_Taskprogress = string.Format("{0:f2}", it.F_Currentamount / it.F_Money * 100);
  366. var res = _sys_taskmanagement_Repository.Update(it);
  367. if (!res.Result)
  368. return 3;
  369. if (it.F_Parentid > 0)
  370. {
  371. n = TreeRecursion(it.F_Parentid, (float)t_Bus_Order.F_RealPrice);
  372. }
  373. }
  374. else
  375. n = (int)it .F_State;
  376. }
  377. return n;
  378. }
  379. /// <summary>
  380. /// 递归算法
  381. /// </summary>
  382. /// <param name="data"></param>
  383. /// <param name="parentId"></param>
  384. /// <returns></returns>
  385. private int TreeRecursion( int parentId = 0,float money=0)
  386. {
  387. int n = 0;
  388. var model = _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == parentId).Result;
  389. if (model == null)
  390. return 5;
  391. if (model.F_State == 0)
  392. {
  393. model.F_Currentamount += money;
  394. model.F_Taskprogress = string.Format("{0:f2}", model.F_Currentamount / model.F_Money * 100);
  395. var res = _sys_taskmanagement_Repository.Update(model);
  396. if (!res.Result)
  397. return 6;
  398. if (model.F_Parentid > 0)
  399. {
  400. n = TreeRecursion(model.F_Parentid, money);
  401. }
  402. }
  403. else
  404. n = (int)model.F_State+6;
  405. return n;
  406. }
  407. /// <summary>
  408. /// 上传文件并导入数据库
  409. /// </summary>
  410. /// <returns></returns>
  411. [HttpPost("importexcel")]
  412. public async Task<IActionResult> ImportExcel(int headrow = 0,int parentid=0)
  413. {
  414. Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
  415. 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"))
  416. return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
  417. NPOIHelper npoi = new NPOIHelper();
  418. var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
  419. int num = dtExcel.Rows.Count;
  420. var cols = dtExcel.Columns;
  421. int colnum = cols.Count;
  422. string errmsg = string.Empty;
  423. if (num > 0)
  424. {
  425. int index = 1;
  426. foreach (DataRow dr in dtExcel.Rows)
  427. {
  428. var model = new T_Sys_TaskManagement();
  429. string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  430. model.F_Name = dr["任务名称"].ToString();
  431. model.F_Content = dr["任务内容"].ToString();
  432. if (string .IsNullOrEmpty (dr["任务开始时间"].ToString()))
  433. {
  434. errmsg = errmsg + "\r\n第" + index + "行导入失败!请输入任务开始时间";
  435. continue;
  436. }
  437. if (string.IsNullOrEmpty(dr["任务结束时间"].ToString()))
  438. {
  439. errmsg = errmsg + "\r\n第" + index + "行导入失败!请输入任务结束时间";
  440. continue;
  441. }
  442. if (string.IsNullOrEmpty(dr["目标金额"].ToString()))
  443. {
  444. errmsg = errmsg + "\r\n第" + index + "行导入失败!请输入目标金额";
  445. continue;
  446. }
  447. try
  448. {
  449. model.F_StartTime = DateTime.Parse(dr["任务开始时间"].ToString());
  450. }
  451. catch
  452. {
  453. errmsg = errmsg + "\r\n第" + index + "行导入失败!任务开始时间格式错误";
  454. continue;
  455. }
  456. try
  457. {
  458. model.F_EndTime = DateTime.Parse(dr["任务结束时间"].ToString());
  459. }
  460. catch
  461. {
  462. errmsg = errmsg + "\r\n第" + index + "行导入失败!任务结束时间格式错误";
  463. continue;
  464. }
  465. if (dr["任务类型"].ToString() == "月")
  466. model.F_Type = 2;
  467. else if (dr["任务类型"].ToString() == "周")
  468. model.F_Type = 1;
  469. else
  470. model.F_Type = 0;
  471. if (_sys_departmentRepository.GetSingle(x => x.F_DeptName == dr["任务部门"].ToString()).Result !=null )
  472. {
  473. model.F_Deptid = _sys_departmentRepository.GetSingle(x => x.F_DeptName == dr["任务部门"].ToString()).Result.F_DeptId.ToString ();
  474. }
  475. else
  476. {
  477. errmsg = errmsg + "\r\n第" + index + "行导入失败!请检查任务部门";
  478. continue;
  479. }
  480. if (_sys_useraccountRepository.GetSingle(x => x.F_UserName == dr["任务人"].ToString()&&x.F_DeleteFlag ==0).Result != null)
  481. {
  482. model.F_Tasker = _sys_useraccountRepository.GetSingle(x => x.F_UserName == dr["任务人"].ToString() && x.F_DeleteFlag == 0).Result.F_UserId ;
  483. }
  484. else
  485. {
  486. errmsg = errmsg + "\r\n第" + index + "行导入失败!请检查任务人";
  487. continue;
  488. }
  489. try
  490. {
  491. model.F_Money = float .Parse(dr["目标金额"].ToString());
  492. }
  493. catch
  494. {
  495. errmsg = errmsg + "\r\n第" + index + "行导入失败!请输入正确的目标金额";
  496. continue;
  497. }
  498. if (string.IsNullOrEmpty(dr["当前金额"].ToString()))
  499. model.F_Currentamount = 0;
  500. try
  501. {
  502. model.F_Currentamount = float.Parse(dr["当前金额"].ToString());
  503. }
  504. catch
  505. {
  506. errmsg = errmsg + "\r\n第" + index + "行导入失败!请输入正确的当前金额";
  507. continue;
  508. }
  509. if (!string .IsNullOrEmpty (dr["任务提醒时间"].ToString()))
  510. {
  511. try
  512. {
  513. model.F_Remindertime = DateTime.Parse(dr["任务提醒时间"].ToString());
  514. }
  515. catch
  516. {
  517. errmsg = errmsg + "\r\n第" + index + "行导入失败!任务提醒时间格式错误";
  518. continue;
  519. }
  520. }
  521. else
  522. {
  523. model.F_Remindertime = model.F_EndTime.Value.AddDays(-3);
  524. }
  525. model.F_Taskprogress = string.Format("{0:f2}", model.F_Currentamount / model.F_Money * 100);
  526. if (dr["任务类型"].ToString() == "终止")
  527. model.F_State = 2;
  528. else if (dr["任务类型"].ToString() == "暂停")
  529. model.F_State = 1;
  530. else
  531. model.F_State = 0;
  532. model.F_CreateTime = DateTime.Now;
  533. model.F_CreateUser = user;
  534. model.F_IsDelete = 0;
  535. model.F_Parentid = parentid;
  536. int b = await _sys_taskmanagement_Repository.Add(model);
  537. if (b <= 0)
  538. {
  539. if (!string.IsNullOrEmpty(errmsg))
  540. {
  541. errmsg = errmsg + "\r\n第" + index + "行导入失败!";
  542. }
  543. else
  544. {
  545. errmsg = "第" + index + "行导入失败!";
  546. }
  547. }
  548. index++;
  549. }
  550. }
  551. else
  552. {
  553. return Error("文件中无数据");
  554. }
  555. if (!string.IsNullOrEmpty(errmsg))
  556. {
  557. return Error(errmsg);
  558. }
  559. return Success("导入成功");
  560. }
  561. #region 私有方法
  562. private TaskManagementInput Convert(T_Sys_TaskManagement it, int type = 0)
  563. {
  564. TaskManagementInput task = new TaskManagementInput();
  565. task.F_ID = it.F_ID;
  566. task.F_Name = it.F_Name;
  567. task.F_Content = it.F_Content;
  568. task.F_StartTime = it.F_StartTime;
  569. task.F_EndTime = it.F_EndTime;
  570. task.F_Type = it.F_Type;
  571. task.F_Deptid = it.F_Deptid;
  572. string deptmsg = "";
  573. if (!string.IsNullOrEmpty(it.F_Deptid))
  574. {
  575. try
  576. {
  577. string[] sprit = it.F_Deptid.Split(',');
  578. foreach (var iv in sprit)
  579. {
  580. if (deptmsg == "")
  581. 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 : "";
  582. else
  583. 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 : "";
  584. }
  585. }
  586. catch
  587. {
  588. }
  589. }
  590. task.F_DeptName = deptmsg;// 部门名称
  591. task.F_Tasker = it.F_Tasker;
  592. 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 : "";
  593. task.F_Money = it.F_Money;
  594. task.F_Currentamount = it.F_Currentamount;
  595. task.F_Taskprogress = it.F_Taskprogress;
  596. task.F_State = it.F_State;
  597. task.F_Remindertime = it.F_Remindertime;
  598. if (type ==0)
  599. {
  600. 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 ;
  601. if (taskcount!=null )
  602. {
  603. task.children = ConvertAsync(taskcount);
  604. }
  605. }
  606. task.F_Parentid = it.F_Parentid;
  607. 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 : "";// 部门名称
  608. return task;
  609. }
  610. /// model转input
  611. /// </summary>
  612. /// <param name="input"></param>
  613. /// <returns></returns>
  614. private List<TaskManagementInput> ConvertAsync(List<T_Sys_TaskManagement> model)
  615. {
  616. List<TaskManagementInput> input = new List<TaskManagementInput>();
  617. if (model != null)
  618. {
  619. foreach (var it in model)
  620. {
  621. TaskManagementInput marketing = new TaskManagementInput();
  622. marketing = Convert(it);
  623. input.Add(marketing);
  624. }
  625. }
  626. return input;
  627. }
  628. #endregion
  629. }
  630. }