PerformanceItemController.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. string strCalMonth = "";
  835. if (queryFilter.CalMonthId.HasValue)
  836. {
  837. strCalMonth = $"s.CalMonthId == {queryFilter.CalMonthId}";
  838. }
  839. else
  840. {
  841. strCalMonth = $"s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
  842. }
  843. if (!string.IsNullOrEmpty(strExpress))
  844. {
  845. strExpress = $"{strExpress} && {strCalMonth}";
  846. }
  847. else
  848. {
  849. strExpress = strCalMonth;
  850. }
  851. if (queryFilter.ConditionTree != null)
  852. {
  853. string strTem = GetExpress(queryFilter.ConditionTree);
  854. if (!string.IsNullOrEmpty(strTem))
  855. {
  856. strExpress = $"{strExpress} && ({strTem})";
  857. }
  858. }
  859. var interpreter = new Interpreter();
  860. Expression<Func<PerformanceItem, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<PerformanceItem, bool>>(strExpress, "s");
  861. IQueryable<PerformanceItem> response;
  862. if (queryFilter.userId > 0)
  863. {
  864. 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));
  865. }
  866. else
  867. {
  868. response = new spDbContext().PerformanceItems.Where<PerformanceItem>(dynamicWhere);
  869. }
  870. return response;
  871. }
  872. public ApiSaveResponse AddProjectContents(ProjectContents projectContents)
  873. {
  874. ApiSaveResponse retResponse = new ApiSaveResponse();
  875. retResponse.Success = true;
  876. if (projectContents != null && projectContents.ProjectWorkContents != null && projectContents.ProjectWorkContents.Count > 0)
  877. {
  878. using (var t = Context.Database.BeginTransaction())
  879. {
  880. try
  881. {
  882. CalMonth calMonth = Context.CalMonths.FirstOrDefault<CalMonth>(c => c.Status == 0);
  883. if (calMonth == null)
  884. {
  885. retResponse.Success = false;
  886. retResponse.ErrorMessage = "不存在正在处理的绩效月度!";
  887. return retResponse;
  888. }
  889. else
  890. {
  891. projectContents.ProjectContentRecord.CalMonthId = calMonth.Id;
  892. projectContents.ProjectContentRecord.CalMonth = null;
  893. var staff = Context.Staffs.FirstOrDefault(s=>s.Name == User.Identity.Name);
  894. projectContents.ProjectContentRecord.StaffId = staff.Id;
  895. projectContents.ProjectContentRecord.State = 0;
  896. }
  897. var project = Context.ProjectInfos.FirstOrDefault(p => p.CaseNo == projectContents.ProjectContentRecord.ProjectNo && p.CaseState ==0);
  898. if (project != null)
  899. {
  900. var pRecord = Context.ProjectContentRecords.FirstOrDefault(p=>p.ProjectNo == projectContents.ProjectContentRecord.ProjectNo
  901. && p.StaffId == projectContents.ProjectContentRecord.StaffId
  902. && p.CalMonthId == projectContents.ProjectContentRecord.CalMonthId);
  903. if(pRecord != null)
  904. {
  905. retResponse.Success = false;
  906. retResponse.ErrorMessage = $"您已提交专案【{projectContents.ProjectContentRecord.ProjectNo}】{pRecord.CalMonth.Year}年{pRecord.CalMonth.Month}月的工作内容!";
  907. return retResponse;
  908. }
  909. Context.ProjectContentRecords.Add(projectContents.ProjectContentRecord);
  910. foreach (var doItem in projectContents.ProjectWorkContents)
  911. {
  912. doItem.ContentRecordId = projectContents.ProjectContentRecord.Id;
  913. Context.ProjectWorkContents.Add(doItem);
  914. }
  915. t.Commit();
  916. }
  917. else
  918. {
  919. retResponse.Success = false;
  920. retResponse.ErrorMessage = "专案不存在或专案已完成!";
  921. return retResponse;
  922. }
  923. }
  924. catch (Exception ex)
  925. {
  926. retResponse.Success = false;
  927. retResponse.ErrorMessage = ex.Message;
  928. t.Rollback();
  929. return retResponse;
  930. }
  931. }
  932. }
  933. return retResponse;
  934. }
  935. public PerformanceItem GetCaseInfo(string CaseNo)
  936. {
  937. var retObj = Context.PerformanceItems.OrderByDescending(p=>p.CalMonthId).FirstOrDefault<PerformanceItem>(p=>p.CaseNo == CaseNo.Trim());
  938. if(retObj == null)
  939. {
  940. retObj = new IPEasyController(Context).GetCaseInfo(CaseNo);
  941. }
  942. return retObj;
  943. }
  944. public PerformanceItem GetItemInfo(string CaseNo, string DoItem)
  945. {
  946. var retObj = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.CaseNo == CaseNo.Trim() && p.DoItem == DoItem.Trim());
  947. if (retObj == null)
  948. {
  949. retObj = new IPEasyController(Context).GetItemInfo(CaseNo,DoItem);
  950. }
  951. return retObj;
  952. }
  953. public PerformanceItem GetItemInfoByCaseStage(string CaseNo, string DoItem,string caseStage)
  954. {
  955. var retObj = Context.PerformanceItems.FirstOrDefault<PerformanceItem>(p => p.CaseNo == CaseNo.Trim()
  956. && p.DoItem == DoItem.Trim() && p.CaseStage == caseStage);
  957. if (retObj == null)
  958. {
  959. retObj = IPEasyUtility.GetPerformanceRecord(CaseNo, DoItem, caseStage);
  960. }
  961. return retObj;
  962. }
  963. public bool MovePerformance2ProjectInfo()
  964. {
  965. var response = Context.PerformanceItems.Include(p=>p.ItemStaffs)
  966. .Where<PerformanceItem>(p => p.CaseNo.StartsWith("S") && p.CalMonth.Status == 0);
  967. var pList = response.ToList<PerformanceItem>();
  968. foreach(var p in pList)
  969. {
  970. ProjectInfo project = new ProjectInfo();
  971. project.CaseNo = p.CaseNo;
  972. project.CaseName = p.CaseName;
  973. project.CaseState = 0;
  974. project.CaseType = p.CaseType;
  975. project.CustomerId = p.CustomerId;
  976. project.ReviewerId = p.ReviewerId;
  977. Context.ProjectInfos.Add(project);
  978. foreach(ItemStaff iStaff in p.ItemStaffs)
  979. {
  980. Context.ItemStaffs.Remove(iStaff);
  981. }
  982. Context.PerformanceItems.Remove(p);
  983. Context.SaveChanges();
  984. }
  985. return true;
  986. }
  987. }
  988. }