TaicAIBoxTemplateService.cs 13 KB

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