TaicAIBoxTemplateService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 result = await _taicAIBoxRepository.GetList(searchModel);
  119. //var predicate = PredicateBuilder.New<TaicAibox>(x=>x.CStoreCode==searchModel.CStoreCode);//查询条件,推荐后台使用这种方式灵活筛选
  120. //#region 添加条件查询
  121. //if (!string.IsNullOrEmpty(searchModel.Name))
  122. //{
  123. // predicate = predicate.And(i => i.CName.Contains(searchModel.Name));
  124. //}
  125. //if (searchModel.CStatus.HasValue)
  126. //{
  127. // predicate = predicate.And(i => i.CStatus == searchModel.CStatus);
  128. //}
  129. //#endregion
  130. //var pageData = await _taicAIBoxRepository.GetPageAsync(predicate, "I_Sort", true, searchModel.PageIndex, searchModel.PageSize);
  131. //searchModel.TotalCount = pageData.Totals;
  132. //var result = _mapper.Map<List<TaicAibox>, List<AiBoxModel>>(pageData.Rows);
  133. return result.ToList();
  134. }
  135. public async Task<AiBoxModel> GetAiBox(string id)
  136. {
  137. var aibox =await _taicAIBoxRepository.GetByIdAsync(id);
  138. var result = _mapper.Map<AiBoxModel>(aibox);
  139. return result;
  140. }
  141. public async Task<bool> AddAiBox(AiBoxModel aiBoxModel)
  142. {
  143. var taicAibox = _mapper.Map<TaicAibox>(aiBoxModel);
  144. taicAibox.CId = Guid.NewGuid().ToString();
  145. taicAibox.CCreateBy = _claims.ApiUserId.ToString();
  146. _taicAIBoxRepository.Create(taicAibox);
  147. var result = await _taicAIBoxRepository.SaveAsync();
  148. if (!result)
  149. {
  150. throw new Exception("创建失败");
  151. }
  152. return result;
  153. }
  154. public async Task<bool> DelAiBox(string id)
  155. {
  156. var taicAibox = await _taicAIBoxRepository.GetByIdAsync(id);
  157. if (taicAibox == null)
  158. {
  159. throw new Exception("数据库中没有此数据");
  160. }
  161. taicAibox.CStatus = 0;
  162. taicAibox.DLastUpdatedOn = DateTime.Now;
  163. taicAibox.CLastUpdatedBy = _claims.ApiUserId.ToString();
  164. _taicAIBoxRepository.Update(taicAibox);
  165. var result = await _taicAIBoxRepository.SaveAsync();
  166. if (!result)
  167. {
  168. throw new Exception("删除失败");
  169. }
  170. return result;
  171. }
  172. public async Task<bool> UpdateAiBox(string id, AiBoxModel aiBoxModel)
  173. {
  174. var taicAibox = await _taicAIBoxRepository.GetByIdAsync(id);
  175. if (taicAibox == null)
  176. {
  177. throw new Exception("数据库中没有此数据");
  178. }
  179. _mapper.Map(aiBoxModel, taicAibox, typeof(AiBoxModel), typeof(TaicAibox));
  180. _taicAIBoxRepository.Update(taicAibox);
  181. var result = await _taicAIBoxRepository.SaveAsync();
  182. if (!result)
  183. {
  184. throw new Exception("更新失败");
  185. }
  186. return result;
  187. }
  188. //添加盒子摄像头关联
  189. public async Task<bool> AddDevAIBox(DevAiboxModel devAiboxModel)
  190. {
  191. List<TaicDevAibox> devAiboxes = new ();
  192. devAiboxModel.CCameraCode.ForEach(x =>
  193. {
  194. devAiboxes.Add(new TaicDevAibox
  195. {
  196. CId = Guid.NewGuid().ToString(),
  197. CAiboxCode = devAiboxModel.CAiboxCode,
  198. CCameraCode = x,
  199. CCreateBy = _claims.ApiUserId.ToString(),
  200. CCreator = _claims.ApiUserName,
  201. DCreateOn = DateTime.Now,
  202. });
  203. });
  204. var dels = await _taicAIBoxRepository.DbContext.TaicDevAiboxes.Where(x => x.CAiboxCode == devAiboxModel.CAiboxCode).ToListAsync();
  205. _taicAIBoxRepository.DbContext.TaicDevAiboxes.RemoveRange(dels);
  206. await _taicAIBoxRepository.DbContext.TaicDevAiboxes.AddRangeAsync(devAiboxes);
  207. var row =await _taicAIBoxRepository.SaveAsync();
  208. return row;
  209. }
  210. //获取盒子摄像头关联
  211. public async Task<object> GetDevAibox(string aiboxCode)
  212. {
  213. var queryAble= from tda in _taicAIBoxRepository.DbContext.TaicDevAiboxes
  214. join aibox in _taicAIBoxRepository.DbContext.TaicAiboxes on tda.CAiboxCode equals aibox.CId
  215. join camera in _taicAIBoxRepository.DbContext.TVMC_Camera on tda.CCameraCode equals camera.C_ID
  216. where tda.CAiboxCode == aiboxCode
  217. select new
  218. {
  219. tda.CId,
  220. camera.C_Name,
  221. camera.C_CameraNo,
  222. tda.DCreateOn
  223. };
  224. var result=await queryAble.ToListAsync();
  225. return result;
  226. }
  227. //删除盒子摄像头关联
  228. public async Task<bool> DelDevAibox(string id)
  229. {
  230. var devAibox=_taicAIBoxRepository.DbContext.TaicDevAiboxes.Where(x => x.CId == id).FirstOrDefault();
  231. _taicAIBoxRepository.DbContext.TaicDevAiboxes.Remove(devAibox);
  232. var row = await _taicAIBoxRepository.SaveAsync();
  233. return row;
  234. }
  235. //迁移盒子
  236. public async Task<bool> AddAIBoxMigrate(AddAIBoxMigrateModel addModel)
  237. {
  238. var aiboxMigrate = _mapper.Map<TaicAiboxMigrate>(addModel);
  239. aiboxMigrate.CId = Guid.NewGuid().ToString();
  240. aiboxMigrate.CCreateBy = _claims.ApiUserId.ToString();
  241. aiboxMigrate.DCreateOn = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss");
  242. var taicAibox = await _taicAIBoxRepository.GetByIdAsync(addModel.CAiboxCode);
  243. if (taicAibox == null)
  244. {
  245. throw new Exception("数据库中没有此数据");
  246. }
  247. taicAibox.CStoreCode = addModel.CCurrentStoreCode;
  248. taicAibox.DLastUpdatedOn = DateTime.Now;
  249. taicAibox.CLastUpdatedBy = _claims.ApiUserId.ToString();
  250. _taicAIBoxRepository.Update(taicAibox);
  251. _taicAIBoxMigrateRepository.Create(aiboxMigrate);
  252. var result = await _taicAIBoxMigrateRepository.SaveAsync();
  253. if (!result)
  254. {
  255. throw new Exception("迁移失败");
  256. }
  257. return result;
  258. }
  259. //获取迁移盒子列表
  260. public async Task<object> GetAIBoxMigrate(string aiboxCode)
  261. {
  262. var queryAble = from tda in _taicAIBoxMigrateRepository.DbContext.TaicAiboxMigrates
  263. join aibox in _taicAIBoxRepository.DbContext.TaicAiboxes on tda.CAiboxCode equals aibox.CId
  264. join store1 in _taicAIBoxRepository.DbContext.TPNT_Store on tda.CLastStoreCode equals store1.C_Code
  265. join store2 in _taicAIBoxRepository.DbContext.TPNT_Store on tda.CCurrentStoreCode equals store2.C_Code
  266. where tda.CAiboxCode == aiboxCode
  267. select new
  268. {
  269. tda.CId,
  270. tda.CName,
  271. tda.CRemark,
  272. aiboxName = aibox.CName,
  273. CLastStoreName = store1.C_Name,
  274. CCurrentStoreName = store2.C_Name,
  275. tda.DCreateOn
  276. };
  277. var result = await queryAble.ToListAsync();
  278. return result;
  279. }
  280. #endregion
  281. }
  282. }