You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
6.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<BatteryOpModelDetail>
{
private BatteryOpModelDetailRepository _batteryOpModelDetailRepository;
private BatteryOpModelRepository _batteryOpModelRepository;
public BatteryOpModelDetailService(BatteryOpModelDetailRepository dal,
BatteryOpModelRepository dalBatteryOpModelRepository)
{
_batteryOpModelDetailRepository = dal;
BaseDal = dal;
_batteryOpModelRepository = dalBatteryOpModelRepository;
}
/// <summary>
/// 电池运营模型详情分页列表 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<PageResult<BatteryOpModelDetail>> Page(PageBatteryOpModelDetailReq input)
{
RefAsync<int> 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<BatteryOpModelDetail>()
{
PageNum = input.PageNum,
PageSize = input.PageSize,
ToTal = total,
Rows = items,
};
}
/// <summary>
/// 删除电池运营模型 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<bool> 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;
var batteryOpModel = _batteryOpModelRepository.QueryListByClause(i => i.ModelId == user.ModelId);
if (isExistModel != null)
{
if (batteryOpModel.Count > 1) //如果模型里面还有数据,此模型就不删除
batteay = await _batteryOpModelRepository.DeleteAsync(isExistModel);
}
// 删除模型详情
bool batteryOpModelDetail = await _batteryOpModelDetailRepository.DeleteAsync(user);
if (batteay && batteryOpModelDetail)
{
return true;
}
return false;
}
/// <summary>
/// 增加电池运营模型 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<Result<string>> AddBatteryOpModelDetail(AddBatteryOpModelDetailReq input)
{
// 事务
using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
if (DateTime.TryParse(input.StartTime, out DateTime startTime) &&
DateTime.TryParse(input.EndTime, out DateTime endTime))
{
if (startTime > endTime)
{
return Result<string>.Fail("开始时间不能大于结束时间");
}
}
List<BatteryOpModelDetail> batteryOpModelDetails = await _batteryOpModelDetailRepository.QueryAsync();
foreach (var detail in batteryOpModelDetails)
{
if (detail.StartTime == input.StartTime && detail.Id != input.Id ||
detail.EndTime == input.EndTime && detail.Id != input.Id)
{
return Result<string>.Fail("相同时间已经存在");
}
}
// 检查两个表模型id是否已存在
/*var isExistModel = await _batteryOpModelRepository.QueryByClauseAsync(u => u.ModelId == input.ModelId);
if (isExistModel != null)
{
return Result<string>.Fail("模型id在模型详情表已存在");
}*/
var batteryOpModel = new BatteryOpModel
{
ModelId = input.ModelId
};
await _batteryOpModelDetailRepository.InsertAsync(input);
var isExistModel = await _batteryOpModelRepository.QueryByClauseAsync(u => u.ModelId == input.ModelId);
if (isExistModel == null)
{
//return Result<string>.Fail("模型id在模型详情表已存在");
await _batteryOpModelRepository.InsertAsync(batteryOpModel);
}
var insertedDetail =
await _batteryOpModelDetailRepository.QueryByClauseAsync(u => u.ModelId == input.ModelId);
// 新增
transactionScope.Complete();
return Result<string>.Success("新增id" + insertedDetail.Id);
}
}
public virtual async Task<Result<string>> UpdateBatteryOpModelDetail(UpdateBatteryOpModelDetailReq batteryOpReq)
{
if (DateTime.TryParse(batteryOpReq.StartTime, out DateTime startTime) &&
DateTime.TryParse(batteryOpReq.EndTime, out DateTime endTime))
{
if (startTime > endTime)
{
return Result<string>.Fail("开始时间不能大于结束时间");
}
}
List<BatteryOpModelDetail> batteryOpModelDetails = await _batteryOpModelDetailRepository.QueryAsync();
foreach (var detail in batteryOpModelDetails)
{
if (detail.StartTime == batteryOpReq.StartTime && detail.Id != batteryOpReq.Id ||
detail.EndTime == batteryOpReq.EndTime && detail.Id != batteryOpReq.Id)
{
return Result<string>.Fail("已存在相同时间");
}
}
// 通过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 Result<string>.Success("修改成功");
}
return Result<string>.Success("修改失败");
}
}