PerformanceItemController.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. using DynamicExpresso;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.EntityFrameworkCore;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Linq.Expressions;
  10. using System.Threading.Tasks;
  11. using wispro.sp.api.Utility;
  12. using wispro.sp.entity;
  13. using wispro.sp.share;
  14. using wispro.sp.utility;
  15. namespace wispro.sp.api.Controllers
  16. {
  17. [Route("api/[controller]/[action]")]
  18. [ApiController]
  19. //[Authorize]
  20. public class PerformanceItemController : ControllerBase
  21. {
  22. spDbContext Context;
  23. public PerformanceItemController(spDbContext context)
  24. {
  25. Context = context;
  26. }
  27. public ApiSaveResponse New(PerformanceItem item)
  28. {
  29. ApiSaveResponse ret = new ApiSaveResponse();
  30. ret.Success = true;
  31. using (Context.Database.BeginTransaction())
  32. {
  33. try
  34. {
  35. var results = Context.PerformanceItems.Where<PerformanceItem>(x =>
  36. x.CaseNo == item.CaseNo && x.DoItem == item.DoItem && x.DoItem != "提出报告" && x.CaseStage == item.CaseStage);
  37. var items = results.Include(pi => pi.CalMonth).FirstOrDefault<PerformanceItem>();
  38. if (items != null)
  39. {
  40. item.AgentFeedbackMemo = "已算绩效";
  41. item.DoItemMemo = $"{items.DoItemMemo}\r\n{items.CalMonth.Year}-{items.CalMonth.Month}已计算!";
  42. item.BasePoint = 0;
  43. }
  44. if (item.CalMonth != null)
  45. {
  46. var calMonth = Context.CalMonths.Where<CalMonth>(c => c.Year == item.CalMonth.Year && c.Month == item.CalMonth.Month).FirstOrDefault();
  47. if(calMonth == null)
  48. {
  49. Context.CalMonths.Add(item.CalMonth);
  50. Context.SaveChanges();
  51. }
  52. else
  53. {
  54. item.CalMonth = calMonth;
  55. }
  56. item.CalMonthId = item.CalMonth.Id;
  57. item.CalMonth = null;
  58. }
  59. if (!string.IsNullOrEmpty(item.Customer.Name))
  60. {
  61. var temCustomer = Context.Customers.Where<Customer>(c => c.Name == item.Customer.Name).FirstOrDefault();
  62. if (temCustomer == null)
  63. {
  64. Context.Customers.Add(item.Customer);
  65. Context.SaveChanges();
  66. }
  67. else
  68. {
  69. item.Customer = temCustomer;
  70. }
  71. item.CustomerId = item.Customer.Id;
  72. item.Customer = null;
  73. }
  74. else
  75. {
  76. item.Customer = null;
  77. }
  78. var ItemStaffs = item.ItemStaffs;
  79. item.ItemStaffs = null;
  80. Context.PerformanceItems.Add(item);
  81. Context.SaveChanges();
  82. foreach (ItemStaff itemStaff in ItemStaffs)
  83. {
  84. itemStaff.ItemId = item.Id;
  85. itemStaff.Item = null;
  86. if (itemStaff.DoPersonId == 0 && itemStaff.DoPerson != null)
  87. {
  88. var temStaff = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == itemStaff.DoPerson.Name);
  89. if (temStaff != null)
  90. {
  91. itemStaff.DoPersonId = temStaff.Id;
  92. itemStaff.DoPerson = null;
  93. }
  94. else
  95. {
  96. Context.Staffs.Add(itemStaff.DoPerson);
  97. Context.SaveChanges();
  98. itemStaff.DoPersonId = itemStaff.DoPerson.Id;
  99. itemStaff.DoPerson = null;
  100. }
  101. }
  102. }
  103. Context.ItemStaffs.AddRange(ItemStaffs);
  104. Context.SaveChanges();
  105. Context.Database.CommitTransaction();
  106. }
  107. catch (Exception ex)
  108. {
  109. ret.Success = false;
  110. ret.ErrorMessage = ex.Message;
  111. Context.Database.RollbackTransaction();
  112. }
  113. }
  114. return ret;
  115. }
  116. /// <summary>
  117. /// 更新绩效记录信息
  118. /// </summary>
  119. /// <param name="id">绩效记录编号</param>
  120. /// <param name="field">栏位,多个位以|杠隔开</param>
  121. /// <param name="value">栏位值,多个以|杠隔开</param>
  122. /// <returns></returns>
  123. public ApiSaveResponse UpdateFieldValue(int id,string field,string value)
  124. {
  125. ApiSaveResponse ret = new ApiSaveResponse();
  126. ret.Success = true;
  127. var item = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.Id == id);
  128. if (item == null)
  129. {
  130. ret.Success = false;
  131. ret.ErrorMessage = $"不存在的{id}";
  132. return ret;
  133. }
  134. if(string.IsNullOrEmpty(field) )
  135. {
  136. ret.Success = false;
  137. ret.ErrorMessage = $"参数不对!";
  138. return ret;
  139. }
  140. string[] fields = field.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  141. string[] values = new string[] { null };
  142. if (!string.IsNullOrEmpty(value))
  143. {
  144. values = value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  145. }
  146. if (fields.Length != values.Length) {
  147. ret.Success = false;
  148. ret.ErrorMessage = "栏位和值对不匹配";
  149. }
  150. else
  151. {
  152. for(int i = 0; i < fields.Length; i++)
  153. {
  154. string temField = fields[i];
  155. string temValue = values[i];
  156. switch (temField)
  157. {
  158. case "AgentFeedbackMemo":
  159. item.AgentFeedbackMemo = temValue;
  160. break;
  161. case "CaseCoefficient":
  162. item.CaseCoefficient = temValue;
  163. //此处添加保存到流程系统的代码
  164. break;
  165. case "DoItemCoefficient":
  166. item.DoItemCoefficient = temValue;
  167. //此处添加保存到流程系统的代码
  168. break;
  169. case "WordCount":
  170. int wordCount;
  171. if (int.TryParse(temValue, out wordCount))
  172. {
  173. item.WordCount = wordCount;
  174. }
  175. else
  176. {
  177. ret.Success = false;
  178. ret.ErrorMessage = "所给的栏位值不能转换成数字!";
  179. return ret;
  180. }
  181. break;
  182. case "ReturnCasseNo":
  183. item.ReturnCasseNo = temValue;
  184. break;
  185. }
  186. }
  187. Utility.Utility.CalBasePoint(item, Context.BasePointRules.ToList());
  188. Context.SaveChanges();
  189. }
  190. return ret;
  191. }
  192. public ListApiResponse<PerformanceItem> Query(int pageIndex,int pageSize)
  193. {
  194. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  195. var results = Context.PerformanceItems
  196. .Where<PerformanceItem>(s =>
  197. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Name == User.Identity.Name).Count() > 0 || s.Reviewer.Name == User.Identity.Name)
  198. && s.CalMonth.Status != 4);
  199. ret.TotalCount = results.Count();
  200. List<PerformanceItem> retList = results
  201. .Include(pi=>pi.ItemStaffs).ThenInclude(iStaff=>iStaff.DoPerson)
  202. .Include(pi=>pi.Reviewer)
  203. .Include(pi=>pi.Customer)
  204. .Include(pi=>pi.CalMonth)
  205. .OrderByDescending(o=>o.Id)
  206. .Skip<PerformanceItem>((pageIndex - 1) * pageSize).Take(pageSize).ToList<PerformanceItem>();
  207. #region 将某些属性设为null,避免循环取值造成返回json过大
  208. foreach (PerformanceItem item in retList)
  209. {
  210. foreach (ItemStaff itemStaff in item.ItemStaffs)
  211. {
  212. itemStaff.DoPerson.ItemStaffs = null;
  213. itemStaff.DoPerson.ReviewerItems = null;
  214. itemStaff.Item = null;
  215. }
  216. item.Reviewer.ReviewerItems = null;
  217. item.Reviewer.Customers = null;
  218. item.Reviewer.ItemStaffs = null;
  219. item.Customer.PerformanceItems = null;
  220. item.CalMonth.PerformanceItems = null;
  221. }
  222. #endregion
  223. ret.Results = retList;
  224. return ret;
  225. }
  226. /// <summary>
  227. /// 获取给定用户的绩效清单
  228. /// </summary>
  229. /// <param name="userid">用户id</param>
  230. /// <param name="type">获取类型;0:处理中;1:所有;4:已归档</param>
  231. /// <returns></returns>
  232. public ListApiResponse<PerformanceItem> GetMyList(int userid, int type,int pageIndex=1,int pageSize = 10)
  233. {
  234. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  235. var results = Context.PerformanceItems
  236. .Where<PerformanceItem>(s =>
  237. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid ).Count() > 0 || s.Reviewer.Id == userid )
  238. && s.CalMonth.Status == type);
  239. ret.TotalCount = results.Count();
  240. List<PerformanceItem> retList = results
  241. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  242. .Include(pi => pi.Reviewer)
  243. .Include(pi => pi.Customer)
  244. .Include(pi => pi.CalMonth)
  245. .OrderByDescending(o => o.Id)
  246. .Skip<PerformanceItem>((pageIndex - 1) * pageSize).Take(pageSize)
  247. .ToList<PerformanceItem>();
  248. #region 将某些属性设为null,避免循环取值造成返回json过大
  249. foreach (PerformanceItem item in retList)
  250. {
  251. foreach(ItemStaff itemStaff in item.ItemStaffs)
  252. {
  253. itemStaff.DoPerson.ItemStaffs = null;
  254. itemStaff.DoPerson.ReviewerItems = null;
  255. itemStaff.Item = null;
  256. }
  257. item.Reviewer.ReviewerItems = null;
  258. item.Reviewer.Customers = null;
  259. item.Reviewer.ItemStaffs = null;
  260. item.Customer.PerformanceItems = null;
  261. item.CalMonth.PerformanceItems = null;
  262. }
  263. #endregion
  264. ret.Results = retList;
  265. return ret;
  266. }
  267. /// <summary>
  268. /// 计算指定用户,指定年月的绩效统计信息
  269. /// </summary>
  270. /// <param name="userid"></param>
  271. /// <param name="year"></param>
  272. /// <param name="month"></param>
  273. /// <returns></returns>
  274. public List<StaffStatistics> CalMyStatistics(int year,int month, int? userid=null)
  275. {
  276. CalMonth calMonth = Context.CalMonths.Where<CalMonth>(c => c.Month == month && c.Year == year).FirstOrDefault();
  277. if(calMonth == null)
  278. {
  279. return null;
  280. }
  281. else
  282. {
  283. if(calMonth.Status == 4)
  284. {
  285. //已归档,归档数据库中直接取出记录
  286. return null;
  287. }
  288. else
  289. {
  290. //未归档,从绩效记录中统计数据
  291. var results = Context.PerformanceItems.Where<PerformanceItem>(s =>s.CalMonth.Id == calMonth.Id);
  292. if (userid != null)
  293. {
  294. results = Context.PerformanceItems.Where<PerformanceItem>(s =>
  295. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid).Count() > 0 || s.Reviewer.Id == userid)
  296. && s.CalMonth.Id == calMonth.Id);
  297. }
  298. List<PerformanceItem> ItemList = results
  299. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  300. .Include(pi => pi.Reviewer)
  301. .OrderByDescending(o => o.Id)
  302. .ToList<PerformanceItem>();
  303. List<StaffStatistics> retList = new List<StaffStatistics>();
  304. List<VerifyCoefficient> verifyCoefficients = Context.VerifyCoefficients.ToList<VerifyCoefficient>();
  305. foreach (PerformanceItem item in ItemList)
  306. {
  307. if (item.BasePoint != null && item.BasePoint.Value > 0)
  308. {
  309. double doPersonBasePoint = item.BasePoint.Value;
  310. System.Collections.Hashtable doPersonsBL = new System.Collections.Hashtable();
  311. bool isPJFP = true;
  312. double total = item.ItemStaffs.Count();
  313. if(item.ItemStaffs.Where<ItemStaff>(p=>p.PerformancePoint == null || p.PerformancePoint ==0).Count() == 0)
  314. {
  315. total = item.ItemStaffs.Select(i => i.PerformancePoint.Value).Sum();
  316. isPJFP = false;
  317. }
  318. List<StaffStatistics> itemStatistics = new List<StaffStatistics>();
  319. if (item.Reviewer != null)
  320. {
  321. Context.Entry(item.Reviewer).Reference(b => b.StaffGrade).Load();
  322. }
  323. foreach (ItemStaff itemStaff in item.ItemStaffs)
  324. {
  325. Context.Entry(itemStaff).Reference(b => b.DoPerson).Load();
  326. Context.Entry(itemStaff.DoPerson).Reference(b => b.StaffGrade).Load();
  327. #region 计算审核人绩效点数,核稿人绩效点数按照核稿人与个处理人的核稿系数计算后加总,没有找到核稿系数(比如同级别),核稿系数为0
  328. if (item.ReviewerId != null && userid == item.ReviewerId)
  329. {
  330. #region 取审核人等级审核等级系数
  331. VerifyCoefficient vcoefficient = verifyCoefficients.Where<VerifyCoefficient>(v =>
  332. v.CheckerId == item.Reviewer.StaffGrade.Id
  333. && v.DoPersonId == itemStaff.DoPerson.StaffGradeId)
  334. .FirstOrDefault<VerifyCoefficient>();
  335. #endregion
  336. if (vcoefficient != null)
  337. {
  338. double reviewerBasePoint = item.BasePoint.Value * vcoefficient.Coefficient;
  339. string temJxType = $"{item.Type}审核";
  340. var temReviewerStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == item.ReviewerId && s.jxType == temJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  341. if (temReviewerStatic != null)
  342. {
  343. temReviewerStatic.totalBasePoint += reviewerBasePoint;
  344. }
  345. else
  346. {
  347. if (item.Reviewer.IsOnJob) //判断是否在职
  348. {
  349. temReviewerStatic = new StaffStatistics()
  350. {
  351. CalMonth = calMonth,
  352. CalMonthId = calMonth.Id,
  353. StaffId = item.ReviewerId.Value,
  354. totalBasePoint = reviewerBasePoint,
  355. jxType = temJxType
  356. };
  357. itemStatistics.Add(temReviewerStatic);
  358. }
  359. }
  360. }
  361. }
  362. #endregion
  363. #region 计算个处理人的绩效点数
  364. double handlerBasePoint;
  365. if (isPJFP) {
  366. handlerBasePoint = item.BasePoint.Value * 1.0/total;
  367. }
  368. else
  369. {
  370. handlerBasePoint = item.BasePoint.Value * itemStaff.PerformancePoint.Value / total;
  371. }
  372. string handlerJxType = $"{item.Type}处理";
  373. var temStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == itemStaff.DoPersonId && s.jxType == handlerJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  374. if (temStatic != null)
  375. {
  376. temStatic.totalBasePoint += handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient;
  377. }
  378. else
  379. {
  380. if (itemStaff.DoPerson.StaffGrade != null && itemStaff.DoPerson.IsOnJob)
  381. {
  382. temStatic = new StaffStatistics()
  383. {
  384. CalMonth = calMonth,
  385. CalMonthId = calMonth.Id,
  386. StaffId = itemStaff.DoPersonId,
  387. totalBasePoint = handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient,
  388. jxType = handlerJxType
  389. };
  390. itemStatistics.Add(temStatic);
  391. }
  392. }
  393. #endregion
  394. }
  395. List<StaffStatistics> temItemStatics;
  396. if(userid != null)
  397. {
  398. temItemStatics = itemStatistics.Where<StaffStatistics>(s => s.StaffId == userid).ToList();
  399. }
  400. else
  401. {
  402. temItemStatics = itemStatistics;
  403. }
  404. foreach (StaffStatistics retUserValue in temItemStatics)
  405. {
  406. var temValue = retList.Where<StaffStatistics>(s => s.StaffId == retUserValue.StaffId && s.jxType == retUserValue.jxType && s.CalMonthId == calMonth.Id).FirstOrDefault();
  407. if (temValue != null)
  408. {
  409. temValue.totalBasePoint += retUserValue.totalBasePoint;
  410. }
  411. else
  412. {
  413. retList.Add(retUserValue);
  414. }
  415. }
  416. }
  417. }
  418. foreach(StaffStatistics ss in retList)
  419. {
  420. ss.CalMonth.PerformanceItems = null;
  421. }
  422. return retList;
  423. }
  424. }
  425. }
  426. private string GetExpress(IList<FieldCondition> conditions)
  427. {
  428. string str = "";
  429. foreach(var c in conditions)
  430. {
  431. if (string.IsNullOrEmpty(str))
  432. {
  433. str = c.ToExpressString("s");
  434. }
  435. else
  436. {
  437. if(c.LogicOperate == LogicEnum.And)
  438. {
  439. str = $"({str}) && {c.ToExpressString("s")}";
  440. }
  441. else
  442. {
  443. str = $"({str}) || {c.ToExpressString("s")}";
  444. }
  445. }
  446. }
  447. return str;
  448. }
  449. [HttpPost]
  450. public ListApiResponse<PerformanceItem> QueryFilter(QueryFilter queryFilter)
  451. {
  452. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  453. string strExpress = "";
  454. if (!string.IsNullOrEmpty(strExpress))
  455. {
  456. strExpress = $"{strExpress} && s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  457. }
  458. else
  459. {
  460. strExpress = $"s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  461. }
  462. if (queryFilter.ConditionTree != null)
  463. {
  464. string strTem = GetExpress(queryFilter.ConditionTree);
  465. if (!string.IsNullOrEmpty(strTem))
  466. {
  467. strExpress = $"{strExpress} && ({strTem})";
  468. }
  469. }
  470. var interpreter = new Interpreter();
  471. Expression<Func<PerformanceItem, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<PerformanceItem, bool>>(strExpress, "s");
  472. IQueryable<PerformanceItem> response;
  473. if (queryFilter.userId > 0)
  474. {
  475. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere).Where(s => (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == queryFilter.userId).Count() > 0 || s.ReviewerId == queryFilter.userId));
  476. }
  477. else
  478. {
  479. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere);
  480. }
  481. int totals;
  482. response = response
  483. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  484. .Include(pi => pi.Reviewer)
  485. .Include(pi => pi.Customer)
  486. .Include(pi => pi.CalMonth)
  487. .OrderConditions<PerformanceItem>(queryFilter.Sorts)
  488. .Pager<PerformanceItem>(queryFilter.PageIndex,queryFilter.PageSize,out totals);
  489. ret.TotalCount = totals;
  490. var retList = response.ToList<PerformanceItem>();
  491. #region 将某些属性设为null,避免循环取值造成返回json过大
  492. foreach (PerformanceItem item in retList)
  493. {
  494. foreach (ItemStaff itemStaff in item.ItemStaffs)
  495. {
  496. itemStaff.DoPerson.ItemStaffs = null;
  497. itemStaff.DoPerson.ReviewerItems = null;
  498. itemStaff.Item = null;
  499. }
  500. if (item.Reviewer != null)
  501. {
  502. item.Reviewer.ReviewerItems = null;
  503. item.Reviewer.Customers = null;
  504. item.Reviewer.ItemStaffs = null;
  505. }
  506. if (item.Customer != null)
  507. {
  508. item.Customer.PerformanceItems = null;
  509. }
  510. if (item.CalMonth != null)
  511. {
  512. item.CalMonth.PerformanceItems = null;
  513. }
  514. }
  515. #endregion
  516. ret.Results = retList;
  517. return ret;
  518. }
  519. }
  520. }