PerformanceItemController.cs 21 KB

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