using AutoMapper; using Ropin.Inspection.Common.Accessor.Interface; using Ropin.Inspection.Model.Entities; using Ropin.Inspection.Model.SearchModel.LGS; using Ropin.Inspection.Model.ViewModel.LGS; using Ropin.Inspection.Repository.LGS.Interface; using Ropin.Inspection.Service.LGS.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Ropin.Inspection.Service.LGS { public class LargeScreenControlService: ILargeScreenControlService { private readonly ILargeScreenControlRepository _repository; private readonly IMapper _mapper; private readonly IClaimsAccessor _claims; private readonly InspectionDbContext _sqlDBContext; public LargeScreenControlService(IClaimsAccessor claims, InspectionDbContext sqlDBContext, ILargeScreenControlRepository repository, IMapper mapper) { _repository = repository; _mapper = mapper; _claims = claims; _sqlDBContext = sqlDBContext; } public async Task> GetConditionAsync(LargeScreenControlSearch searchModel) { var list = await _repository.GetConditionAsync(searchModel); return list; } public async Task CreateOneAsync(LargeScreenControlViewModel viewModel) { var content = _mapper.Map(viewModel); content.C_ID = Guid.NewGuid().ToString(); content.C_CreateBy = _claims.ApiUserId.ToString(); content.C_Creator = _claims.ApiUserName; content.D_CreateOn = DateTime.Now; content.C_Status = "1"; _repository.Create(content); var result = await _repository.SaveAsync(); if (!result) { throw new Exception("创建失败"); } } public async Task UpdateAsync(LargeScreenControlViewModel viewModel, string Id) { var content = await _repository.GetByIdAsync(Id); if (content == null) { throw new Exception("没有此数据"); } _mapper.Map(viewModel, content, typeof(LargeScreenControlViewModel), typeof(TLGS_LargeScreenControl)); content.C_LastUpdatedBy = _claims.Linsence == null ? _claims.ApiUserId.ToString() : "6e864cbc-5252-11ec-8681-fa163e02b3e4"; content.D_LastUpdatedOn = DateTime.Now; content.C_Modifier = _claims.ApiUserName; _repository.Update(content); var result = await _repository.SaveAsync(); if (!result) { throw new Exception("更新失败"); } } public async Task SaveAsync(List viewModel) { try { foreach (var item in viewModel) { if (string.IsNullOrEmpty(item.C_ID)) { var content = _mapper.Map(item); content.C_ID = Guid.NewGuid().ToString(); content.C_CreateBy = _claims.ApiUserId.ToString(); content.C_Creator = _claims.ApiUserName; content.D_CreateOn = DateTime.Now; content.C_Status = "1"; _repository.Create(content); } else { var content = await _repository.GetByIdAsync(item.C_ID); if (content == null) { throw new Exception("没有此数据"); } _mapper.Map(item, content, typeof(LargeScreenControlViewModel), typeof(TLGS_LargeScreenControl)); content.C_LastUpdatedBy = _claims.Linsence == null ? _claims.ApiUserId.ToString() : "6e864cbc-5252-11ec-8681-fa163e02b3e4"; content.D_LastUpdatedOn = DateTime.Now; content.C_Modifier = _claims.ApiUserName; _repository.Update(content); } } var result = await _repository.SaveAsync(); if (!result) { throw new Exception("创建失败"); } } catch (Exception ex) { throw new Exception("创建异常"); } } public async Task DeleteAsync(string id) { var content = await _repository.GetByIdAsync(id); if (content == null) { throw new Exception("数据库中没有此数据"); } content.C_LastUpdatedBy = _claims.ApiUserId.ToString(); content.D_LastUpdatedOn = DateTime.Now; content.C_Modifier = _claims.ApiUserName; content.C_Status = "0"; _repository.Update(content); var result = await _repository.SaveAsync(); if (!result) { throw new Exception("删除失败"); } } public async Task DeleteByLargeScreenCodeAsync(string LargeScreenCode) { var content=await _repository.GetByConditionAsync(t=>t.C_LargeScreenCode == LargeScreenCode); if (content!=null&& content.Count()>0) { var result = await _repository.DeleteByLargeScreenCodeAsync(LargeScreenCode); if (!result) { throw new Exception("删除失败"); } } } public async Task GetByIdAsync(string id) { LargeScreenControlSearch searchModel = new LargeScreenControlSearch(); searchModel.IsPagination = false; searchModel.C_ID = id; var list = await _repository.GetConditionAsync(searchModel); return list?.FirstOrDefault(); } public Task IsExistAsync(string id) { throw new NotImplementedException(); } public Task UpdateOneAsync(LargeScreenControlViewModel viewModel, params string[] fields) { throw new NotImplementedException(); } } }