PerformanceItemController.cs 35 KB

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