PerformanceItemController.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. temCustomer = new Customer() { Name = item.Customer.Name };
  65. //item.Customer.Id = 0;
  66. Context.Customers.Add(temCustomer);
  67. Context.SaveChanges();
  68. item.Customer = temCustomer;
  69. //item.CustomerId = item.Customer.Id;
  70. }
  71. else
  72. {
  73. item.Customer = temCustomer;
  74. }
  75. item.CustomerId = item.Customer.Id;
  76. item.Customer = null;
  77. }
  78. else
  79. {
  80. item.Customer = null;
  81. }
  82. var ItemStaffs = item.ItemStaffs;
  83. item.ItemStaffs = null;
  84. Context.PerformanceItems.Add(item);
  85. Context.SaveChanges();
  86. foreach (ItemStaff itemStaff in ItemStaffs)
  87. {
  88. itemStaff.ItemId = item.Id;
  89. itemStaff.Item = null;
  90. if (itemStaff.DoPersonId == 0 && itemStaff.DoPerson != null)
  91. {
  92. var temStaff = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == itemStaff.DoPerson.Name);
  93. if (temStaff != null)
  94. {
  95. itemStaff.DoPersonId = temStaff.Id;
  96. itemStaff.DoPerson = null;
  97. }
  98. else
  99. {
  100. Context.Staffs.Add(itemStaff.DoPerson);
  101. Context.SaveChanges();
  102. itemStaff.DoPersonId = itemStaff.DoPerson.Id;
  103. itemStaff.DoPerson = null;
  104. }
  105. }
  106. }
  107. Context.ItemStaffs.AddRange(ItemStaffs);
  108. Context.SaveChanges();
  109. Context.Database.CommitTransaction();
  110. }
  111. catch (Exception ex)
  112. {
  113. ret.Success = false;
  114. ret.ErrorMessage = ex.Message;
  115. Context.Database.RollbackTransaction();
  116. }
  117. }
  118. return ret;
  119. }
  120. /// <summary>
  121. /// 更新绩效记录信息
  122. /// </summary>
  123. /// <param name="id">绩效记录编号</param>
  124. /// <param name="field">栏位,多个位以|杠隔开</param>
  125. /// <param name="value">栏位值,多个以|杠隔开</param>
  126. /// <returns></returns>
  127. public ApiSaveResponse UpdateFieldValue(int id,string field,string value)
  128. {
  129. ApiSaveResponse ret = new ApiSaveResponse();
  130. ret.Success = true;
  131. var item = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.Id == id);
  132. if (item == null)
  133. {
  134. ret.Success = false;
  135. ret.ErrorMessage = $"不存在的{id}";
  136. return ret;
  137. }
  138. if(string.IsNullOrEmpty(field) )
  139. {
  140. ret.Success = false;
  141. ret.ErrorMessage = $"参数不对!";
  142. return ret;
  143. }
  144. string[] fields = field.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  145. string[] values = new string[] { null };
  146. if (!string.IsNullOrEmpty(value))
  147. {
  148. values = value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  149. }
  150. if (fields.Length != values.Length) {
  151. ret.Success = false;
  152. ret.ErrorMessage = "栏位和值对不匹配";
  153. }
  154. else
  155. {
  156. for(int i = 0; i < fields.Length; i++)
  157. {
  158. string temField = fields[i];
  159. string temValue = values[i];
  160. switch (temField)
  161. {
  162. case "AgentFeedbackMemo":
  163. item.AgentFeedbackMemo = temValue;
  164. break;
  165. case "CaseCoefficient":
  166. item.CaseCoefficient = temValue;
  167. //此处添加保存到流程系统的代码
  168. break;
  169. case "DoItemCoefficient":
  170. item.DoItemCoefficient = temValue;
  171. //此处添加保存到流程系统的代码
  172. break;
  173. case "WordCount":
  174. int wordCount;
  175. if (int.TryParse(temValue, out wordCount))
  176. {
  177. item.WordCount = wordCount;
  178. }
  179. else
  180. {
  181. ret.Success = false;
  182. ret.ErrorMessage = "所给的栏位值不能转换成数字!";
  183. return ret;
  184. }
  185. break;
  186. case "ReturnCasseNo":
  187. item.ReturnCasseNo = temValue;
  188. break;
  189. }
  190. }
  191. Utility.Utility.CalBasePoint(item, Context.BasePointRules.ToList());
  192. Context.SaveChanges();
  193. }
  194. return ret;
  195. }
  196. public ListApiResponse<PerformanceItem> Query(int pageIndex,int pageSize)
  197. {
  198. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  199. var results = Context.PerformanceItems
  200. .Where<PerformanceItem>(s =>
  201. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Name == User.Identity.Name).Count() > 0 || s.Reviewer.Name == User.Identity.Name)
  202. && s.CalMonth.Status != 4);
  203. ret.TotalCount = results.Count();
  204. List<PerformanceItem> retList = results
  205. .Include(pi=>pi.ItemStaffs).ThenInclude(iStaff=>iStaff.DoPerson)
  206. .Include(pi=>pi.Reviewer)
  207. .Include(pi=>pi.Customer)
  208. .Include(pi=>pi.CalMonth)
  209. .OrderByDescending(o=>o.Id)
  210. .Skip<PerformanceItem>((pageIndex - 1) * pageSize).Take(pageSize).ToList<PerformanceItem>();
  211. #region 将某些属性设为null,避免循环取值造成返回json过大
  212. foreach (PerformanceItem item in retList)
  213. {
  214. foreach (ItemStaff itemStaff in item.ItemStaffs)
  215. {
  216. itemStaff.DoPerson.ItemStaffs = null;
  217. itemStaff.DoPerson.ReviewerItems = null;
  218. itemStaff.Item = null;
  219. }
  220. item.Reviewer.ReviewerItems = null;
  221. item.Reviewer.Customers = null;
  222. item.Reviewer.ItemStaffs = null;
  223. item.Customer.PerformanceItems = null;
  224. item.CalMonth.PerformanceItems = null;
  225. }
  226. #endregion
  227. ret.Results = retList;
  228. return ret;
  229. }
  230. public PerformanceItem Get(int Id)
  231. {
  232. var results = Context.PerformanceItems
  233. .Where<PerformanceItem>(s =>s.Id == Id);
  234. PerformanceItem item = results
  235. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  236. .Include(pi => pi.Reviewer)
  237. .Include(pi => pi.Customer)
  238. .Include(pi => pi.CalMonth)
  239. .OrderByDescending(o => o.Id)
  240. .FirstOrDefault();
  241. #region 将某些属性设为null,避免循环取值造成返回json过大
  242. foreach (ItemStaff itemStaff in item.ItemStaffs)
  243. {
  244. itemStaff.DoPerson.ItemStaffs = null;
  245. itemStaff.DoPerson.ReviewerItems = null;
  246. itemStaff.Item = null;
  247. }
  248. if (item.Reviewer != null)
  249. {
  250. item.Reviewer.ReviewerItems = null;
  251. item.Reviewer.Customers = null;
  252. item.Reviewer.ItemStaffs = null;
  253. }
  254. item.Customer.PerformanceItems = null;
  255. item.CalMonth.PerformanceItems = null;
  256. #endregion
  257. return item;
  258. }
  259. /// <summary>
  260. /// 获取给定用户的绩效清单
  261. /// </summary>
  262. /// <param name="userid">用户id</param>
  263. /// <param name="type">获取类型;0:处理中;1:所有;4:已归档</param>
  264. /// <returns></returns>
  265. public ListApiResponse<PerformanceItem> GetMyList(int userid, int type,int pageIndex=1,int pageSize = 10)
  266. {
  267. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  268. var results = Context.PerformanceItems
  269. .Where<PerformanceItem>(s =>
  270. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid ).Count() > 0 || s.Reviewer.Id == userid )
  271. && s.CalMonth.Status == type);
  272. ret.TotalCount = results.Count();
  273. List<PerformanceItem> retList = results
  274. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  275. .Include(pi => pi.Reviewer)
  276. .Include(pi => pi.Customer)
  277. .Include(pi => pi.CalMonth)
  278. .OrderByDescending(o => o.Id)
  279. .Skip<PerformanceItem>((pageIndex - 1) * pageSize).Take(pageSize)
  280. .ToList<PerformanceItem>();
  281. #region 将某些属性设为null,避免循环取值造成返回json过大
  282. foreach (PerformanceItem item in retList)
  283. {
  284. foreach(ItemStaff itemStaff in item.ItemStaffs)
  285. {
  286. itemStaff.DoPerson.ItemStaffs = null;
  287. itemStaff.DoPerson.ReviewerItems = null;
  288. itemStaff.Item = null;
  289. }
  290. item.Reviewer.ReviewerItems = null;
  291. item.Reviewer.Customers = null;
  292. item.Reviewer.ItemStaffs = null;
  293. item.Customer.PerformanceItems = null;
  294. item.CalMonth.PerformanceItems = null;
  295. }
  296. #endregion
  297. ret.Results = retList;
  298. return ret;
  299. }
  300. public double DegreeOfDifficulty(int year,int month, int? userId = null)
  301. {
  302. IDictionary<string, double> CaseXiShu = new Dictionary<string, double>();
  303. var list = Context.CaseCeoffcients;
  304. foreach(var cx in list.ToList<CaseCeoffcient>())
  305. {
  306. CaseXiShu.Add(cx.Ceoffcient, cx.Value);
  307. }
  308. var results = Context.PerformanceItems.Where<PerformanceItem>(p => p.CalMonth.Year == year && p.CalMonth.Month == month && ((p.Type == "新申请" && p.BasePoint > 0) || p.Type == "专案"));
  309. if(userId != null)
  310. {
  311. results = Context.PerformanceItems.Where<PerformanceItem>(p =>
  312. p.CalMonth.Year == year && p.CalMonth.Month == month && ((p.Type == "新申请" && p.BasePoint >0) || p.Type == "专案") &&
  313. (p.ItemStaffs.Where<ItemStaff>(s=>s.DoPerson.Id == userId).Count ()>0 || p.ReviewerId == userId ));
  314. }
  315. var groupResult = results.GroupBy(x => x.CaseCoefficient).Select(g=> new {
  316. CaseCeoffcient = g.Key,
  317. count = g.Count()
  318. });
  319. int iCount = 0;
  320. double d = 0.0;
  321. foreach(var g in groupResult)
  322. {
  323. if (!string.IsNullOrEmpty(g.CaseCeoffcient))
  324. {
  325. if (CaseXiShu.ContainsKey(g.CaseCeoffcient))
  326. {
  327. d += g.count * CaseXiShu[g.CaseCeoffcient];
  328. iCount += g.count;
  329. }
  330. }
  331. }
  332. return d/(double)iCount;
  333. }
  334. public List<string> GetFeedbackString(int itemId)
  335. {
  336. PerformanceItem item = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.Id == itemId);
  337. if(item != null)
  338. {
  339. return Utility.Utility.GetFeedbackMemos(item, Context.BasePointRules.ToList());
  340. }
  341. return new List<string>();
  342. }
  343. private List<StaffStatistics> _CalMyStatistics(CalMonth calMonth, int? userid = null)
  344. {
  345. double gspjXS = DegreeOfDifficulty(calMonth.Year , calMonth.Month);
  346. //未归档,从绩效记录中统计数据
  347. var results = Context.PerformanceItems.Where<PerformanceItem>(s => s.CalMonth.Id == calMonth.Id);
  348. if (userid != null)
  349. {
  350. results = Context.PerformanceItems.Where<PerformanceItem>(s =>
  351. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid).Count() > 0 || s.Reviewer.Id == userid)
  352. && s.CalMonth.Id == calMonth.Id);
  353. }
  354. List<PerformanceItem> ItemList = results
  355. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  356. .Include(pi => pi.Reviewer)
  357. .OrderByDescending(o => o.Id)
  358. .ToList<PerformanceItem>();
  359. List<StaffStatistics> retList = new List<StaffStatistics>();
  360. List<VerifyCoefficient> verifyCoefficients = Context.VerifyCoefficients.ToList<VerifyCoefficient>();
  361. var Rules = Context.BasePointRules.ToList();
  362. foreach (PerformanceItem item in ItemList)
  363. {
  364. //if (item.BasePoint == null)
  365. //{
  366. Utility.Utility.CalBasePoint(item,Rules);
  367. Context.SaveChanges();
  368. //}
  369. if (item.BasePoint != null && item.BasePoint.Value > 0)
  370. {
  371. double doPersonBasePoint = item.BasePoint.Value;
  372. System.Collections.Hashtable doPersonsBL = new System.Collections.Hashtable();
  373. bool isPJFP = true;
  374. double total = item.ItemStaffs.Count();
  375. if (item.ItemStaffs.Where<ItemStaff>(p => p.PerformancePoint != null || p.PerformancePoint == 0).Count() > 0)
  376. {
  377. total = item.ItemStaffs.Select(i => i.PerformancePoint.Value).Sum();
  378. isPJFP = false;
  379. }
  380. List<StaffStatistics> itemStatistics = new List<StaffStatistics>();
  381. if (item.Reviewer != null)
  382. {
  383. Context.Entry(item.Reviewer).Reference(b => b.StaffGrade).Load();
  384. }
  385. foreach (ItemStaff itemStaff in item.ItemStaffs)
  386. {
  387. Context.Entry(itemStaff).Reference(b => b.DoPerson).Load();
  388. Context.Entry(itemStaff.DoPerson).Reference(b => b.StaffGrade).Load();
  389. #region 计算审核人绩效点数,核稿人绩效点数按照核稿人与个处理人的核稿系数计算后加总,没有找到核稿系数(比如同级别),核稿系数为0
  390. if (item.ReviewerId != null && item.Type !="专案")
  391. {
  392. #region 取审核人等级审核等级系数
  393. VerifyCoefficient vcoefficient
  394. = verifyCoefficients.Where<VerifyCoefficient>(v =>
  395. v.CheckerId == item.Reviewer.StaffGrade.Id
  396. && v.DoPersonId == itemStaff.DoPerson.StaffGradeId)
  397. .FirstOrDefault<VerifyCoefficient>();
  398. #endregion
  399. if (vcoefficient != null)
  400. {
  401. double reviewerBasePoint = item.BasePoint.Value * vcoefficient.Coefficient;
  402. string temJxType = $"{item.Type}审核";
  403. var temReviewerStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == item.ReviewerId && s.jxType == temJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  404. if (temReviewerStatic != null)
  405. {
  406. temReviewerStatic.totalBasePoint += reviewerBasePoint;
  407. }
  408. else
  409. {
  410. if (item.Reviewer.IsOnJob && itemStaff.DoPerson.Status != "试用期") //判断是否在职
  411. {
  412. temReviewerStatic = new StaffStatistics()
  413. {
  414. CalMonth = calMonth,
  415. CalMonthId = calMonth.Id,
  416. StaffId = item.ReviewerId.Value,
  417. totalBasePoint = reviewerBasePoint,
  418. jxType = temJxType
  419. };
  420. itemStatistics.Add(temReviewerStatic);
  421. }
  422. }
  423. }
  424. }
  425. #endregion
  426. #region 计算各处理人的绩效点数
  427. double handlerBasePoint;
  428. if (item.Type != "专案")
  429. {
  430. if (isPJFP)
  431. {
  432. handlerBasePoint = item.BasePoint.Value * 1.0 / total;
  433. }
  434. else
  435. {
  436. handlerBasePoint = item.BasePoint.Value * itemStaff.PerformancePoint.Value / total;
  437. }
  438. }
  439. else
  440. {
  441. handlerBasePoint = itemStaff.PerformancePoint.Value;
  442. }
  443. string handlerJxType = $"{item.Type}处理";
  444. var temStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == itemStaff.DoPersonId && s.jxType == handlerJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  445. if (temStatic != null)
  446. {
  447. if (item.Type != "专案")
  448. {
  449. temStatic.totalBasePoint += handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient;
  450. }
  451. else
  452. {
  453. temStatic.totalBasePoint += handlerBasePoint;
  454. }
  455. }
  456. else
  457. {
  458. if (itemStaff.DoPerson.StaffGrade != null && itemStaff.DoPerson.IsOnJob)
  459. {
  460. if (item.Type != "专案")
  461. {
  462. if (itemStaff.DoPerson.Status == "试用期" && item.Reviewer != null)
  463. {
  464. temStatic = new StaffStatistics()
  465. {
  466. CalMonth = calMonth,
  467. CalMonthId = calMonth.Id,
  468. StaffId = item.Reviewer.Id,
  469. totalBasePoint = handlerBasePoint * item.Reviewer.StaffGrade.Coefficient,
  470. jxType = handlerJxType
  471. };
  472. itemStatistics.Add(temStatic);
  473. }
  474. else
  475. {
  476. temStatic = new StaffStatistics()
  477. {
  478. CalMonth = calMonth,
  479. CalMonthId = calMonth.Id,
  480. StaffId = itemStaff.DoPersonId,
  481. totalBasePoint = handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient,
  482. jxType = handlerJxType
  483. };
  484. itemStatistics.Add(temStatic);
  485. }
  486. }
  487. else
  488. {
  489. temStatic = new StaffStatistics()
  490. {
  491. CalMonth = calMonth,
  492. CalMonthId = calMonth.Id,
  493. StaffId = itemStaff.DoPersonId,
  494. totalBasePoint = handlerBasePoint ,
  495. jxType = handlerJxType
  496. };
  497. itemStatistics.Add(temStatic);
  498. }
  499. }
  500. }
  501. #endregion
  502. }
  503. List<StaffStatistics> temItemStatics;
  504. if (userid != null)
  505. {
  506. temItemStatics = itemStatistics.Where<StaffStatistics>(s => s.StaffId == userid).ToList();
  507. }
  508. else
  509. {
  510. temItemStatics = itemStatistics;
  511. }
  512. foreach (StaffStatistics retUserValue in temItemStatics)
  513. {
  514. var temValue = retList.Where<StaffStatistics>(s => s.StaffId == retUserValue.StaffId && s.jxType == retUserValue.jxType && s.CalMonthId == calMonth.Id).FirstOrDefault();
  515. if (temValue != null)
  516. {
  517. temValue.totalBasePoint += retUserValue.totalBasePoint;
  518. }
  519. else
  520. {
  521. retList.Add(retUserValue);
  522. }
  523. }
  524. }
  525. }
  526. if(userid != null)
  527. {
  528. retList = retList.Where<StaffStatistics>(s => s.StaffId == userid.Value).ToList();
  529. }
  530. IDictionary<int, double> staffXiShu = new Dictionary<int, double>();
  531. foreach (StaffStatistics ss in retList)
  532. {
  533. if (ss.jxType.Contains("新申请") || ss.jxType.Contains("专案"))
  534. {
  535. if (!staffXiShu.ContainsKey(ss.StaffId))
  536. {
  537. staffXiShu.Add(ss.StaffId, DegreeOfDifficulty(calMonth.Year ,calMonth.Month, ss.StaffId));
  538. }
  539. ss.totalActuallyPoint = ss.totalBasePoint * staffXiShu[ss.StaffId] / gspjXS;
  540. }
  541. else
  542. {
  543. ss.totalActuallyPoint = ss.totalBasePoint;
  544. }
  545. ss.CalMonth.PerformanceItems = null;
  546. }
  547. return retList;
  548. }
  549. /// <summary>
  550. /// 计算指定用户,指定年月的绩效统计信息
  551. /// </summary>
  552. /// <param name="userid"></param>
  553. /// <param name="year"></param>
  554. /// <param name="month"></param>
  555. /// <returns></returns>
  556. public List<StaffStatistics> CalMyStatistics(int year,int month, int? userid=null)
  557. {
  558. CalMonth calMonth = Context.CalMonths.Where<CalMonth>(c => c.Month == month && c.Year == year).FirstOrDefault();
  559. if(calMonth == null)
  560. {
  561. return null;
  562. }
  563. else
  564. {
  565. if(calMonth.Status == 4)
  566. {
  567. //已归档,归档数据库中直接取出记录
  568. if (userid == null)
  569. {
  570. return Context.StaffStatistics.Where<StaffStatistics>(s => s.CalMonthId == calMonth.Id).ToList<StaffStatistics>();
  571. }
  572. else
  573. {
  574. return Context.StaffStatistics.Where<StaffStatistics>(s => s.CalMonthId == calMonth.Id && s.StaffId == userid).ToList<StaffStatistics>();
  575. }
  576. }
  577. else
  578. {
  579. return _CalMyStatistics(calMonth, userid);
  580. }
  581. }
  582. }
  583. private string GetExpress(IList<FieldCondition> conditions)
  584. {
  585. string str = "";
  586. foreach(var c in conditions)
  587. {
  588. if (string.IsNullOrEmpty(str))
  589. {
  590. str = c.ToExpressString("s");
  591. }
  592. else
  593. {
  594. if(c.LogicOperate == LogicEnum.And)
  595. {
  596. str = $"({str}) && {c.ToExpressString("s")}";
  597. }
  598. else
  599. {
  600. str = $"({str}) || {c.ToExpressString("s")}";
  601. }
  602. }
  603. }
  604. return str;
  605. }
  606. [HttpPost]
  607. public ListApiResponse<PerformanceItem> QueryFilter(QueryFilter queryFilter)
  608. {
  609. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  610. string strExpress = "";
  611. if (!string.IsNullOrEmpty(strExpress))
  612. {
  613. strExpress = $"{strExpress} && s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  614. }
  615. else
  616. {
  617. strExpress = $"s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  618. }
  619. if (queryFilter.ConditionTree != null)
  620. {
  621. string strTem = GetExpress(queryFilter.ConditionTree);
  622. if (!string.IsNullOrEmpty(strTem))
  623. {
  624. strExpress = $"{strExpress} && ({strTem})";
  625. }
  626. }
  627. var interpreter = new Interpreter();
  628. Expression<Func<PerformanceItem, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<PerformanceItem, bool>>(strExpress, "s");
  629. IQueryable<PerformanceItem> response;
  630. if (queryFilter.userId > 0)
  631. {
  632. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere).Where(s => (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == queryFilter.userId).Count() > 0));// || s.ReviewerId == queryFilter.userId));
  633. }
  634. else
  635. {
  636. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere);
  637. }
  638. int totals = response.ToList<PerformanceItem>().Count;
  639. if (totals > 0 && totals < (queryFilter.PageIndex - 1) * queryFilter.PageSize) {
  640. response = response
  641. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  642. .Include(pi => pi.Reviewer)
  643. .Include(pi => pi.Customer)
  644. .Include(pi => pi.CalMonth)
  645. .OrderConditions<PerformanceItem>(queryFilter.Sorts)
  646. .Pager<PerformanceItem>(1, queryFilter.PageSize, out totals);
  647. }
  648. else
  649. {
  650. response = response
  651. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  652. .Include(pi => pi.Reviewer)
  653. .Include(pi => pi.Customer)
  654. .Include(pi => pi.CalMonth)
  655. .OrderConditions<PerformanceItem>(queryFilter.Sorts)
  656. .Pager<PerformanceItem>(queryFilter.PageIndex, queryFilter.PageSize, out totals);
  657. }
  658. ret.TotalCount = totals;
  659. var retList = response.ToList<PerformanceItem>();
  660. #region 将某些属性设为null,避免循环取值造成返回json过大
  661. foreach (PerformanceItem item in retList)
  662. {
  663. foreach (ItemStaff itemStaff in item.ItemStaffs)
  664. {
  665. itemStaff.DoPerson.ItemStaffs = null;
  666. itemStaff.DoPerson.ReviewerItems = null;
  667. itemStaff.Item = null;
  668. }
  669. if (item.Reviewer != null)
  670. {
  671. item.Reviewer.ReviewerItems = null;
  672. item.Reviewer.Customers = null;
  673. item.Reviewer.ItemStaffs = null;
  674. }
  675. if (item.Customer != null)
  676. {
  677. item.Customer.PerformanceItems = null;
  678. }
  679. if (item.CalMonth != null)
  680. {
  681. item.CalMonth.PerformanceItems = null;
  682. }
  683. }
  684. #endregion
  685. ret.Results = retList;
  686. return ret;
  687. }
  688. public ApiSaveResponse AddProjectPerformance(ProjectPointRecord pointRecord)
  689. {
  690. ApiSaveResponse retResponse = new ApiSaveResponse();
  691. retResponse.Success = true;
  692. if (pointRecord != null && pointRecord.ProjectDoItemPoints != null && pointRecord.ProjectDoItemPoints.Count > 0)
  693. {
  694. using (var t = Context.Database.BeginTransaction())
  695. {
  696. try
  697. {
  698. CalMonth calMonth = Context.CalMonths.FirstOrDefault<CalMonth>(c=>c.Status ==0);
  699. if(calMonth == null)
  700. {
  701. retResponse.Success = false;
  702. retResponse.ErrorMessage = "不存在正在处理的绩效月度!";
  703. return retResponse;
  704. }
  705. foreach (var doItem in pointRecord.ProjectDoItemPoints)
  706. {
  707. PerformanceItem item = new PerformanceItem();
  708. item.CaseNo = pointRecord.CaseNo;
  709. item.CaseName = pointRecord.CaseName;
  710. item.CaseMemo = pointRecord.Reason;
  711. item.DoItem = doItem.DoItem;
  712. item.CaseCoefficient = doItem.DoItemCoefficient;
  713. item.Type = "专案";
  714. item.CalMonthId = calMonth.Id;
  715. Context.PerformanceItems.Add(item);
  716. Context.SaveChanges();
  717. item.ItemStaffs = new List<ItemStaff>();
  718. foreach (var p in doItem.PersonPoints)
  719. {
  720. ItemStaff itemStaff = new ItemStaff();
  721. itemStaff.PerformancePoint = p.Point;
  722. var staff = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == p.Person);
  723. if (staff != null)
  724. {
  725. itemStaff.DoPersonId = staff.Id;
  726. itemStaff.ItemId = item.Id;
  727. Context.ItemStaffs.Add(itemStaff);
  728. Context.SaveChanges();
  729. }
  730. else
  731. {
  732. retResponse.Success = false;
  733. retResponse.ErrorMessage = $"用户【{p.Person}】不存在!";
  734. t.Rollback();
  735. return retResponse;
  736. }
  737. }
  738. }
  739. t.Commit();
  740. }
  741. catch(Exception ex)
  742. {
  743. retResponse.Success = false;
  744. retResponse.ErrorMessage = ex.Message;
  745. t.Rollback();
  746. return retResponse;
  747. }
  748. }
  749. }
  750. return retResponse;
  751. }
  752. public PerformanceItem GetCaseInfo(string CaseNo)
  753. {
  754. var retObj = Context.PerformanceItems.OrderByDescending(p=>p.CalMonthId).FirstOrDefault<PerformanceItem>(p=>p.CaseNo == CaseNo.Trim());
  755. if(retObj == null)
  756. {
  757. retObj = new IPEasyController(Context).GetCaseInfo(CaseNo);
  758. }
  759. return retObj;
  760. }
  761. public PerformanceItem GetItemInfo(string CaseNo, string DoItem)
  762. {
  763. var retObj = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.CaseNo == CaseNo.Trim() && p.DoItem == DoItem.Trim());
  764. if (retObj == null)
  765. {
  766. retObj = new IPEasyController(Context).GetItemInfo(CaseNo,DoItem);
  767. }
  768. return retObj;
  769. }
  770. public PerformanceItem GetItemInfoByCaseStage(string CaseNo, string DoItem,string caseStage)
  771. {
  772. var retObj = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.CaseNo == CaseNo.Trim()
  773. && p.DoItem == DoItem.Trim() && p.CaseStage == caseStage);
  774. if (retObj == null)
  775. {
  776. retObj = IPEasyUtility.GetPerformanceRecord(CaseNo, DoItem, caseStage);
  777. }
  778. return retObj;
  779. }
  780. }
  781. }