AppealController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 = true;
  110. response.ErrorMessage = ex.Message;
  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. t.Commit();
  184. return response;
  185. }
  186. catch (Exception ex)
  187. {
  188. t.Rollback();
  189. response.Success = false;
  190. response.ErrorMessage = ex.Message;
  191. return response;
  192. }
  193. }
  194. else
  195. {
  196. response.Success = true;
  197. response.ErrorMessage = "申诉不存在!";
  198. return response;
  199. }
  200. }
  201. private object ConvertSimpleType(object value, Type destinationType)
  202. {
  203. object returnValue;
  204. if ((value == null) || destinationType.IsInstanceOfType(value))
  205. {
  206. return value;
  207. }
  208. string str = value as string;
  209. if ((str != null) && (str.Length == 0))
  210. {
  211. return null;
  212. }
  213. TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
  214. bool flag = converter.CanConvertFrom(value.GetType());
  215. if (!flag)
  216. {
  217. converter = TypeDescriptor.GetConverter(value.GetType());
  218. }
  219. if (!flag && !converter.CanConvertTo(destinationType))
  220. {
  221. throw new InvalidOperationException("无法转换成类型:" + value.ToString() + "==>" + destinationType);
  222. }
  223. try
  224. {
  225. returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType);
  226. }
  227. catch (Exception e)
  228. {
  229. throw new InvalidOperationException("类型转换出错:" + value.ToString() + "==>" + destinationType, e);
  230. }
  231. return returnValue;
  232. }
  233. private void SaveValueToMapObject(AppealRecord appealRecord, InputFieldValue inputFieldValue)
  234. {
  235. if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapObjectField))
  236. {
  237. bool isSave = false;
  238. if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapSaveCondition))
  239. {
  240. string[] conditions = inputFieldValue.InputField.MapSaveCondition.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  241. if (conditions.Length == 1)
  242. {
  243. if (appealRecord.State == int.Parse(conditions[0]))
  244. {
  245. isSave = true;
  246. }
  247. }
  248. else
  249. {
  250. if (appealRecord.State == int.Parse(conditions[0]))
  251. {
  252. InputFieldValue temValue =
  253. Context.InputFieldValues.Where<InputFieldValue>(v =>
  254. v.AppealRecordId == appealRecord.Id &&
  255. v.InputField.Id == int.Parse(conditions[1]) &&
  256. v.Value == conditions[2])
  257. .FirstOrDefault();
  258. if (temValue != null)
  259. {
  260. isSave = true;
  261. }
  262. }
  263. }
  264. }
  265. else
  266. {
  267. isSave = true;
  268. }
  269. if (isSave)
  270. {
  271. InputField field = inputFieldValue.InputField;
  272. PerformanceItem Item = Context.PerformanceItems.Where<PerformanceItem>(p =>
  273. p.Id == appealRecord.ItemId)
  274. .Include(p => p.ItemStaffs).ThenInclude(it=>it.DoPerson)
  275. .Include(p => p.Reviewer)
  276. .FirstOrDefault();
  277. bool ItemIsNull = false;
  278. if(Item == null)
  279. {
  280. ItemIsNull = true;
  281. Item = new PerformanceItem();
  282. }
  283. if (!string.IsNullOrEmpty(field.MapObjectField) && !string.IsNullOrEmpty(field.MapObjectFieldLabel))
  284. {
  285. List<InputFieldValue> temValues = new List<InputFieldValue>();
  286. string[] pList = field.MapObjectField.Split(new char[] { '.' });
  287. string[] plList = field.MapObjectFieldLabel.Split(new char[] { '.' });
  288. var pInfo = Item.GetType().GetProperty(pList[0]);
  289. var o = pInfo.GetValue(Item);
  290. if (Array.IndexOf(o.GetType().GetInterfaces(), typeof(IEnumerable)) > -1)
  291. {
  292. List<string> Values = new List<string>();
  293. List<string> Labels = new List<string>();
  294. var objList = o as IEnumerable;
  295. foreach (var obj in objList)
  296. {
  297. var objLabel = obj;
  298. for (int i = 1; i < plList.Length; i++)
  299. {
  300. objLabel = objLabel.GetType().GetProperty(plList[i]).GetValue(objLabel);
  301. }
  302. if (objLabel.ToString() == inputFieldValue.Label)
  303. {
  304. var objValue = obj;
  305. for (int i = 1; i < pList.Length - 1; i++)
  306. {
  307. objValue = objValue.GetType().GetProperty(pList[i]).GetValue(objValue);
  308. }
  309. objValue.GetType().GetProperty(pList[pList.Length - 1])
  310. .SetValue(objValue, ConvertSimpleType(inputFieldValue.Value,Type.GetType(inputFieldValue.InputField.FieldType)));
  311. }
  312. }
  313. }
  314. }
  315. else
  316. {
  317. Item.GetType().GetProperty(field.MapObjectField).SetValue(Item, ConvertSimpleType(inputFieldValue.Value, Type.GetType(inputFieldValue.InputField.FieldType)));
  318. }
  319. }
  320. }
  321. }
  322. public List<AppealRecord> GetAppealRecords(int userId)
  323. {
  324. var data = Context.AppealRecords.Where<AppealRecord>(p=>p.Id >0);
  325. if (userId > 0)
  326. {
  327. data = Context.AppealRecords.Where<AppealRecord>(ar => (ar.CreaterId == userId || (ar.ReviewerId == userId && ar.State != 1)) && ar.Type.NeedReview == true);
  328. }
  329. var retList = data.Include(p => p.Reviewer)
  330. .Include(p => p.Creater)
  331. .Include(p => p.Item)
  332. .Include(p => p.Type).ToList();
  333. foreach(var record in retList)
  334. {
  335. record.Creater.ItemStaffs = null;
  336. record.Creater.ReviewerItems = null;
  337. record.Creater.Customers = null;
  338. if (record.Item != null)
  339. {
  340. record.Item.ItemStaffs = null;
  341. record.Item.PreOastaff = null;
  342. record.Item.Reviewer = null;
  343. }
  344. }
  345. return retList;
  346. }
  347. [HttpPost]
  348. public List<AppealRecord> QueryByFilter(AppealRecordFilter filter)
  349. {
  350. var data = Context.AppealRecords.Where<AppealRecord>(s => true);
  351. if(filter.AppealTypeId > 0)
  352. {
  353. data = Context.AppealRecords.Where(f=>f.TypeId == filter.AppealTypeId);
  354. }
  355. if(!string.IsNullOrEmpty(filter.CaseNo))
  356. {
  357. data = data.Where(s=>s.Item.CaseNo.Contains(filter.CaseNo));
  358. }
  359. if (!string.IsNullOrEmpty(filter.CreateUser))
  360. {
  361. data = data.Where(s => s.Creater.Name.Contains(filter.CreateUser));
  362. }
  363. if (!string.IsNullOrEmpty(filter.Reviewer))
  364. {
  365. data = data.Where(s => s.Reviewer.Name.Contains(filter.Reviewer));
  366. }
  367. if (filter.beginCreateTime.HasValue)
  368. {
  369. data = data.Where(s => s.CreateTime>=filter.beginCreateTime.Value );
  370. }
  371. if (filter.endCreateTime.HasValue)
  372. {
  373. data = data.Where(s => s.CreateTime <= filter.endCreateTime.Value);
  374. }
  375. if (filter.beginReviewTime.HasValue)
  376. {
  377. data = data.Where(s => s.ReviewTime >= filter.beginReviewTime.Value);
  378. }
  379. if (filter.endReviewTime.HasValue)
  380. {
  381. data = data.Where(s => s.ReviewTime <= filter.endReviewTime.Value);
  382. }
  383. var retList = data
  384. .Include(p => p.Reviewer)
  385. .Include(p => p.Creater)
  386. .Include(p => p.Item)
  387. .Include(p => p.Type)
  388. .ToList();
  389. foreach (var record in retList)
  390. {
  391. record.Creater.ItemStaffs = null;
  392. record.Creater.ReviewerItems = null;
  393. record.Creater.Customers = null;
  394. if (record.Item != null)
  395. {
  396. record.Item.ItemStaffs = null;
  397. record.Item.PreOastaff = null;
  398. record.Item.Reviewer = null;
  399. }
  400. }
  401. return retList;
  402. }
  403. public AppealRecord GetAppealRecord(int Id)
  404. {
  405. var data = Context.AppealRecords.Where<AppealRecord>(ar => ar.Id == Id);
  406. return data.Include(p => p.Reviewer)
  407. .Include(p => p.Creater)
  408. .Include(p => p.Item)
  409. .Include(p => p.Type).FirstOrDefault();
  410. }
  411. public List<InputFieldValue> GetInputFieldValues(int id, int state)
  412. {
  413. var result = Context.InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == id && f.InputField.AppealState == state);
  414. return result.Include(i => i.InputField).ToList();
  415. }
  416. public List<InputFieldValue> GetAllInputFieldValue(int id)
  417. {
  418. var result = Context.InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == id);
  419. return result.Include(i => i.InputField).ToList();
  420. }
  421. public List<AttachFile> GetAppealRecordAttachFiles(int appealRecordId)
  422. {
  423. var result = Context.AttachFiles.Where<AttachFile>(at => at.AppealRecordId == appealRecordId).Include(f => f.UploadUser);
  424. return result.ToList();
  425. }
  426. public ApiSaveResponse ChangeRecordReviewer(int RecordId,int ReviewerId)
  427. {
  428. var data = Context.AppealRecords.FirstOrDefault<AppealRecord>(ar => ar.Id == RecordId);
  429. if(data != null)
  430. {
  431. var reviewer = Context.Staffs.FirstOrDefault(s => s.Id == ReviewerId);
  432. var CurrentUser = Context.Staffs.FirstOrDefault(s=>s.Name == User.Identity.Name);
  433. if ((CurrentUser.Id == data.CreaterId || CurrentUser.Id == data.ReviewerId) && data.State ==0)
  434. {
  435. if (reviewer != null)
  436. {
  437. data.ReviewerId = ReviewerId;
  438. Context.SaveChanges();
  439. return new ApiSaveResponse()
  440. {
  441. Success = true
  442. };
  443. }
  444. else
  445. {
  446. return new ApiSaveResponse()
  447. {
  448. Success = false,
  449. ErrorMessage = "指定的审核人不存在"
  450. };
  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. }
  472. }