using AutoMapper; using LinqKit; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.NodeServices; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using Ropin.Core.Common; using Ropin.Inspection.Common; using Ropin.Inspection.Common.Accessor.Interface; using Ropin.Inspection.Common.Helper; using Ropin.Inspection.Model; using Ropin.Inspection.Model.Entities; using Ropin.Inspection.Repository; using Ropin.Inspection.Repository.Interface; using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Drawing; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; using static System.Net.Mime.MediaTypeNames; namespace Ropin.Inspection.Service { public class TdevBoxTemplateService : ITdevBoxTemplateService { private readonly ITdevBoxTemplateRepository _repository; private readonly IMapper _mapper; private readonly IClaimsAccessor _claims; private readonly InspectionDbContext _sqlDBContext; private readonly ITmtnDevOpsRecordRepository _tmtnDevOpsRecordRepository; public TdevBoxTemplateService(ITmtnDevOpsRecordRepository tmtnDevOpsRecordRepository, IClaimsAccessor claims, InspectionDbContext sqlDBContext, ITdevBoxTemplateRepository repository, IMapper mapper) { _repository = repository; _mapper = mapper; _claims = claims; _sqlDBContext = sqlDBContext; _tmtnDevOpsRecordRepository = tmtnDevOpsRecordRepository; } public async Task CreateOneAsync(TdevBoxTemplateViewModel viewModel) { var id = Guid.NewGuid().ToString(); var path = QRCoderHelper.RenderQrCode(id, "M", _claims.Linsence); var content = _mapper.Map(viewModel); content.C_ID = id; content.C_CreateBy = _claims.ApiUserId; content.D_CreateOn = DateTime.Now; content.C_Status = "1"; content.C_QRCode = path; _repository.Create(content); var result = await _repository.SaveAsync(); if (!result) { 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; content.D_LastUpdatedOn = DateTime.Now; content.C_Status = "0"; _repository.Update(content); var result = await _repository.SaveAsync(); if (!result) { throw new Exception("删除失败"); } } public async Task UpdateAsync(string code, TdevBoxTemplateUpdateModel updateModel) { var items = await _repository.GetByConditionAsync(C => C.C_ID == code); var content = items.FirstOrDefault(); if (content == null) { throw new Exception("没有此数据"); } content.C_LastUpdatedBy = _claims.Linsence == null ? _claims.ApiUserId : Guid.Parse("6e864cbc-5252-11ec-8681-fa163e02b3e4"); content.D_LastUpdatedOn = DateTime.Now; _mapper.Map(updateModel, content, typeof(TdevBoxTemplateUpdateModel), typeof(TDEV_BoxTemplate)); _repository.Update(content); var result = await _repository.SaveAsync(); if (!result) { throw new Exception("更新失败"); } } public async Task> GetConditionAsync(TdevBoxTemplateSearchModel searchModel) { var predicate = PredicateBuilder.New(true);//查询条件,推荐后台使用这种方式灵活筛选 #region 添加条件查询 if (!string.IsNullOrEmpty(searchModel.C_Status)) { predicate = predicate.And(i => i.C_Status.Equals(searchModel.C_Status)); } else { predicate = predicate.And(i => !i.C_Status.Equals("0")); } if (!string.IsNullOrEmpty(searchModel.C_ID)) { predicate = predicate.And(i => i.C_ID.Equals(searchModel.C_ID)); } if (!string.IsNullOrEmpty(searchModel.C_Name)) { predicate = predicate.And(i => i.C_Name.Contains(searchModel.C_Name)); } #endregion var list = await _repository.GetPageAsync(predicate, "I_Sort", searchModel.IsPagination, searchModel.PageIndex, searchModel.PageSize);//C_Name,-D_CreateOn searchModel.TotalCount = list.Totals; var dtoList = _mapper.Map, List>(list.Rows); return dtoList; } public Task UpdateOneAsync(TdevBoxTemplateViewModel viewModel, params string[] fields) { throw new NotImplementedException(); } public Task GetByIdAsync(string id) { throw new NotImplementedException(); } public Task IsExistAsync(string id) { throw new NotImplementedException(); } } }