using LinqKit; using Microsoft.AspNetCore.Authentication; using Ropin.Inspection.Common.Accessor.Interface; using Ropin.Inspection.Model; using Ropin.Inspection.Model.Entities; using Ropin.Inspection.Model.ViewModel.LGS; using Ropin.Inspection.Repository.LGS; using Ropin.Inspection.Repository.LGS.Interface; using Ropin.Inspection.Service.LGS.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Net.NetworkInformation; using System.Security.Claims; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace Ropin.Inspection.Service.LGS { public class ImageLibraryService : IImageLibraryService { private readonly IImageLibraryRepository _imageLibraryRepository; private readonly IClaimsAccessor _claims; public ImageLibraryService(IImageLibraryRepository imageLibraryRepository, IClaimsAccessor claims) { _imageLibraryRepository=imageLibraryRepository; _claims = claims; } public async Task CreateOneAsync(ImageLibraryModel viewModel) { TLGS_ImageLibrary entity = new TLGS_ImageLibrary() { C_ID = viewModel.C_Id, C_Name = viewModel.C_Name, C_Type = viewModel.C_Type, C_Remark = viewModel.C_Remark, C_Status = viewModel.C_Status, I_Sort = viewModel.I_Sort, C_ImagePath = viewModel.C_ImagePath, C_CreateBy = _claims.ApiUserId.ToString(), C_Creator = _claims.ApiUserName, D_CreateOn = DateTime.Now, }; _imageLibraryRepository.Create(entity); var result = await _imageLibraryRepository.SaveAsync(); if (!result) { throw new Exception("创建失败"); } } public async Task DeleteAsync(string id) { await _imageLibraryRepository.DeleteAsync(id); } public async Task GetByIdAsync(string id) { var entity=await _imageLibraryRepository.GetByIdAsync(id); var model = new ImageLibraryModel { C_Id = entity.C_ID, C_Name = entity.C_Name, C_Type = entity.C_Type, C_Remark = entity.C_Remark, C_Status = entity.C_Status, I_Sort = entity.I_Sort, C_ImagePath = entity.C_ImagePath, }; return model; } public Task IsExistAsync(string id) { throw new NotImplementedException(); } public async Task> PageAsync(ImageLibraryInput input) { var predicate = PredicateBuilder.New(true);//查询条件,推荐后台使用这种方式灵活筛选 if (!string.IsNullOrEmpty(input.C_Status)) { predicate = predicate.And(i => i.C_Status==input.C_Status); } if (!string.IsNullOrEmpty(input.C_Type)) { predicate = predicate.And(i => i.C_Type==input.C_Type); } var pageData = await _imageLibraryRepository.GetPageAsync(predicate, "I_Sort", input.IsPagination, input.PageIndex, input.PageSize); input.TotalCount = pageData.Totals; var result = pageData.Rows.Select(x => new ImageLibraryModel { C_Id = x.C_ID, C_Name = x.C_Name, C_Type = x.C_Type, C_Remark = x.C_Remark, C_Status = x.C_Status, I_Sort = x.I_Sort, C_ImagePath = x.C_ImagePath, }).ToList(); return result; } public async Task UpdateOneAsync(ImageLibraryModel viewModel, params string[] fields) { var entity= await _imageLibraryRepository.GetByIdAsync(viewModel.C_Id); entity.C_Name = viewModel.C_Name; entity.C_Type = viewModel.C_Type; entity.C_Remark = viewModel.C_Remark; entity.C_Status = viewModel.C_Status; entity.I_Sort=viewModel.I_Sort; if (!string.IsNullOrEmpty(viewModel.base64Image)) { entity.C_ImagePath = viewModel.C_ImagePath; } entity.D_lastUpdatedOn = DateTime.Now; entity.C_LastUpdatedBy= _claims.ApiUserId.ToString(); entity.C_Modifier= _claims.ApiUserName; _imageLibraryRepository.Update(entity); var result=await _imageLibraryRepository.SaveAsync(); return result?1:0; } } }