diff --git a/Entity/Base/EntityBase.cs b/Entity/Base/EntityBase.cs index 96d7e96..191f0b3 100644 --- a/Entity/Base/EntityBase.cs +++ b/Entity/Base/EntityBase.cs @@ -10,7 +10,7 @@ namespace Entity.Base /// /// 自增 /// - [SugarColumn(ColumnName = "id", ColumnDescription = "主键Id", IsPrimaryKey = true, IsIdentity = false)] + [SugarColumn(ColumnName = "id", ColumnDescription = "主键Id", IsPrimaryKey = true, IsIdentity = true)] public virtual long Id { get; set; } } diff --git a/Entity/DbModel/Station/BatteryOpModelDetail.cs b/Entity/DbModel/Station/BatteryOpModelDetail.cs index a56b97e..728ea23 100644 --- a/Entity/DbModel/Station/BatteryOpModelDetail.cs +++ b/Entity/DbModel/Station/BatteryOpModelDetail.cs @@ -20,7 +20,7 @@ namespace Entity.DbModel.Station /// Default: /// Nullable:False /// - [SugarColumn(IsPrimaryKey=true,ColumnName="id")] + [SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "id")] public int Id {get;set;} /// diff --git a/Service/Station/BatteryOpModelDetailService.cs b/Service/Station/BatteryOpModelDetailService.cs index 90ad997..1eefa4c 100644 --- a/Service/Station/BatteryOpModelDetailService.cs +++ b/Service/Station/BatteryOpModelDetailService.cs @@ -1,4 +1,5 @@ -using Entity.DbModel.Station; +using System.Transactions; +using Entity.DbModel.Station; using Entity.Dto.Req; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Entity; @@ -11,12 +12,14 @@ namespace Service.Station; public class BatteryOpModelDetailService: BaseServices { private BatteryOpModelDetailRepository _batteryOpModelDetailRepository; + private BatteryOpModelRepository _batteryOpModelRepository; + - - public BatteryOpModelDetailService(BatteryOpModelDetailRepository dal) + public BatteryOpModelDetailService(BatteryOpModelDetailRepository dal,BatteryOpModelRepository dalBatteryOpModelRepository) { _batteryOpModelDetailRepository = dal; BaseDal = dal; + _batteryOpModelRepository= dalBatteryOpModelRepository; } /// /// 电池运营模型详情分页列表 🔖 @@ -51,6 +54,115 @@ public class BatteryOpModelDetailService: BaseServices var user = await _batteryOpModelDetailRepository.QueryByClauseAsync(u => u.Id == input.Id); if (user == null) throw new ArgumentException($"电池运营模型不存在"); - return await _batteryOpModelDetailRepository.DeleteAsync(user); + // 删除模型 + 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; + } + } \ No newline at end of file diff --git a/WebStarter/Controllers/BatteryOpModelDetailController.cs b/WebStarter/Controllers/BatteryOpModelDetailController.cs index 9fb730e..d363620 100644 --- a/WebStarter/Controllers/BatteryOpModelDetailController.cs +++ b/WebStarter/Controllers/BatteryOpModelDetailController.cs @@ -3,6 +3,7 @@ using Entity.DbModel.Station; using Entity.Dto.Req; using HybirdFrameworkCore.Entity; using Microsoft.AspNetCore.Mvc; +using Service.Init; using Service.Station; namespace WebStarter.Controllers; @@ -22,18 +23,43 @@ public class BatteryOpModelDetailController } /// - /// 获取电池运营模型分页列表 + /// 获取电池运营模型列表 /// /// /// [HttpPost] - [Route("page")] - public async Task> BatteryOpModelPageList( - [FromBody] PageBatteryOpModelDetailReq input) + [Route("list")] + public async Task> BatteryOpModelPageList() { - return await _batteryOpModelDetailService.Page(input); + return await _batteryOpModelDetailService.QueryAsync(); + } + + /// + /// 增加电池运营模型 + /// + /// + /// + [HttpPost] + [Route("add")] + public async Task> AddBatteryOpModelDetail([FromBody] AddBatteryOpModelDetailReq input) + { + return await _batteryOpModelDetailService.AddBatteryOpModelDetail(input); + } + /// + /// 更新电池运营模型 + /// + /// + /// + [HttpPost] + [Route("update")] + public async Task> UpdateBatteryOpModelDetail([FromBody] UpdateBatteryOpModelDetailReq batteryOpReq) + { + var data = await _batteryOpModelDetailService.UpdateBatteryOpModelDetail(batteryOpReq); + if (data) + return Result.Success(data); + else + return Result.Fail(data); } - /// /// 删除电池运营模型 /// diff --git a/WebStarter/db/lxw0527.sql b/WebStarter/db/lxw0527.sql index 57bcc0c..93d0587 100644 --- a/WebStarter/db/lxw0527.sql +++ b/WebStarter/db/lxw0527.sql @@ -66,4 +66,7 @@ CREATE TABLE `equip_alarm_process_record` ( `updated_time` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间' ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '设备报警处理记录' ROW_FORMAT = DYNAMIC; -SET FOREIGN_KEY_CHECKS = 1; \ No newline at end of file +SET FOREIGN_KEY_CHECKS = 1; +-- 模型两个表id自增 +ALTER TABLE battery_op_model MODIFY COLUMN id INT AUTO_INCREMENT; +ALTER TABLE battery_op_model_detail MODIFY COLUMN id INT AUTO_INCREMENT;