using AutoMapper; using LinqKit; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Internal; using Ropin.Inspection.Common.Accessor.Interface; using Ropin.Inspection.Model; using Ropin.Inspection.Model.Entities; using Ropin.Inspection.Model.ViewModel; using Ropin.Inspection.Repository.TAIC; using Ropin.Inspection.Repository.TAIC.Interface; using Ropin.Inspection.Service.TAIC.Interface; using System; using System.Collections.Generic; using System.Diagnostics.SymbolStore; using System.Linq; using System.Threading.Tasks; namespace Ropin.Inspection.Service.TAIC { public class TaicAIBoxTemplateService : ITaicAIBoxTemplateService { private readonly ITaicAIBoxTemplateRepository _taicAIBoxTemplateRepository; private readonly ITaicAIBoxRepository _taicAIBoxRepository; private readonly ITaicAIBoxMigrateRepository _taicAIBoxMigrateRepository; private readonly IMapper _mapper; private readonly IClaimsAccessor _claims; public TaicAIBoxTemplateService(ITaicAIBoxTemplateRepository taicAIBoxTemplateRepository,IMapper mapper , IClaimsAccessor claims, ITaicAIBoxRepository taicAIBoxRepository, ITaicAIBoxMigrateRepository taicAIBoxMigrateRepository) { _taicAIBoxTemplateRepository = taicAIBoxTemplateRepository; _mapper = mapper; _claims = claims; _taicAIBoxRepository = taicAIBoxRepository; _taicAIBoxMigrateRepository = taicAIBoxMigrateRepository; } #region 盒子模板维护 public async Task> GetTemplatePage(TaicTemplateSearchModel searchModel) { var predicate = PredicateBuilder.New(true);//查询条件,推荐后台使用这种方式灵活筛选 #region 添加条件查询 if (!string.IsNullOrEmpty(searchModel.Name)) { predicate = predicate.And(i => i.CName.Contains(searchModel.Name)); } #endregion var pageData = await _taicAIBoxTemplateRepository.GetPageAsync(predicate, "I_Sort", true, searchModel.PageIndex, searchModel.PageSize); searchModel.TotalCount = pageData.Totals; var result = _mapper.Map, List>(pageData.Rows); return result; } public async Task GetTemplate(string id ) { var template = await _taicAIBoxTemplateRepository.GetByIdAsync(id); var result = _mapper.Map(template); return result; } public async Task CreateOneAsync(TaicTemplateModel viewModel) { var aiboxTemplate = _mapper.Map(viewModel); aiboxTemplate.CId = Guid.NewGuid().ToString(); aiboxTemplate.CCreateBy = _claims.ApiUserId.ToString(); _taicAIBoxTemplateRepository.Create(aiboxTemplate); var result = await _taicAIBoxTemplateRepository.SaveAsync(); if (!result) { throw new Exception("创建失败"); } return result; } public Task UpdateOneAsync(TaicTemplateModel viewModel, params string[] fields) { throw new NotImplementedException(); } public Task GetByIdAsync(string id) { throw new NotImplementedException(); } public Task IsExistAsync(string id) { throw new NotImplementedException(); } public async Task DeleteAsync(string id) { var aiboxTemplate = await _taicAIBoxTemplateRepository.GetByIdAsync(id); if (aiboxTemplate == null) { throw new Exception("数据库中没有此数据"); } aiboxTemplate.CStatus = 0; aiboxTemplate.DLastUpdatedOn = DateTime.Now; aiboxTemplate.CLastUpdatedBy = _claims.ApiUserId.ToString(); _taicAIBoxTemplateRepository.Update(aiboxTemplate); var result = await _taicAIBoxTemplateRepository.SaveAsync(); if (!result) { throw new Exception("删除失败"); } return result; } public async Task UpdateTemplate(string id, TaicTemplateModel templateModel) { var aiboxTemplate = await _taicAIBoxTemplateRepository.GetByIdAsync(id); if (aiboxTemplate == null) { throw new Exception("数据库中没有此数据"); } _mapper.Map(templateModel, aiboxTemplate, typeof(TaicTemplateModel), typeof(TaicAiboxTemplate)); _taicAIBoxTemplateRepository.Update(aiboxTemplate); var result = await _taicAIBoxTemplateRepository.SaveAsync(); if (!result) { throw new Exception("更新失败"); } return result; } #endregion #region ai盒子维护 public async Task> GetAiBoxPage(AiBoxSearchModel searchModel) { var result = await _taicAIBoxRepository.GetList(searchModel); //var predicate = PredicateBuilder.New(x=>x.CStoreCode==searchModel.CStoreCode);//查询条件,推荐后台使用这种方式灵活筛选 //#region 添加条件查询 //if (!string.IsNullOrEmpty(searchModel.Name)) //{ // predicate = predicate.And(i => i.CName.Contains(searchModel.Name)); //} //if (searchModel.CStatus.HasValue) //{ // predicate = predicate.And(i => i.CStatus == searchModel.CStatus); //} //#endregion //var pageData = await _taicAIBoxRepository.GetPageAsync(predicate, "I_Sort", true, searchModel.PageIndex, searchModel.PageSize); //searchModel.TotalCount = pageData.Totals; //var result = _mapper.Map, List>(pageData.Rows); return result.ToList(); } public async Task GetAiBox(string id) { var aibox =await _taicAIBoxRepository.GetByIdAsync(id); var result = _mapper.Map(aibox); return result; } public async Task AddAiBox(AiBoxModel aiBoxModel) { var taicAibox = _mapper.Map(aiBoxModel); taicAibox.CId = Guid.NewGuid().ToString(); taicAibox.CCreateBy = _claims.ApiUserId.ToString(); _taicAIBoxRepository.Create(taicAibox); var result = await _taicAIBoxRepository.SaveAsync(); if (!result) { throw new Exception("创建失败"); } return result; } public async Task DelAiBox(string id) { var taicAibox = await _taicAIBoxRepository.GetByIdAsync(id); if (taicAibox == null) { throw new Exception("数据库中没有此数据"); } taicAibox.CStatus = 0; taicAibox.DLastUpdatedOn = DateTime.Now; taicAibox.CLastUpdatedBy = _claims.ApiUserId.ToString(); _taicAIBoxRepository.Update(taicAibox); var result = await _taicAIBoxRepository.SaveAsync(); if (!result) { throw new Exception("删除失败"); } return result; } public async Task UpdateAiBox(string id, AiBoxModel aiBoxModel) { var taicAibox = await _taicAIBoxRepository.GetByIdAsync(id); if (taicAibox == null) { throw new Exception("数据库中没有此数据"); } _mapper.Map(aiBoxModel, taicAibox, typeof(AiBoxModel), typeof(TaicAibox)); _taicAIBoxRepository.Update(taicAibox); var result = await _taicAIBoxRepository.SaveAsync(); if (!result) { throw new Exception("更新失败"); } return result; } //添加盒子摄像头关联 public async Task AddDevAIBox(DevAiboxModel devAiboxModel) { List devAiboxes = new (); devAiboxModel.CCameraCode.ForEach(x => { devAiboxes.Add(new TaicDevAibox { CId = Guid.NewGuid().ToString(), CAiboxCode = devAiboxModel.CAiboxCode, CCameraCode = x, CCreateBy = _claims.ApiUserId.ToString(), CCreator = _claims.ApiUserName, DCreateOn = DateTime.Now, }); }); var dels = await _taicAIBoxRepository.DbContext.TaicDevAiboxes.Where(x => x.CAiboxCode == devAiboxModel.CAiboxCode).ToListAsync(); _taicAIBoxRepository.DbContext.TaicDevAiboxes.RemoveRange(dels); await _taicAIBoxRepository.DbContext.TaicDevAiboxes.AddRangeAsync(devAiboxes); var row =await _taicAIBoxRepository.SaveAsync(); return row; } //获取盒子摄像头关联 public async Task GetDevAibox(string aiboxCode) { var queryAble= from tda in _taicAIBoxRepository.DbContext.TaicDevAiboxes join aibox in _taicAIBoxRepository.DbContext.TaicAiboxes on tda.CAiboxCode equals aibox.CId join camera in _taicAIBoxRepository.DbContext.TVMC_Camera on tda.CCameraCode equals camera.C_ID where tda.CAiboxCode == aiboxCode select new { tda.CId, camera.C_Name, camera.C_CameraNo, tda.DCreateOn }; var result=await queryAble.ToListAsync(); return result; } //删除盒子摄像头关联 public async Task DelDevAibox(string id) { var devAibox=_taicAIBoxRepository.DbContext.TaicDevAiboxes.Where(x => x.CId == id).FirstOrDefault(); _taicAIBoxRepository.DbContext.TaicDevAiboxes.Remove(devAibox); var row = await _taicAIBoxRepository.SaveAsync(); return row; } //迁移盒子 public async Task AddAIBoxMigrate(AddAIBoxMigrateModel addModel) { var aiboxMigrate = _mapper.Map(addModel); aiboxMigrate.CId = Guid.NewGuid().ToString(); aiboxMigrate.CCreateBy = _claims.ApiUserId.ToString(); aiboxMigrate.DCreateOn = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"); var taicAibox = await _taicAIBoxRepository.GetByIdAsync(addModel.CAiboxCode); if (taicAibox == null) { throw new Exception("数据库中没有此数据"); } taicAibox.CStoreCode = addModel.CCurrentStoreCode; taicAibox.DLastUpdatedOn = DateTime.Now; taicAibox.CLastUpdatedBy = _claims.ApiUserId.ToString(); _taicAIBoxRepository.Update(taicAibox); _taicAIBoxMigrateRepository.Create(aiboxMigrate); var result = await _taicAIBoxMigrateRepository.SaveAsync(); if (!result) { throw new Exception("迁移失败"); } return result; } //获取迁移盒子列表 public async Task GetAIBoxMigrate(string aiboxCode) { var queryAble = from tda in _taicAIBoxMigrateRepository.DbContext.TaicAiboxMigrates join aibox in _taicAIBoxRepository.DbContext.TaicAiboxes on tda.CAiboxCode equals aibox.CId join store1 in _taicAIBoxRepository.DbContext.TPNT_Store on tda.CLastStoreCode equals store1.C_Code join store2 in _taicAIBoxRepository.DbContext.TPNT_Store on tda.CCurrentStoreCode equals store2.C_Code where tda.CAiboxCode == aiboxCode select new { tda.CId, tda.CName, tda.CRemark, aiboxName = aibox.CName, CLastStoreName = store1.C_Name, CCurrentStoreName = store2.C_Name, tda.DCreateOn }; var result = await queryAble.ToListAsync(); return result; } #endregion } }