AppealController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.entity;
  13. using wispro.sp.share;
  14. namespace wispro.sp.api.Controllers
  15. {
  16. [Route("api/[controller]/[action]")]
  17. [ApiController]
  18. [Authorize]
  19. public class AppealController : ControllerBase
  20. {
  21. spDbContext Context;
  22. public AppealController(spDbContext context)
  23. {
  24. Context = context;
  25. }
  26. public List<AppealType> GetAppealTypes()
  27. {
  28. return Context.AppealTypes.ToList();
  29. }
  30. public List<InputField> GetInputField(int appealTypeId, int state)
  31. {
  32. return Context.InputFields
  33. .Where<InputField>(ip => ip.AppealTypeId == appealTypeId && ip.AppealState == state)
  34. .Include(p => p.SelectValues)
  35. .ToList();
  36. }
  37. /// <summary>
  38. /// 创建申诉流程
  39. /// </summary>
  40. /// <param name="ItemId"></param>
  41. /// <param name="typeid"></param>
  42. /// <param name="reviewerId"></param>
  43. /// <param name="appealObject"></param>
  44. /// <returns></returns>
  45. public ApiSaveResponse CreateAppeal(int ItemId, int typeid, int reviewerId, AppealObject appealObject)
  46. {
  47. ApiSaveResponse response = new ApiSaveResponse();
  48. response.Success = true;
  49. AppealRecord appealRecord = new AppealRecord();
  50. if(ItemId > 0)
  51. {
  52. appealRecord.ItemId = ItemId;
  53. }
  54. else
  55. {
  56. appealRecord.ItemId = null;
  57. }
  58. appealRecord.TypeId = typeid;
  59. appealRecord.ReviewerId = reviewerId;
  60. appealRecord.CreaterId = Context.Staffs.Where<Staff>(s => s.Name == User.Identity.Name).FirstOrDefault().Id;
  61. appealRecord.CreateTime = DateTime.Now;
  62. var t = Context.Database.BeginTransaction();
  63. try
  64. {
  65. Context.AppealRecords.Add(appealRecord);
  66. Context.SaveChanges();
  67. foreach (var fieldValue in appealObject.inputFieldValues)
  68. {
  69. fieldValue.InputField = null;
  70. fieldValue.AppealRecordId = appealRecord.Id;
  71. }
  72. Context.InputFieldValues.AddRange(appealObject.inputFieldValues);
  73. foreach (var file in appealObject.attachFiles)
  74. {
  75. var temFile = Context.AttachFiles.Where<AttachFile>(f => f.Id == file.Id).FirstOrDefault();
  76. temFile.AppealRecordId = appealRecord.Id;
  77. temFile.UploadUserId = appealRecord.CreaterId;
  78. }
  79. Context.SaveChanges();
  80. var temType = Context.AppealTypes.FirstOrDefault(c=>c.Id == appealRecord.TypeId);
  81. if (!string.IsNullOrEmpty(temType.ApplealObject))
  82. {
  83. IDoAppealObject doAppeal = (IDoAppealObject)Activator.CreateInstance(Type.GetType(appealRecord.Type.ApplealObject));
  84. doAppeal.DoAppeal(appealObject, appealRecord.Id, Context);
  85. }
  86. t.Commit();
  87. return response;
  88. }
  89. catch (Exception ex)
  90. {
  91. t.Rollback();
  92. response.Success = true;
  93. response.ErrorMessage = ex.Message;
  94. return response;
  95. }
  96. }
  97. public ApiSaveResponse ReviewerAppeal(int appealRecordId,AppealObject appealObject)
  98. {
  99. ApiSaveResponse response = new ApiSaveResponse();
  100. response.Success = true;
  101. var appealRecord = Context.AppealRecords.Where<AppealRecord>(p => p.Id == appealRecordId).FirstOrDefault();
  102. if(appealRecord != null)
  103. {
  104. var t = Context.Database.BeginTransaction();
  105. try
  106. {
  107. appealRecord.ReviewerId = Context.Staffs.Where<Staff>(s => s.Name == User.Identity.Name).FirstOrDefault().Id;
  108. appealRecord.State = 1;
  109. appealRecord.ReviewTime = DateTime.Now;
  110. Context.SaveChanges();
  111. foreach (var fieldValue in appealObject.inputFieldValues)
  112. {
  113. fieldValue.InputField = null;
  114. fieldValue.AppealRecordId = appealRecord.Id;
  115. }
  116. Context.InputFieldValues.AddRange(appealObject.inputFieldValues);
  117. Context.SaveChanges();
  118. if (appealObject.attachFiles != null)
  119. {
  120. foreach (var file in appealObject.attachFiles)
  121. {
  122. var temFile = Context.AttachFiles.Where<AttachFile>(f => f.Id == file.Id).FirstOrDefault();
  123. temFile.AppealRecordId = appealRecord.Id;
  124. temFile.UploadUserId = appealRecord.ReviewerId;
  125. }
  126. }
  127. List<InputFieldValue> inputFieldValues = Context.InputFieldValues
  128. .Where<InputFieldValue>(p => p.AppealRecordId == appealRecordId
  129. && p.InputField.MapObjectField != null)
  130. .Include(i=>i.InputField)
  131. .ToList();
  132. foreach(InputFieldValue inputFieldValue in inputFieldValues)
  133. {
  134. SaveValueToMapObject(appealRecord, inputFieldValue);
  135. }
  136. Context.SaveChanges();
  137. if(appealRecord.Type == null)
  138. {
  139. appealRecord.Type = Context.AppealTypes.FirstOrDefault(s=>s.Id == appealRecord.TypeId);
  140. }
  141. if (!string.IsNullOrEmpty(appealRecord.Type.ReviewObject))
  142. {
  143. IDoAppealObject doAppeal = (IDoAppealObject)Activator.CreateInstance(Type.GetType(appealRecord.Type.ReviewObject));
  144. doAppeal.DoAppeal(appealObject, appealRecord.Id,Context);
  145. }
  146. t.Commit();
  147. return response;
  148. }
  149. catch (Exception ex)
  150. {
  151. t.Rollback();
  152. response.Success = true;
  153. response.ErrorMessage = ex.Message;
  154. return response;
  155. }
  156. }
  157. else
  158. {
  159. response.Success = true;
  160. response.ErrorMessage = "申诉不存在!";
  161. return response;
  162. }
  163. }
  164. private object ConvertSimpleType(object value, Type destinationType)
  165. {
  166. object returnValue;
  167. if ((value == null) || destinationType.IsInstanceOfType(value))
  168. {
  169. return value;
  170. }
  171. string str = value as string;
  172. if ((str != null) && (str.Length == 0))
  173. {
  174. return null;
  175. }
  176. TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
  177. bool flag = converter.CanConvertFrom(value.GetType());
  178. if (!flag)
  179. {
  180. converter = TypeDescriptor.GetConverter(value.GetType());
  181. }
  182. if (!flag && !converter.CanConvertTo(destinationType))
  183. {
  184. throw new InvalidOperationException("无法转换成类型:" + value.ToString() + "==>" + destinationType);
  185. }
  186. try
  187. {
  188. returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType);
  189. }
  190. catch (Exception e)
  191. {
  192. throw new InvalidOperationException("类型转换出错:" + value.ToString() + "==>" + destinationType, e);
  193. }
  194. return returnValue;
  195. }
  196. private void SaveValueToMapObject(AppealRecord appealRecord, InputFieldValue inputFieldValue)
  197. {
  198. if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapObjectField))
  199. {
  200. bool isSave = false;
  201. if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapSaveCondition))
  202. {
  203. string[] conditions = inputFieldValue.InputField.MapSaveCondition.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  204. if (conditions.Length == 1)
  205. {
  206. if (appealRecord.State == int.Parse(conditions[0]))
  207. {
  208. isSave = true;
  209. }
  210. }
  211. else
  212. {
  213. if (appealRecord.State == int.Parse(conditions[0]))
  214. {
  215. InputFieldValue temValue =
  216. Context.InputFieldValues.Where<InputFieldValue>(v =>
  217. v.AppealRecordId == appealRecord.Id &&
  218. v.InputField.Id == int.Parse(conditions[1]) &&
  219. v.Value == conditions[2])
  220. .FirstOrDefault();
  221. if (temValue != null)
  222. {
  223. isSave = true;
  224. }
  225. }
  226. }
  227. }
  228. else
  229. {
  230. isSave = true;
  231. }
  232. if (isSave)
  233. {
  234. InputField field = inputFieldValue.InputField;
  235. PerformanceItem Item = Context.PerformanceItems.Where<PerformanceItem>(p =>
  236. p.Id == appealRecord.ItemId)
  237. .Include(p => p.ItemStaffs).ThenInclude(it=>it.DoPerson)
  238. .Include(p => p.Reviewer)
  239. .FirstOrDefault();
  240. bool ItemIsNull = false;
  241. if(Item == null)
  242. {
  243. ItemIsNull = true;
  244. Item = new PerformanceItem();
  245. }
  246. if (!string.IsNullOrEmpty(field.MapObjectField) && !string.IsNullOrEmpty(field.MapObjectFieldLabel))
  247. {
  248. List<InputFieldValue> temValues = new List<InputFieldValue>();
  249. string[] pList = field.MapObjectField.Split(new char[] { '.' });
  250. string[] plList = field.MapObjectFieldLabel.Split(new char[] { '.' });
  251. var pInfo = Item.GetType().GetProperty(pList[0]);
  252. var o = pInfo.GetValue(Item);
  253. if (Array.IndexOf(o.GetType().GetInterfaces(), typeof(IEnumerable)) > -1)
  254. {
  255. List<string> Values = new List<string>();
  256. List<string> Labels = new List<string>();
  257. var objList = o as IEnumerable;
  258. foreach (var obj in objList)
  259. {
  260. var objLabel = obj;
  261. for (int i = 1; i < plList.Length; i++)
  262. {
  263. objLabel = objLabel.GetType().GetProperty(plList[i]).GetValue(objLabel);
  264. }
  265. if (objLabel.ToString() == inputFieldValue.Label)
  266. {
  267. var objValue = obj;
  268. for (int i = 1; i < pList.Length - 1; i++)
  269. {
  270. objValue = objValue.GetType().GetProperty(pList[i]).GetValue(objValue);
  271. }
  272. objValue.GetType().GetProperty(pList[pList.Length - 1])
  273. .SetValue(objValue, ConvertSimpleType(inputFieldValue.Value,Type.GetType(inputFieldValue.InputField.FieldType)));
  274. }
  275. }
  276. }
  277. }
  278. else
  279. {
  280. Item.GetType().GetProperty(field.MapObjectField).SetValue(Item, ConvertSimpleType(inputFieldValue.Value, Type.GetType(inputFieldValue.InputField.FieldType)));
  281. }
  282. }
  283. }
  284. }
  285. public List<AppealRecord> GetAppealRecords(int userId)
  286. {
  287. var data = Context.AppealRecords.Where<AppealRecord>(ar => (ar.CreaterId == userId || (ar.ReviewerId == userId && ar.State !=1)) && ar.Type.NeedReview ==true);
  288. var retList = data.Include(p => p.Reviewer)
  289. .Include(p => p.Creater)
  290. .Include(p => p.Item)
  291. .Include(p => p.Type).ToList();
  292. foreach(var record in retList)
  293. {
  294. record.Creater.ItemStaffs = null;
  295. record.Creater.ReviewerItems = null;
  296. record.Creater.Customers = null;
  297. if (record.Item != null)
  298. {
  299. record.Item.ItemStaffs = null;
  300. record.Item.PreOastaff = null;
  301. record.Item.Reviewer = null;
  302. }
  303. }
  304. return retList;
  305. }
  306. public AppealRecord GetAppealRecord(int Id)
  307. {
  308. var data = Context.AppealRecords.Where<AppealRecord>(ar => ar.Id == Id);
  309. return data.Include(p => p.Reviewer)
  310. .Include(p => p.Creater)
  311. .Include(p => p.Item)
  312. .Include(p => p.Type).FirstOrDefault();
  313. }
  314. public List<InputFieldValue> GetInputFieldValues(int id, int state)
  315. {
  316. var result = Context.InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == id && f.InputField.AppealState == state);
  317. return result.Include(i => i.InputField).ToList();
  318. }
  319. public List<AttachFile> GetAppealRecordAttachFiles(int appealRecordId)
  320. {
  321. var result = Context.AttachFiles.Where<AttachFile>(at => at.AppealRecordId == appealRecordId).Include(f => f.UploadUser);
  322. return result.ToList();
  323. }
  324. }
  325. }