TaicAIBoxTemplateService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using AutoMapper;
  2. using LinqKit;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.EntityFrameworkCore.Internal;
  5. using Ropin.Inspection.Common.Accessor.Interface;
  6. using Ropin.Inspection.Model;
  7. using Ropin.Inspection.Model.Entities;
  8. using Ropin.Inspection.Model.ViewModel;
  9. using Ropin.Inspection.Repository.TAIC;
  10. using Ropin.Inspection.Repository.TAIC.Interface;
  11. using Ropin.Inspection.Service.TAIC.Interface;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Diagnostics.SymbolStore;
  15. using System.Linq;
  16. using System.Threading.Tasks;
  17. namespace Ropin.Inspection.Service.TAIC
  18. {
  19. public class TaicAIBoxTemplateService : ITaicAIBoxTemplateService
  20. {
  21. private readonly ITaicAIBoxTemplateRepository _taicAIBoxTemplateRepository;
  22. private readonly ITaicAIBoxRepository _taicAIBoxRepository;
  23. private readonly ITaicAIBoxMigrateRepository _taicAIBoxMigrateRepository;
  24. private readonly IMapper _mapper;
  25. private readonly IClaimsAccessor _claims;
  26. public TaicAIBoxTemplateService(ITaicAIBoxTemplateRepository taicAIBoxTemplateRepository,IMapper mapper , IClaimsAccessor claims, ITaicAIBoxRepository taicAIBoxRepository, ITaicAIBoxMigrateRepository taicAIBoxMigrateRepository)
  27. {
  28. _taicAIBoxTemplateRepository = taicAIBoxTemplateRepository;
  29. _mapper = mapper;
  30. _claims = claims;
  31. _taicAIBoxRepository = taicAIBoxRepository;
  32. _taicAIBoxMigrateRepository = taicAIBoxMigrateRepository;
  33. }
  34. #region 盒子模板维护
  35. public async Task<List<TaicTemplateModel>> GetTemplatePage(TaicTemplateSearchModel searchModel)
  36. {
  37. var predicate = PredicateBuilder.New<TaicAiboxTemplate>(true);//查询条件,推荐后台使用这种方式灵活筛选
  38. #region 添加条件查询
  39. if (!string.IsNullOrEmpty(searchModel.Name))
  40. {
  41. predicate = predicate.And(i => i.CName.Contains(searchModel.Name));
  42. }
  43. #endregion
  44. var pageData = await _taicAIBoxTemplateRepository.GetPageAsync(predicate, "I_Sort", true, searchModel.PageIndex, searchModel.PageSize);
  45. searchModel.TotalCount = pageData.Totals;
  46. var result = _mapper.Map<List<TaicAiboxTemplate>, List<TaicTemplateModel>>(pageData.Rows);
  47. return result;
  48. }
  49. public async Task<TaicTemplateModel> GetTemplate(string id )
  50. {
  51. var template = await _taicAIBoxTemplateRepository.GetByIdAsync(id);
  52. var result = _mapper.Map<TaicTemplateModel>(template);
  53. return result;
  54. }
  55. public async Task<bool> CreateOneAsync(TaicTemplateModel viewModel)
  56. {
  57. var aiboxTemplate = _mapper.Map<TaicAiboxTemplate>(viewModel);
  58. aiboxTemplate.CId = Guid.NewGuid().ToString();
  59. aiboxTemplate.CCreateBy = _claims.ApiUserId.ToString();
  60. _taicAIBoxTemplateRepository.Create(aiboxTemplate);
  61. var result = await _taicAIBoxTemplateRepository.SaveAsync();
  62. if (!result)
  63. {
  64. throw new Exception("创建失败");
  65. }
  66. return result;
  67. }
  68. public Task<int> UpdateOneAsync(TaicTemplateModel viewModel, params string[] fields)
  69. {
  70. throw new NotImplementedException();
  71. }
  72. public Task<TaicTemplateModel> GetByIdAsync(string id)
  73. {
  74. throw new NotImplementedException();
  75. }
  76. public Task<bool> IsExistAsync(string id)
  77. {
  78. throw new NotImplementedException();
  79. }
  80. public async Task<bool> DeleteAsync(string id)
  81. {
  82. var aiboxTemplate = await _taicAIBoxTemplateRepository.GetByIdAsync(id);
  83. if (aiboxTemplate == null)
  84. {
  85. throw new Exception("数据库中没有此数据");
  86. }
  87. aiboxTemplate.CStatus = 0;
  88. aiboxTemplate.DLastUpdatedOn = DateTime.Now;
  89. aiboxTemplate.CLastUpdatedBy = _claims.ApiUserId.ToString();
  90. _taicAIBoxTemplateRepository.Update(aiboxTemplate);
  91. var result = await _taicAIBoxTemplateRepository.SaveAsync();
  92. if (!result)
  93. {
  94. throw new Exception("删除失败");
  95. }
  96. return result;
  97. }
  98. public async Task<bool> UpdateTemplate(string id, TaicTemplateModel templateModel)
  99. {
  100. var aiboxTemplate = await _taicAIBoxTemplateRepository.GetByIdAsync(id);
  101. if (aiboxTemplate == null)
  102. {
  103. throw new Exception("数据库中没有此数据");
  104. }
  105. _mapper.Map(templateModel, aiboxTemplate, typeof(TaicTemplateModel), typeof(TaicAiboxTemplate));
  106. _taicAIBoxTemplateRepository.Update(aiboxTemplate);
  107. var result = await _taicAIBoxTemplateRepository.SaveAsync();
  108. if (!result)
  109. {
  110. throw new Exception("更新失败");
  111. }
  112. return result;
  113. }
  114. #endregion
  115. #region ai盒子维护
  116. public async Task<List<AiBoxModel>> GetAiBoxPage(AiBoxSearchModel searchModel)
  117. {
  118. var predicate = PredicateBuilder.New<TaicAibox>(x=>x.CStoreCode==searchModel.CStoreCode);//查询条件,推荐后台使用这种方式灵活筛选
  119. #region 添加条件查询
  120. if (!string.IsNullOrEmpty(searchModel.Name))
  121. {
  122. predicate = predicate.And(i => i.CName.Contains(searchModel.Name));
  123. }
  124. if (searchModel.CStatus.HasValue)
  125. {
  126. predicate = predicate.And(i => i.CStatus == searchModel.CStatus);
  127. }
  128. #endregion
  129. var pageData = await _taicAIBoxRepository.GetPageAsync(predicate, "I_Sort", true, searchModel.PageIndex, searchModel.PageSize);
  130. searchModel.TotalCount = pageData.Totals;
  131. var result = _mapper.Map<List<TaicAibox>, List<AiBoxModel>>(pageData.Rows);
  132. return result;
  133. }
  134. public async Task<AiBoxModel> GetAiBox(string id)
  135. {
  136. var aibox =await _taicAIBoxRepository.GetByIdAsync(id);
  137. var result = _mapper.Map<AiBoxModel>(aibox);
  138. return result;
  139. }
  140. public async Task<bool> AddAiBox(AiBoxModel aiBoxModel)
  141. {
  142. var taicAibox = _mapper.Map<TaicAibox>(aiBoxModel);
  143. taicAibox.CId = Guid.NewGuid().ToString();
  144. taicAibox.CCreateBy = _claims.ApiUserId.ToString();
  145. _taicAIBoxRepository.Create(taicAibox);
  146. var result = await _taicAIBoxRepository.SaveAsync();
  147. if (!result)
  148. {
  149. throw new Exception("创建失败");
  150. }
  151. return result;
  152. }
  153. public async Task<bool> DelAiBox(string id)
  154. {
  155. var taicAibox = await _taicAIBoxRepository.GetByIdAsync(id);
  156. if (taicAibox == null)
  157. {
  158. throw new Exception("数据库中没有此数据");
  159. }
  160. taicAibox.CStatus = 0;
  161. taicAibox.DLastUpdatedOn = DateTime.Now;
  162. taicAibox.CLastUpdatedBy = _claims.ApiUserId.ToString();
  163. _taicAIBoxRepository.Update(taicAibox);
  164. var result = await _taicAIBoxRepository.SaveAsync();
  165. if (!result)
  166. {
  167. throw new Exception("删除失败");
  168. }
  169. return result;
  170. }
  171. public async Task<bool> UpdateAiBox(string id, AiBoxModel aiBoxModel)
  172. {
  173. var taicAibox = await _taicAIBoxRepository.GetByIdAsync(id);
  174. if (taicAibox == null)
  175. {
  176. throw new Exception("数据库中没有此数据");
  177. }
  178. _mapper.Map(aiBoxModel, taicAibox, typeof(AiBoxModel), typeof(TaicAibox));
  179. _taicAIBoxRepository.Update(taicAibox);
  180. var result = await _taicAIBoxRepository.SaveAsync();
  181. if (!result)
  182. {
  183. throw new Exception("更新失败");
  184. }
  185. return result;
  186. }
  187. //添加盒子摄像头关联
  188. public async Task<bool> AddDevAIBox(DevAiboxModel devAiboxModel)
  189. {
  190. List<TaicDevAibox> devAiboxes = new ();
  191. devAiboxModel.CCameraCode.ForEach(x =>
  192. {
  193. devAiboxes.Add(new TaicDevAibox
  194. {
  195. CId = Guid.NewGuid().ToString(),
  196. CAiboxCode = devAiboxModel.CAiboxCode,
  197. CCameraCode = x,
  198. CCreateBy = _claims.ApiUserId.ToString(),
  199. CCreator = _claims.ApiUserName,
  200. DCreateOn = DateTime.Now,
  201. });
  202. });
  203. var dels = await _taicAIBoxRepository.DbContext.TaicDevAiboxes.Where(x => x.CAiboxCode == devAiboxModel.CAiboxCode).ToListAsync();
  204. _taicAIBoxRepository.DbContext.TaicDevAiboxes.RemoveRange(dels);
  205. await _taicAIBoxRepository.DbContext.TaicDevAiboxes.AddRangeAsync(devAiboxes);
  206. var row =await _taicAIBoxRepository.SaveAsync();
  207. return row;
  208. }
  209. //获取盒子摄像头关联
  210. public async Task<object> GetDevAibox(string aiboxCode)
  211. {
  212. var queryAble= from tda in _taicAIBoxRepository.DbContext.TaicDevAiboxes
  213. join aibox in _taicAIBoxRepository.DbContext.TaicAiboxes on tda.CAiboxCode equals aibox.CId
  214. join camera in _taicAIBoxRepository.DbContext.TVMC_Camera on tda.CCameraCode equals camera.C_ID
  215. where tda.CAiboxCode == aiboxCode
  216. select new
  217. {
  218. tda.CId,
  219. camera.C_Name,
  220. camera.C_CameraNo,
  221. tda.DCreateOn
  222. };
  223. var result=await queryAble.ToListAsync();
  224. return result;
  225. }
  226. //删除盒子摄像头关联
  227. public async Task<bool> DelDevAibox(string id)
  228. {
  229. var devAibox=_taicAIBoxRepository.DbContext.TaicDevAiboxes.Where(x => x.CId == id).FirstOrDefault();
  230. _taicAIBoxRepository.DbContext.TaicDevAiboxes.Remove(devAibox);
  231. var row = await _taicAIBoxRepository.SaveAsync();
  232. return row;
  233. }
  234. //迁移盒子
  235. public async Task<bool> AddAIBoxMigrate(AddAIBoxMigrateModel addModel)
  236. {
  237. var aiboxMigrate = _mapper.Map<TaicAiboxMigrate>(addModel);
  238. aiboxMigrate.CId = Guid.NewGuid().ToString();
  239. aiboxMigrate.CCreateBy = _claims.ApiUserId.ToString();
  240. aiboxMigrate.DCreateOn = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss");
  241. var taicAibox = await _taicAIBoxRepository.GetByIdAsync(addModel.CAiboxCode);
  242. if (taicAibox == null)
  243. {
  244. throw new Exception("数据库中没有此数据");
  245. }
  246. taicAibox.CStoreCode = addModel.CCurrentStoreCode;
  247. taicAibox.DLastUpdatedOn = DateTime.Now;
  248. taicAibox.CLastUpdatedBy = _claims.ApiUserId.ToString();
  249. _taicAIBoxRepository.Update(taicAibox);
  250. _taicAIBoxMigrateRepository.Create(aiboxMigrate);
  251. var result = await _taicAIBoxMigrateRepository.SaveAsync();
  252. if (!result)
  253. {
  254. throw new Exception("迁移失败");
  255. }
  256. return result;
  257. }
  258. //获取迁移盒子列表
  259. public async Task<object> GetAIBoxMigrate(string aiboxCode)
  260. {
  261. var queryAble = from tda in _taicAIBoxMigrateRepository.DbContext.TaicAiboxMigrates
  262. join aibox in _taicAIBoxRepository.DbContext.TaicAiboxes on tda.CAiboxCode equals aibox.CId
  263. join store1 in _taicAIBoxRepository.DbContext.TPNT_Store on tda.CLastStoreCode equals store1.C_Code
  264. join store2 in _taicAIBoxRepository.DbContext.TPNT_Store on tda.CCurrentStoreCode equals store2.C_Code
  265. where tda.CAiboxCode == aiboxCode
  266. select new
  267. {
  268. tda.CId,
  269. tda.CName,
  270. tda.CRemark,
  271. aiboxName = aibox.CName,
  272. CLastStoreName = store1.C_Name,
  273. CCurrentStoreName = store2.C_Name,
  274. tda.DCreateOn
  275. };
  276. var result = await queryAble.ToListAsync();
  277. return result;
  278. }
  279. #endregion
  280. }
  281. }