AppealController.cs 13 KB

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