PerformanceItemController.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. using DynamicExpresso;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.StaticFiles;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.Extensions.Hosting;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Linq.Expressions;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using wispro.sp.api.Services;
  17. using wispro.sp.api.Utility;
  18. using wispro.sp.entity;
  19. using wispro.sp.share;
  20. using wispro.sp.utility;
  21. namespace wispro.sp.api.Controllers
  22. {
  23. [Route("api/[controller]/[action]")]
  24. [ApiController]
  25. //[Authorize]
  26. public class PerformanceItemController : ControllerBase
  27. {
  28. spDbContext Context;
  29. IFileTaskService fileTaskService;
  30. public PerformanceItemController(spDbContext context, IFileTaskService _fileTaskService)
  31. {
  32. Context = context;
  33. fileTaskService = _fileTaskService;
  34. }
  35. public ApiSaveResponse New(PerformanceItem item)
  36. {
  37. ApiSaveResponse ret = new ApiSaveResponse();
  38. ret.Success = true;
  39. using (Context.Database.BeginTransaction())
  40. {
  41. try
  42. {
  43. var results = Context.PerformanceItems.Where<PerformanceItem>(x =>
  44. x.CaseNo == item.CaseNo && x.DoItem == item.DoItem && x.DoItem != "提出报告" && x.CaseStage == item.CaseStage);
  45. var items = results.Include(pi => pi.CalMonth).FirstOrDefault<PerformanceItem>();
  46. if (items != null)
  47. {
  48. item.AgentFeedbackMemo = "已算绩效";
  49. item.DoItemMemo = $"{items.DoItemMemo}\r\n{items.CalMonth.Year}-{items.CalMonth.Month}已计算!";
  50. item.BasePoint = 0;
  51. }
  52. if (item.CalMonth != null)
  53. {
  54. var calMonth = Context.CalMonths.Where<CalMonth>(c => c.Year == item.CalMonth.Year && c.Month == item.CalMonth.Month).FirstOrDefault();
  55. if(calMonth == null)
  56. {
  57. Context.CalMonths.Add(item.CalMonth);
  58. Context.SaveChanges();
  59. }
  60. else
  61. {
  62. item.CalMonth = calMonth;
  63. }
  64. item.CalMonthId = item.CalMonth.Id;
  65. item.CalMonth = null;
  66. }
  67. if (!string.IsNullOrEmpty(item.Customer.Name))
  68. {
  69. var temCustomer = Context.Customers.Where<Customer>(c => c.Name == item.Customer.Name).FirstOrDefault();
  70. if (temCustomer == null)
  71. {
  72. temCustomer = new Customer() { Name = item.Customer.Name };
  73. //item.Customer.Id = 0;
  74. Context.Customers.Add(temCustomer);
  75. Context.SaveChanges();
  76. item.Customer = temCustomer;
  77. //item.CustomerId = item.Customer.Id;
  78. }
  79. else
  80. {
  81. item.Customer = temCustomer;
  82. }
  83. item.CustomerId = item.Customer.Id;
  84. item.Customer = null;
  85. }
  86. else
  87. {
  88. item.Customer = null;
  89. }
  90. var ItemStaffs = item.ItemStaffs;
  91. item.ItemStaffs = null;
  92. Context.PerformanceItems.Add(item);
  93. Context.SaveChanges();
  94. foreach (ItemStaff itemStaff in ItemStaffs)
  95. {
  96. itemStaff.ItemId = item.Id;
  97. itemStaff.Item = null;
  98. if (itemStaff.DoPersonId == 0 && itemStaff.DoPerson != null)
  99. {
  100. var temStaff = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == itemStaff.DoPerson.Name);
  101. if (temStaff != null)
  102. {
  103. itemStaff.DoPersonId = temStaff.Id;
  104. itemStaff.DoPerson = null;
  105. }
  106. else
  107. {
  108. Context.Staffs.Add(itemStaff.DoPerson);
  109. Context.SaveChanges();
  110. itemStaff.DoPersonId = itemStaff.DoPerson.Id;
  111. itemStaff.DoPerson = null;
  112. }
  113. }
  114. }
  115. Context.ItemStaffs.AddRange(ItemStaffs);
  116. Context.SaveChanges();
  117. Context.Database.CommitTransaction();
  118. }
  119. catch (Exception ex)
  120. {
  121. ret.Success = false;
  122. ret.ErrorMessage = ex.Message;
  123. Context.Database.RollbackTransaction();
  124. }
  125. }
  126. return ret;
  127. }
  128. /// <summary>
  129. /// 更新绩效记录信息
  130. /// </summary>
  131. /// <param name="id">绩效记录编号</param>
  132. /// <param name="field">栏位,多个位以|杠隔开</param>
  133. /// <param name="value">栏位值,多个以|杠隔开</param>
  134. /// <returns></returns>
  135. public ApiSaveResponse UpdateFieldValue(int id,string field,string value)
  136. {
  137. ApiSaveResponse ret = new ApiSaveResponse();
  138. ret.Success = true;
  139. var item = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.Id == id);
  140. if (item == null)
  141. {
  142. ret.Success = false;
  143. ret.ErrorMessage = $"不存在的{id}";
  144. return ret;
  145. }
  146. if(string.IsNullOrEmpty(field) )
  147. {
  148. ret.Success = false;
  149. ret.ErrorMessage = $"参数不对!";
  150. return ret;
  151. }
  152. string[] fields = field.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  153. string[] values = new string[] { null };
  154. if (!string.IsNullOrEmpty(value))
  155. {
  156. values = value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  157. }
  158. if (fields.Length != values.Length) {
  159. ret.Success = false;
  160. ret.ErrorMessage = "栏位和值对不匹配";
  161. }
  162. else
  163. {
  164. for(int i = 0; i < fields.Length; i++)
  165. {
  166. string temField = fields[i];
  167. string temValue = values[i];
  168. switch (temField)
  169. {
  170. case "AgentFeedbackMemo":
  171. item.AgentFeedbackMemo = temValue;
  172. break;
  173. case "CaseCoefficient":
  174. item.CaseCoefficient = temValue;
  175. //此处添加保存到流程系统的代码
  176. break;
  177. case "DoItemCoefficient":
  178. item.DoItemCoefficient = temValue;
  179. //此处添加保存到流程系统的代码
  180. break;
  181. case "WordCount":
  182. int wordCount;
  183. if (int.TryParse(temValue, out wordCount))
  184. {
  185. item.WordCount = wordCount;
  186. }
  187. else
  188. {
  189. ret.Success = false;
  190. ret.ErrorMessage = "所给的栏位值不能转换成数字!";
  191. return ret;
  192. }
  193. break;
  194. case "ReturnCasseNo":
  195. item.ReturnCasseNo = temValue;
  196. break;
  197. }
  198. }
  199. Utility.Utility.CalBasePoint(item, Context.BasePointRules.ToList());
  200. Context.SaveChanges();
  201. }
  202. return ret;
  203. }
  204. public ListApiResponse<PerformanceItem> Query(int pageIndex,int pageSize)
  205. {
  206. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  207. var results = Context.PerformanceItems
  208. .Where<PerformanceItem>(s =>
  209. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Name == User.Identity.Name).Count() > 0 || s.Reviewer.Name == User.Identity.Name)
  210. && s.CalMonth.Status != 4);
  211. ret.TotalCount = results.Count();
  212. List<PerformanceItem> retList = results
  213. .Include(pi=>pi.ItemStaffs).ThenInclude(iStaff=>iStaff.DoPerson)
  214. .Include(pi=>pi.Reviewer)
  215. .Include(pi=>pi.Customer)
  216. .Include(pi=>pi.CalMonth)
  217. .OrderByDescending(o=>o.Id)
  218. .Skip<PerformanceItem>((pageIndex - 1) * pageSize).Take(pageSize).ToList<PerformanceItem>();
  219. #region 将某些属性设为null,避免循环取值造成返回json过大
  220. foreach (PerformanceItem item in retList)
  221. {
  222. foreach (ItemStaff itemStaff in item.ItemStaffs)
  223. {
  224. itemStaff.DoPerson.ItemStaffs = null;
  225. itemStaff.DoPerson.ReviewerItems = null;
  226. itemStaff.Item = null;
  227. }
  228. item.Reviewer.ReviewerItems = null;
  229. item.Reviewer.Customers = null;
  230. item.Reviewer.ItemStaffs = null;
  231. item.Customer.PerformanceItems = null;
  232. item.CalMonth.PerformanceItems = null;
  233. }
  234. #endregion
  235. ret.Results = retList;
  236. return ret;
  237. }
  238. public PerformanceItem Get(int Id)
  239. {
  240. var results = Context.PerformanceItems
  241. .Where<PerformanceItem>(s =>s.Id == Id);
  242. PerformanceItem item = results
  243. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  244. .Include(pi => pi.Reviewer)
  245. .Include(pi => pi.Customer)
  246. .Include(pi => pi.CalMonth)
  247. .OrderByDescending(o => o.Id)
  248. .FirstOrDefault();
  249. #region 将某些属性设为null,避免循环取值造成返回json过大
  250. foreach (ItemStaff itemStaff in item.ItemStaffs)
  251. {
  252. itemStaff.DoPerson.ItemStaffs = null;
  253. itemStaff.DoPerson.ReviewerItems = null;
  254. itemStaff.Item = null;
  255. }
  256. if (item.Reviewer != null)
  257. {
  258. item.Reviewer.ReviewerItems = null;
  259. item.Reviewer.Customers = null;
  260. item.Reviewer.ItemStaffs = null;
  261. }
  262. item.Customer.PerformanceItems = null;
  263. item.CalMonth.PerformanceItems = null;
  264. #endregion
  265. return item;
  266. }
  267. /// <summary>
  268. /// 获取给定用户的绩效清单
  269. /// </summary>
  270. /// <param name="userid">用户id</param>
  271. /// <param name="type">获取类型;0:处理中;1:所有;4:已归档</param>
  272. /// <returns></returns>
  273. public ListApiResponse<PerformanceItem> GetMyList(int userid, int type,int pageIndex=1,int pageSize = 10)
  274. {
  275. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  276. var results = Context.PerformanceItems
  277. .Where<PerformanceItem>(s =>
  278. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid ).Count() > 0 || s.Reviewer.Id == userid )
  279. && s.CalMonth.Status == type);
  280. ret.TotalCount = results.Count();
  281. List<PerformanceItem> retList = results
  282. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  283. .Include(pi => pi.Reviewer)
  284. .Include(pi => pi.Customer)
  285. .Include(pi => pi.CalMonth)
  286. .OrderByDescending(o => o.Id)
  287. .Skip<PerformanceItem>((pageIndex - 1) * pageSize).Take(pageSize)
  288. .ToList<PerformanceItem>();
  289. #region 将某些属性设为null,避免循环取值造成返回json过大
  290. foreach (PerformanceItem item in retList)
  291. {
  292. foreach(ItemStaff itemStaff in item.ItemStaffs)
  293. {
  294. itemStaff.DoPerson.ItemStaffs = null;
  295. itemStaff.DoPerson.ReviewerItems = null;
  296. itemStaff.Item = null;
  297. }
  298. item.Reviewer.ReviewerItems = null;
  299. item.Reviewer.Customers = null;
  300. item.Reviewer.ItemStaffs = null;
  301. item.Customer.PerformanceItems = null;
  302. item.CalMonth.PerformanceItems = null;
  303. }
  304. #endregion
  305. ret.Results = retList;
  306. return ret;
  307. }
  308. public double DegreeOfDifficulty(int year,int month, int? userId = null)
  309. {
  310. IDictionary<string, double> CaseXiShu = new Dictionary<string, double>();
  311. var list = Context.CaseCeoffcients;
  312. foreach(var cx in list.ToList<CaseCeoffcient>())
  313. {
  314. CaseXiShu.Add(cx.Ceoffcient, cx.Value);
  315. }
  316. var results = Context.PerformanceItems.Where<PerformanceItem>(p => p.CalMonth.Year == year && p.CalMonth.Month == month && ((p.Type == "新申请" && p.BasePoint > 0) || p.Type == "专案"));
  317. if(userId != null)
  318. {
  319. results = Context.PerformanceItems.Where<PerformanceItem>(p =>
  320. p.CalMonth.Year == year && p.CalMonth.Month == month && ((p.Type == "新申请" && p.BasePoint >0) || p.Type == "专案") &&
  321. (p.ItemStaffs.Where<ItemStaff>(s=>s.DoPerson.Id == userId).Count ()>0 || p.ReviewerId == userId ));
  322. }
  323. var groupResult = results.GroupBy(x => x.CaseCoefficient).Select(g=> new {
  324. CaseCeoffcient = g.Key,
  325. count = g.Count()
  326. });
  327. int iCount = 0;
  328. double d = 0.0;
  329. foreach(var g in groupResult)
  330. {
  331. if (!string.IsNullOrEmpty(g.CaseCeoffcient))
  332. {
  333. if (CaseXiShu.ContainsKey(g.CaseCeoffcient))
  334. {
  335. d += g.count * CaseXiShu[g.CaseCeoffcient];
  336. iCount += g.count;
  337. }
  338. }
  339. }
  340. return d/(double)iCount;
  341. }
  342. public List<string> GetFeedbackString(int itemId)
  343. {
  344. PerformanceItem item = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.Id == itemId);
  345. if(item != null)
  346. {
  347. return Utility.Utility.GetFeedbackMemos(item, Context.BasePointRules.ToList());
  348. }
  349. return new List<string>();
  350. }
  351. private List<StaffStatistics> _CalMyStatistics(CalMonth calMonth, int? userid = null)
  352. {
  353. double gspjXS = DegreeOfDifficulty(calMonth.Year, calMonth.Month);
  354. //未归档,从绩效记录中统计数据
  355. var results = Context.PerformanceItems.Where<PerformanceItem>(s => s.CalMonth.Id == calMonth.Id);
  356. if (userid != null)
  357. {
  358. results = Context.PerformanceItems.Where<PerformanceItem>(s =>
  359. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == userid).Count() > 0 || s.Reviewer.Id == userid)
  360. && s.CalMonth.Id == calMonth.Id);
  361. }
  362. List<PerformanceItem> ItemList = results
  363. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  364. .Include(pi => pi.Reviewer)
  365. .OrderByDescending(o => o.Id)
  366. .ToList<PerformanceItem>();
  367. List<StaffStatistics> retList = new List<StaffStatistics>();
  368. List<VerifyCoefficient> verifyCoefficients = Context.VerifyCoefficients.ToList<VerifyCoefficient>();
  369. var Rules = Context.BasePointRules.ToList();
  370. foreach (PerformanceItem item in ItemList)
  371. {
  372. //if (item.BasePoint == null)
  373. //{
  374. //Utility.Utility.CalBasePoint(item,Rules);
  375. //Context.SaveChanges();
  376. //}
  377. if (item.BasePoint != null && item.BasePoint.Value > 0)
  378. {
  379. double doPersonBasePoint = item.BasePoint.Value;
  380. List<StaffStatistics> itemStatistics = _calItemJX(calMonth, verifyCoefficients, item,Context);
  381. List<StaffStatistics> temItemStatics;
  382. if (userid != null)
  383. {
  384. temItemStatics = itemStatistics.Where<StaffStatistics>(s => s.StaffId == userid).ToList();
  385. }
  386. else
  387. {
  388. temItemStatics = itemStatistics;
  389. }
  390. foreach (StaffStatistics retUserValue in temItemStatics)
  391. {
  392. var temValue = retList.Where<StaffStatistics>(s => s.StaffId == retUserValue.StaffId && s.jxType == retUserValue.jxType && s.CalMonthId == calMonth.Id).FirstOrDefault();
  393. if (temValue != null)
  394. {
  395. temValue.totalBasePoint += retUserValue.totalBasePoint;
  396. }
  397. else
  398. {
  399. retList.Add(retUserValue);
  400. }
  401. }
  402. }
  403. }
  404. if (userid != null)
  405. {
  406. retList = retList.Where<StaffStatistics>(s => s.StaffId == userid.Value).ToList();
  407. }
  408. IDictionary<int, double> staffXiShu = new Dictionary<int, double>();
  409. foreach (StaffStatistics ss in retList)
  410. {
  411. if (ss.jxType.Contains("新申请") || ss.jxType.Contains("专案"))
  412. {
  413. if (!staffXiShu.ContainsKey(ss.StaffId))
  414. {
  415. staffXiShu.Add(ss.StaffId, DegreeOfDifficulty(calMonth.Year, calMonth.Month, ss.StaffId));
  416. }
  417. ss.totalActuallyPoint = ss.totalBasePoint * staffXiShu[ss.StaffId] / gspjXS;
  418. }
  419. else
  420. {
  421. ss.totalActuallyPoint = ss.totalBasePoint;
  422. }
  423. ss.CalMonth.PerformanceItems = null;
  424. }
  425. return retList;
  426. }
  427. private List<StaffStatistics> _calItemJX(CalMonth calMonth, List<VerifyCoefficient> verifyCoefficients, PerformanceItem item,spDbContext spDb)
  428. {
  429. System.Collections.Hashtable doPersonsBL = new System.Collections.Hashtable();
  430. bool isPJFP = true;
  431. double total = item.ItemStaffs.Count();
  432. if (item.ItemStaffs.Where<ItemStaff>(p => p.PerformancePoint != null || p.PerformancePoint == 0).Count() > 0)
  433. {
  434. total = item.ItemStaffs.Select(i => i.PerformancePoint.Value).Sum();
  435. isPJFP = false;
  436. }
  437. List<StaffStatistics> itemStatistics = new List<StaffStatistics>();
  438. if (item.ReviewerId != null)
  439. {
  440. item.Reviewer = spDb.Staffs.Include(s => s.StaffGrade).FirstOrDefault(p => p.Id == item.ReviewerId);
  441. //spDb.Entry(item.Reviewer).Reference(b => b.StaffGrade).Load();
  442. }
  443. foreach (ItemStaff itemStaff in item.ItemStaffs)
  444. {
  445. if(itemStaff.DoPerson == null)
  446. {
  447. itemStaff.DoPerson = spDb.Staffs.Include(s=>s.StaffGrade).FirstOrDefault(p=>p.Id==itemStaff.DoPersonId);
  448. }
  449. //spDb.Entry(itemStaff).Reference(b => b.DoPerson).Load();
  450. //spDb.Entry(itemStaff.DoPerson).Reference(b => b.StaffGrade).Load();
  451. #region 计算审核人绩效点数,核稿人绩效点数按照核稿人与个处理人的核稿系数计算后加总,没有找到核稿系数(比如同级别),核稿系数为0
  452. if (item.ReviewerId != null && item.Type != "专案")
  453. {
  454. #region 取审核人等级审核等级系数
  455. VerifyCoefficient vcoefficient
  456. = verifyCoefficients.Where<VerifyCoefficient>(v =>
  457. v.CheckerId == item.Reviewer.StaffGrade.Id
  458. && v.DoPersonId == itemStaff.DoPerson.StaffGradeId)
  459. .FirstOrDefault<VerifyCoefficient>();
  460. #endregion
  461. if (vcoefficient != null)
  462. {
  463. double reviewerBasePoint = item.BasePoint.Value * vcoefficient.Coefficient;
  464. string temJxType = $"{item.Type}审核";
  465. var temReviewerStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == item.ReviewerId && s.jxType == temJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  466. if (temReviewerStatic != null)
  467. {
  468. temReviewerStatic.totalBasePoint += reviewerBasePoint;
  469. }
  470. else
  471. {
  472. if (item.Reviewer.IsOnJob && itemStaff.DoPerson.Status != "试用期") //判断是否在职
  473. {
  474. temReviewerStatic = new StaffStatistics()
  475. {
  476. CalMonth = calMonth,
  477. CalMonthId = calMonth.Id,
  478. StaffId = item.ReviewerId.Value,
  479. totalBasePoint = reviewerBasePoint,
  480. jxType = temJxType
  481. };
  482. itemStatistics.Add(temReviewerStatic);
  483. }
  484. }
  485. }
  486. }
  487. #endregion
  488. #region 计算各处理人的绩效点数
  489. double handlerBasePoint;
  490. if (item.Type != "专案")
  491. {
  492. if (isPJFP)
  493. {
  494. handlerBasePoint = item.BasePoint.Value * 1.0 / total;
  495. }
  496. else
  497. {
  498. handlerBasePoint = item.BasePoint.Value * itemStaff.PerformancePoint.Value / total;
  499. }
  500. }
  501. else
  502. {
  503. handlerBasePoint = itemStaff.PerformancePoint.Value;
  504. }
  505. string handlerJxType = $"{item.Type}处理";
  506. var temStatic = itemStatistics.Where<StaffStatistics>(s => s.StaffId == itemStaff.DoPersonId && s.jxType == handlerJxType && s.CalMonth.Id == calMonth.Id).FirstOrDefault();
  507. if (temStatic != null)
  508. {
  509. if (item.Type != "专案")
  510. {
  511. temStatic.totalBasePoint += handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient;
  512. }
  513. else
  514. {
  515. temStatic.totalBasePoint += handlerBasePoint;
  516. }
  517. }
  518. else
  519. {
  520. if (itemStaff.DoPerson.StaffGrade != null && itemStaff.DoPerson.IsOnJob)
  521. {
  522. if (item.Type != "专案")
  523. {
  524. if (itemStaff.DoPerson.Status == "试用期" && item.Reviewer != null)
  525. {
  526. temStatic = new StaffStatistics()
  527. {
  528. CalMonth = calMonth,
  529. CalMonthId = calMonth.Id,
  530. StaffId = item.Reviewer.Id,
  531. totalBasePoint = handlerBasePoint * item.Reviewer.StaffGrade.Coefficient,
  532. jxType = handlerJxType
  533. };
  534. itemStatistics.Add(temStatic);
  535. }
  536. else
  537. {
  538. temStatic = new StaffStatistics()
  539. {
  540. CalMonth = calMonth,
  541. CalMonthId = calMonth.Id,
  542. StaffId = itemStaff.DoPersonId,
  543. totalBasePoint = handlerBasePoint * itemStaff.DoPerson.StaffGrade.Coefficient,
  544. jxType = handlerJxType
  545. };
  546. itemStatistics.Add(temStatic);
  547. }
  548. }
  549. else
  550. {
  551. temStatic = new StaffStatistics()
  552. {
  553. CalMonth = calMonth,
  554. CalMonthId = calMonth.Id,
  555. StaffId = itemStaff.DoPersonId,
  556. totalBasePoint = handlerBasePoint,
  557. jxType = handlerJxType
  558. };
  559. itemStatistics.Add(temStatic);
  560. }
  561. }
  562. }
  563. #endregion
  564. }
  565. return itemStatistics;
  566. }
  567. /// <summary>
  568. /// 计算指定用户,指定年月的绩效统计信息
  569. /// </summary>
  570. /// <param name="userid"></param>
  571. /// <param name="year"></param>
  572. /// <param name="month"></param>
  573. /// <returns></returns>
  574. public List<StaffStatistics> CalMyStatistics(int year,int month, int? userid=null)
  575. {
  576. CalMonth calMonth = Context.CalMonths.Where<CalMonth>(c => c.Month == month && c.Year == year).FirstOrDefault();
  577. if(calMonth == null)
  578. {
  579. return null;
  580. }
  581. else
  582. {
  583. if(calMonth.Status == 4)
  584. {
  585. //已归档,归档数据库中直接取出记录
  586. if (userid == null)
  587. {
  588. return Context.StaffStatistics.Where<StaffStatistics>(s => s.CalMonthId == calMonth.Id).ToList<StaffStatistics>();
  589. }
  590. else
  591. {
  592. return Context.StaffStatistics.Where<StaffStatistics>(s => s.CalMonthId == calMonth.Id && s.StaffId == userid).ToList<StaffStatistics>();
  593. }
  594. }
  595. else
  596. {
  597. return _CalMyStatistics(calMonth, userid);
  598. }
  599. }
  600. }
  601. private string GetExpress(IList<FieldCondition> conditions)
  602. {
  603. string str = "";
  604. foreach(var c in conditions)
  605. {
  606. if (string.IsNullOrEmpty(str))
  607. {
  608. str = c.ToExpressString("s");
  609. }
  610. else
  611. {
  612. if(c.LogicOperate == LogicEnum.And)
  613. {
  614. str = $"({str}) && {c.ToExpressString("s")}";
  615. }
  616. else
  617. {
  618. str = $"({str}) || {c.ToExpressString("s")}";
  619. }
  620. }
  621. }
  622. return str;
  623. }
  624. [HttpGet,HttpPost]
  625. public FileProcessTask ExportData(QueryFilter queryFilter)
  626. {
  627. var filename = $"{DateTime.Now.ToString("yyyyMMddhhmmss")}-绩效数据下载.xlsx";
  628. var attachfileSavePath = utility.ConfigHelper.GetSectionValue("AttachFileSavePath");
  629. var filePath = Path.Combine(attachfileSavePath, filename);
  630. var fileTask = new FileProcessTask()
  631. {
  632. Id = Guid.NewGuid().ToString(),
  633. FileName = filename,
  634. FilePath = filePath,
  635. Processed = 0
  636. };
  637. fileTaskService.Add(fileTask);
  638. ThreadObject threadObject = new ThreadObject()
  639. {
  640. queryFilter = queryFilter,
  641. fileTask = fileTask
  642. };
  643. System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(ExportDataThread));
  644. t.Start(threadObject);
  645. return fileTask;
  646. }
  647. internal class ThreadObject
  648. {
  649. public QueryFilter queryFilter { get; set; }
  650. public FileProcessTask fileTask { get; set; }
  651. }
  652. private void ExportDataThread(object tObj)
  653. {
  654. QueryFilter queryFilter = ((ThreadObject)tObj).queryFilter;
  655. FileProcessTask fileTask = ((ThreadObject)tObj).fileTask;
  656. IQueryable<PerformanceItem> response = NewMethod(queryFilter);
  657. var retList = response
  658. .Include(p=>p.Customer)
  659. .Include(p=>p.ItemStaffs).ThenInclude(p=>p.DoPerson).ThenInclude(p=>p.StaffGrade)
  660. .Include(p=>p.Reviewer).ThenInclude(p=>p.StaffGrade)
  661. .Include(p=>p.PreOastaff)
  662. .Include(p=>p.CalMonth)
  663. .ToList<PerformanceItem>();
  664. DataTable dt = new DataTable();
  665. #region 添加栏位
  666. dt.Columns.Add("我方文号",typeof(string));
  667. dt.Columns.Add("申请类型", typeof(string));
  668. dt.Columns.Add("业务类型", typeof(string));
  669. dt.Columns.Add("备注(填表注意事项)", typeof(string));
  670. dt.Columns.Add("处理事项", typeof(string));
  671. dt.Columns.Add("案件阶段", typeof(string));
  672. dt.Columns.Add("案件系数", typeof(string));
  673. dt.Columns.Add("处理事项系数", typeof(string));
  674. dt.Columns.Add("前一次OA处理事项系数", typeof(string));
  675. dt.Columns.Add("前一次OA处理人", typeof(string));
  676. dt.Columns.Add("处理人等级", typeof(string));
  677. dt.Columns.Add("基本点数", typeof(string));
  678. dt.Columns.Add("核稿系数", typeof(string));
  679. dt.Columns.Add("核稿绩效", typeof(string));
  680. dt.Columns.Add("处理人", typeof(string));
  681. dt.Columns.Add("核稿人", typeof(string));
  682. dt.Columns.Add("客户名称", typeof(string));
  683. dt.Columns.Add("申请人", typeof(string));
  684. dt.Columns.Add("处理事项完成日", typeof(string));
  685. dt.Columns.Add("定稿日", typeof(string));
  686. dt.Columns.Add("返稿日", typeof(string));
  687. dt.Columns.Add("案件类型", typeof(string));
  688. dt.Columns.Add("案件状态", typeof(string));
  689. dt.Columns.Add("处理事项备注", typeof(string));
  690. dt.Columns.Add("处理状态", typeof(string));
  691. dt.Columns.Add("案件名称", typeof(string));
  692. dt.Columns.Add("委案日期", typeof(string));
  693. dt.Columns.Add("客户期限", typeof(string));
  694. dt.Columns.Add("内部期限", typeof(string));
  695. dt.Columns.Add("初稿日", typeof(string));
  696. dt.Columns.Add("备注(发文严重超期是否属客观原因,若为否,请填写原因)", typeof(string));
  697. dt.Columns.Add("备注", typeof(string));
  698. #endregion
  699. List<VerifyCoefficient> verifyCoefficients = new spDbContext().VerifyCoefficients.ToList();
  700. fileTask.Size = retList.Count;
  701. foreach (var item in retList)
  702. {
  703. fileTask.Processed += 1;
  704. if (item.CaseNo.StartsWith("J"))
  705. {
  706. continue;
  707. }
  708. try
  709. {
  710. if (item.CaseNo == "")
  711. {
  712. System.Diagnostics.Debug.WriteLine(item.CaseNo);
  713. }
  714. var row = dt.NewRow();
  715. row["我方文号"] = item.CaseNo;
  716. row["申请类型"] = item.ApplicationType;
  717. row["业务类型"] = item.BusinessType;
  718. row["备注(填表注意事项)"] = item.AgentFeedbackMemo;
  719. row["处理事项"] = item.DoItem;
  720. row["案件阶段"] = item.CaseStage;
  721. row["案件系数"] = item.CaseCoefficient;
  722. row["处理事项系数"] = item.DoItemCoefficient;
  723. row["前一次OA处理事项系数"] = "";
  724. if (item.PreOastaffId.HasValue)
  725. {
  726. row["前一次OA处理人"] = item.PreOastaff?.Name;
  727. }
  728. string strISLevels = "";
  729. string strISNames = "";
  730. foreach (var istaff in item.ItemStaffs)
  731. {
  732. strISLevels = string.IsNullOrEmpty(strISLevels) ? istaff.DoPerson.StaffGrade.Grade : $"{strISLevels},{istaff.DoPerson.StaffGrade.Grade}";
  733. strISNames = string.IsNullOrEmpty(strISNames) ? istaff.DoPerson.Name : $"{strISNames},{istaff.DoPerson.Name}";
  734. }
  735. row["处理人等级"] = strISLevels;
  736. row["基本点数"] = item.BasePoint;
  737. row["处理人"] = strISNames;
  738. row["核稿人"] = item.Reviewer?.Name;
  739. if (item.ReviewerId != null && item.BasePoint.HasValue)
  740. {
  741. var jxList = _calItemJX(item.CalMonth, verifyCoefficients, item, new spDbContext());
  742. row["核稿系数"] = "";
  743. var temJx = jxList.FirstOrDefault<StaffStatistics>(s => s.jxType.Contains("审核") && s.StaffId == item.ReviewerId);
  744. if (temJx != null)
  745. {
  746. row["核稿绩效"] = temJx.totalBasePoint;
  747. }
  748. }
  749. row["客户名称"] = item.Customer?.Name;
  750. row["申请人"] = item.ApplicationName;
  751. row["处理事项完成日"] = item.FinishedDate?.ToString("yyyy-MM-dd");
  752. row["定稿日"] = item.FinalizationDate?.ToString("yyyy-MM-dd");
  753. row["返稿日"] = item.ReturnDate?.ToString("yyyy-MM-dd");
  754. row["案件类型"] = item.CaseType;
  755. row["案件状态"] = item.CaseState;
  756. row["处理事项备注"] = item.DoItemState;
  757. row["处理状态"] = item.DoItemState;
  758. row["案件名称"] = item.CaseName;
  759. row["委案日期"] = item.EntrustingDate?.ToString("yyyy-MM-dd");
  760. row["客户期限"] = item.CustomerLimitDate?.ToString("yyyy-MM-dd");
  761. row["内部期限"] = item.InternalDate?.ToString("yyyy-MM-dd"); ;
  762. row["初稿日"] = item.FirstDraftDate?.ToString("yyyy-MM-dd");
  763. row["备注(发文严重超期是否属客观原因,若为否,请填写原因)"] = item.OverDueMemo;
  764. row["备注"] = item.DoItemMemo;
  765. dt.Rows.Add(row);
  766. }
  767. catch(Exception ex)
  768. {
  769. throw ex;
  770. }
  771. }
  772. utility.NPOIExcel.DataTableToExcel(dt,fileTask.FilePath);
  773. fileTask.Finished = true;
  774. }
  775. [HttpPost]
  776. public ListApiResponse<PerformanceItem> QueryFilter(QueryFilter queryFilter)
  777. {
  778. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  779. IQueryable<PerformanceItem> response = NewMethod(queryFilter);
  780. int totals = response.ToList<PerformanceItem>().Count;
  781. if (totals > 0 && totals < (queryFilter.PageIndex - 1) * queryFilter.PageSize)
  782. {
  783. response = response
  784. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  785. .Include(pi => pi.Reviewer)
  786. .Include(pi => pi.Customer)
  787. .Include(pi => pi.CalMonth)
  788. .OrderConditions<PerformanceItem>(queryFilter.Sorts)
  789. .Pager<PerformanceItem>(1, queryFilter.PageSize, out totals);
  790. }
  791. else
  792. {
  793. response = response
  794. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  795. .Include(pi => pi.Reviewer)
  796. .Include(pi => pi.Customer)
  797. .Include(pi => pi.CalMonth)
  798. .OrderConditions<PerformanceItem>(queryFilter.Sorts)
  799. .Pager<PerformanceItem>(queryFilter.PageIndex, queryFilter.PageSize, out totals);
  800. }
  801. ret.TotalCount = totals;
  802. var retList = response.ToList<PerformanceItem>();
  803. #region 将某些属性设为null,避免循环取值造成返回json过大
  804. foreach (PerformanceItem item in retList)
  805. {
  806. foreach (ItemStaff itemStaff in item.ItemStaffs)
  807. {
  808. itemStaff.DoPerson.ItemStaffs = null;
  809. itemStaff.DoPerson.ReviewerItems = null;
  810. itemStaff.Item = null;
  811. }
  812. if (item.Reviewer != null)
  813. {
  814. item.Reviewer.ReviewerItems = null;
  815. item.Reviewer.Customers = null;
  816. item.Reviewer.ItemStaffs = null;
  817. }
  818. if (item.Customer != null)
  819. {
  820. item.Customer.PerformanceItems = null;
  821. }
  822. if (item.CalMonth != null)
  823. {
  824. item.CalMonth.PerformanceItems = null;
  825. }
  826. }
  827. #endregion
  828. ret.Results = retList;
  829. return ret;
  830. }
  831. private IQueryable<PerformanceItem> NewMethod(QueryFilter queryFilter)
  832. {
  833. string strExpress = "";
  834. if (!string.IsNullOrEmpty(strExpress))
  835. {
  836. strExpress = $"{strExpress} && s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  837. }
  838. else
  839. {
  840. strExpress = $"s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  841. }
  842. if (queryFilter.ConditionTree != null)
  843. {
  844. string strTem = GetExpress(queryFilter.ConditionTree);
  845. if (!string.IsNullOrEmpty(strTem))
  846. {
  847. strExpress = $"{strExpress} && ({strTem})";
  848. }
  849. }
  850. var interpreter = new Interpreter();
  851. Expression<Func<PerformanceItem, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<PerformanceItem, bool>>(strExpress, "s");
  852. IQueryable<PerformanceItem> response;
  853. if (queryFilter.userId > 0)
  854. {
  855. response = new spDbContext().PerformanceItems.Where<PerformanceItem>(dynamicWhere).Where(s => (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == queryFilter.userId).Count() > 0));// || s.ReviewerId == queryFilter.userId));
  856. }
  857. else
  858. {
  859. response = new spDbContext().PerformanceItems.Where<PerformanceItem>(dynamicWhere);
  860. }
  861. return response;
  862. }
  863. public ApiSaveResponse AddProjectPerformance(ProjectPointRecord pointRecord)
  864. {
  865. ApiSaveResponse retResponse = new ApiSaveResponse();
  866. retResponse.Success = true;
  867. if (pointRecord != null && pointRecord.ProjectDoItemPoints != null && pointRecord.ProjectDoItemPoints.Count > 0)
  868. {
  869. using (var t = Context.Database.BeginTransaction())
  870. {
  871. try
  872. {
  873. CalMonth calMonth = Context.CalMonths.FirstOrDefault<CalMonth>(c=>c.Status ==0);
  874. if(calMonth == null)
  875. {
  876. retResponse.Success = false;
  877. retResponse.ErrorMessage = "不存在正在处理的绩效月度!";
  878. return retResponse;
  879. }
  880. foreach (var doItem in pointRecord.ProjectDoItemPoints)
  881. {
  882. PerformanceItem item = new PerformanceItem();
  883. item.CaseNo = pointRecord.CaseNo;
  884. item.CaseName = pointRecord.CaseName;
  885. item.CaseMemo = pointRecord.Reason;
  886. item.DoItem = doItem.DoItem;
  887. item.CaseCoefficient = doItem.DoItemCoefficient;
  888. item.Type = "专案";
  889. item.CalMonthId = calMonth.Id;
  890. Context.PerformanceItems.Add(item);
  891. Context.SaveChanges();
  892. item.ItemStaffs = new List<ItemStaff>();
  893. foreach (var p in doItem.PersonPoints)
  894. {
  895. ItemStaff itemStaff = new ItemStaff();
  896. itemStaff.PerformancePoint = p.Point;
  897. var staff = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == p.Person);
  898. if (staff != null)
  899. {
  900. itemStaff.DoPersonId = staff.Id;
  901. itemStaff.ItemId = item.Id;
  902. Context.ItemStaffs.Add(itemStaff);
  903. Context.SaveChanges();
  904. }
  905. else
  906. {
  907. retResponse.Success = false;
  908. retResponse.ErrorMessage = $"用户【{p.Person}】不存在!";
  909. t.Rollback();
  910. return retResponse;
  911. }
  912. }
  913. }
  914. t.Commit();
  915. }
  916. catch(Exception ex)
  917. {
  918. retResponse.Success = false;
  919. retResponse.ErrorMessage = ex.Message;
  920. t.Rollback();
  921. return retResponse;
  922. }
  923. }
  924. }
  925. return retResponse;
  926. }
  927. public PerformanceItem GetCaseInfo(string CaseNo)
  928. {
  929. var retObj = Context.PerformanceItems.OrderByDescending(p=>p.CalMonthId).FirstOrDefault<PerformanceItem>(p=>p.CaseNo == CaseNo.Trim());
  930. if(retObj == null)
  931. {
  932. retObj = new IPEasyController(Context).GetCaseInfo(CaseNo);
  933. }
  934. return retObj;
  935. }
  936. public PerformanceItem GetItemInfo(string CaseNo, string DoItem)
  937. {
  938. var retObj = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.CaseNo == CaseNo.Trim() && p.DoItem == DoItem.Trim());
  939. if (retObj == null)
  940. {
  941. retObj = new IPEasyController(Context).GetItemInfo(CaseNo,DoItem);
  942. }
  943. return retObj;
  944. }
  945. public PerformanceItem GetItemInfoByCaseStage(string CaseNo, string DoItem,string caseStage)
  946. {
  947. var retObj = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.CaseNo == CaseNo.Trim()
  948. && p.DoItem == DoItem.Trim() && p.CaseStage == caseStage);
  949. if (retObj == null)
  950. {
  951. retObj = IPEasyUtility.GetPerformanceRecord(CaseNo, DoItem, caseStage);
  952. }
  953. return retObj;
  954. }
  955. }
  956. }