PerformanceItemController.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. public PerformanceItem Get(int Id)
  227. {
  228. var results = Context.PerformanceItems
  229. .Where<PerformanceItem>(s =>s.Id == Id);
  230. PerformanceItem item = results
  231. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  232. .Include(pi => pi.Reviewer)
  233. .Include(pi => pi.Customer)
  234. .Include(pi => pi.CalMonth)
  235. .OrderByDescending(o => o.Id)
  236. .FirstOrDefault();
  237. #region 将某些属性设为null,避免循环取值造成返回json过大
  238. foreach (ItemStaff itemStaff in item.ItemStaffs)
  239. {
  240. itemStaff.DoPerson.ItemStaffs = null;
  241. itemStaff.DoPerson.ReviewerItems = null;
  242. itemStaff.Item = null;
  243. }
  244. item.Reviewer.ReviewerItems = null;
  245. item.Reviewer.Customers = null;
  246. item.Reviewer.ItemStaffs = null;
  247. item.Customer.PerformanceItems = null;
  248. item.CalMonth.PerformanceItems = null;
  249. #endregion
  250. return item;
  251. }
  252. /// <summary>
  253. /// 获取给定用户的绩效清单
  254. /// </summary>
  255. /// <param name="userid">用户id</param>
  256. /// <param name="type">获取类型;0:处理中;1:所有;4:已归档</param>
  257. /// <returns></returns>
  258. public ListApiResponse<PerformanceItem> GetMyList(int userid, int type,int pageIndex=1,int pageSize = 10)
  259. {
  260. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  261. var results = Context.PerformanceItems
  262. .Where<PerformanceItem>(s =>
  263. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid ).Count() > 0 || s.Reviewer.Id == userid )
  264. && s.CalMonth.Status == type);
  265. ret.TotalCount = results.Count();
  266. List<PerformanceItem> retList = results
  267. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  268. .Include(pi => pi.Reviewer)
  269. .Include(pi => pi.Customer)
  270. .Include(pi => pi.CalMonth)
  271. .OrderByDescending(o => o.Id)
  272. .Skip<PerformanceItem>((pageIndex - 1) * pageSize).Take(pageSize)
  273. .ToList<PerformanceItem>();
  274. #region 将某些属性设为null,避免循环取值造成返回json过大
  275. foreach (PerformanceItem item in retList)
  276. {
  277. foreach(ItemStaff itemStaff in item.ItemStaffs)
  278. {
  279. itemStaff.DoPerson.ItemStaffs = null;
  280. itemStaff.DoPerson.ReviewerItems = null;
  281. itemStaff.Item = null;
  282. }
  283. item.Reviewer.ReviewerItems = null;
  284. item.Reviewer.Customers = null;
  285. item.Reviewer.ItemStaffs = null;
  286. item.Customer.PerformanceItems = null;
  287. item.CalMonth.PerformanceItems = null;
  288. }
  289. #endregion
  290. ret.Results = retList;
  291. return ret;
  292. }
  293. public double DegreeOfDifficulty(int year,int month, int? userId = null)
  294. {
  295. IDictionary<string, double> CaseXiShu = new Dictionary<string, double>();
  296. var list = Context.CaseCeoffcients;
  297. foreach(var cx in list.ToList<CaseCeoffcient>())
  298. {
  299. CaseXiShu.Add(cx.Ceoffcient, cx.Value);
  300. }
  301. var results = Context.PerformanceItems.Where<PerformanceItem>(p => p.CalMonth.Year == year && p.CalMonth.Month == month && ((p.Type == "新申请" && p.BasePoint > 0) || p.Type == "专案"));
  302. if(userId != null)
  303. {
  304. results = Context.PerformanceItems.Where<PerformanceItem>(p =>
  305. p.CalMonth.Year == year && p.CalMonth.Month == month && ((p.Type == "新申请" && p.BasePoint >0) || p.Type == "专案") &&
  306. (p.ItemStaffs.Where<ItemStaff>(s=>s.DoPerson.Id == userId).Count ()>0 || p.ReviewerId == userId ));
  307. }
  308. var groupResult = results.GroupBy(x => x.CaseCoefficient).Select(g=> new {
  309. CaseCeoffcient = g.Key,
  310. count = g.Count()
  311. });
  312. int iCount = 0;
  313. double d = 0.0;
  314. foreach(var g in groupResult)
  315. {
  316. if (!string.IsNullOrEmpty(g.CaseCeoffcient))
  317. {
  318. if (CaseXiShu.ContainsKey(g.CaseCeoffcient))
  319. {
  320. d += g.count * CaseXiShu[g.CaseCeoffcient];
  321. iCount += g.count;
  322. }
  323. }
  324. }
  325. return d/(double)iCount;
  326. }
  327. public List<string> GetFeedbackString(int itemId)
  328. {
  329. PerformanceItem item = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.Id == itemId);
  330. if(item != null)
  331. {
  332. return Utility.Utility.GetFeedbackMemos(item, Context.BasePointRules.ToList());
  333. }
  334. return new List<string>();
  335. }
  336. private List<StaffStatistics> _CalMyStatistics(CalMonth calMonth, int? userid = null)
  337. {
  338. double gspjXS = DegreeOfDifficulty(calMonth.Year , calMonth.Month);
  339. //未归档,从绩效记录中统计数据
  340. var results = Context.PerformanceItems.Where<PerformanceItem>(s => s.CalMonth.Id == calMonth.Id);
  341. if (userid != null)
  342. {
  343. results = Context.PerformanceItems.Where<PerformanceItem>(s =>
  344. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid).Count() > 0 || s.Reviewer.Id == userid)
  345. && s.CalMonth.Id == calMonth.Id);
  346. }
  347. List<PerformanceItem> ItemList = results
  348. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  349. .Include(pi => pi.Reviewer)
  350. .OrderByDescending(o => o.Id)
  351. .ToList<PerformanceItem>();
  352. List<StaffStatistics> retList = new List<StaffStatistics>();
  353. List<VerifyCoefficient> verifyCoefficients = Context.VerifyCoefficients.ToList<VerifyCoefficient>();
  354. foreach (PerformanceItem item in ItemList)
  355. {
  356. if (item.BasePoint != null && item.BasePoint.Value > 0)
  357. {
  358. double doPersonBasePoint = item.BasePoint.Value;
  359. System.Collections.Hashtable doPersonsBL = new System.Collections.Hashtable();
  360. bool isPJFP = true;
  361. double total = item.ItemStaffs.Count();
  362. if (item.ItemStaffs.Where<ItemStaff>(p => p.PerformancePoint == null || p.PerformancePoint == 0).Count() == 0)
  363. {
  364. total = item.ItemStaffs.Select(i => i.PerformancePoint.Value).Sum();
  365. isPJFP = false;
  366. }
  367. List<StaffStatistics> itemStatistics = new List<StaffStatistics>();
  368. if (item.Reviewer != null)
  369. {
  370. Context.Entry(item.Reviewer).Reference(b => b.StaffGrade).Load();
  371. }
  372. foreach (ItemStaff itemStaff in item.ItemStaffs)
  373. {
  374. Context.Entry(itemStaff).Reference(b => b.DoPerson).Load();
  375. Context.Entry(itemStaff.DoPerson).Reference(b => b.StaffGrade).Load();
  376. #region 计算审核人绩效点数,核稿人绩效点数按照核稿人与个处理人的核稿系数计算后加总,没有找到核稿系数(比如同级别),核稿系数为0
  377. if (item.ReviewerId != null && item.Type !="专案")
  378. {
  379. #region 取审核人等级审核等级系数
  380. VerifyCoefficient vcoefficient = verifyCoefficients.Where<VerifyCoefficient>(v =>
  381. v.CheckerId == item.Reviewer.StaffGrade.Id
  382. && v.DoPersonId == itemStaff.DoPerson.StaffGradeId)
  383. .FirstOrDefault<VerifyCoefficient>();
  384. #endregion
  385. if (vcoefficient != null)
  386. {
  387. double reviewerBasePoint = item.BasePoint.Value * vcoefficient.Coefficient;
  388. string temJxType = $"{item.Type}审核";
  389. var temReviewerStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == item.ReviewerId && s.jxType == temJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  390. if (temReviewerStatic != null)
  391. {
  392. temReviewerStatic.totalBasePoint += reviewerBasePoint;
  393. }
  394. else
  395. {
  396. if (item.Reviewer.IsOnJob) //判断是否在职
  397. {
  398. temReviewerStatic = new StaffStatistics()
  399. {
  400. CalMonth = calMonth,
  401. CalMonthId = calMonth.Id,
  402. StaffId = item.ReviewerId.Value,
  403. totalBasePoint = reviewerBasePoint,
  404. jxType = temJxType
  405. };
  406. itemStatistics.Add(temReviewerStatic);
  407. }
  408. }
  409. }
  410. }
  411. #endregion
  412. #region 计算各处理人的绩效点数
  413. double handlerBasePoint;
  414. if (item.Type != "专案")
  415. {
  416. if (isPJFP)
  417. {
  418. handlerBasePoint = item.BasePoint.Value * 1.0 / total;
  419. }
  420. else
  421. {
  422. handlerBasePoint = item.BasePoint.Value * itemStaff.PerformancePoint.Value / total;
  423. }
  424. }
  425. else
  426. {
  427. handlerBasePoint = itemStaff.PerformancePoint.Value;
  428. }
  429. string handlerJxType = $"{item.Type}处理";
  430. var temStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == itemStaff.DoPersonId && s.jxType == handlerJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  431. if (temStatic != null)
  432. {
  433. if (item.Type != "专案")
  434. {
  435. temStatic.totalBasePoint += handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient;
  436. }
  437. else
  438. {
  439. temStatic.totalBasePoint += handlerBasePoint;
  440. }
  441. }
  442. else
  443. {
  444. if (itemStaff.DoPerson.StaffGrade != null && itemStaff.DoPerson.IsOnJob)
  445. {
  446. if (item.Type != "专案")
  447. {
  448. temStatic = new StaffStatistics()
  449. {
  450. CalMonth = calMonth,
  451. CalMonthId = calMonth.Id,
  452. StaffId = itemStaff.DoPersonId,
  453. totalBasePoint = handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient,
  454. jxType = handlerJxType
  455. };
  456. itemStatistics.Add(temStatic);
  457. }
  458. else
  459. {
  460. temStatic = new StaffStatistics()
  461. {
  462. CalMonth = calMonth,
  463. CalMonthId = calMonth.Id,
  464. StaffId = itemStaff.DoPersonId,
  465. totalBasePoint = handlerBasePoint ,
  466. jxType = handlerJxType
  467. };
  468. itemStatistics.Add(temStatic);
  469. }
  470. }
  471. }
  472. #endregion
  473. }
  474. List<StaffStatistics> temItemStatics;
  475. if (userid != null)
  476. {
  477. temItemStatics = itemStatistics.Where<StaffStatistics>(s => s.StaffId == userid).ToList();
  478. }
  479. else
  480. {
  481. temItemStatics = itemStatistics;
  482. }
  483. foreach (StaffStatistics retUserValue in temItemStatics)
  484. {
  485. var temValue = retList.Where<StaffStatistics>(s => s.StaffId == retUserValue.StaffId && s.jxType == retUserValue.jxType && s.CalMonthId == calMonth.Id).FirstOrDefault();
  486. if (temValue != null)
  487. {
  488. temValue.totalBasePoint += retUserValue.totalBasePoint;
  489. }
  490. else
  491. {
  492. retList.Add(retUserValue);
  493. }
  494. }
  495. }
  496. }
  497. if(userid != null)
  498. {
  499. retList = retList.Where<StaffStatistics>(s => s.StaffId == userid.Value).ToList();
  500. }
  501. IDictionary<int, double> staffXiShu = new Dictionary<int, double>();
  502. foreach (StaffStatistics ss in retList)
  503. {
  504. if (ss.jxType.Contains("新申请") || ss.jxType.Contains("专案"))
  505. {
  506. if (!staffXiShu.ContainsKey(ss.StaffId))
  507. {
  508. staffXiShu.Add(ss.StaffId, DegreeOfDifficulty(calMonth.Year ,calMonth.Month, ss.StaffId));
  509. }
  510. ss.totalActuallyPoint = ss.totalBasePoint * staffXiShu[ss.StaffId] / gspjXS;
  511. }
  512. else
  513. {
  514. ss.totalActuallyPoint = ss.totalBasePoint;
  515. }
  516. ss.CalMonth.PerformanceItems = null;
  517. }
  518. return retList;
  519. }
  520. /// <summary>
  521. /// 计算指定用户,指定年月的绩效统计信息
  522. /// </summary>
  523. /// <param name="userid"></param>
  524. /// <param name="year"></param>
  525. /// <param name="month"></param>
  526. /// <returns></returns>
  527. public List<StaffStatistics> CalMyStatistics(int year,int month, int? userid=null)
  528. {
  529. CalMonth calMonth = Context.CalMonths.Where<CalMonth>(c => c.Month == month && c.Year == year).FirstOrDefault();
  530. if(calMonth == null)
  531. {
  532. return null;
  533. }
  534. else
  535. {
  536. if(calMonth.Status == 4)
  537. {
  538. //已归档,归档数据库中直接取出记录
  539. if (userid == null)
  540. {
  541. return Context.StaffStatistics.Where<StaffStatistics>(s => s.CalMonthId == calMonth.Id).ToList<StaffStatistics>();
  542. }
  543. else
  544. {
  545. return Context.StaffStatistics.Where<StaffStatistics>(s => s.CalMonthId == calMonth.Id && s.StaffId == userid).ToList<StaffStatistics>();
  546. }
  547. }
  548. else
  549. {
  550. return _CalMyStatistics(calMonth, userid);
  551. }
  552. }
  553. }
  554. private string GetExpress(IList<FieldCondition> conditions)
  555. {
  556. string str = "";
  557. foreach(var c in conditions)
  558. {
  559. if (string.IsNullOrEmpty(str))
  560. {
  561. str = c.ToExpressString("s");
  562. }
  563. else
  564. {
  565. if(c.LogicOperate == LogicEnum.And)
  566. {
  567. str = $"({str}) && {c.ToExpressString("s")}";
  568. }
  569. else
  570. {
  571. str = $"({str}) || {c.ToExpressString("s")}";
  572. }
  573. }
  574. }
  575. return str;
  576. }
  577. [HttpPost]
  578. public ListApiResponse<PerformanceItem> QueryFilter(QueryFilter queryFilter)
  579. {
  580. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  581. string strExpress = "";
  582. if (!string.IsNullOrEmpty(strExpress))
  583. {
  584. strExpress = $"{strExpress} && s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  585. }
  586. else
  587. {
  588. strExpress = $"s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  589. }
  590. if (queryFilter.ConditionTree != null)
  591. {
  592. string strTem = GetExpress(queryFilter.ConditionTree);
  593. if (!string.IsNullOrEmpty(strTem))
  594. {
  595. strExpress = $"{strExpress} && ({strTem})";
  596. }
  597. }
  598. var interpreter = new Interpreter();
  599. Expression<Func<PerformanceItem, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<PerformanceItem, bool>>(strExpress, "s");
  600. IQueryable<PerformanceItem> response;
  601. if (queryFilter.userId > 0)
  602. {
  603. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere).Where(s => (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == queryFilter.userId).Count() > 0 || s.ReviewerId == queryFilter.userId));
  604. }
  605. else
  606. {
  607. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere);
  608. }
  609. int totals = response.ToList<PerformanceItem>().Count;
  610. if (totals > 0 && totals < (queryFilter.PageIndex - 1) * queryFilter.PageSize) {
  611. response = response
  612. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  613. .Include(pi => pi.Reviewer)
  614. .Include(pi => pi.Customer)
  615. .Include(pi => pi.CalMonth)
  616. .OrderConditions<PerformanceItem>(queryFilter.Sorts)
  617. .Pager<PerformanceItem>(1, queryFilter.PageSize, out totals);
  618. }
  619. else
  620. {
  621. response = response
  622. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  623. .Include(pi => pi.Reviewer)
  624. .Include(pi => pi.Customer)
  625. .Include(pi => pi.CalMonth)
  626. .OrderConditions<PerformanceItem>(queryFilter.Sorts)
  627. .Pager<PerformanceItem>(queryFilter.PageIndex, queryFilter.PageSize, out totals);
  628. }
  629. ret.TotalCount = totals;
  630. var retList = response.ToList<PerformanceItem>();
  631. #region 将某些属性设为null,避免循环取值造成返回json过大
  632. foreach (PerformanceItem item in retList)
  633. {
  634. foreach (ItemStaff itemStaff in item.ItemStaffs)
  635. {
  636. itemStaff.DoPerson.ItemStaffs = null;
  637. itemStaff.DoPerson.ReviewerItems = null;
  638. itemStaff.Item = null;
  639. }
  640. if (item.Reviewer != null)
  641. {
  642. item.Reviewer.ReviewerItems = null;
  643. item.Reviewer.Customers = null;
  644. item.Reviewer.ItemStaffs = null;
  645. }
  646. if (item.Customer != null)
  647. {
  648. item.Customer.PerformanceItems = null;
  649. }
  650. if (item.CalMonth != null)
  651. {
  652. item.CalMonth.PerformanceItems = null;
  653. }
  654. }
  655. #endregion
  656. ret.Results = retList;
  657. return ret;
  658. }
  659. public ApiSaveResponse AddProjectPerformance(ProjectPointRecord pointRecord)
  660. {
  661. ApiSaveResponse retResponse = new ApiSaveResponse();
  662. retResponse.Success = true;
  663. if (pointRecord != null && pointRecord.ProjectDoItemPoints != null && pointRecord.ProjectDoItemPoints.Count > 0)
  664. {
  665. using (var t = Context.Database.BeginTransaction())
  666. {
  667. try
  668. {
  669. CalMonth calMonth = Context.CalMonths.FirstOrDefault<CalMonth>(c=>c.Status ==0);
  670. if(calMonth == null)
  671. {
  672. retResponse.Success = false;
  673. retResponse.ErrorMessage = "不存在正在处理的绩效月度!";
  674. return retResponse;
  675. }
  676. foreach (var doItem in pointRecord.ProjectDoItemPoints)
  677. {
  678. PerformanceItem item = new PerformanceItem();
  679. item.CaseNo = pointRecord.CaseNo;
  680. item.CaseName = pointRecord.CaseName;
  681. item.CaseMemo = pointRecord.Reason;
  682. item.DoItem = doItem.DoItem;
  683. item.CaseCoefficient = doItem.DoItemCoefficient;
  684. item.Type = "专案";
  685. item.CalMonthId = calMonth.Id;
  686. Context.PerformanceItems.Add(item);
  687. Context.SaveChanges();
  688. item.ItemStaffs = new List<ItemStaff>();
  689. foreach (var p in doItem.PersonPoints)
  690. {
  691. ItemStaff itemStaff = new ItemStaff();
  692. itemStaff.PerformancePoint = p.Point;
  693. var staff = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == p.Person);
  694. if (staff != null)
  695. {
  696. itemStaff.DoPersonId = staff.Id;
  697. itemStaff.ItemId = item.Id;
  698. Context.ItemStaffs.Add(itemStaff);
  699. Context.SaveChanges();
  700. }
  701. else
  702. {
  703. retResponse.Success = false;
  704. retResponse.ErrorMessage = $"用户【{p.Person}】不存在!";
  705. t.Rollback();
  706. return retResponse;
  707. }
  708. }
  709. }
  710. t.Commit();
  711. }
  712. catch(Exception ex)
  713. {
  714. retResponse.Success = false;
  715. retResponse.ErrorMessage = ex.Message;
  716. t.Rollback();
  717. return retResponse;
  718. }
  719. }
  720. }
  721. return retResponse;
  722. }
  723. public PerformanceItem GetCaseInfo(string CaseNo)
  724. {
  725. var retObj = Context.PerformanceItems.OrderByDescending(p=>p.CalMonthId).FirstOrDefault<PerformanceItem>(p=>p.CaseNo == CaseNo.Trim());
  726. if(retObj == null)
  727. {
  728. retObj = new IPEasyController(Context).GetCaseInfo(CaseNo);
  729. }
  730. return retObj;
  731. }
  732. public PerformanceItem GetItemInfo(string CaseNo, string DoItem)
  733. {
  734. var retObj = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.CaseNo == CaseNo.Trim() && p.DoItem == DoItem.Trim());
  735. if (retObj == null)
  736. {
  737. retObj = new IPEasyController(Context).GetItemInfo(CaseNo,DoItem);
  738. }
  739. return retObj;
  740. }
  741. }
  742. }