using AutoMapper; using Ropin.Core.Common; using Ropin.Inspection.Common.Accessor.Interface; using Ropin.Inspection.Model.Entities; using Ropin.Inspection.Model.SearchModel.DEV; using Ropin.Inspection.Model.ViewModel.DEV; using Ropin.Inspection.Repository.DEV.Interface; using Ropin.Inspection.Service.DEV.Interface; using Ropin.Inspection.Service.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Ropin.Inspection.Service.DEV { public class DEVCmdService: IDEVCmdService { private readonly IDevCmdRepository _repository; private readonly IDevInstructionRepository _InstructionRepository; private readonly IDevCmdInstructionRepository _CmdInstructionRepository; private readonly IMapper _mapper; private readonly IClaimsAccessor _claims; private readonly InspectionDbContext _sqlDBContext; public DEVCmdService(IClaimsAccessor claims, InspectionDbContext sqlDBContext, IDevCmdRepository repository, IMapper mapper, IDevInstructionRepository InstructionRepository, IDevCmdInstructionRepository CmdInstructionRepository) { _repository = repository; _mapper = mapper; _claims = claims; _sqlDBContext = sqlDBContext; _InstructionRepository = InstructionRepository; _CmdInstructionRepository = CmdInstructionRepository; } #region 命令 //新增【命令】 public async Task CreateOneAsync(DevCmdViewModel viewModel) { var id = Guid.NewGuid().ToString(); var content = _mapper.Map(viewModel); content.C_ID = id; content.C_CreateBy = _claims.ApiUserId.ToString(); content.D_CreateOn = DateTime.Now; content.C_Creator = _claims.ApiUserName; content.C_Status = "1"; _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("数据库中没有此数据"); } var data = await _CmdInstructionRepository.GetByConditionAsync(t => t.C_CmdCode == id); if (data != null && data.Count() > 0) { throw new Exception("该数据已被关联,无法删除"); } _repository.Delete(content); var result = await _repository.SaveAsync(); if (!result) { throw new Exception("删除失败"); } } //修改为禁用【命令】 public async Task DisableAsync(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("删除失败"); } } //根据ID获取实体【命令】 public async Task GetByIdAsync(string id) { DevCmdSearchModel searchModel = new DevCmdSearchModel(); searchModel.C_ID = id; searchModel.IsPagination = false; var items = await _repository.GetConditionAsync(searchModel); var content = items.FirstOrDefault(); return content; } // 更新 public async Task UpdateAsync(string code, DevCmdViewModel updateModel) { TDEV_Cmd content = await _repository.GetByIdAsync(code); if (content == null) { throw new Exception("没有此数据"); } content.C_LastUpdatedBy = _claims.Linsence == null ? _claims.ApiUserId.ToString() : "6e864cbc-5252-11ec-8681-fa163e02b3e4"; content.D_LastUpdatedOn = DateTime.Now; content.C_Modifier = _claims.ApiUserName; _mapper.Map(updateModel, content, typeof(DevCmdViewModel), typeof(TDEV_Cmd)); _repository.Update(content); var result = await _repository.SaveAsync(); if (!result) { throw new Exception("更新失败"); } } public async Task> GetConditionAsync(DevCmdSearchModel searchModel) { var dtoList = await _repository.GetConditionAsync(searchModel); return dtoList; } public Task IsExistAsync(string id) { throw new NotImplementedException(); } public Task UpdateOneAsync(DevCmdViewModel viewModel, params string[] fields) { throw new NotImplementedException(); } #endregion #region 指令 //新增 public async Task InstructionCreateOneAsync(DevInstructionViewModel viewModel) { var id = Guid.NewGuid().ToString(); var content = _mapper.Map(viewModel); content.C_ID = id; content.C_CreateBy = _claims.ApiUserId.ToString(); content.D_CreateOn = DateTime.Now; content.C_Creator = _claims.ApiUserName; content.C_Status = "1"; _InstructionRepository.Create(content); var result = await _InstructionRepository.SaveAsync(); if (!result) { throw new Exception("创建失败"); } } //删除 public async Task InstructionDeleteAsync(string id,bool bol=false) { var content = await _InstructionRepository.GetByIdAsync(id); if (content == null) { throw new Exception("数据库中没有此数据"); } if (bol) { var data = await _CmdInstructionRepository.GetByConditionAsync(t => t.C_Instruction == id); if (data != null&&data.Count()>0) { throw new Exception("该数据已被关联,无法删除"); } else { _InstructionRepository.Delete(content); } } else { content.C_LastUpdatedBy = _claims.ApiUserId.ToString(); content.D_LastUpdatedOn = DateTime.Now; content.C_Modifier = _claims.ApiUserName; content.C_Status = "0"; _InstructionRepository.Update(content); } var result = await _InstructionRepository.SaveAsync(); if (!result) { throw new Exception("删除失败"); } } //根据ID获取实体 public async Task GetInstructionByIdAsync(string id) { DevInstructionSearchModel searchModel = new DevInstructionSearchModel(); searchModel.C_ID = id; searchModel.IsPagination = false; var items = await _InstructionRepository.GetConditionAsync(searchModel); var content = items.FirstOrDefault(); return content; } // 更新 public async Task InstructionUpdateAsync(string code, DevInstructionViewModel updateModel) { TDEV_Instruction content = await _InstructionRepository.GetByIdAsync(code); if (content == null) { throw new Exception("没有此数据"); } content.C_LastUpdatedBy = _claims.Linsence == null ? _claims.ApiUserId.ToString() : "6e864cbc-5252-11ec-8681-fa163e02b3e4"; content.D_LastUpdatedOn = DateTime.Now; content.C_Modifier = _claims.ApiUserName; _mapper.Map(updateModel, content, typeof(DevInstructionViewModel), typeof(TDEV_Instruction)); _InstructionRepository.Update(content); var result = await _InstructionRepository.SaveAsync(); if (!result) { throw new Exception("更新失败"); } } public async Task> GetInstructionConditionAsync(DevInstructionSearchModel searchModel) { var dtoList = await _InstructionRepository.GetConditionAsync(searchModel); return dtoList; } public async Task> GetInstructionByCmdCode(DevInstructionSearchModel searchModel) { var dtoList = await _InstructionRepository.GetInstructionByCmdCode(searchModel); return dtoList; } #endregion #region 命令-指令 //新增 public async Task CmdInstructionCreateOneAsync(DevCmdInstructionViewModel viewModel) { var id = Guid.NewGuid().ToString(); var content = _mapper.Map(viewModel); content.C_ID = id; content.C_CreateBy = _claims.ApiUserId.ToString(); content.D_CreateOn = DateTime.Now; content.C_Creator = _claims.ApiUserName; _CmdInstructionRepository.Create(content); var result = await _CmdInstructionRepository.SaveAsync(); if (!result) { throw new Exception("创建失败"); } } //删除 public async Task CmdInstructionDeleteAsync(string id) { var content = await _CmdInstructionRepository.GetByIdAsync(id); if (content == null) { throw new Exception("数据库中没有此数据"); } _CmdInstructionRepository.Delete(content); var result = await _CmdInstructionRepository.SaveAsync(); if (!result) { throw new Exception("删除失败"); } } //根据ID获取实体 public async Task GetCmdInstructionByIdAsync(string id) { DevCmdInstructionSearchModel searchModel = new DevCmdInstructionSearchModel(); searchModel.C_ID = id; searchModel.IsPagination = false; var items = await _CmdInstructionRepository.GetConditionAsync(searchModel); var content = items.FirstOrDefault(); return content; } // 更新 public async Task CmdInstructionUpdateAsync(string code, DevCmdInstructionViewModel updateModel) { TDEV_CmdInstruction content = await _CmdInstructionRepository.GetByIdAsync(code); if (content == null) { throw new Exception("没有此数据"); } _mapper.Map(updateModel, content, typeof(DevCmdInstructionViewModel), typeof(TDEV_CmdInstruction)); _CmdInstructionRepository.Update(content); var result = await _CmdInstructionRepository.SaveAsync(); if (!result) { throw new Exception("更新失败"); } } public async Task> GetCmdInstructionConditionAsync(DevCmdInstructionSearchModel searchModel) { var dtoList = await _CmdInstructionRepository.GetConditionAsync(searchModel); return dtoList; } #endregion } }