using System.Transactions; using Entity.DbModel.Station; using Entity.Dto.Req; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Entity; using Repository.Station; using SqlSugar; namespace Service.Station; [Scope("SingleInstance")] public class BatteryOpModelDetailService: BaseServices { private BatteryOpModelDetailRepository _batteryOpModelDetailRepository; private BatteryOpModelRepository _batteryOpModelRepository; public BatteryOpModelDetailService(BatteryOpModelDetailRepository dal,BatteryOpModelRepository dalBatteryOpModelRepository) { _batteryOpModelDetailRepository = dal; BaseDal = dal; _batteryOpModelRepository= dalBatteryOpModelRepository; } /// /// 电池运营模型详情分页列表 🔖 /// /// /// public async Task> Page(PageBatteryOpModelDetailReq input) { RefAsync total = 0; var items = await _batteryOpModelDetailRepository.QueryPageAsync( entity => true, false, entity => true, false, entity => true, !string.IsNullOrEmpty(input.BatteryType), u => u.BatteryType == input.BatteryType, u => u.CreatedTime, input.PageNum, input.PageSize, total ); return new PageResult() { PageNum = input.PageNum, PageSize = input.PageSize, ToTal = total, Rows = items, }; } /// /// 删除电池运营模型 🔖 /// /// /// public virtual async Task DeleteBatteryOpModelDetail(DeleteBatteryOpModelDetailReq input) { var user = await _batteryOpModelDetailRepository.QueryByClauseAsync(u => u.Id == input.Id); if (user == null) throw new ArgumentException($"电池运营模型不存在"); // 删除模型 var isExistModel = await _batteryOpModelRepository.QueryByClauseAsync(u => u.ModelId == user.ModelId); bool batteay = true; if (isExistModel!=null) { batteay = await _batteryOpModelRepository.DeleteAsync(isExistModel); } // 删除模型详情 bool batteryOpModelDetail = await _batteryOpModelDetailRepository.DeleteAsync(user); if (batteay && batteryOpModelDetail) { return true; } return false; } /// /// 增加电池运营模型 🔖 /// /// /// public async Task> AddBatteryOpModelDetail(AddBatteryOpModelDetailReq input) { // 事务 using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { List batteryOpModelDetails = await _batteryOpModelDetailRepository.QueryAsync(); foreach (var detail in batteryOpModelDetails) { if (detail.ModelId == input.ModelId) { return Result.Fail("模型id在模型详情表已存在"); } if (detail.StartTime == input.StartTime || detail.EndTime == input.EndTime) { return Result.Fail("相同时间已经存在"); } } // 检查两个表模型id是否已存在 var isExistModel = await _batteryOpModelRepository.QueryByClauseAsync(u => u.ModelId == input.ModelId); if (isExistModel != null) { return Result.Fail("模型id在模型详情表已存在"); } var batteryOpModel = new BatteryOpModel { ModelId = input.ModelId }; await _batteryOpModelDetailRepository.InsertAsync(input); await _batteryOpModelRepository.InsertAsync(batteryOpModel); var insertedDetail = await _batteryOpModelDetailRepository.QueryByClauseAsync(u => u.ModelId == input.ModelId); // 新增 transactionScope.Complete(); return Result.Success("新增id:" + insertedDetail.Id); } } public virtual async Task UpdateBatteryOpModelDetail(UpdateBatteryOpModelDetailReq batteryOpReq) { List batteryOpModelDetails = await _batteryOpModelDetailRepository.QueryAsync(); foreach (var detail in batteryOpModelDetails) { if (detail.ModelId == batteryOpReq.ModelId) { throw new ArgumentException($"模型id在模型详情表已存在"); } if (detail.StartTime == batteryOpReq.StartTime || detail.EndTime == batteryOpReq.EndTime) { throw new ArgumentException($"已存在相同时间"); } } var isExistModel = await _batteryOpModelRepository.QueryByClauseAsync(u => u.ModelId == batteryOpReq.ModelId); if (isExistModel != null) { return false; } // 通过id查找修改之前的模型id var batteryOpModelDetail = await _batteryOpModelDetailRepository.QueryByClauseAsync(u => u.Id == batteryOpReq.Id); var batteryOpModel = await _batteryOpModelRepository.QueryByClauseAsync(u => u.ModelId == batteryOpModelDetail.ModelId); batteryOpModel.ModelId = batteryOpReq.ModelId; bool updateModelDetail = await _batteryOpModelDetailRepository.UpdateAsync(batteryOpReq); bool updateMode = await _batteryOpModelRepository.UpdateAsync(batteryOpModel); if (updateModelDetail && updateMode) { return true; } return false; } }