using AutoMapper; using LinqKit; 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.Threading.Tasks; namespace Ropin.Inspection.Service.TAIC { public class TaicAIBoxTemplateService : ITaicAIBoxTemplateService { private readonly ITaicAIBoxTemplateRepository _taicAIBoxTemplateRepository; private readonly ITaicAIBoxRepository _taicAIBoxRepository; private readonly IMapper _mapper; private readonly IClaimsAccessor _claims; public TaicAIBoxTemplateService(ITaicAIBoxTemplateRepository taicAIBoxTemplateRepository,IMapper mapper , IClaimsAccessor claims, ITaicAIBoxRepository taicAIBoxRepository) { _taicAIBoxTemplateRepository = taicAIBoxTemplateRepository; _mapper = mapper; _claims = claims; _taicAIBoxRepository = taicAIBoxRepository; } #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 predicate = PredicateBuilder.New(true);//查询条件,推荐后台使用这种方式灵活筛选 #region 添加条件查询 if (!string.IsNullOrEmpty(searchModel.Name)) { predicate = predicate.And(i => i.CName.Contains(searchModel.Name)); } #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; } 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, }); }); await _taicAIBoxRepository.DbContext.TaicDevAiboxes.AddRangeAsync(devAiboxes); var row =await _taicAIBoxRepository.SaveAsync(); return row; } #endregion } }