AppealController.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. {
  213. customer = AllCustomers.FirstOrDefault(c => c.Id == g.CustomerId)
  214. };
  215. retList.Add(temObject);
  216. }
  217. string strKey = g.CaseCoefficient;
  218. switch (strKey)
  219. {
  220. case "A":
  221. temObject.ACount += g.Count;
  222. temObject.Totals += g.Count;
  223. break;
  224. case "S":
  225. temObject.SCount += g.Count;
  226. temObject.Totals += g.Count;
  227. break;
  228. default:
  229. temObject.Totals += g.Count;
  230. break;
  231. }
  232. }
  233. return retList;
  234. }
  235. public List<InputField> GetInputField(int appealTypeId, int state)
  236. {
  237. var retList = Context.InputFields
  238. .Where<InputField>(ip => ip.AppealTypeId == appealTypeId && ip.AppealState == state)
  239. .Include(p => p.SelectValues)
  240. .ToList();
  241. string str = System.Text.Json.JsonSerializer.Serialize(retList, new JsonSerializerOptions()
  242. {
  243. ReferenceHandler = ReferenceHandler.Preserve
  244. });
  245. Log(str);
  246. foreach (var inputField in retList)
  247. {
  248. if (inputField.SelectValues != null)
  249. {
  250. foreach (var ifValue in inputField.SelectValues)
  251. {
  252. ifValue.InputField = null;
  253. }
  254. }
  255. }
  256. return retList;
  257. }
  258. /// <summary>
  259. /// 创建申诉流程
  260. /// </summary>
  261. /// <param name="ItemId"></param>
  262. /// <param name="typeid"></param>
  263. /// <param name="reviewerId"></param>
  264. /// <param name="appealObject"></param>
  265. /// <returns></returns>
  266. public ApiSaveResponse CreateAppeal(int ItemId, int typeid, int reviewerId, AppealObject appealObject)
  267. {
  268. ApiSaveResponse response = new ApiSaveResponse();
  269. response.Success = true;
  270. AppealRecord appealRecord = new AppealRecord();
  271. if (ItemId > 0)
  272. {
  273. appealRecord.ItemId = ItemId;
  274. }
  275. else
  276. {
  277. appealRecord.ItemId = null;
  278. }
  279. appealRecord.TypeId = typeid;
  280. appealRecord.ReviewerId = reviewerId;
  281. appealRecord.CreaterId = Context.Staffs.Where<Staff>(s => s.Name == User.Identity.Name).FirstOrDefault().Id;
  282. appealRecord.CreateTime = DateTime.Now;
  283. var t = Context.Database.BeginTransaction();
  284. try
  285. {
  286. Context.AppealRecords.Add(appealRecord);
  287. Context.SaveChanges();
  288. foreach (var fieldValue in appealObject.inputFieldValues)
  289. {
  290. fieldValue.InputField = null;
  291. fieldValue.AppealRecordId = appealRecord.Id;
  292. }
  293. Context.InputFieldValues.AddRange(appealObject.inputFieldValues);
  294. foreach (var file in appealObject.attachFiles)
  295. {
  296. var temFile = Context.AttachFiles.Where<AttachFile>(f => f.Id == file.Id).FirstOrDefault();
  297. temFile.AppealRecordId = appealRecord.Id;
  298. temFile.UploadUserId = appealRecord.CreaterId;
  299. }
  300. Context.SaveChanges();
  301. var temType = Context.AppealTypes.FirstOrDefault(c => c.Id == appealRecord.TypeId);
  302. if (!string.IsNullOrEmpty(temType.ApplealObject))
  303. {
  304. IDoAppealObject doAppeal = (IDoAppealObject)Activator.CreateInstance(Type.GetType(appealRecord.Type.ApplealObject));
  305. doAppeal.DoAppeal(appealObject, appealRecord.Id, Context);
  306. }
  307. t.Commit();
  308. var Reviewer = Context.Staffs.Where<Staff>(s => s.Id == appealRecord.ReviewerId).FirstOrDefault();
  309. if (appealRecord.Type == null)
  310. {
  311. appealRecord.Type = Context.AppealTypes.FirstOrDefault(p => p.Id == appealRecord.TypeId);
  312. }
  313. string strSubject = appealRecord.Type.Name;
  314. 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>";
  315. string strTo = Reviewer.Mail;
  316. if (!string.IsNullOrEmpty(strTo))
  317. {
  318. _ = QuartzUtil.AddMailJob(strSubject, strBody, Reviewer.Name, strTo);
  319. }
  320. 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>";
  321. _ = QuartzUtil.AddMailJob(strSubject, strBody, "钟子敏", "zhongzimin@china-wispro.com");
  322. //_ = QuartzUtil.AddMailJob(strSubject, strBody, "夏敏", "xiamin@china-wispro.com");
  323. _ = QuartzUtil.AddMailJob(strSubject, strBody, "吴芳", "wufang@china-wispro.com");
  324. return response;
  325. }
  326. catch (Exception ex)
  327. {
  328. t.Rollback();
  329. response.Success = false;
  330. response.ErrorMessage = "申诉时发生错误!";
  331. return response;
  332. }
  333. }
  334. public ApiSaveResponse ReviewerAppeal(int appealRecordId, AppealObject appealObject)
  335. {
  336. ApiSaveResponse response = new ApiSaveResponse();
  337. response.Success = true;
  338. var appealRecord = Context.AppealRecords.Where<AppealRecord>(p => p.Id == appealRecordId).FirstOrDefault();
  339. if (appealRecord != null)
  340. {
  341. if (appealRecord.ItemId.HasValue)
  342. {
  343. var item = Context.PerformanceItems.Include(p => p.CalMonth).FirstOrDefault(p => p.Id == appealRecord.ItemId.Value);
  344. if (item == null)
  345. {
  346. response.Success = false;
  347. response.ErrorMessage = "无效的ItemId!";
  348. return response;
  349. }
  350. else
  351. {
  352. if (item.CalMonth.Status != 0)
  353. {
  354. response.Success = false;
  355. response.ErrorMessage = "指定的绩效已经归档!";
  356. return response;
  357. }
  358. }
  359. }
  360. var t = Context.Database.BeginTransaction();
  361. try
  362. {
  363. appealRecord.ReviewerId = Context.Staffs.Where<Staff>(s => s.Name == User.Identity.Name).FirstOrDefault().Id;
  364. appealRecord.State = 1;
  365. appealRecord.ReviewTime = DateTime.Now;
  366. Context.SaveChanges();
  367. foreach (var fieldValue in appealObject.inputFieldValues)
  368. {
  369. fieldValue.InputField = null;
  370. fieldValue.AppealRecordId = appealRecord.Id;
  371. }
  372. Context.InputFieldValues.AddRange(appealObject.inputFieldValues);
  373. Context.SaveChanges();
  374. if (appealObject.attachFiles != null)
  375. {
  376. foreach (var file in appealObject.attachFiles)
  377. {
  378. var temFile = Context.AttachFiles.Where<AttachFile>(f => f.Id == file.Id).FirstOrDefault();
  379. temFile.AppealRecordId = appealRecord.Id;
  380. temFile.UploadUserId = appealRecord.ReviewerId;
  381. }
  382. }
  383. List<InputFieldValue> inputFieldValues = Context.InputFieldValues
  384. .Where<InputFieldValue>(p => p.AppealRecordId == appealRecordId
  385. && p.InputField.MapObjectField != null)
  386. .Include(i => i.InputField)
  387. .ToList();
  388. foreach (InputFieldValue inputFieldValue in inputFieldValues)
  389. {
  390. SaveValueToMapObject(appealRecord, inputFieldValue);
  391. }
  392. Context.SaveChanges();
  393. if (appealRecord.Type == null)
  394. {
  395. appealRecord.Type = Context.AppealTypes.FirstOrDefault(s => s.Id == appealRecord.TypeId);
  396. }
  397. if (!string.IsNullOrEmpty(appealRecord.Type.ReviewObject))
  398. {
  399. IDoAppealObject doAppeal = (IDoAppealObject)Activator.CreateInstance(Type.GetType(appealRecord.Type.ReviewObject));
  400. doAppeal.DoAppeal(appealObject, appealRecord.Id, Context);
  401. }
  402. Context.SaveChanges();
  403. if (appealRecord.ItemId.HasValue)
  404. {
  405. var item = Context.PerformanceItems
  406. .Include(p => p.ItemStaffs)
  407. .Include(p => p.Customer)
  408. .Include(p => p.Reviewer)
  409. .FirstOrDefault(p => p.Id == appealRecord.ItemId);
  410. new PerformanceItemController(Context, new Services.FileTaskCacheService()).RefreshPoint(item);
  411. }
  412. t.Commit();
  413. return response;
  414. }
  415. catch (Exception ex)
  416. {
  417. t.Rollback();
  418. response.Success = false;
  419. response.ErrorMessage = ex.Message;
  420. return response;
  421. }
  422. }
  423. else
  424. {
  425. response.Success = true;
  426. response.ErrorMessage = "申诉不存在!";
  427. return response;
  428. }
  429. }
  430. private object ConvertSimpleType(object value, Type destinationType)
  431. {
  432. object returnValue;
  433. if ((value == null) || destinationType.IsInstanceOfType(value))
  434. {
  435. return value;
  436. }
  437. string str = value as string;
  438. if ((str != null) && (str.Length == 0))
  439. {
  440. return null;
  441. }
  442. TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
  443. bool flag = converter.CanConvertFrom(value.GetType());
  444. if (!flag)
  445. {
  446. converter = TypeDescriptor.GetConverter(value.GetType());
  447. }
  448. if (!flag && !converter.CanConvertTo(destinationType))
  449. {
  450. throw new InvalidOperationException("无法转换成类型:" + value.ToString() + "==>" + destinationType);
  451. }
  452. try
  453. {
  454. returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType);
  455. }
  456. catch (Exception e)
  457. {
  458. throw new InvalidOperationException("类型转换出错:" + value.ToString() + "==>" + destinationType, e);
  459. }
  460. return returnValue;
  461. }
  462. private void SaveValueToMapObject(AppealRecord appealRecord, InputFieldValue inputFieldValue)
  463. {
  464. if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapObjectField))
  465. {
  466. bool isSave = false;
  467. if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapSaveCondition))
  468. {
  469. string[] conditions = inputFieldValue.InputField.MapSaveCondition.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  470. if (conditions.Length == 1)
  471. {
  472. if (appealRecord.State == int.Parse(conditions[0]))
  473. {
  474. isSave = true;
  475. }
  476. }
  477. else
  478. {
  479. if (appealRecord.State == int.Parse(conditions[0]))
  480. {
  481. InputFieldValue temValue =
  482. Context.InputFieldValues.Where<InputFieldValue>(v =>
  483. v.AppealRecordId == appealRecord.Id &&
  484. v.InputField.Id == int.Parse(conditions[1]))
  485. .FirstOrDefault();
  486. if (temValue != null)
  487. {
  488. if (temValue.Value == conditions[2])
  489. {
  490. isSave = true;
  491. }
  492. else
  493. {
  494. if (string.IsNullOrEmpty(temValue.Value))
  495. {
  496. throw (new ApplicationException(string.Format("请选择或输入栏位【{0}】的值!", temValue.InputField.FieldName)));
  497. }
  498. else
  499. {
  500. isSave = false;
  501. }
  502. }
  503. }
  504. }
  505. }
  506. }
  507. else
  508. {
  509. isSave = true;
  510. }
  511. if (isSave)
  512. {
  513. InputField field = inputFieldValue.InputField;
  514. PerformanceItem Item = Context.PerformanceItems.Where<PerformanceItem>(p =>
  515. p.Id == appealRecord.ItemId)
  516. .Include(p => p.ItemStaffs).ThenInclude(it => it.DoPerson)
  517. .Include(p => p.Reviewer)
  518. .FirstOrDefault();
  519. bool ItemIsNull = false;
  520. if (Item == null)
  521. {
  522. ItemIsNull = true;
  523. Item = new PerformanceItem();
  524. }
  525. if (!string.IsNullOrEmpty(field.MapObjectField) && !string.IsNullOrEmpty(field.MapObjectFieldLabel))
  526. {
  527. List<InputFieldValue> temValues = new List<InputFieldValue>();
  528. string[] pList = field.MapObjectField.Split(new char[] { '.' });
  529. string[] plList = field.MapObjectFieldLabel.Split(new char[] { '.' });
  530. var pInfo = Item.GetType().GetProperty(pList[0]);
  531. var o = pInfo.GetValue(Item);
  532. if (Array.IndexOf(o.GetType().GetInterfaces(), typeof(IEnumerable)) > -1)
  533. {
  534. List<string> Values = new List<string>();
  535. List<string> Labels = new List<string>();
  536. var objList = o as IEnumerable;
  537. foreach (var obj in objList)
  538. {
  539. var objLabel = obj;
  540. for (int i = 1; i < plList.Length; i++)
  541. {
  542. objLabel = objLabel.GetType().GetProperty(plList[i]).GetValue(objLabel);
  543. }
  544. if (objLabel.ToString() == inputFieldValue.Label)
  545. {
  546. var objValue = obj;
  547. for (int i = 1; i < pList.Length - 1; i++)
  548. {
  549. objValue = objValue.GetType().GetProperty(pList[i]).GetValue(objValue);
  550. }
  551. objValue.GetType().GetProperty(pList[pList.Length - 1])
  552. .SetValue(objValue, ConvertSimpleType(inputFieldValue.Value, Type.GetType(inputFieldValue.InputField.FieldType)));
  553. }
  554. }
  555. }
  556. }
  557. else
  558. {
  559. Item.GetType().GetProperty(field.MapObjectField).SetValue(Item, ConvertSimpleType(inputFieldValue.Value, Type.GetType(inputFieldValue.InputField.FieldType)));
  560. }
  561. }
  562. }
  563. }
  564. public List<AppealRecord> GetAppealRecords(int userId)
  565. {
  566. try
  567. {
  568. var data = Context.AppealRecords.Where<AppealRecord>(p => p.Id > 0);
  569. if (userId > 0)
  570. {
  571. data = Context.AppealRecords.Where<AppealRecord>(ar => (ar.CreaterId == userId || (ar.ReviewerId == userId && ar.State != 1)) && ar.Type.NeedReview == true);
  572. }
  573. var retList = data
  574. .Include(p => p.Reviewer)
  575. .Include(p => p.Creater)
  576. .Include(p => p.Item)
  577. .Include(p => p.Type)
  578. .OrderBy(p => p.State)
  579. .ToList();
  580. foreach (var record in retList)
  581. {
  582. replaceDataForSerialize(record);
  583. }
  584. //string str = System.Text.Json.JsonSerializer.Serialize(retList);
  585. //System.Diagnostics.Debug.WriteLine(str);
  586. //Log(str);
  587. return retList;
  588. }
  589. catch (Exception ex)
  590. {
  591. Log(ex.ToString());
  592. return new List<AppealRecord>();
  593. }
  594. }
  595. private void replaceDataForSerialize(AppealRecord record)
  596. {
  597. if (record.Creater != null)
  598. {
  599. Staff temStaff = new Staff()
  600. {
  601. Name = record.Creater.Name,
  602. Id = record.Creater.Id,
  603. ItemStaffs = null,
  604. ReviewerItems = null,
  605. Customers = null,
  606. Positions = null,
  607. StaffGrade = null,
  608. };
  609. record.Creater = temStaff;
  610. //record.Creater.ItemStaffs = null;
  611. //record.Creater.ReviewerItems = null;
  612. //record.Creater.Customers = null;
  613. //record.Creater.Positions = null;
  614. //record.Creater.StaffGrade = null;
  615. }
  616. if (record.Reviewer != null)
  617. {
  618. Staff temStaff = new Staff()
  619. {
  620. Name = record.Reviewer.Name,
  621. Id = record.Creater.Id,
  622. ItemStaffs = null,
  623. ReviewerItems = null,
  624. Customers = null,
  625. Positions = null,
  626. StaffGrade = null,
  627. };
  628. record.Reviewer = temStaff;
  629. //record.Reviewer.ItemStaffs = null;
  630. //record.Reviewer.Customers = null;
  631. //record.Reviewer.ReviewerItems = null;
  632. //record.Reviewer.Positions = null;
  633. //record.Reviewer.StaffGrade = null;
  634. }
  635. if (record.Item != null)
  636. {
  637. PerformanceItem temItem = new PerformanceItem()
  638. {
  639. Id = record.Item.Id,
  640. CaseNo = record.Item.CaseNo,
  641. DoItem = record.Item.DoItem,
  642. CaseName = record.Item.CaseName,
  643. CalMonthId = record.Item.CalMonthId
  644. };
  645. record.Item = temItem;
  646. //record.Item.ItemStaffs = null;
  647. //record.Item.PreOastaff = null;
  648. //record.Item.Reviewer = null;
  649. //record.Item.WorkflowUser = null;
  650. //record.Item.Customer = null;
  651. //record.Item.CalMonth = null;
  652. }
  653. if (record.Type != null)
  654. {
  655. record.Type.ReviewerExpress = null;
  656. record.Type.CanDoExpress = null;
  657. record.Type.ApplealObject = null;
  658. record.Type.CanDoExpress = null;
  659. record.Type.ReviewerExpress = null;
  660. }
  661. //record.Type.ReviewerExpress = null;
  662. }
  663. [HttpPost]
  664. public List<AppealRecord> QueryByFilter(AppealRecordFilter filter)
  665. {
  666. var data = Context.AppealRecords.Where<AppealRecord>(s => true);
  667. if (filter.AppealTypeId > 0)
  668. {
  669. data = Context.AppealRecords.Where(f => f.TypeId == filter.AppealTypeId);
  670. }
  671. if (!string.IsNullOrEmpty(filter.CaseNo))
  672. {
  673. data = data.Where(s => s.Item.CaseNo.Contains(filter.CaseNo));
  674. }
  675. if (!string.IsNullOrEmpty(filter.CreateUser))
  676. {
  677. data = data.Where(s => s.Creater.Name.Contains(filter.CreateUser));
  678. }
  679. if (!string.IsNullOrEmpty(filter.Reviewer))
  680. {
  681. data = data.Where(s => s.Reviewer.Name.Contains(filter.Reviewer));
  682. }
  683. if (filter.beginCreateTime.HasValue)
  684. {
  685. data = data.Where(s => s.CreateTime >= filter.beginCreateTime.Value);
  686. }
  687. if (filter.endCreateTime.HasValue)
  688. {
  689. data = data.Where(s => s.CreateTime <= filter.endCreateTime.Value);
  690. }
  691. if (filter.beginReviewTime.HasValue)
  692. {
  693. data = data.Where(s => s.ReviewTime >= filter.beginReviewTime.Value);
  694. }
  695. if (filter.endReviewTime.HasValue)
  696. {
  697. data = data.Where(s => s.ReviewTime <= filter.endReviewTime.Value);
  698. }
  699. var retList = data
  700. .Include(p => p.Reviewer)
  701. .Include(p => p.Creater)
  702. .Include(p => p.Item)
  703. .Include(p => p.Type)
  704. .OrderBy(p => p.State)
  705. .ToList();
  706. foreach (var record in retList)
  707. {
  708. replaceDataForSerialize(record);
  709. }
  710. return retList;
  711. }
  712. public AppealRecord GetAppealRecord(int Id)
  713. {
  714. var data = Context.AppealRecords.Where<AppealRecord>(ar => ar.Id == Id);
  715. return data.Include(p => p.Reviewer)
  716. .Include(p => p.Creater)
  717. .Include(p => p.Item)
  718. .Include(p => p.Type).FirstOrDefault();
  719. }
  720. public List<InputFieldValue> GetInputFieldValues(int id, int state)
  721. {
  722. var result = Context.InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == id && f.InputField.AppealState == state);
  723. return result.Include(i => i.InputField).ToList();
  724. }
  725. public List<InputFieldValue> GetAllInputFieldValue(int id)
  726. {
  727. var result = Context.InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == id);
  728. return result.Include(i => i.InputField).ToList();
  729. }
  730. public List<AttachFile> GetAppealRecordAttachFiles(int appealRecordId)
  731. {
  732. var result = Context.AttachFiles.Where<AttachFile>(at => at.AppealRecordId == appealRecordId).Include(f => f.UploadUser);
  733. return result.ToList();
  734. }
  735. public ApiSaveResponse ChangeRecordReviewer(int RecordId, int ReviewerId)
  736. {
  737. var data = Context.AppealRecords.FirstOrDefault<AppealRecord>(ar => ar.Id == RecordId);
  738. if (data != null)
  739. {
  740. var reviewer = Context.Staffs.FirstOrDefault(s => s.Id == ReviewerId);
  741. var CurrentUser = Context.Staffs.FirstOrDefault(s => s.Name == User.Identity.Name);
  742. if ((CurrentUser.Id == data.CreaterId || CurrentUser.Id == data.ReviewerId) && data.State == 0)
  743. {
  744. if (reviewer != null)
  745. {
  746. data.ReviewerId = ReviewerId;
  747. Context.SaveChanges();
  748. return new ApiSaveResponse()
  749. {
  750. Success = true
  751. };
  752. }
  753. else
  754. {
  755. return new ApiSaveResponse()
  756. {
  757. Success = false,
  758. ErrorMessage = "指定的审核人不存在"
  759. };
  760. }
  761. }
  762. else
  763. {
  764. return new ApiSaveResponse()
  765. {
  766. Success = false,
  767. ErrorMessage = "只有申诉人和审核人在未审核状态时才能变更审核人"
  768. };
  769. }
  770. }
  771. else
  772. {
  773. return new ApiSaveResponse()
  774. {
  775. Success = false,
  776. ErrorMessage = "指定的申诉记录不存在"
  777. };
  778. }
  779. }
  780. private void Log(string strMessage)
  781. {
  782. StreamWriter sw = System.IO.File.AppendText("c:\\temp\\log.txt");
  783. sw.WriteLine($"{strMessage}");
  784. sw.Flush();
  785. sw.Close();
  786. sw.Dispose();
  787. }
  788. }
  789. }