|
|
|
@ -1,10 +1,13 @@
|
|
|
|
|
using System.Transactions;
|
|
|
|
|
using AutoMapper;
|
|
|
|
|
using Entity.Api.Req;
|
|
|
|
|
using Entity.Api.Resp;
|
|
|
|
|
using Entity.DbModel.Station;
|
|
|
|
|
using HybirdFrameworkCore.Entity;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Service.Init;
|
|
|
|
|
using Service.Station;
|
|
|
|
|
using Swapping.Business.Common;
|
|
|
|
|
|
|
|
|
|
namespace WebStarter.Controllers;
|
|
|
|
|
|
|
|
|
@ -84,20 +87,44 @@ public class SwapOrderController : ControllerBase
|
|
|
|
|
[HttpPost("Add")]
|
|
|
|
|
public async Task<Result<bool>> Add([FromBody] AddSwapOrderReq req)
|
|
|
|
|
{
|
|
|
|
|
//映射数据
|
|
|
|
|
var config = new MapperConfiguration(cfg => cfg.CreateMap<SwapOrder, AddSwapOrderReq>().ReverseMap());
|
|
|
|
|
IMapper mapper = config.CreateMapper();
|
|
|
|
|
SwapOrder swapOrder = mapper.Map<SwapOrder>(req);
|
|
|
|
|
|
|
|
|
|
var swap = swapOrderService.QueryByClause(u => u.Sn == req.Sn);
|
|
|
|
|
if (swap != null)
|
|
|
|
|
{
|
|
|
|
|
return Result<bool>.Fail("新增失败!订单号重复");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
|
|
|
|
|
{
|
|
|
|
|
swapOrderService.Insert(swapOrder);
|
|
|
|
|
return Result<bool>.Success(true,"新增成功");
|
|
|
|
|
// 映射数据
|
|
|
|
|
var config = new MapperConfiguration(cfg =>
|
|
|
|
|
{
|
|
|
|
|
cfg.CreateMap<SwapOrder, AddSwapOrderReq>().ReverseMap();
|
|
|
|
|
cfg.CreateMap<SwapOrderBattery, SwapOrderBatteryReq>().ReverseMap();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
IMapper mapper = config.CreateMapper();
|
|
|
|
|
|
|
|
|
|
// 转换换电订单
|
|
|
|
|
SwapOrder swapOrder = mapper.Map<SwapOrder>(req);
|
|
|
|
|
swapOrder.Sn = SwapOrderNoGenerator.GenerateOrderNo(StaticStationInfo.StationNo);
|
|
|
|
|
SwapOrder order = await swapOrderService.InsertAsync(swapOrder);
|
|
|
|
|
|
|
|
|
|
bool batteriesInserted = true;
|
|
|
|
|
|
|
|
|
|
if (req.batteryList.Any())
|
|
|
|
|
{
|
|
|
|
|
// 绑定订单
|
|
|
|
|
List<SwapOrderBattery> swapOrderBatteries = mapper.Map<List<SwapOrderBattery>>(req.batteryList);
|
|
|
|
|
foreach (var swapOrderBattery in swapOrderBatteries)
|
|
|
|
|
{
|
|
|
|
|
swapOrderBattery.SwapOrderSn = swapOrder.Sn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
batteriesInserted = await swapOrderBatteryService.InsertAsync(swapOrderBatteries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (order != null && batteriesInserted)
|
|
|
|
|
{
|
|
|
|
|
// 提交
|
|
|
|
|
transactionScope.Complete();
|
|
|
|
|
return Result<bool>.Success(true, "新增成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result<bool>.Fail(false, "新增失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -109,17 +136,47 @@ public class SwapOrderController : ControllerBase
|
|
|
|
|
[HttpPost("Modify")]
|
|
|
|
|
public async Task<Result<bool>> Modify([FromBody] ModifySwapOrderReq req)
|
|
|
|
|
{
|
|
|
|
|
//映射数据
|
|
|
|
|
var config = new MapperConfiguration(cfg => cfg.CreateMap<SwapOrder, ModifySwapOrderReq>().ReverseMap());
|
|
|
|
|
IMapper mapper = config.CreateMapper();
|
|
|
|
|
SwapOrder swapOrder = mapper.Map<SwapOrder>(req);
|
|
|
|
|
|
|
|
|
|
if (swapOrderService.Update(swapOrder))
|
|
|
|
|
{
|
|
|
|
|
return Result<bool>.Success(true,"更改成功");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
|
|
|
|
|
{
|
|
|
|
|
var existingOrder = await swapOrderService.QueryByClauseAsync(u => u.Sn == req.Sn && u.Id != req.Id);
|
|
|
|
|
if (existingOrder != null)
|
|
|
|
|
{
|
|
|
|
|
return Result<bool>.Fail("订单号已存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 映射数据
|
|
|
|
|
var config = new MapperConfiguration(cfg =>
|
|
|
|
|
{
|
|
|
|
|
cfg.CreateMap<SwapOrder, ModifySwapOrderReq>().ReverseMap();
|
|
|
|
|
cfg.CreateMap<SwapOrderBattery, SwapOrderBatteryReq>().ReverseMap();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
IMapper mapper = config.CreateMapper();
|
|
|
|
|
SwapOrder swapOrder = mapper.Map<SwapOrder>(req);
|
|
|
|
|
|
|
|
|
|
bool orderUpdated = swapOrderService.Update(swapOrder);
|
|
|
|
|
bool batteriesUpdated = true;
|
|
|
|
|
|
|
|
|
|
if ( req.batteryList.Any())
|
|
|
|
|
{
|
|
|
|
|
// 绑定订单
|
|
|
|
|
List<SwapOrderBattery> swapOrderBatteries = mapper.Map<List<SwapOrderBattery>>(req.batteryList);
|
|
|
|
|
foreach (var swapOrderBattery in swapOrderBatteries)
|
|
|
|
|
{
|
|
|
|
|
swapOrderBattery.SwapOrderSn = swapOrder.Sn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新电池订单
|
|
|
|
|
batteriesUpdated = await swapOrderBatteryService.UpdateAsync(swapOrderBatteries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (orderUpdated && batteriesUpdated)
|
|
|
|
|
{
|
|
|
|
|
// 提交
|
|
|
|
|
transactionScope.Complete();
|
|
|
|
|
return Result<bool>.Success(true, "更改成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result<bool>.Fail("更改失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|