AppealController.cs 13 KB

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