PerformanceItemController.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. /// <summary>
  294. /// 计算指定用户,指定年月的绩效统计信息
  295. /// </summary>
  296. /// <param name="userid"></param>
  297. /// <param name="year"></param>
  298. /// <param name="month"></param>
  299. /// <returns></returns>
  300. public List<StaffStatistics> CalMyStatistics(int year,int month, int? userid=null)
  301. {
  302. CalMonth calMonth = Context.CalMonths.Where<CalMonth>(c => c.Month == month && c.Year == year).FirstOrDefault();
  303. if(calMonth == null)
  304. {
  305. return null;
  306. }
  307. else
  308. {
  309. if(calMonth.Status == 4)
  310. {
  311. //已归档,归档数据库中直接取出记录
  312. return null;
  313. }
  314. else
  315. {
  316. //未归档,从绩效记录中统计数据
  317. var results = Context.PerformanceItems.Where<PerformanceItem>(s =>s.CalMonth.Id == calMonth.Id);
  318. if (userid != null)
  319. {
  320. results = Context.PerformanceItems.Where<PerformanceItem>(s =>
  321. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid).Count() > 0 || s.Reviewer.Id == userid)
  322. && s.CalMonth.Id == calMonth.Id);
  323. }
  324. List<PerformanceItem> ItemList = results
  325. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  326. .Include(pi => pi.Reviewer)
  327. .OrderByDescending(o => o.Id)
  328. .ToList<PerformanceItem>();
  329. List<StaffStatistics> retList = new List<StaffStatistics>();
  330. List<VerifyCoefficient> verifyCoefficients = Context.VerifyCoefficients.ToList<VerifyCoefficient>();
  331. foreach (PerformanceItem item in ItemList)
  332. {
  333. if (item.BasePoint != null && item.BasePoint.Value > 0)
  334. {
  335. double doPersonBasePoint = item.BasePoint.Value;
  336. System.Collections.Hashtable doPersonsBL = new System.Collections.Hashtable();
  337. bool isPJFP = true;
  338. double total = item.ItemStaffs.Count();
  339. if(item.ItemStaffs.Where<ItemStaff>(p=>p.PerformancePoint == null || p.PerformancePoint ==0).Count() == 0)
  340. {
  341. total = item.ItemStaffs.Select(i => i.PerformancePoint.Value).Sum();
  342. isPJFP = false;
  343. }
  344. List<StaffStatistics> itemStatistics = new List<StaffStatistics>();
  345. if (item.Reviewer != null)
  346. {
  347. Context.Entry(item.Reviewer).Reference(b => b.StaffGrade).Load();
  348. }
  349. foreach (ItemStaff itemStaff in item.ItemStaffs)
  350. {
  351. Context.Entry(itemStaff).Reference(b => b.DoPerson).Load();
  352. Context.Entry(itemStaff.DoPerson).Reference(b => b.StaffGrade).Load();
  353. #region 计算审核人绩效点数,核稿人绩效点数按照核稿人与个处理人的核稿系数计算后加总,没有找到核稿系数(比如同级别),核稿系数为0
  354. if (item.ReviewerId != null && userid == item.ReviewerId)
  355. {
  356. #region 取审核人等级审核等级系数
  357. VerifyCoefficient vcoefficient = verifyCoefficients.Where<VerifyCoefficient>(v =>
  358. v.CheckerId == item.Reviewer.StaffGrade.Id
  359. && v.DoPersonId == itemStaff.DoPerson.StaffGradeId)
  360. .FirstOrDefault<VerifyCoefficient>();
  361. #endregion
  362. if (vcoefficient != null)
  363. {
  364. double reviewerBasePoint = item.BasePoint.Value * vcoefficient.Coefficient;
  365. string temJxType = $"{item.Type}审核";
  366. var temReviewerStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == item.ReviewerId && s.jxType == temJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  367. if (temReviewerStatic != null)
  368. {
  369. temReviewerStatic.totalBasePoint += reviewerBasePoint;
  370. }
  371. else
  372. {
  373. if (item.Reviewer.IsOnJob) //判断是否在职
  374. {
  375. temReviewerStatic = new StaffStatistics()
  376. {
  377. CalMonth = calMonth,
  378. CalMonthId = calMonth.Id,
  379. StaffId = item.ReviewerId.Value,
  380. totalBasePoint = reviewerBasePoint,
  381. jxType = temJxType
  382. };
  383. itemStatistics.Add(temReviewerStatic);
  384. }
  385. }
  386. }
  387. }
  388. #endregion
  389. #region 计算个处理人的绩效点数
  390. double handlerBasePoint;
  391. if (isPJFP) {
  392. handlerBasePoint = item.BasePoint.Value * 1.0/total;
  393. }
  394. else
  395. {
  396. handlerBasePoint = item.BasePoint.Value * itemStaff.PerformancePoint.Value / total;
  397. }
  398. string handlerJxType = $"{item.Type}处理";
  399. var temStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == itemStaff.DoPersonId && s.jxType == handlerJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  400. if (temStatic != null)
  401. {
  402. temStatic.totalBasePoint += handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient;
  403. }
  404. else
  405. {
  406. if (itemStaff.DoPerson.StaffGrade != null && itemStaff.DoPerson.IsOnJob)
  407. {
  408. temStatic = new StaffStatistics()
  409. {
  410. CalMonth = calMonth,
  411. CalMonthId = calMonth.Id,
  412. StaffId = itemStaff.DoPersonId,
  413. totalBasePoint = handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient,
  414. jxType = handlerJxType
  415. };
  416. itemStatistics.Add(temStatic);
  417. }
  418. }
  419. #endregion
  420. }
  421. List<StaffStatistics> temItemStatics;
  422. if(userid != null)
  423. {
  424. temItemStatics = itemStatistics.Where<StaffStatistics>(s => s.StaffId == userid).ToList();
  425. }
  426. else
  427. {
  428. temItemStatics = itemStatistics;
  429. }
  430. foreach (StaffStatistics retUserValue in temItemStatics)
  431. {
  432. var temValue = retList.Where<StaffStatistics>(s => s.StaffId == retUserValue.StaffId && s.jxType == retUserValue.jxType && s.CalMonthId == calMonth.Id).FirstOrDefault();
  433. if (temValue != null)
  434. {
  435. temValue.totalBasePoint += retUserValue.totalBasePoint;
  436. }
  437. else
  438. {
  439. retList.Add(retUserValue);
  440. }
  441. }
  442. }
  443. }
  444. foreach(StaffStatistics ss in retList)
  445. {
  446. ss.CalMonth.PerformanceItems = null;
  447. }
  448. return retList;
  449. }
  450. }
  451. }
  452. private string GetExpress(IList<FieldCondition> conditions)
  453. {
  454. string str = "";
  455. foreach(var c in conditions)
  456. {
  457. if (string.IsNullOrEmpty(str))
  458. {
  459. str = c.ToExpressString("s");
  460. }
  461. else
  462. {
  463. if(c.LogicOperate == LogicEnum.And)
  464. {
  465. str = $"({str}) && {c.ToExpressString("s")}";
  466. }
  467. else
  468. {
  469. str = $"({str}) || {c.ToExpressString("s")}";
  470. }
  471. }
  472. }
  473. return str;
  474. }
  475. [HttpPost]
  476. public ListApiResponse<PerformanceItem> QueryFilter(QueryFilter queryFilter)
  477. {
  478. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  479. string strExpress = "";
  480. if (!string.IsNullOrEmpty(strExpress))
  481. {
  482. strExpress = $"{strExpress} && s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  483. }
  484. else
  485. {
  486. strExpress = $"s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  487. }
  488. if (queryFilter.ConditionTree != null)
  489. {
  490. string strTem = GetExpress(queryFilter.ConditionTree);
  491. if (!string.IsNullOrEmpty(strTem))
  492. {
  493. strExpress = $"{strExpress} && ({strTem})";
  494. }
  495. }
  496. var interpreter = new Interpreter();
  497. Expression<Func<PerformanceItem, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<PerformanceItem, bool>>(strExpress, "s");
  498. IQueryable<PerformanceItem> response;
  499. if (queryFilter.userId > 0)
  500. {
  501. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere).Where(s => (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == queryFilter.userId).Count() > 0 || s.ReviewerId == queryFilter.userId));
  502. }
  503. else
  504. {
  505. response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere);
  506. }
  507. int totals;
  508. response = response
  509. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  510. .Include(pi => pi.Reviewer)
  511. .Include(pi => pi.Customer)
  512. .Include(pi => pi.CalMonth)
  513. .OrderConditions<PerformanceItem>(queryFilter.Sorts)
  514. .Pager<PerformanceItem>(queryFilter.PageIndex,queryFilter.PageSize,out totals);
  515. ret.TotalCount = totals;
  516. var retList = response.ToList<PerformanceItem>();
  517. #region 将某些属性设为null,避免循环取值造成返回json过大
  518. foreach (PerformanceItem item in retList)
  519. {
  520. foreach (ItemStaff itemStaff in item.ItemStaffs)
  521. {
  522. itemStaff.DoPerson.ItemStaffs = null;
  523. itemStaff.DoPerson.ReviewerItems = null;
  524. itemStaff.Item = null;
  525. }
  526. if (item.Reviewer != null)
  527. {
  528. item.Reviewer.ReviewerItems = null;
  529. item.Reviewer.Customers = null;
  530. item.Reviewer.ItemStaffs = null;
  531. }
  532. if (item.Customer != null)
  533. {
  534. item.Customer.PerformanceItems = null;
  535. }
  536. if (item.CalMonth != null)
  537. {
  538. item.CalMonth.PerformanceItems = null;
  539. }
  540. }
  541. #endregion
  542. ret.Results = retList;
  543. return ret;
  544. }
  545. public ApiSaveResponse AddProjectPerformance(ProjectPointRecord pointRecord)
  546. {
  547. ApiSaveResponse retResponse = new ApiSaveResponse();
  548. retResponse.Success = true;
  549. if (pointRecord != null && pointRecord.ProjectDoItemPoints != null && pointRecord.ProjectDoItemPoints.Count > 0)
  550. {
  551. using (var t = Context.Database.BeginTransaction())
  552. {
  553. try
  554. {
  555. CalMonth calMonth = Context.CalMonths.FirstOrDefault<CalMonth>(c=>c.Status ==0);
  556. if(calMonth == null)
  557. {
  558. retResponse.Success = false;
  559. retResponse.ErrorMessage = "不存在正在处理的绩效月度!";
  560. return retResponse;
  561. }
  562. foreach (var doItem in pointRecord.ProjectDoItemPoints)
  563. {
  564. PerformanceItem item = new PerformanceItem();
  565. item.CaseNo = pointRecord.CaseNo;
  566. item.CaseName = pointRecord.CaseName;
  567. item.CaseMemo = pointRecord.Reason;
  568. item.DoItem = doItem.DoItem;
  569. item.CaseCoefficient = doItem.DoItemCoefficient;
  570. item.CalMonthId = calMonth.Id;
  571. Context.PerformanceItems.Add(item);
  572. Context.SaveChanges();
  573. item.ItemStaffs = new List<ItemStaff>();
  574. foreach (var p in doItem.PersonPoints)
  575. {
  576. ItemStaff itemStaff = new ItemStaff();
  577. itemStaff.PerformancePoint = p.Point;
  578. var staff = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == p.Person);
  579. if (staff != null)
  580. {
  581. itemStaff.DoPersonId = staff.Id;
  582. itemStaff.ItemId = item.Id;
  583. Context.ItemStaffs.Add(itemStaff);
  584. Context.SaveChanges();
  585. }
  586. else
  587. {
  588. retResponse.Success = false;
  589. retResponse.ErrorMessage = $"用户【{p.Person}】不存在!";
  590. t.Rollback();
  591. return retResponse;
  592. }
  593. }
  594. }
  595. t.Commit();
  596. }
  597. catch(Exception ex)
  598. {
  599. retResponse.Success = false;
  600. retResponse.ErrorMessage = ex.Message;
  601. t.Rollback();
  602. return retResponse;
  603. }
  604. }
  605. }
  606. return retResponse;
  607. }
  608. }
  609. }