DEVCmdService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using AutoMapper;
  2. using Ropin.Core.Common;
  3. using Ropin.Inspection.Common.Accessor.Interface;
  4. using Ropin.Inspection.Model.Entities;
  5. using Ropin.Inspection.Model.SearchModel.DEV;
  6. using Ropin.Inspection.Model.ViewModel.DEV;
  7. using Ropin.Inspection.Repository.DEV.Interface;
  8. using Ropin.Inspection.Service.DEV.Interface;
  9. using Ropin.Inspection.Service.Interface;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace Ropin.Inspection.Service.DEV
  16. {
  17. public class DEVCmdService: IDEVCmdService
  18. {
  19. private readonly IDevCmdRepository _repository;
  20. private readonly IDevInstructionRepository _InstructionRepository;
  21. private readonly IDevCmdInstructionRepository _CmdInstructionRepository;
  22. private readonly IMapper _mapper;
  23. private readonly IClaimsAccessor _claims;
  24. private readonly InspectionDbContext _sqlDBContext;
  25. public DEVCmdService(IClaimsAccessor claims, InspectionDbContext sqlDBContext, IDevCmdRepository repository, IMapper mapper, IDevInstructionRepository InstructionRepository, IDevCmdInstructionRepository CmdInstructionRepository)
  26. {
  27. _repository = repository;
  28. _mapper = mapper;
  29. _claims = claims;
  30. _sqlDBContext = sqlDBContext;
  31. _InstructionRepository = InstructionRepository;
  32. _CmdInstructionRepository = CmdInstructionRepository;
  33. }
  34. #region 命令
  35. //新增【命令】
  36. public async Task CreateOneAsync(DevCmdViewModel viewModel)
  37. {
  38. var id = Guid.NewGuid().ToString();
  39. var content = _mapper.Map<TDEV_Cmd>(viewModel);
  40. content.C_ID = id;
  41. content.C_CreateBy = _claims.ApiUserId.ToString();
  42. content.D_CreateOn = DateTime.Now;
  43. content.C_Creator = _claims.ApiUserName;
  44. content.C_Status = "1";
  45. _repository.Create(content);
  46. var result = await _repository.SaveAsync();
  47. if (!result)
  48. {
  49. throw new Exception("创建失败");
  50. }
  51. }
  52. //删除【命令】
  53. public async Task DeleteAsync(string id)
  54. {
  55. var content = await _repository.GetByIdAsync(id);
  56. if (content == null)
  57. {
  58. throw new Exception("数据库中没有此数据");
  59. }
  60. var data = await _CmdInstructionRepository.GetByConditionAsync(t => t.C_CmdCode == id);
  61. if (data != null && data.Count() > 0)
  62. {
  63. throw new Exception("该数据已被关联,无法删除");
  64. }
  65. _repository.Delete(content);
  66. var result = await _repository.SaveAsync();
  67. if (!result)
  68. {
  69. throw new Exception("删除失败");
  70. }
  71. }
  72. //修改为禁用【命令】
  73. public async Task DisableAsync(string id)
  74. {
  75. var content = await _repository.GetByIdAsync(id);
  76. if (content == null)
  77. {
  78. throw new Exception("数据库中没有此数据");
  79. }
  80. content.C_LastUpdatedBy = _claims.ApiUserId.ToString();
  81. content.D_LastUpdatedOn = DateTime.Now;
  82. content.C_Modifier = _claims.ApiUserName;
  83. content.C_Status = "0";
  84. _repository.Update(content);
  85. var result = await _repository.SaveAsync();
  86. if (!result)
  87. {
  88. throw new Exception("删除失败");
  89. }
  90. }
  91. //根据ID获取实体【命令】
  92. public async Task<DevCmdViewModel> GetByIdAsync(string id)
  93. {
  94. DevCmdSearchModel searchModel = new DevCmdSearchModel();
  95. searchModel.C_ID = id;
  96. searchModel.IsPagination = false;
  97. var items = await _repository.GetConditionAsync(searchModel);
  98. var content = items.FirstOrDefault();
  99. return content;
  100. }
  101. // 更新
  102. public async Task UpdateAsync(string code, DevCmdViewModel updateModel)
  103. {
  104. TDEV_Cmd content = await _repository.GetByIdAsync(code);
  105. if (content == null)
  106. {
  107. throw new Exception("没有此数据");
  108. }
  109. content.C_LastUpdatedBy = _claims.Linsence == null ? _claims.ApiUserId.ToString() : "6e864cbc-5252-11ec-8681-fa163e02b3e4";
  110. content.D_LastUpdatedOn = DateTime.Now;
  111. content.C_Modifier = _claims.ApiUserName;
  112. _mapper.Map(updateModel, content, typeof(DevCmdViewModel), typeof(TDEV_Cmd));
  113. _repository.Update(content);
  114. var result = await _repository.SaveAsync();
  115. if (!result)
  116. {
  117. throw new Exception("更新失败");
  118. }
  119. }
  120. public async Task<IEnumerable<DevCmdViewModel>> GetConditionAsync(DevCmdSearchModel searchModel)
  121. {
  122. var dtoList = await _repository.GetConditionAsync(searchModel);
  123. return dtoList;
  124. }
  125. public Task<bool> IsExistAsync(string id)
  126. {
  127. throw new NotImplementedException();
  128. }
  129. public Task<int> UpdateOneAsync(DevCmdViewModel viewModel, params string[] fields)
  130. {
  131. throw new NotImplementedException();
  132. }
  133. #endregion
  134. #region 指令
  135. //新增
  136. public async Task InstructionCreateOneAsync(DevInstructionViewModel viewModel)
  137. {
  138. var id = Guid.NewGuid().ToString();
  139. var content = _mapper.Map<TDEV_Instruction>(viewModel);
  140. content.C_ID = id;
  141. content.C_CreateBy = _claims.ApiUserId.ToString();
  142. content.D_CreateOn = DateTime.Now;
  143. content.C_Creator = _claims.ApiUserName;
  144. content.C_Status = "1";
  145. _InstructionRepository.Create(content);
  146. var result = await _InstructionRepository.SaveAsync();
  147. if (!result)
  148. {
  149. throw new Exception("创建失败");
  150. }
  151. }
  152. //删除
  153. public async Task InstructionDeleteAsync(string id,bool bol=false)
  154. {
  155. var content = await _InstructionRepository.GetByIdAsync(id);
  156. if (content == null)
  157. {
  158. throw new Exception("数据库中没有此数据");
  159. }
  160. if (bol)
  161. {
  162. var data = await _CmdInstructionRepository.GetByConditionAsync(t => t.C_Instruction == id);
  163. if (data != null&&data.Count()>0)
  164. {
  165. throw new Exception("该数据已被关联,无法删除");
  166. }
  167. else
  168. {
  169. _InstructionRepository.Delete(content);
  170. }
  171. }
  172. else
  173. {
  174. content.C_LastUpdatedBy = _claims.ApiUserId.ToString();
  175. content.D_LastUpdatedOn = DateTime.Now;
  176. content.C_Modifier = _claims.ApiUserName;
  177. content.C_Status = "0";
  178. _InstructionRepository.Update(content);
  179. }
  180. var result = await _InstructionRepository.SaveAsync();
  181. if (!result)
  182. {
  183. throw new Exception("删除失败");
  184. }
  185. }
  186. //根据ID获取实体
  187. public async Task<DevInstructionViewModel> GetInstructionByIdAsync(string id)
  188. {
  189. DevInstructionSearchModel searchModel = new DevInstructionSearchModel();
  190. searchModel.C_ID = id;
  191. searchModel.IsPagination = false;
  192. var items = await _InstructionRepository.GetConditionAsync(searchModel);
  193. var content = items.FirstOrDefault();
  194. return content;
  195. }
  196. // 更新
  197. public async Task InstructionUpdateAsync(string code, DevInstructionViewModel updateModel)
  198. {
  199. TDEV_Instruction content = await _InstructionRepository.GetByIdAsync(code);
  200. if (content == null)
  201. {
  202. throw new Exception("没有此数据");
  203. }
  204. content.C_LastUpdatedBy = _claims.Linsence == null ? _claims.ApiUserId.ToString() : "6e864cbc-5252-11ec-8681-fa163e02b3e4";
  205. content.D_LastUpdatedOn = DateTime.Now;
  206. content.C_Modifier = _claims.ApiUserName;
  207. _mapper.Map(updateModel, content, typeof(DevInstructionViewModel), typeof(TDEV_Instruction));
  208. _InstructionRepository.Update(content);
  209. var result = await _InstructionRepository.SaveAsync();
  210. if (!result)
  211. {
  212. throw new Exception("更新失败");
  213. }
  214. }
  215. public async Task<IEnumerable<DevInstructionViewModel>> GetInstructionConditionAsync(DevInstructionSearchModel searchModel)
  216. {
  217. var dtoList = await _InstructionRepository.GetConditionAsync(searchModel);
  218. return dtoList;
  219. }
  220. public async Task<List<DevInstructionInfo>> GetInstructionByCmdCode(DevInstructionSearchModel searchModel)
  221. {
  222. var dtoList = await _InstructionRepository.GetInstructionByCmdCode(searchModel);
  223. return dtoList;
  224. }
  225. #endregion
  226. #region 命令-指令
  227. //新增
  228. public async Task CmdInstructionCreateOneAsync(DevCmdInstructionViewModel viewModel)
  229. {
  230. var id = Guid.NewGuid().ToString();
  231. var content = _mapper.Map<TDEV_CmdInstruction>(viewModel);
  232. content.C_ID = id;
  233. content.C_CreateBy = _claims.ApiUserId.ToString();
  234. content.D_CreateOn = DateTime.Now;
  235. content.C_Creator = _claims.ApiUserName;
  236. _CmdInstructionRepository.Create(content);
  237. var result = await _CmdInstructionRepository.SaveAsync();
  238. if (!result)
  239. {
  240. throw new Exception("创建失败");
  241. }
  242. }
  243. //删除
  244. public async Task CmdInstructionDeleteAsync(string id)
  245. {
  246. var content = await _CmdInstructionRepository.GetByIdAsync(id);
  247. if (content == null)
  248. {
  249. throw new Exception("数据库中没有此数据");
  250. }
  251. _CmdInstructionRepository.Delete(content);
  252. var result = await _CmdInstructionRepository.SaveAsync();
  253. if (!result)
  254. {
  255. throw new Exception("删除失败");
  256. }
  257. }
  258. //根据ID获取实体
  259. public async Task<DevCmdInstructionViewModel> GetCmdInstructionByIdAsync(string id)
  260. {
  261. DevCmdInstructionSearchModel searchModel = new DevCmdInstructionSearchModel();
  262. searchModel.C_ID = id;
  263. searchModel.IsPagination = false;
  264. var items = await _CmdInstructionRepository.GetConditionAsync(searchModel);
  265. var content = items.FirstOrDefault();
  266. return content;
  267. }
  268. // 更新
  269. public async Task CmdInstructionUpdateAsync(string code, DevCmdInstructionViewModel updateModel)
  270. {
  271. TDEV_CmdInstruction content = await _CmdInstructionRepository.GetByIdAsync(code);
  272. if (content == null)
  273. {
  274. throw new Exception("没有此数据");
  275. }
  276. _mapper.Map(updateModel, content, typeof(DevCmdInstructionViewModel), typeof(TDEV_CmdInstruction));
  277. _CmdInstructionRepository.Update(content);
  278. var result = await _CmdInstructionRepository.SaveAsync();
  279. if (!result)
  280. {
  281. throw new Exception("更新失败");
  282. }
  283. }
  284. public async Task<IEnumerable<DevCmdInstructionViewModel>> GetCmdInstructionConditionAsync(DevCmdInstructionSearchModel searchModel)
  285. {
  286. var dtoList = await _CmdInstructionRepository.GetConditionAsync(searchModel);
  287. return dtoList;
  288. }
  289. #endregion
  290. }
  291. }