PerformanceItemController.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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) || string.IsNullOrEmpty(value))
  135. {
  136. ret.Success = false;
  137. ret.ErrorMessage = $"参数不对!";
  138. return ret;
  139. }
  140. string[] fields = field.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  141. string[] values= value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  142. if (fields.Length != values.Length) {
  143. ret.Success = false;
  144. ret.ErrorMessage = "栏位和值对不匹配";
  145. }
  146. else
  147. {
  148. for(int i = 0; i < fields.Length; i++)
  149. {
  150. string temField = fields[i];
  151. string temValue = values[i];
  152. switch (temField)
  153. {
  154. case "AgentFeedbackMemo":
  155. item.AgentFeedbackMemo = temValue;
  156. break;
  157. case "CaseCoefficient":
  158. item.CaseCoefficient = temValue;
  159. //此处添加保存到流程系统的代码
  160. break;
  161. case "DoItemCoefficient":
  162. item.DoItemCoefficient = temValue;
  163. //此处添加保存到流程系统的代码
  164. break;
  165. case "WordCount":
  166. int wordCount;
  167. if (int.TryParse(temValue, out wordCount))
  168. {
  169. item.WordCount = wordCount;
  170. }
  171. else
  172. {
  173. ret.Success = false;
  174. ret.ErrorMessage = "所给的栏位值不能转换成数字!";
  175. return ret;
  176. }
  177. break;
  178. case "ReturnCasseNo":
  179. item.ReturnCasseNo = temValue;
  180. break;
  181. }
  182. }
  183. Utility.Utility.CalBasePoint(item, Context.BasePointRules.ToList());
  184. Context.SaveChanges();
  185. }
  186. return ret;
  187. }
  188. public ListApiResponse<PerformanceItem> Query(int pageIndex,int pageSize)
  189. {
  190. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  191. var results = Context.PerformanceItems
  192. .Where<PerformanceItem>(s =>
  193. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Name == User.Identity.Name).Count() > 0 || s.Reviewer.Name == User.Identity.Name)
  194. && s.CalMonth.Status != 4);
  195. ret.TotalCount = results.Count();
  196. List<PerformanceItem> retList = results
  197. .Include(pi=>pi.ItemStaffs).ThenInclude(iStaff=>iStaff.DoPerson)
  198. .Include(pi=>pi.Reviewer)
  199. .Include(pi=>pi.Customer)
  200. .Include(pi=>pi.CalMonth)
  201. .OrderByDescending(o=>o.Id)
  202. .Skip<PerformanceItem>((pageIndex - 1) * pageSize).Take(pageSize).ToList<PerformanceItem>();
  203. #region 将某些属性设为null,避免循环取值造成返回json过大
  204. foreach (PerformanceItem item in retList)
  205. {
  206. foreach (ItemStaff itemStaff in item.ItemStaffs)
  207. {
  208. itemStaff.DoPerson.ItemStaffs = null;
  209. itemStaff.DoPerson.ReviewerItems = null;
  210. itemStaff.Item = null;
  211. }
  212. item.Reviewer.ReviewerItems = null;
  213. item.Reviewer.Customers = null;
  214. item.Reviewer.ItemStaffs = null;
  215. item.Customer.PerformanceItems = null;
  216. item.CalMonth.PerformanceItems = null;
  217. }
  218. #endregion
  219. ret.Results = retList;
  220. return ret;
  221. }
  222. /// <summary>
  223. /// 获取给定用户的绩效清单
  224. /// </summary>
  225. /// <param name="userid">用户id</param>
  226. /// <param name="type">获取类型;0:处理中;1:所有;4:已归档</param>
  227. /// <returns></returns>
  228. public ListApiResponse<PerformanceItem> GetMyList(int userid, int type,int pageIndex=1,int pageSize = 10)
  229. {
  230. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  231. var results = Context.PerformanceItems
  232. .Where<PerformanceItem>(s =>
  233. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid ).Count() > 0 || s.Reviewer.Id == userid )
  234. && s.CalMonth.Status == type);
  235. ret.TotalCount = results.Count();
  236. List<PerformanceItem> retList = results
  237. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  238. .Include(pi => pi.Reviewer)
  239. .Include(pi => pi.Customer)
  240. .Include(pi => pi.CalMonth)
  241. .OrderByDescending(o => o.Id)
  242. .Skip<PerformanceItem>((pageIndex - 1) * pageSize).Take(pageSize)
  243. .ToList<PerformanceItem>();
  244. #region 将某些属性设为null,避免循环取值造成返回json过大
  245. foreach (PerformanceItem item in retList)
  246. {
  247. foreach(ItemStaff itemStaff in item.ItemStaffs)
  248. {
  249. itemStaff.DoPerson.ItemStaffs = null;
  250. itemStaff.DoPerson.ReviewerItems = null;
  251. itemStaff.Item = null;
  252. }
  253. item.Reviewer.ReviewerItems = null;
  254. item.Reviewer.Customers = null;
  255. item.Reviewer.ItemStaffs = null;
  256. item.Customer.PerformanceItems = null;
  257. item.CalMonth.PerformanceItems = null;
  258. }
  259. #endregion
  260. ret.Results = retList;
  261. return ret;
  262. }
  263. /// <summary>
  264. /// 计算指定用户,指定年月的绩效统计信息
  265. /// </summary>
  266. /// <param name="userid"></param>
  267. /// <param name="year"></param>
  268. /// <param name="month"></param>
  269. /// <returns></returns>
  270. public List<StaffStatistics> CalMyStatistics(int year,int month, int? userid=null)
  271. {
  272. CalMonth calMonth = Context.CalMonths.Where<CalMonth>(c => c.Month == month && c.Year == year).FirstOrDefault();
  273. if(calMonth == null)
  274. {
  275. return null;
  276. }
  277. else
  278. {
  279. if(calMonth.Status == 4)
  280. {
  281. //已归档,归档数据库中直接取出记录
  282. return null;
  283. }
  284. else
  285. {
  286. //未归档,从绩效记录中统计数据
  287. var results = Context.PerformanceItems.Where<PerformanceItem>(s =>s.CalMonth.Id == calMonth.Id);
  288. if (userid != null)
  289. {
  290. results = Context.PerformanceItems.Where<PerformanceItem>(s =>
  291. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid).Count() > 0 || s.Reviewer.Id == userid)
  292. && s.CalMonth.Id == calMonth.Id);
  293. }
  294. List<PerformanceItem> ItemList = results
  295. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  296. .Include(pi => pi.Reviewer)
  297. .OrderByDescending(o => o.Id)
  298. .ToList<PerformanceItem>();
  299. List<StaffStatistics> retList = new List<StaffStatistics>();
  300. List<VerifyCoefficient> verifyCoefficients = Context.VerifyCoefficients.ToList<VerifyCoefficient>();
  301. foreach (PerformanceItem item in ItemList)
  302. {
  303. if (item.BasePoint != null && item.BasePoint.Value > 0)
  304. {
  305. double doPersonBasePoint = item.BasePoint.Value;
  306. System.Collections.Hashtable doPersonsBL = new System.Collections.Hashtable();
  307. bool isPJFP = true;
  308. double total = item.ItemStaffs.Count();
  309. if(item.ItemStaffs.Where<ItemStaff>(p=>p.PerformancePoint == null || p.PerformancePoint ==0).Count() == 0)
  310. {
  311. total = item.ItemStaffs.Select(i => i.PerformancePoint.Value).Sum();
  312. isPJFP = false;
  313. }
  314. List<StaffStatistics> itemStatistics = new List<StaffStatistics>();
  315. if (item.Reviewer != null)
  316. {
  317. Context.Entry(item.Reviewer).Reference(b => b.StaffGrade).Load();
  318. }
  319. foreach (ItemStaff itemStaff in item.ItemStaffs)
  320. {
  321. Context.Entry(itemStaff).Reference(b => b.DoPerson).Load();
  322. Context.Entry(itemStaff.DoPerson).Reference(b => b.StaffGrade).Load();
  323. #region 计算审核人绩效点数,核稿人绩效点数按照核稿人与个处理人的核稿系数计算后加总,没有找到核稿系数(比如同级别),核稿系数为0
  324. if (item.ReviewerId != null && userid == item.ReviewerId)
  325. {
  326. #region 取审核人等级审核等级系数
  327. VerifyCoefficient vcoefficient = verifyCoefficients.Where<VerifyCoefficient>(v =>
  328. v.CheckerId == item.Reviewer.StaffGrade.Id
  329. && v.DoPersonId == itemStaff.DoPerson.StaffGradeId)
  330. .FirstOrDefault<VerifyCoefficient>();
  331. #endregion
  332. if (vcoefficient != null)
  333. {
  334. double reviewerBasePoint = item.BasePoint.Value * vcoefficient.Coefficient;
  335. string temJxType = $"{item.Type}审核";
  336. var temReviewerStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == item.ReviewerId && s.jxType == temJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  337. if (temReviewerStatic != null)
  338. {
  339. temReviewerStatic.totalBasePoint += reviewerBasePoint;
  340. }
  341. else
  342. {
  343. if (item.Reviewer.IsOnJob) //判断是否在职
  344. {
  345. temReviewerStatic = new StaffStatistics()
  346. {
  347. CalMonth = calMonth,
  348. CalMonthId = calMonth.Id,
  349. StaffId = item.ReviewerId.Value,
  350. totalBasePoint = reviewerBasePoint,
  351. jxType = temJxType
  352. };
  353. itemStatistics.Add(temReviewerStatic);
  354. }
  355. }
  356. }
  357. }
  358. #endregion
  359. #region 计算个处理人的绩效点数
  360. double handlerBasePoint;
  361. if (isPJFP) {
  362. handlerBasePoint = item.BasePoint.Value * 1.0/total;
  363. }
  364. else
  365. {
  366. handlerBasePoint = item.BasePoint.Value * itemStaff.PerformancePoint.Value / total;
  367. }
  368. string handlerJxType = $"{item.Type}处理";
  369. var temStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == itemStaff.DoPersonId && s.jxType == handlerJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  370. if (temStatic != null)
  371. {
  372. temStatic.totalBasePoint += handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient;
  373. }
  374. else
  375. {
  376. if (itemStaff.DoPerson.StaffGrade != null && itemStaff.DoPerson.IsOnJob)
  377. {
  378. temStatic = new StaffStatistics()
  379. {
  380. CalMonth = calMonth,
  381. CalMonthId = calMonth.Id,
  382. StaffId = itemStaff.DoPersonId,
  383. totalBasePoint = handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient,
  384. jxType = handlerJxType
  385. };
  386. itemStatistics.Add(temStatic);
  387. }
  388. }
  389. #endregion
  390. }
  391. List<StaffStatistics> temItemStatics;
  392. if(userid != null)
  393. {
  394. temItemStatics = itemStatistics.Where<StaffStatistics>(s => s.StaffId == userid).ToList();
  395. }
  396. else
  397. {
  398. temItemStatics = itemStatistics;
  399. }
  400. foreach (StaffStatistics retUserValue in temItemStatics)
  401. {
  402. var temValue = retList.Where<StaffStatistics>(s => s.StaffId == retUserValue.StaffId && s.jxType == retUserValue.jxType && s.CalMonthId == calMonth.Id).FirstOrDefault();
  403. if (temValue != null)
  404. {
  405. temValue.totalBasePoint += retUserValue.totalBasePoint;
  406. }
  407. else
  408. {
  409. retList.Add(retUserValue);
  410. }
  411. }
  412. }
  413. }
  414. foreach(StaffStatistics ss in retList)
  415. {
  416. ss.CalMonth.PerformanceItems = null;
  417. }
  418. return retList;
  419. }
  420. }
  421. }
  422. private string GetExpress(IList<FieldCondition> conditions)
  423. {
  424. string str = "";
  425. foreach(var c in conditions)
  426. {
  427. if (string.IsNullOrEmpty(str))
  428. {
  429. str = c.ToExpressString("s");
  430. }
  431. else
  432. {
  433. if(c.LogicOperate == LogicEnum.And)
  434. {
  435. str = $"({str}) && {c.ToExpressString("s")}";
  436. }
  437. else
  438. {
  439. str = $"({str}) || {c.ToExpressString("s")}";
  440. }
  441. }
  442. }
  443. return str;
  444. }
  445. [HttpPost]
  446. public ListApiResponse<PerformanceItem> QueryFilter(QueryFilter queryFilter)
  447. {
  448. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  449. string strExpress = "";
  450. if (!string.IsNullOrEmpty(strExpress))
  451. {
  452. strExpress = $"{strExpress} && s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  453. }
  454. else
  455. {
  456. strExpress = $"s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  457. }
  458. if (queryFilter.ConditionTree != null)
  459. {
  460. string strTem = GetExpress(queryFilter.ConditionTree);
  461. if (!string.IsNullOrEmpty(strTem))
  462. {
  463. strExpress = $"{strExpress} && ({strTem})";
  464. }
  465. }
  466. var interpreter = new Interpreter();
  467. Expression<Func<PerformanceItem, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<PerformanceItem, bool>>(strExpress, "s");
  468. IQueryable<PerformanceItem> response;
  469. if (queryFilter.userId > 0)
  470. {
  471. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere).Where(s => (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == queryFilter.userId).Count() > 0 || s.ReviewerId == queryFilter.userId));
  472. }
  473. else
  474. {
  475. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere);
  476. }
  477. int totals;
  478. response = response
  479. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  480. .Include(pi => pi.Reviewer)
  481. .Include(pi => pi.Customer)
  482. .Include(pi => pi.CalMonth)
  483. .OrderConditions<PerformanceItem>(queryFilter.Sorts)
  484. .Pager<PerformanceItem>(queryFilter.PageIndex,queryFilter.PageSize,out totals);
  485. ret.TotalCount = totals;
  486. var retList = response.ToList<PerformanceItem>();
  487. #region 将某些属性设为null,避免循环取值造成返回json过大
  488. foreach (PerformanceItem item in retList)
  489. {
  490. foreach (ItemStaff itemStaff in item.ItemStaffs)
  491. {
  492. itemStaff.DoPerson.ItemStaffs = null;
  493. itemStaff.DoPerson.ReviewerItems = null;
  494. itemStaff.Item = null;
  495. }
  496. item.Reviewer.ReviewerItems = null;
  497. item.Reviewer.Customers = null;
  498. item.Reviewer.ItemStaffs = null;
  499. item.Customer.PerformanceItems = null;
  500. item.CalMonth.PerformanceItems = null;
  501. }
  502. #endregion
  503. ret.Results = retList;
  504. return ret;
  505. }
  506. }
  507. }