AppealController.cs 15 KB

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