|
|
|
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;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 换电订单
|
|
|
|
/// </summary>
|
|
|
|
[ApiController]
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
public class SwapOrderController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly SwapOrderService swapOrderService;
|
|
|
|
private readonly SwapOrderBatteryService swapOrderBatteryService;
|
|
|
|
private readonly SwapOrderStepService swapOrderStepService;
|
|
|
|
|
|
|
|
public SwapOrderController(SwapOrderService swapOrderService, SwapOrderBatteryService swapOrderBatteryService,
|
|
|
|
SwapOrderStepService swapOrderStepService)
|
|
|
|
{
|
|
|
|
this.swapOrderService = swapOrderService;
|
|
|
|
this.swapOrderBatteryService = swapOrderBatteryService;
|
|
|
|
this.swapOrderStepService = swapOrderStepService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 查询分页数据
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpPost("QueryPage")]
|
|
|
|
public async Task<Result<PageResult<SwapOrderResp>>> QueryPage([FromBody] QuerySwapOrderPageReq req)
|
|
|
|
{
|
|
|
|
var swapOrderResp = swapOrderService.QuerySwapOrder(req);
|
|
|
|
|
|
|
|
//获取所有订单号
|
|
|
|
var orderSns = swapOrderResp.Rows.Select(row => row.Sn).ToList();
|
|
|
|
|
|
|
|
//电池不要了
|
|
|
|
////查询订单电池数据
|
|
|
|
List< SwapOrderBattery> batteryList = swapOrderBatteryService.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn));
|
|
|
|
|
|
|
|
//映射
|
|
|
|
var config = new MapperConfiguration(cfg => cfg.CreateMap<SwapOrderBatteryResp,SwapOrderBattery > ().ReverseMap());
|
|
|
|
IMapper mapper = config.CreateMapper();
|
|
|
|
List<SwapOrderBatteryResp> swapOrderBattery = mapper.Map<List<SwapOrderBatteryResp>>(batteryList);
|
|
|
|
|
|
|
|
|
|
|
|
//将电池数据添加到换电订单数据中
|
|
|
|
for (int i = 0; i < swapOrderResp.Rows.Count; i++)
|
|
|
|
{
|
|
|
|
swapOrderResp.Rows[i].BatteryList = new List<SwapOrderBatteryResp>();
|
|
|
|
swapOrderResp.Rows[i].BatteryList.AddRange(swapOrderBattery.Where(battery => battery.SwapOrderSn == swapOrderResp.Rows[i].Sn).ToList());
|
|
|
|
}
|
|
|
|
|
|
|
|
//查询订单步序数据
|
|
|
|
var stepList = swapOrderStepService.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn));
|
|
|
|
stepList = stepList.OrderBy(s => s.Sort).ToList();
|
|
|
|
|
|
|
|
//映射
|
|
|
|
var config2 = new MapperConfiguration(cfg => cfg.CreateMap<SwapOrderStep, SwapOrderStepResp>().ReverseMap());
|
|
|
|
IMapper mapper2 = config2.CreateMapper();
|
|
|
|
List<SwapOrderStepResp> swapOrderStep = mapper2.Map<List<SwapOrderStepResp>>(stepList);
|
|
|
|
|
|
|
|
|
|
|
|
//将步序数据添加到换电订单数据中
|
|
|
|
for (int i = 0; i < swapOrderResp.Rows.Count; i++)
|
|
|
|
{
|
|
|
|
swapOrderResp.Rows[i].StepList = new List<SwapOrderStepResp>();
|
|
|
|
swapOrderResp.Rows[i].StepList
|
|
|
|
.AddRange(swapOrderStep.Where(step => step.SwapOrderSn == swapOrderResp.Rows[i].Sn).ToList());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result<PageResult<SwapOrderResp>>.Success(swapOrderResp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 新增
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpPost("Add")]
|
|
|
|
public async Task<Result<bool>> Add([FromBody] AddSwapOrderReq req)
|
|
|
|
{
|
|
|
|
using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
|
|
|
|
{
|
|
|
|
// 映射数据
|
|
|
|
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, "新增失败");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 修改换电订单
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="req"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpPost("Modify")]
|
|
|
|
public async Task<Result<bool>> Modify([FromBody] ModifySwapOrderReq req)
|
|
|
|
{
|
|
|
|
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("更改失败");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 删除
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="ids">ids id列表</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpPost("DeleteByIds")]
|
|
|
|
public async Task<Result<bool>> DeleteByIds([FromBody] List<long> ids)
|
|
|
|
{
|
|
|
|
if (swapOrderService.DeleteByIds(ids))
|
|
|
|
{
|
|
|
|
return Result<bool>.Success(true,"删除成功");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Result<bool>.Fail("删除失败");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 换电订单导出
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="req"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpPost]
|
|
|
|
[Route("export")]
|
|
|
|
public async Task<IActionResult> ExportSwapOrder([FromBody] QuerySwapOrderPageReq req)
|
|
|
|
{
|
|
|
|
return await swapOrderService.ExportSwapOrder(req);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// 换电订单上报云端
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpGet("UploadCloud/{id}")]
|
|
|
|
public Result<bool> UploadCloud(int id)
|
|
|
|
{
|
|
|
|
|
|
|
|
return swapOrderService.UploadCloud(id);
|
|
|
|
}
|
|
|
|
}
|