AppealController.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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.Linq;
  10. using System.Reflection;
  11. using System.Threading.Tasks;
  12. using wispro.sp.api.Job;
  13. using wispro.sp.entity;
  14. using wispro.sp.share;
  15. namespace wispro.sp.api.Controllers
  16. {
  17. [Route("api/[controller]/[action]")]
  18. [ApiController]
  19. [Authorize]
  20. public class AppealController : ControllerBase
  21. {
  22. spDbContext Context;
  23. public AppealController(spDbContext context)
  24. {
  25. Context = context;
  26. }
  27. public List<AppealType> GetAppealTypes()
  28. {
  29. return Context.AppealTypes.ToList();
  30. }
  31. public List<InputField> GetInputField(int appealTypeId, int state)
  32. {
  33. return Context.InputFields
  34. .Where<InputField>(ip => ip.AppealTypeId == appealTypeId && ip.AppealState == state)
  35. .Include(p => p.SelectValues)
  36. .ToList();
  37. }
  38. /// <summary>
  39. /// 创建申诉流程
  40. /// </summary>
  41. /// <param name="ItemId"></param>
  42. /// <param name="typeid"></param>
  43. /// <param name="reviewerId"></param>
  44. /// <param name="appealObject"></param>
  45. /// <returns></returns>
  46. public ApiSaveResponse CreateAppeal(int ItemId, int typeid, int reviewerId, AppealObject appealObject)
  47. {
  48. ApiSaveResponse response = new ApiSaveResponse();
  49. response.Success = true;
  50. AppealRecord appealRecord = new AppealRecord();
  51. if(ItemId > 0)
  52. {
  53. appealRecord.ItemId = ItemId;
  54. }
  55. else
  56. {
  57. appealRecord.ItemId = null;
  58. }
  59. appealRecord.TypeId = typeid;
  60. appealRecord.ReviewerId = reviewerId;
  61. appealRecord.CreaterId = Context.Staffs.Where<Staff>(s => s.Name == User.Identity.Name).FirstOrDefault().Id;
  62. appealRecord.CreateTime = DateTime.Now;
  63. var t = Context.Database.BeginTransaction();
  64. try
  65. {
  66. Context.AppealRecords.Add(appealRecord);
  67. Context.SaveChanges();
  68. foreach (var fieldValue in appealObject.inputFieldValues)
  69. {
  70. fieldValue.InputField = null;
  71. fieldValue.AppealRecordId = appealRecord.Id;
  72. }
  73. Context.InputFieldValues.AddRange(appealObject.inputFieldValues);
  74. foreach (var file in appealObject.attachFiles)
  75. {
  76. var temFile = Context.AttachFiles.Where<AttachFile>(f => f.Id == file.Id).FirstOrDefault();
  77. temFile.AppealRecordId = appealRecord.Id;
  78. temFile.UploadUserId = appealRecord.CreaterId;
  79. }
  80. Context.SaveChanges();
  81. var temType = Context.AppealTypes.FirstOrDefault(c=>c.Id == appealRecord.TypeId);
  82. if (!string.IsNullOrEmpty(temType.ApplealObject))
  83. {
  84. IDoAppealObject doAppeal = (IDoAppealObject)Activator.CreateInstance(Type.GetType(appealRecord.Type.ApplealObject));
  85. doAppeal.DoAppeal(appealObject, appealRecord.Id, Context);
  86. }
  87. t.Commit();
  88. var Reviewer = Context.Staffs.Where<Staff>(s => s.Id == appealRecord.ReviewerId).FirstOrDefault();
  89. if(appealRecord.Type == null)
  90. {
  91. appealRecord.Type = Context.AppealTypes.FirstOrDefault(p=>p.Id == appealRecord.TypeId);
  92. }
  93. string strSubject = appealRecord.Type.Name;
  94. 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= \"https://47.106.221.167:8080/ \">绩效系统</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>";
  95. string strTo = Reviewer.Mail;
  96. if (!string.IsNullOrEmpty(strTo))
  97. {
  98. _ = QuartzUtil.AddMailJob(strSubject, strBody, Reviewer.Name, strTo);
  99. }
  100. 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= \"https://47.106.221.167:8080/ \">绩效系统</a>查看详情!</div><div style= \"margin-top: 100px;margin-right:15px; padding-bottom: 20px; font-size:14px;color:#888888;float:right; \">小美集团绩效管理系统</div></div>";
  101. _ = QuartzUtil.AddMailJob(strSubject, strBody, "钟子敏", "zhongzimin@china-wispro.com");
  102. _ = QuartzUtil.AddMailJob(strSubject, strBody, "夏敏", "xiamin@china-wispro.com");
  103. _ = QuartzUtil.AddMailJob(strSubject, strBody, "吴芳", "wufang@china-wispro.com");
  104. return response;
  105. }
  106. catch (Exception ex)
  107. {
  108. t.Rollback();
  109. response.Success = false;
  110. response.ErrorMessage = "申诉时发生错误!";
  111. return response;
  112. }
  113. }
  114. public ApiSaveResponse ReviewerAppeal(int appealRecordId,AppealObject appealObject)
  115. {
  116. ApiSaveResponse response = new ApiSaveResponse();
  117. response.Success = true;
  118. var appealRecord = Context.AppealRecords.Where<AppealRecord>(p => p.Id == appealRecordId).FirstOrDefault();
  119. if(appealRecord != null)
  120. {
  121. if (appealRecord.ItemId.HasValue)
  122. {
  123. var item = Context.PerformanceItems.Include(p=>p.CalMonth).FirstOrDefault(p => p.Id == appealRecord.ItemId.Value);
  124. if(item == null)
  125. {
  126. response.Success = false;
  127. response.ErrorMessage = "无效的ItemId!";
  128. return response;
  129. }
  130. else
  131. {
  132. if(item.CalMonth.Status != 0)
  133. {
  134. response.Success = false;
  135. response.ErrorMessage = "指定的绩效已经归档!";
  136. return response;
  137. }
  138. }
  139. }
  140. var t = Context.Database.BeginTransaction();
  141. try
  142. {
  143. appealRecord.ReviewerId = Context.Staffs.Where<Staff>(s => s.Name == User.Identity.Name).FirstOrDefault().Id;
  144. appealRecord.State = 1;
  145. appealRecord.ReviewTime = DateTime.Now;
  146. Context.SaveChanges();
  147. foreach (var fieldValue in appealObject.inputFieldValues)
  148. {
  149. fieldValue.InputField = null;
  150. fieldValue.AppealRecordId = appealRecord.Id;
  151. }
  152. Context.InputFieldValues.AddRange(appealObject.inputFieldValues);
  153. Context.SaveChanges();
  154. if (appealObject.attachFiles != null)
  155. {
  156. foreach (var file in appealObject.attachFiles)
  157. {
  158. var temFile = Context.AttachFiles.Where<AttachFile>(f => f.Id == file.Id).FirstOrDefault();
  159. temFile.AppealRecordId = appealRecord.Id;
  160. temFile.UploadUserId = appealRecord.ReviewerId;
  161. }
  162. }
  163. List<InputFieldValue> inputFieldValues = Context.InputFieldValues
  164. .Where<InputFieldValue>(p => p.AppealRecordId == appealRecordId
  165. && p.InputField.MapObjectField != null)
  166. .Include(i=>i.InputField)
  167. .ToList();
  168. foreach(InputFieldValue inputFieldValue in inputFieldValues)
  169. {
  170. SaveValueToMapObject(appealRecord, inputFieldValue);
  171. }
  172. Context.SaveChanges();
  173. if(appealRecord.Type == null)
  174. {
  175. appealRecord.Type = Context.AppealTypes.FirstOrDefault(s=>s.Id == appealRecord.TypeId);
  176. }
  177. if (!string.IsNullOrEmpty(appealRecord.Type.ReviewObject))
  178. {
  179. IDoAppealObject doAppeal = (IDoAppealObject)Activator.CreateInstance(Type.GetType(appealRecord.Type.ReviewObject));
  180. doAppeal.DoAppeal(appealObject, appealRecord.Id,Context);
  181. }
  182. Context.SaveChanges();
  183. if(appealRecord.ItemId.HasValue)
  184. {
  185. var item = Context.PerformanceItems
  186. .Include(p => p.ItemStaffs)
  187. .Include(p => p.Customer)
  188. .Include(p=>p.Reviewer)
  189. .FirstOrDefault(p=>p.Id == appealRecord.ItemId);
  190. new PerformanceItemController(Context, new Services.FileTaskCacheService()).RefreshPoint(item);
  191. }
  192. t.Commit();
  193. return response;
  194. }
  195. catch (Exception ex)
  196. {
  197. t.Rollback();
  198. response.Success = false;
  199. response.ErrorMessage = ex.Message;
  200. return response;
  201. }
  202. }
  203. else
  204. {
  205. response.Success = true;
  206. response.ErrorMessage = "申诉不存在!";
  207. return response;
  208. }
  209. }
  210. private object ConvertSimpleType(object value, Type destinationType)
  211. {
  212. object returnValue;
  213. if ((value == null) || destinationType.IsInstanceOfType(value))
  214. {
  215. return value;
  216. }
  217. string str = value as string;
  218. if ((str != null) && (str.Length == 0))
  219. {
  220. return null;
  221. }
  222. TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
  223. bool flag = converter.CanConvertFrom(value.GetType());
  224. if (!flag)
  225. {
  226. converter = TypeDescriptor.GetConverter(value.GetType());
  227. }
  228. if (!flag && !converter.CanConvertTo(destinationType))
  229. {
  230. throw new InvalidOperationException("无法转换成类型:" + value.ToString() + "==>" + destinationType);
  231. }
  232. try
  233. {
  234. returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType);
  235. }
  236. catch (Exception e)
  237. {
  238. throw new InvalidOperationException("类型转换出错:" + value.ToString() + "==>" + destinationType, e);
  239. }
  240. return returnValue;
  241. }
  242. private void SaveValueToMapObject(AppealRecord appealRecord, InputFieldValue inputFieldValue)
  243. {
  244. if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapObjectField))
  245. {
  246. bool isSave = false;
  247. if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapSaveCondition))
  248. {
  249. string[] conditions = inputFieldValue.InputField.MapSaveCondition.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  250. if (conditions.Length == 1)
  251. {
  252. if (appealRecord.State == int.Parse(conditions[0]))
  253. {
  254. isSave = true;
  255. }
  256. }
  257. else
  258. {
  259. if (appealRecord.State == int.Parse(conditions[0]))
  260. {
  261. InputFieldValue temValue =
  262. Context.InputFieldValues.Where<InputFieldValue>(v =>
  263. v.AppealRecordId == appealRecord.Id &&
  264. v.InputField.Id == int.Parse(conditions[1]) &&
  265. v.Value == conditions[2])
  266. .FirstOrDefault();
  267. if (temValue != null)
  268. {
  269. isSave = true;
  270. }
  271. }
  272. }
  273. }
  274. else
  275. {
  276. isSave = true;
  277. }
  278. if (isSave)
  279. {
  280. InputField field = inputFieldValue.InputField;
  281. PerformanceItem Item = Context.PerformanceItems.Where<PerformanceItem>(p =>
  282. p.Id == appealRecord.ItemId)
  283. .Include(p => p.ItemStaffs).ThenInclude(it=>it.DoPerson)
  284. .Include(p => p.Reviewer)
  285. .FirstOrDefault();
  286. bool ItemIsNull = false;
  287. if(Item == null)
  288. {
  289. ItemIsNull = true;
  290. Item = new PerformanceItem();
  291. }
  292. if (!string.IsNullOrEmpty(field.MapObjectField) && !string.IsNullOrEmpty(field.MapObjectFieldLabel))
  293. {
  294. List<InputFieldValue> temValues = new List<InputFieldValue>();
  295. string[] pList = field.MapObjectField.Split(new char[] { '.' });
  296. string[] plList = field.MapObjectFieldLabel.Split(new char[] { '.' });
  297. var pInfo = Item.GetType().GetProperty(pList[0]);
  298. var o = pInfo.GetValue(Item);
  299. if (Array.IndexOf(o.GetType().GetInterfaces(), typeof(IEnumerable)) > -1)
  300. {
  301. List<string> Values = new List<string>();
  302. List<string> Labels = new List<string>();
  303. var objList = o as IEnumerable;
  304. foreach (var obj in objList)
  305. {
  306. var objLabel = obj;
  307. for (int i = 1; i < plList.Length; i++)
  308. {
  309. objLabel = objLabel.GetType().GetProperty(plList[i]).GetValue(objLabel);
  310. }
  311. if (objLabel.ToString() == inputFieldValue.Label)
  312. {
  313. var objValue = obj;
  314. for (int i = 1; i < pList.Length - 1; i++)
  315. {
  316. objValue = objValue.GetType().GetProperty(pList[i]).GetValue(objValue);
  317. }
  318. objValue.GetType().GetProperty(pList[pList.Length - 1])
  319. .SetValue(objValue, ConvertSimpleType(inputFieldValue.Value,Type.GetType(inputFieldValue.InputField.FieldType)));
  320. }
  321. }
  322. }
  323. }
  324. else
  325. {
  326. Item.GetType().GetProperty(field.MapObjectField).SetValue(Item, ConvertSimpleType(inputFieldValue.Value, Type.GetType(inputFieldValue.InputField.FieldType)));
  327. }
  328. }
  329. }
  330. }
  331. public List<AppealRecord> GetAppealRecords(int userId)
  332. {
  333. var data = Context.AppealRecords.Where<AppealRecord>(p=>p.Id >0);
  334. if (userId > 0)
  335. {
  336. data = Context.AppealRecords.Where<AppealRecord>(ar => (ar.CreaterId == userId || (ar.ReviewerId == userId && ar.State != 1)) && ar.Type.NeedReview == true);
  337. }
  338. var retList = data.Include(p => p.Reviewer)
  339. .Include(p => p.Creater)
  340. .Include(p => p.Item)
  341. .Include(p => p.Type).ToList();
  342. foreach(var record in retList)
  343. {
  344. record.Creater.ItemStaffs = null;
  345. record.Creater.ReviewerItems = null;
  346. record.Creater.Customers = null;
  347. if (record.Item != null)
  348. {
  349. record.Item.ItemStaffs = null;
  350. record.Item.PreOastaff = null;
  351. record.Item.Reviewer = null;
  352. }
  353. }
  354. return retList;
  355. }
  356. [HttpPost]
  357. public List<AppealRecord> QueryByFilter(AppealRecordFilter filter)
  358. {
  359. var data = Context.AppealRecords.Where<AppealRecord>(s => true);
  360. if(filter.AppealTypeId > 0)
  361. {
  362. data = Context.AppealRecords.Where(f=>f.TypeId == filter.AppealTypeId);
  363. }
  364. if(!string.IsNullOrEmpty(filter.CaseNo))
  365. {
  366. data = data.Where(s=>s.Item.CaseNo.Contains(filter.CaseNo));
  367. }
  368. if (!string.IsNullOrEmpty(filter.CreateUser))
  369. {
  370. data = data.Where(s => s.Creater.Name.Contains(filter.CreateUser));
  371. }
  372. if (!string.IsNullOrEmpty(filter.Reviewer))
  373. {
  374. data = data.Where(s => s.Reviewer.Name.Contains(filter.Reviewer));
  375. }
  376. if (filter.beginCreateTime.HasValue)
  377. {
  378. data = data.Where(s => s.CreateTime>=filter.beginCreateTime.Value );
  379. }
  380. if (filter.endCreateTime.HasValue)
  381. {
  382. data = data.Where(s => s.CreateTime <= filter.endCreateTime.Value);
  383. }
  384. if (filter.beginReviewTime.HasValue)
  385. {
  386. data = data.Where(s => s.ReviewTime >= filter.beginReviewTime.Value);
  387. }
  388. if (filter.endReviewTime.HasValue)
  389. {
  390. data = data.Where(s => s.ReviewTime <= filter.endReviewTime.Value);
  391. }
  392. var retList = data
  393. .Include(p => p.Reviewer)
  394. .Include(p => p.Creater)
  395. .Include(p => p.Item)
  396. .Include(p => p.Type)
  397. .ToList();
  398. foreach (var record in retList)
  399. {
  400. record.Creater.ItemStaffs = null;
  401. record.Creater.ReviewerItems = null;
  402. record.Creater.Customers = null;
  403. if (record.Item != null)
  404. {
  405. record.Item.ItemStaffs = null;
  406. record.Item.PreOastaff = null;
  407. record.Item.Reviewer = null;
  408. }
  409. }
  410. return retList;
  411. }
  412. public AppealRecord GetAppealRecord(int Id)
  413. {
  414. var data = Context.AppealRecords.Where<AppealRecord>(ar => ar.Id == Id);
  415. return data.Include(p => p.Reviewer)
  416. .Include(p => p.Creater)
  417. .Include(p => p.Item)
  418. .Include(p => p.Type).FirstOrDefault();
  419. }
  420. public List<InputFieldValue> GetInputFieldValues(int id, int state)
  421. {
  422. var result = Context.InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == id && f.InputField.AppealState == state);
  423. return result.Include(i => i.InputField).ToList();
  424. }
  425. public List<InputFieldValue> GetAllInputFieldValue(int id)
  426. {
  427. var result = Context.InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == id);
  428. return result.Include(i => i.InputField).ToList();
  429. }
  430. public List<AttachFile> GetAppealRecordAttachFiles(int appealRecordId)
  431. {
  432. var result = Context.AttachFiles.Where<AttachFile>(at => at.AppealRecordId == appealRecordId).Include(f => f.UploadUser);
  433. return result.ToList();
  434. }
  435. public ApiSaveResponse ChangeRecordReviewer(int RecordId,int ReviewerId)
  436. {
  437. var data = Context.AppealRecords.FirstOrDefault<AppealRecord>(ar => ar.Id == RecordId);
  438. if(data != null)
  439. {
  440. var reviewer = Context.Staffs.FirstOrDefault(s => s.Id == ReviewerId);
  441. var CurrentUser = Context.Staffs.FirstOrDefault(s=>s.Name == User.Identity.Name);
  442. if ((CurrentUser.Id == data.CreaterId || CurrentUser.Id == data.ReviewerId) && data.State ==0)
  443. {
  444. if (reviewer != null)
  445. {
  446. data.ReviewerId = ReviewerId;
  447. Context.SaveChanges();
  448. return new ApiSaveResponse()
  449. {
  450. Success = true
  451. };
  452. }
  453. else
  454. {
  455. return new ApiSaveResponse()
  456. {
  457. Success = false,
  458. ErrorMessage = "指定的审核人不存在"
  459. };
  460. }
  461. }
  462. else
  463. {
  464. return new ApiSaveResponse()
  465. {
  466. Success = false,
  467. ErrorMessage = "只有申诉人和审核人在未审核状态时才能变更审核人"
  468. };
  469. }
  470. }
  471. else
  472. {
  473. return new ApiSaveResponse()
  474. {
  475. Success = false,
  476. ErrorMessage = "指定的申诉记录不存在"
  477. };
  478. }
  479. }
  480. }
  481. }