AppealController.cs 12 KB

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