AppealController.cs 22 KB

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