PerformanceItemController.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. private List<StaffStatistics> _CalMyStatistics(CalMonth calMonth, int? userid = null)
  328. {
  329. double gspjXS = DegreeOfDifficulty(calMonth.Year , calMonth.Month);
  330. //未归档,从绩效记录中统计数据
  331. var results = Context.PerformanceItems.Where<PerformanceItem>(s => s.CalMonth.Id == calMonth.Id);
  332. if (userid != null)
  333. {
  334. results = Context.PerformanceItems.Where<PerformanceItem>(s =>
  335. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid).Count() > 0 || s.Reviewer.Id == userid)
  336. && s.CalMonth.Id == calMonth.Id);
  337. }
  338. List<PerformanceItem> ItemList = results
  339. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  340. .Include(pi => pi.Reviewer)
  341. .OrderByDescending(o => o.Id)
  342. .ToList<PerformanceItem>();
  343. List<StaffStatistics> retList = new List<StaffStatistics>();
  344. List<VerifyCoefficient> verifyCoefficients = Context.VerifyCoefficients.ToList<VerifyCoefficient>();
  345. foreach (PerformanceItem item in ItemList)
  346. {
  347. if (item.BasePoint != null && item.BasePoint.Value > 0)
  348. {
  349. double doPersonBasePoint = item.BasePoint.Value;
  350. System.Collections.Hashtable doPersonsBL = new System.Collections.Hashtable();
  351. bool isPJFP = true;
  352. double total = item.ItemStaffs.Count();
  353. if (item.ItemStaffs.Where<ItemStaff>(p => p.PerformancePoint == null || p.PerformancePoint == 0).Count() == 0)
  354. {
  355. total = item.ItemStaffs.Select(i => i.PerformancePoint.Value).Sum();
  356. isPJFP = false;
  357. }
  358. List<StaffStatistics> itemStatistics = new List<StaffStatistics>();
  359. if (item.Reviewer != null)
  360. {
  361. Context.Entry(item.Reviewer).Reference(b => b.StaffGrade).Load();
  362. }
  363. foreach (ItemStaff itemStaff in item.ItemStaffs)
  364. {
  365. Context.Entry(itemStaff).Reference(b => b.DoPerson).Load();
  366. Context.Entry(itemStaff.DoPerson).Reference(b => b.StaffGrade).Load();
  367. #region 计算审核人绩效点数,核稿人绩效点数按照核稿人与个处理人的核稿系数计算后加总,没有找到核稿系数(比如同级别),核稿系数为0
  368. if (item.ReviewerId != null && item.Type !="专案")
  369. {
  370. #region 取审核人等级审核等级系数
  371. VerifyCoefficient vcoefficient = verifyCoefficients.Where<VerifyCoefficient>(v =>
  372. v.CheckerId == item.Reviewer.StaffGrade.Id
  373. && v.DoPersonId == itemStaff.DoPerson.StaffGradeId)
  374. .FirstOrDefault<VerifyCoefficient>();
  375. #endregion
  376. if (vcoefficient != null)
  377. {
  378. double reviewerBasePoint = item.BasePoint.Value * vcoefficient.Coefficient;
  379. string temJxType = $"{item.Type}审核";
  380. var temReviewerStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == item.ReviewerId && s.jxType == temJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  381. if (temReviewerStatic != null)
  382. {
  383. temReviewerStatic.totalBasePoint += reviewerBasePoint;
  384. }
  385. else
  386. {
  387. if (item.Reviewer.IsOnJob) //判断是否在职
  388. {
  389. temReviewerStatic = new StaffStatistics()
  390. {
  391. CalMonth = calMonth,
  392. CalMonthId = calMonth.Id,
  393. StaffId = item.ReviewerId.Value,
  394. totalBasePoint = reviewerBasePoint,
  395. jxType = temJxType
  396. };
  397. itemStatistics.Add(temReviewerStatic);
  398. }
  399. }
  400. }
  401. }
  402. #endregion
  403. #region 计算各处理人的绩效点数
  404. double handlerBasePoint;
  405. if (item.Type != "专案")
  406. {
  407. if (isPJFP)
  408. {
  409. handlerBasePoint = item.BasePoint.Value * 1.0 / total;
  410. }
  411. else
  412. {
  413. handlerBasePoint = item.BasePoint.Value * itemStaff.PerformancePoint.Value / total;
  414. }
  415. }
  416. else
  417. {
  418. handlerBasePoint = itemStaff.PerformancePoint.Value;
  419. }
  420. string handlerJxType = $"{item.Type}处理";
  421. var temStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == itemStaff.DoPersonId && s.jxType == handlerJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  422. if (temStatic != null)
  423. {
  424. if (item.Type != "专案")
  425. {
  426. temStatic.totalBasePoint += handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient;
  427. }
  428. else
  429. {
  430. temStatic.totalBasePoint += handlerBasePoint;
  431. }
  432. }
  433. else
  434. {
  435. if (itemStaff.DoPerson.StaffGrade != null && itemStaff.DoPerson.IsOnJob)
  436. {
  437. if (item.Type != "专案")
  438. {
  439. temStatic = new StaffStatistics()
  440. {
  441. CalMonth = calMonth,
  442. CalMonthId = calMonth.Id,
  443. StaffId = itemStaff.DoPersonId,
  444. totalBasePoint = handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient,
  445. jxType = handlerJxType
  446. };
  447. itemStatistics.Add(temStatic);
  448. }
  449. else
  450. {
  451. temStatic = new StaffStatistics()
  452. {
  453. CalMonth = calMonth,
  454. CalMonthId = calMonth.Id,
  455. StaffId = itemStaff.DoPersonId,
  456. totalBasePoint = handlerBasePoint ,
  457. jxType = handlerJxType
  458. };
  459. itemStatistics.Add(temStatic);
  460. }
  461. }
  462. }
  463. #endregion
  464. }
  465. List<StaffStatistics> temItemStatics;
  466. if (userid != null)
  467. {
  468. temItemStatics = itemStatistics.Where<StaffStatistics>(s => s.StaffId == userid).ToList();
  469. }
  470. else
  471. {
  472. temItemStatics = itemStatistics;
  473. }
  474. foreach (StaffStatistics retUserValue in temItemStatics)
  475. {
  476. var temValue = retList.Where<StaffStatistics>(s => s.StaffId == retUserValue.StaffId && s.jxType == retUserValue.jxType && s.CalMonthId == calMonth.Id).FirstOrDefault();
  477. if (temValue != null)
  478. {
  479. temValue.totalBasePoint += retUserValue.totalBasePoint;
  480. }
  481. else
  482. {
  483. retList.Add(retUserValue);
  484. }
  485. }
  486. }
  487. }
  488. if(userid != null)
  489. {
  490. retList = retList.Where<StaffStatistics>(s => s.StaffId == userid.Value).ToList();
  491. }
  492. IDictionary<int, double> staffXiShu = new Dictionary<int, double>();
  493. foreach (StaffStatistics ss in retList)
  494. {
  495. if (ss.jxType.Contains("新申请") || ss.jxType.Contains("专案"))
  496. {
  497. if (!staffXiShu.ContainsKey(ss.StaffId))
  498. {
  499. staffXiShu.Add(ss.StaffId, DegreeOfDifficulty(calMonth.Year ,calMonth.Month, ss.StaffId));
  500. }
  501. ss.totalActuallyPoint = ss.totalBasePoint * staffXiShu[ss.StaffId] / gspjXS;
  502. }
  503. else
  504. {
  505. ss.totalActuallyPoint = ss.totalBasePoint;
  506. }
  507. ss.CalMonth.PerformanceItems = null;
  508. }
  509. return retList;
  510. }
  511. /// <summary>
  512. /// 计算指定用户,指定年月的绩效统计信息
  513. /// </summary>
  514. /// <param name="userid"></param>
  515. /// <param name="year"></param>
  516. /// <param name="month"></param>
  517. /// <returns></returns>
  518. public List<StaffStatistics> CalMyStatistics(int year,int month, int? userid=null)
  519. {
  520. CalMonth calMonth = Context.CalMonths.Where<CalMonth>(c => c.Month == month && c.Year == year).FirstOrDefault();
  521. if(calMonth == null)
  522. {
  523. return null;
  524. }
  525. else
  526. {
  527. if(calMonth.Status == 4)
  528. {
  529. //已归档,归档数据库中直接取出记录
  530. return null;
  531. }
  532. else
  533. {
  534. return _CalMyStatistics(calMonth, userid);
  535. }
  536. }
  537. }
  538. private string GetExpress(IList<FieldCondition> conditions)
  539. {
  540. string str = "";
  541. foreach(var c in conditions)
  542. {
  543. if (string.IsNullOrEmpty(str))
  544. {
  545. str = c.ToExpressString("s");
  546. }
  547. else
  548. {
  549. if(c.LogicOperate == LogicEnum.And)
  550. {
  551. str = $"({str}) && {c.ToExpressString("s")}";
  552. }
  553. else
  554. {
  555. str = $"({str}) || {c.ToExpressString("s")}";
  556. }
  557. }
  558. }
  559. return str;
  560. }
  561. [HttpPost]
  562. public ListApiResponse<PerformanceItem> QueryFilter(QueryFilter queryFilter)
  563. {
  564. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  565. string strExpress = "";
  566. if (!string.IsNullOrEmpty(strExpress))
  567. {
  568. strExpress = $"{strExpress} && s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  569. }
  570. else
  571. {
  572. strExpress = $"s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  573. }
  574. if (queryFilter.ConditionTree != null)
  575. {
  576. string strTem = GetExpress(queryFilter.ConditionTree);
  577. if (!string.IsNullOrEmpty(strTem))
  578. {
  579. strExpress = $"{strExpress} && ({strTem})";
  580. }
  581. }
  582. var interpreter = new Interpreter();
  583. Expression<Func<PerformanceItem, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<PerformanceItem, bool>>(strExpress, "s");
  584. IQueryable<PerformanceItem> response;
  585. if (queryFilter.userId > 0)
  586. {
  587. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere).Where(s => (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == queryFilter.userId).Count() > 0 || s.ReviewerId == queryFilter.userId));
  588. }
  589. else
  590. {
  591. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere);
  592. }
  593. int totals = response.ToList<PerformanceItem>().Count;
  594. if (totals > 0 && totals < (queryFilter.PageIndex - 1) * queryFilter.PageSize) {
  595. response = response
  596. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  597. .Include(pi => pi.Reviewer)
  598. .Include(pi => pi.Customer)
  599. .Include(pi => pi.CalMonth)
  600. .OrderConditions<PerformanceItem>(queryFilter.Sorts)
  601. .Pager<PerformanceItem>(1, queryFilter.PageSize, out totals);
  602. }
  603. else
  604. {
  605. response = response
  606. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  607. .Include(pi => pi.Reviewer)
  608. .Include(pi => pi.Customer)
  609. .Include(pi => pi.CalMonth)
  610. .OrderConditions<PerformanceItem>(queryFilter.Sorts)
  611. .Pager<PerformanceItem>(queryFilter.PageIndex, queryFilter.PageSize, out totals);
  612. }
  613. ret.TotalCount = totals;
  614. var retList = response.ToList<PerformanceItem>();
  615. #region 将某些属性设为null,避免循环取值造成返回json过大
  616. foreach (PerformanceItem item in retList)
  617. {
  618. foreach (ItemStaff itemStaff in item.ItemStaffs)
  619. {
  620. itemStaff.DoPerson.ItemStaffs = null;
  621. itemStaff.DoPerson.ReviewerItems = null;
  622. itemStaff.Item = null;
  623. }
  624. if (item.Reviewer != null)
  625. {
  626. item.Reviewer.ReviewerItems = null;
  627. item.Reviewer.Customers = null;
  628. item.Reviewer.ItemStaffs = null;
  629. }
  630. if (item.Customer != null)
  631. {
  632. item.Customer.PerformanceItems = null;
  633. }
  634. if (item.CalMonth != null)
  635. {
  636. item.CalMonth.PerformanceItems = null;
  637. }
  638. }
  639. #endregion
  640. ret.Results = retList;
  641. return ret;
  642. }
  643. public ApiSaveResponse AddProjectPerformance(ProjectPointRecord pointRecord)
  644. {
  645. ApiSaveResponse retResponse = new ApiSaveResponse();
  646. retResponse.Success = true;
  647. if (pointRecord != null && pointRecord.ProjectDoItemPoints != null && pointRecord.ProjectDoItemPoints.Count > 0)
  648. {
  649. using (var t = Context.Database.BeginTransaction())
  650. {
  651. try
  652. {
  653. CalMonth calMonth = Context.CalMonths.FirstOrDefault<CalMonth>(c=>c.Status ==0);
  654. if(calMonth == null)
  655. {
  656. retResponse.Success = false;
  657. retResponse.ErrorMessage = "不存在正在处理的绩效月度!";
  658. return retResponse;
  659. }
  660. foreach (var doItem in pointRecord.ProjectDoItemPoints)
  661. {
  662. PerformanceItem item = new PerformanceItem();
  663. item.CaseNo = pointRecord.CaseNo;
  664. item.CaseName = pointRecord.CaseName;
  665. item.CaseMemo = pointRecord.Reason;
  666. item.DoItem = doItem.DoItem;
  667. item.CaseCoefficient = doItem.DoItemCoefficient;
  668. item.Type = "专案";
  669. item.CalMonthId = calMonth.Id;
  670. Context.PerformanceItems.Add(item);
  671. Context.SaveChanges();
  672. item.ItemStaffs = new List<ItemStaff>();
  673. foreach (var p in doItem.PersonPoints)
  674. {
  675. ItemStaff itemStaff = new ItemStaff();
  676. itemStaff.PerformancePoint = p.Point;
  677. var staff = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == p.Person);
  678. if (staff != null)
  679. {
  680. itemStaff.DoPersonId = staff.Id;
  681. itemStaff.ItemId = item.Id;
  682. Context.ItemStaffs.Add(itemStaff);
  683. Context.SaveChanges();
  684. }
  685. else
  686. {
  687. retResponse.Success = false;
  688. retResponse.ErrorMessage = $"用户【{p.Person}】不存在!";
  689. t.Rollback();
  690. return retResponse;
  691. }
  692. }
  693. }
  694. t.Commit();
  695. }
  696. catch(Exception ex)
  697. {
  698. retResponse.Success = false;
  699. retResponse.ErrorMessage = ex.Message;
  700. t.Rollback();
  701. return retResponse;
  702. }
  703. }
  704. }
  705. return retResponse;
  706. }
  707. }
  708. }