AppealController.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.EntityFrameworkCore;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text.Json.Serialization;
  13. using System.Text.Json;
  14. using System.Threading.Tasks;
  15. using wispro.sp.api.Job;
  16. using wispro.sp.entity;
  17. using wispro.sp.share;
  18. using Fizzler;
  19. using static wispro.sp.api.Controllers.PerformanceItemController;
  20. using System.Threading;
  21. using wispro.sp.api.Services;
  22. using Microsoft.Data.SqlClient;
  23. using System.Data;
  24. using wispro.sp.utility;
  25. using OpenQA.Selenium.DevTools.V85.CSS;
  26. namespace wispro.sp.api.Controllers
  27. {
  28. [Route("api/[controller]/[action]")]
  29. [ApiController]
  30. //[Authorize]
  31. public class AppealController : ControllerBase
  32. {
  33. spDbContext Context;
  34. IFileTaskService fileTaskService;
  35. public AppealController(spDbContext context, IFileTaskService _fileTaskService)
  36. {
  37. Context = context;
  38. fileTaskService = _fileTaskService;
  39. }
  40. public List<AppealType> GetAppealTypes()
  41. {
  42. return Context.AppealTypes.ToList();
  43. }
  44. /// <summary>
  45. /// 统计指定客户、指定月份新申请案件各案件系数的案件数量
  46. /// </summary>
  47. /// <param name="CustomerId">指定客户Id</param>
  48. /// <param name="CalmonthId">指定月份Id</param>
  49. /// <returns></returns>
  50. public CaseCoefficientStatistics GetCCSRecord(int? CustomerId,int CalmonthId)
  51. {
  52. CaseCoefficientStatistics retObject = new CaseCoefficientStatistics();
  53. if(CustomerId != null)
  54. {
  55. retObject.customer = Context.Customers.FirstOrDefault(c => c.Id == CustomerId);
  56. }
  57. var myGroups = Context.PerformanceItems
  58. .Where(p=>
  59. p.Type=="新申请"
  60. && (p.AgentFeedbackMemo != "已算绩效" || string.IsNullOrEmpty(p.AgentFeedbackMemo))
  61. && p.CalMonthId == CalmonthId
  62. && p.CustomerId == CustomerId)
  63. .GroupBy(p => p.CaseCoefficient)
  64. .Select(g => new { CaseCoefficient = g.Key, Count = g.Count() });
  65. if(CustomerId == null)
  66. {
  67. myGroups = Context.PerformanceItems
  68. .Where(p =>
  69. p.Type == "新申请"
  70. && (p.AgentFeedbackMemo != "已算绩效" || string.IsNullOrEmpty(p.AgentFeedbackMemo))
  71. && p.CalMonthId == CalmonthId)
  72. .GroupBy(p => p.CaseCoefficient)
  73. .Select(g => new { CaseCoefficient = g.Key, Count = g.Count() });
  74. }
  75. foreach (var g in myGroups)
  76. {
  77. string strKey = ( string.IsNullOrEmpty(g.CaseCoefficient)) ? "B" : g.CaseCoefficient;
  78. switch (strKey)
  79. {
  80. case "A":
  81. retObject.ACount += g.Count;
  82. retObject.Totals += g.Count;
  83. break;
  84. case "S":
  85. retObject.SCount += g.Count;
  86. retObject.Totals += g.Count;
  87. break;
  88. default:
  89. retObject.Totals += g.Count;
  90. break;
  91. }
  92. }
  93. return retObject;
  94. #region SQL
  95. //string strSQL = $"SELECT CaseCoefficient,count(*) as CaseCoefficientCount " +
  96. // "FROM PerformanceItem " +
  97. // $"where CalMonthId = {CalmonthId} and CustomerId = {CustomerId} and Type = '新申请' and(AgentFeedbackMemo <> '已算绩效' or AgentFeedbackMemo is null) " +
  98. // "group by CaseCoefficient";
  99. //var conn= Context.Database.GetDbConnection();
  100. //if(conn.State != System.Data.ConnectionState.Open)
  101. //{
  102. // conn.Open();
  103. //}
  104. //try
  105. //{
  106. // var cmd = conn.CreateCommand();
  107. // cmd.CommandText = strSQL;
  108. // var reader = cmd.ExecuteReader();
  109. // while (reader.Read())
  110. // {
  111. // string strKey = (reader.IsDBNull(0) || string.IsNullOrEmpty(reader.GetString(0))) ? "B" : reader.GetString(0);
  112. // switch (strKey)
  113. // {
  114. // case "A":
  115. // retObject.ACount += reader.GetInt32(1);
  116. // retObject.Totals += reader.GetInt32(1);
  117. // break;
  118. // case "S":
  119. // retObject.SCount += reader.GetInt32(1);
  120. // retObject.Totals += reader.GetInt32(1);
  121. // break;
  122. // default:
  123. // retObject.Totals += reader.GetInt32(1);
  124. // break;
  125. // }
  126. // }
  127. // cmd.Dispose();
  128. // return retObject;
  129. //}
  130. //catch (Exception ex)
  131. //{
  132. // throw ex;
  133. //}
  134. //finally
  135. //{
  136. // conn.Close();
  137. //}
  138. #endregion
  139. }
  140. public FileProcessTask GetStaticsReport(int CalMonthId)
  141. {
  142. CalMonth calMonth = Context.CalMonths.FirstOrDefault(c => c.Id == CalMonthId);
  143. if (calMonth != null)
  144. {
  145. var filename = $"{DateTime.Now.ToString("yyyyMMddhhmmss")}-{calMonth.Year}{calMonth.Month}客户案件系数统计.xlsx";
  146. var attachfileSavePath = utility.ConfigHelper.GetSectionValue("AttachFileSavePath");
  147. var filePath = Path.Combine(attachfileSavePath, filename);
  148. var fileTask = new FileProcessTask()
  149. {
  150. Id = Guid.NewGuid().ToString(),
  151. FileName = filename,
  152. FilePath = filePath,
  153. Processed = 0
  154. };
  155. fileTaskService.Add(fileTask);
  156. ExportDataResult result = new ExportDataResult()
  157. {
  158. fileTask = fileTask,
  159. calMonth = calMonth
  160. };
  161. ExportStaticReport(result);
  162. //System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(ExportStaticReport));
  163. //t.Start(result);
  164. return fileTask;
  165. }
  166. return null;
  167. }
  168. private void ExportStaticReport(object exportDataResult)
  169. {
  170. ExportDataResult result = (ExportDataResult)exportDataResult;
  171. FileProcessTask file = result.fileTask;
  172. CalMonth calMonth = result.calMonth;
  173. var retList = GetCCSRecords(calMonth.Id);
  174. DataTable dt = new DataTable();
  175. dt.Columns.Add("客户名称");
  176. dt.Columns.Add("S案件阈值");
  177. dt.Columns.Add("A案件阈值");
  178. dt.Columns.Add("案件总数",typeof(int));
  179. dt.Columns.Add("S案件数", typeof(int));
  180. dt.Columns.Add("A案件数", typeof(int));
  181. foreach(var ccsObj in retList)
  182. {
  183. var row = dt.NewRow();
  184. row["客户名称"] = ccsObj.customer.Name;
  185. row["S案件阈值"] =$"{((ccsObj.customer.SRate == null) ? 5: ccsObj.customer.SRate)}%" ;
  186. row["A案件阈值"] = $"{((ccsObj.customer.ARate == null) ? 30: ccsObj.customer.ARate)}%";
  187. row["案件总数"] = ccsObj.Totals;
  188. row["S案件数"] = ccsObj.SCount;
  189. row["A案件数"] = ccsObj.ACount;
  190. dt.Rows.Add(row);
  191. }
  192. NPOIExcel.DataTableToExcel(dt, result.fileTask.FilePath);
  193. result.fileTask.Finished = true;
  194. }
  195. public List<CaseCoefficientStatistics> GetCCSRecords(int CalmonthId)
  196. {
  197. var AllCustomers = Context.Customers.ToList();
  198. var myGroups = Context.PerformanceItems
  199. .Where(p =>
  200. p.Type == "新申请"
  201. && (p.AgentFeedbackMemo != "已算绩效" || string.IsNullOrEmpty(p.AgentFeedbackMemo))
  202. && p.CalMonthId == CalmonthId)
  203. .GroupBy(p => new { CustomerId = p.CustomerId, CaseCeoffcient = p.CaseCoefficient })
  204. .Select(g => new { CaseCoefficient = g.Key.CaseCeoffcient,CustomerId=g.Key.CustomerId, Count = g.Count() });
  205. List<CaseCoefficientStatistics> retList= new List<CaseCoefficientStatistics>();
  206. foreach (var g in myGroups)
  207. {
  208. CaseCoefficientStatistics temObject = retList.Where(p=>p.customer.Id == g.CustomerId).FirstOrDefault();
  209. if(temObject == null)
  210. {
  211. temObject = new CaseCoefficientStatistics() {
  212. customer = AllCustomers.FirstOrDefault(c => c.Id == g.CustomerId)
  213. };
  214. retList.Add(temObject);
  215. }
  216. string strKey = g.CaseCoefficient;
  217. switch (strKey)
  218. {
  219. case "A":
  220. temObject.ACount += g.Count;
  221. temObject.Totals += g.Count;
  222. break;
  223. case "S":
  224. temObject.SCount += g.Count;
  225. temObject.Totals += g.Count;
  226. break;
  227. default:
  228. temObject.Totals += g.Count;
  229. break;
  230. }
  231. }
  232. return retList;
  233. }
  234. public List<InputField> GetInputField(int appealTypeId, int state)
  235. {
  236. var retList = Context.InputFields
  237. .Where<InputField>(ip => ip.AppealTypeId == appealTypeId && ip.AppealState == state)
  238. .Include(p => p.SelectValues)
  239. .ToList();
  240. string str = System.Text.Json.JsonSerializer.Serialize(retList, new JsonSerializerOptions()
  241. {
  242. ReferenceHandler = ReferenceHandler.Preserve
  243. });
  244. Log(str);
  245. foreach (var inputField in retList)
  246. {
  247. if (inputField.SelectValues != null)
  248. {
  249. foreach(var ifValue in inputField.SelectValues)
  250. {
  251. ifValue.InputField = null;
  252. }
  253. }
  254. }
  255. return retList;
  256. }
  257. /// <summary>
  258. /// 创建申诉流程
  259. /// </summary>
  260. /// <param name="ItemId"></param>
  261. /// <param name="typeid"></param>
  262. /// <param name="reviewerId"></param>
  263. /// <param name="appealObject"></param>
  264. /// <returns></returns>
  265. public ApiSaveResponse CreateAppeal(int ItemId, int typeid, int reviewerId, AppealObject appealObject)
  266. {
  267. ApiSaveResponse response = new ApiSaveResponse();
  268. response.Success = true;
  269. AppealRecord appealRecord = new AppealRecord();
  270. if(ItemId > 0)
  271. {
  272. appealRecord.ItemId = ItemId;
  273. }
  274. else
  275. {
  276. appealRecord.ItemId = null;
  277. }
  278. appealRecord.TypeId = typeid;
  279. appealRecord.ReviewerId = reviewerId;
  280. appealRecord.CreaterId = Context.Staffs.Where<Staff>(s => s.Name == User.Identity.Name).FirstOrDefault().Id;
  281. appealRecord.CreateTime = DateTime.Now;
  282. var t = Context.Database.BeginTransaction();
  283. try
  284. {
  285. Context.AppealRecords.Add(appealRecord);
  286. Context.SaveChanges();
  287. foreach (var fieldValue in appealObject.inputFieldValues)
  288. {
  289. fieldValue.InputField = null;
  290. fieldValue.AppealRecordId = appealRecord.Id;
  291. }
  292. Context.InputFieldValues.AddRange(appealObject.inputFieldValues);
  293. foreach (var file in appealObject.attachFiles)
  294. {
  295. var temFile = Context.AttachFiles.Where<AttachFile>(f => f.Id == file.Id).FirstOrDefault();
  296. temFile.AppealRecordId = appealRecord.Id;
  297. temFile.UploadUserId = appealRecord.CreaterId;
  298. }
  299. Context.SaveChanges();
  300. var temType = Context.AppealTypes.FirstOrDefault(c=>c.Id == appealRecord.TypeId);
  301. if (!string.IsNullOrEmpty(temType.ApplealObject))
  302. {
  303. IDoAppealObject doAppeal = (IDoAppealObject)Activator.CreateInstance(Type.GetType(appealRecord.Type.ApplealObject));
  304. doAppeal.DoAppeal(appealObject, appealRecord.Id, Context);
  305. }
  306. t.Commit();
  307. var Reviewer = Context.Staffs.Where<Staff>(s => s.Id == appealRecord.ReviewerId).FirstOrDefault();
  308. if(appealRecord.Type == null)
  309. {
  310. appealRecord.Type = Context.AppealTypes.FirstOrDefault(p=>p.Id == appealRecord.TypeId);
  311. }
  312. string strSubject = appealRecord.Type.Name;
  313. string strBody = $"<div style=\"position:absolute;width:800;height:300;margin-top:50px;margin-left:200px;margin-right:200px;box-shadow:0px 0px 3px 3px #ccc \"><div style= \"margin-top: 20px; margin-left:20px;;font-size:14px; \">{Reviewer.Name},您好!</div><div style= \"margin-top: 25px; margin-left:20px;font-size:14px; \"><div>{User.Identity.Name} 向您提交了{appealRecord.Type.Name}申诉,请使用<a href= \"http://1.116.113.26\">绩效系统</a>在<B>{utility.ConfigHelper.GetSectionValue("Lastest_feedback_date")}日</B>前完成审核!</div><div style= \"margin-top: 100px;margin-right:15px; padding-bottom: 20px; font-size:14px;color:#888888;float:right; \">小美集团绩效管理系统</div></div>";
  314. string strTo = Reviewer.Mail;
  315. if (!string.IsNullOrEmpty(strTo))
  316. {
  317. _ = QuartzUtil.AddMailJob(strSubject, strBody, Reviewer.Name, strTo);
  318. }
  319. strBody = $"<div style=\"position:absolute;width:800;height:300;margin-top:50px;margin-left:200px;margin-right:200px;box-shadow:0px 0px 3px 3px #ccc \"><div style= \"margin-top: 20px; margin-left:20px;;font-size:14px; \">{Reviewer.Name},您好!</div><div style= \"margin-top: 25px; margin-left:20px;font-size:14px; \"><div>{User.Identity.Name} 向您提交了{appealRecord.Type.Name}申诉,请使用<a href= \"http://1.116.113.26\">绩效系统</a>查看详情!</div><div style= \"margin-top: 100px;margin-right:15px; padding-bottom: 20px; font-size:14px;color:#888888;float:right; \">小美集团绩效管理系统</div></div>";
  320. _ = QuartzUtil.AddMailJob(strSubject, strBody, "钟子敏", "zhongzimin@china-wispro.com");
  321. //_ = QuartzUtil.AddMailJob(strSubject, strBody, "夏敏", "xiamin@china-wispro.com");
  322. _ = QuartzUtil.AddMailJob(strSubject, strBody, "吴芳", "wufang@china-wispro.com");
  323. return response;
  324. }
  325. catch (Exception ex)
  326. {
  327. t.Rollback();
  328. response.Success = false;
  329. response.ErrorMessage = "申诉时发生错误!";
  330. return response;
  331. }
  332. }
  333. public ApiSaveResponse ReviewerAppeal(int appealRecordId,AppealObject appealObject)
  334. {
  335. ApiSaveResponse response = new ApiSaveResponse();
  336. response.Success = true;
  337. var appealRecord = Context.AppealRecords.Where<AppealRecord>(p => p.Id == appealRecordId).FirstOrDefault();
  338. if(appealRecord != null)
  339. {
  340. if (appealRecord.ItemId.HasValue)
  341. {
  342. var item = Context.PerformanceItems.Include(p=>p.CalMonth).FirstOrDefault(p => p.Id == appealRecord.ItemId.Value);
  343. if(item == null)
  344. {
  345. response.Success = false;
  346. response.ErrorMessage = "无效的ItemId!";
  347. return response;
  348. }
  349. else
  350. {
  351. if(item.CalMonth.Status != 0)
  352. {
  353. response.Success = false;
  354. response.ErrorMessage = "指定的绩效已经归档!";
  355. return response;
  356. }
  357. }
  358. }
  359. var t = Context.Database.BeginTransaction();
  360. try
  361. {
  362. appealRecord.ReviewerId = Context.Staffs.Where<Staff>(s => s.Name == User.Identity.Name).FirstOrDefault().Id;
  363. appealRecord.State = 1;
  364. appealRecord.ReviewTime = DateTime.Now;
  365. Context.SaveChanges();
  366. foreach (var fieldValue in appealObject.inputFieldValues)
  367. {
  368. fieldValue.InputField = null;
  369. fieldValue.AppealRecordId = appealRecord.Id;
  370. }
  371. Context.InputFieldValues.AddRange(appealObject.inputFieldValues);
  372. Context.SaveChanges();
  373. if (appealObject.attachFiles != null)
  374. {
  375. foreach (var file in appealObject.attachFiles)
  376. {
  377. var temFile = Context.AttachFiles.Where<AttachFile>(f => f.Id == file.Id).FirstOrDefault();
  378. temFile.AppealRecordId = appealRecord.Id;
  379. temFile.UploadUserId = appealRecord.ReviewerId;
  380. }
  381. }
  382. List<InputFieldValue> inputFieldValues = Context.InputFieldValues
  383. .Where<InputFieldValue>(p => p.AppealRecordId == appealRecordId
  384. && p.InputField.MapObjectField != null)
  385. .Include(i=>i.InputField)
  386. .ToList();
  387. foreach(InputFieldValue inputFieldValue in inputFieldValues)
  388. {
  389. SaveValueToMapObject(appealRecord, inputFieldValue);
  390. }
  391. Context.SaveChanges();
  392. if(appealRecord.Type == null)
  393. {
  394. appealRecord.Type = Context.AppealTypes.FirstOrDefault(s=>s.Id == appealRecord.TypeId);
  395. }
  396. if (!string.IsNullOrEmpty(appealRecord.Type.ReviewObject))
  397. {
  398. IDoAppealObject doAppeal = (IDoAppealObject)Activator.CreateInstance(Type.GetType(appealRecord.Type.ReviewObject));
  399. doAppeal.DoAppeal(appealObject, appealRecord.Id,Context);
  400. }
  401. Context.SaveChanges();
  402. if(appealRecord.ItemId.HasValue)
  403. {
  404. var item = Context.PerformanceItems
  405. .Include(p => p.ItemStaffs)
  406. .Include(p => p.Customer)
  407. .Include(p=>p.Reviewer)
  408. .FirstOrDefault(p=>p.Id == appealRecord.ItemId);
  409. new PerformanceItemController(Context, new Services.FileTaskCacheService()).RefreshPoint(item);
  410. }
  411. t.Commit();
  412. return response;
  413. }
  414. catch (Exception ex)
  415. {
  416. t.Rollback();
  417. response.Success = false;
  418. response.ErrorMessage = ex.Message;
  419. return response;
  420. }
  421. }
  422. else
  423. {
  424. response.Success = true;
  425. response.ErrorMessage = "申诉不存在!";
  426. return response;
  427. }
  428. }
  429. private object ConvertSimpleType(object value, Type destinationType)
  430. {
  431. object returnValue;
  432. if ((value == null) || destinationType.IsInstanceOfType(value))
  433. {
  434. return value;
  435. }
  436. string str = value as string;
  437. if ((str != null) && (str.Length == 0))
  438. {
  439. return null;
  440. }
  441. TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
  442. bool flag = converter.CanConvertFrom(value.GetType());
  443. if (!flag)
  444. {
  445. converter = TypeDescriptor.GetConverter(value.GetType());
  446. }
  447. if (!flag && !converter.CanConvertTo(destinationType))
  448. {
  449. throw new InvalidOperationException("无法转换成类型:" + value.ToString() + "==>" + destinationType);
  450. }
  451. try
  452. {
  453. returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType);
  454. }
  455. catch (Exception e)
  456. {
  457. throw new InvalidOperationException("类型转换出错:" + value.ToString() + "==>" + destinationType, e);
  458. }
  459. return returnValue;
  460. }
  461. private void SaveValueToMapObject(AppealRecord appealRecord, InputFieldValue inputFieldValue)
  462. {
  463. if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapObjectField))
  464. {
  465. bool isSave = false;
  466. if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapSaveCondition))
  467. {
  468. string[] conditions = inputFieldValue.InputField.MapSaveCondition.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  469. if (conditions.Length == 1)
  470. {
  471. if (appealRecord.State == int.Parse(conditions[0]))
  472. {
  473. isSave = true;
  474. }
  475. }
  476. else
  477. {
  478. if (appealRecord.State == int.Parse(conditions[0]))
  479. {
  480. InputFieldValue temValue =
  481. Context.InputFieldValues.Where<InputFieldValue>(v =>
  482. v.AppealRecordId == appealRecord.Id &&
  483. v.InputField.Id == int.Parse(conditions[1]))
  484. .FirstOrDefault();
  485. if (temValue != null)
  486. {
  487. if(temValue.Value == conditions[2])
  488. {
  489. isSave = true;
  490. }
  491. else
  492. {
  493. if (string.IsNullOrEmpty(temValue.Value))
  494. {
  495. throw (new ApplicationException(string.Format("请选择或输入栏位【{0}】的值!",temValue.InputField.FieldName)));
  496. }
  497. else
  498. {
  499. isSave = false;
  500. }
  501. }
  502. }
  503. }
  504. }
  505. }
  506. else
  507. {
  508. isSave = true;
  509. }
  510. if (isSave)
  511. {
  512. InputField field = inputFieldValue.InputField;
  513. PerformanceItem Item = Context.PerformanceItems.Where<PerformanceItem>(p =>
  514. p.Id == appealRecord.ItemId)
  515. .Include(p => p.ItemStaffs).ThenInclude(it=>it.DoPerson)
  516. .Include(p => p.Reviewer)
  517. .FirstOrDefault();
  518. bool ItemIsNull = false;
  519. if(Item == null)
  520. {
  521. ItemIsNull = true;
  522. Item = new PerformanceItem();
  523. }
  524. if (!string.IsNullOrEmpty(field.MapObjectField) && !string.IsNullOrEmpty(field.MapObjectFieldLabel))
  525. {
  526. List<InputFieldValue> temValues = new List<InputFieldValue>();
  527. string[] pList = field.MapObjectField.Split(new char[] { '.' });
  528. string[] plList = field.MapObjectFieldLabel.Split(new char[] { '.' });
  529. var pInfo = Item.GetType().GetProperty(pList[0]);
  530. var o = pInfo.GetValue(Item);
  531. if (Array.IndexOf(o.GetType().GetInterfaces(), typeof(IEnumerable)) > -1)
  532. {
  533. List<string> Values = new List<string>();
  534. List<string> Labels = new List<string>();
  535. var objList = o as IEnumerable;
  536. foreach (var obj in objList)
  537. {
  538. var objLabel = obj;
  539. for (int i = 1; i < plList.Length; i++)
  540. {
  541. objLabel = objLabel.GetType().GetProperty(plList[i]).GetValue(objLabel);
  542. }
  543. if (objLabel.ToString() == inputFieldValue.Label)
  544. {
  545. var objValue = obj;
  546. for (int i = 1; i < pList.Length - 1; i++)
  547. {
  548. objValue = objValue.GetType().GetProperty(pList[i]).GetValue(objValue);
  549. }
  550. objValue.GetType().GetProperty(pList[pList.Length - 1])
  551. .SetValue(objValue, ConvertSimpleType(inputFieldValue.Value,Type.GetType(inputFieldValue.InputField.FieldType)));
  552. }
  553. }
  554. }
  555. }
  556. else
  557. {
  558. Item.GetType().GetProperty(field.MapObjectField).SetValue(Item, ConvertSimpleType(inputFieldValue.Value, Type.GetType(inputFieldValue.InputField.FieldType)));
  559. }
  560. }
  561. }
  562. }
  563. public List<AppealRecord> GetAppealRecords(int userId)
  564. {
  565. try
  566. {
  567. var data = Context.AppealRecords.Where<AppealRecord>(p => p.Id > 0);
  568. if (userId > 0)
  569. {
  570. data = Context.AppealRecords.Where<AppealRecord>(ar => (ar.CreaterId == userId || (ar.ReviewerId == userId && ar.State != 1)) && ar.Type.NeedReview == true);
  571. }
  572. var retList = data
  573. .Include(p => p.Reviewer)
  574. .Include(p => p.Creater)
  575. .Include(p => p.Item)
  576. .Include(p => p.Type)
  577. .OrderBy(p => p.State)
  578. .ToList();
  579. foreach (var record in retList)
  580. {
  581. replaceDataForSerialize(record);
  582. }
  583. //string str = System.Text.Json.JsonSerializer.Serialize(retList);
  584. //System.Diagnostics.Debug.WriteLine(str);
  585. //Log(str);
  586. return retList;
  587. }
  588. catch (Exception ex)
  589. {
  590. Log(ex.ToString());
  591. return new List<AppealRecord>();
  592. }
  593. }
  594. private void replaceDataForSerialize(AppealRecord record)
  595. {
  596. if(record.Creater !=null)
  597. {
  598. Staff temStaff = new Staff()
  599. {
  600. Name = record.Creater.Name,
  601. Id = record.Creater.Id,
  602. ItemStaffs = null,
  603. ReviewerItems = null,
  604. Customers = null,
  605. Positions = null,
  606. StaffGrade = null,
  607. };
  608. record.Creater = temStaff;
  609. //record.Creater.ItemStaffs = null;
  610. //record.Creater.ReviewerItems = null;
  611. //record.Creater.Customers = null;
  612. //record.Creater.Positions = null;
  613. //record.Creater.StaffGrade = null;
  614. }
  615. if (record.Reviewer != null)
  616. {
  617. Staff temStaff = new Staff() {
  618. Name = record.Reviewer.Name,
  619. Id = record.Creater.Id,
  620. ItemStaffs = null,
  621. ReviewerItems = null,
  622. Customers = null,
  623. Positions = null,
  624. StaffGrade = null,
  625. };
  626. record.Reviewer = temStaff;
  627. //record.Reviewer.ItemStaffs = null;
  628. //record.Reviewer.Customers = null;
  629. //record.Reviewer.ReviewerItems = null;
  630. //record.Reviewer.Positions = null;
  631. //record.Reviewer.StaffGrade = null;
  632. }
  633. if (record.Item != null)
  634. {
  635. PerformanceItem temItem = new PerformanceItem()
  636. {
  637. Id = record.Item.Id,
  638. CaseNo = record.Item.CaseNo,
  639. DoItem = record.Item.DoItem,
  640. CaseName = record.Item.CaseName,
  641. CalMonthId = record.Item.CalMonthId
  642. };
  643. record.Item = temItem;
  644. //record.Item.ItemStaffs = null;
  645. //record.Item.PreOastaff = null;
  646. //record.Item.Reviewer = null;
  647. //record.Item.WorkflowUser = null;
  648. //record.Item.Customer = null;
  649. //record.Item.CalMonth = null;
  650. }
  651. if(record.Type != null)
  652. {
  653. record.Type.ReviewerExpress = null;
  654. record.Type.CanDoExpress = null;
  655. record.Type.ApplealObject = null;
  656. record.Type.CanDoExpress = null;
  657. record.Type.ReviewerExpress = null;
  658. }
  659. //record.Type.ReviewerExpress = null;
  660. }
  661. [HttpPost]
  662. public List<AppealRecord> QueryByFilter(AppealRecordFilter filter)
  663. {
  664. var data = Context.AppealRecords.Where<AppealRecord>(s => true);
  665. if(filter.AppealTypeId > 0)
  666. {
  667. data = Context.AppealRecords.Where(f=>f.TypeId == filter.AppealTypeId);
  668. }
  669. if(!string.IsNullOrEmpty(filter.CaseNo))
  670. {
  671. data = data.Where(s=>s.Item.CaseNo.Contains(filter.CaseNo));
  672. }
  673. if (!string.IsNullOrEmpty(filter.CreateUser))
  674. {
  675. data = data.Where(s => s.Creater.Name.Contains(filter.CreateUser));
  676. }
  677. if (!string.IsNullOrEmpty(filter.Reviewer))
  678. {
  679. data = data.Where(s => s.Reviewer.Name.Contains(filter.Reviewer));
  680. }
  681. if (filter.beginCreateTime.HasValue)
  682. {
  683. data = data.Where(s => s.CreateTime>=filter.beginCreateTime.Value );
  684. }
  685. if (filter.endCreateTime.HasValue)
  686. {
  687. data = data.Where(s => s.CreateTime <= filter.endCreateTime.Value);
  688. }
  689. if (filter.beginReviewTime.HasValue)
  690. {
  691. data = data.Where(s => s.ReviewTime >= filter.beginReviewTime.Value);
  692. }
  693. if (filter.endReviewTime.HasValue)
  694. {
  695. data = data.Where(s => s.ReviewTime <= filter.endReviewTime.Value);
  696. }
  697. var retList = data
  698. .Include(p => p.Reviewer)
  699. .Include(p => p.Creater)
  700. .Include(p => p.Item)
  701. .Include(p => p.Type)
  702. .OrderBy(p => p.State)
  703. .ToList();
  704. foreach (var record in retList)
  705. {
  706. replaceDataForSerialize(record);
  707. }
  708. return retList;
  709. }
  710. public AppealRecord GetAppealRecord(int Id)
  711. {
  712. var data = Context.AppealRecords.Where<AppealRecord>(ar => ar.Id == Id);
  713. return data.Include(p => p.Reviewer)
  714. .Include(p => p.Creater)
  715. .Include(p => p.Item)
  716. .Include(p => p.Type).FirstOrDefault();
  717. }
  718. public List<InputFieldValue> GetInputFieldValues(int id, int state)
  719. {
  720. var result = Context.InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == id && f.InputField.AppealState == state);
  721. return result.Include(i => i.InputField).ToList();
  722. }
  723. public List<InputFieldValue> GetAllInputFieldValue(int id)
  724. {
  725. var result = Context.InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == id);
  726. return result.Include(i => i.InputField).ToList();
  727. }
  728. public List<AttachFile> GetAppealRecordAttachFiles(int appealRecordId)
  729. {
  730. var result = Context.AttachFiles.Where<AttachFile>(at => at.AppealRecordId == appealRecordId).Include(f => f.UploadUser);
  731. return result.ToList();
  732. }
  733. public ApiSaveResponse ChangeRecordReviewer(int RecordId,int ReviewerId)
  734. {
  735. var data = Context.AppealRecords.FirstOrDefault<AppealRecord>(ar => ar.Id == RecordId);
  736. if(data != null)
  737. {
  738. var reviewer = Context.Staffs.FirstOrDefault(s => s.Id == ReviewerId);
  739. var CurrentUser = Context.Staffs.FirstOrDefault(s=>s.Name == User.Identity.Name);
  740. if ((CurrentUser.Id == data.CreaterId || CurrentUser.Id == data.ReviewerId) && data.State ==0)
  741. {
  742. if (reviewer != null)
  743. {
  744. data.ReviewerId = ReviewerId;
  745. Context.SaveChanges();
  746. return new ApiSaveResponse()
  747. {
  748. Success = true
  749. };
  750. }
  751. else
  752. {
  753. return new ApiSaveResponse()
  754. {
  755. Success = false,
  756. ErrorMessage = "指定的审核人不存在"
  757. };
  758. }
  759. }
  760. else
  761. {
  762. return new ApiSaveResponse()
  763. {
  764. Success = false,
  765. ErrorMessage = "只有申诉人和审核人在未审核状态时才能变更审核人"
  766. };
  767. }
  768. }
  769. else
  770. {
  771. return new ApiSaveResponse()
  772. {
  773. Success = false,
  774. ErrorMessage = "指定的申诉记录不存在"
  775. };
  776. }
  777. }
  778. private void Log(string strMessage)
  779. {
  780. StreamWriter sw = System.IO.File.AppendText("c:\\temp\\log.txt");
  781. sw.WriteLine($"{strMessage}");
  782. sw.Flush();
  783. sw.Close();
  784. sw.Dispose();
  785. }
  786. }
  787. }