PerformanceItemController.cs 49 KB

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