AppealController.cs 20 KB

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