PerformanceItemController.cs 22 KB

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