TaicAIBoxTemplateService.cs 13 KB

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