using AutoMapper;
using Entity.Api.Req;
using Entity.Api.Resp;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Entity;
using Microsoft.AspNetCore.Mvc;
using Service.Station;
namespace WebStarter.Controllers;
///
/// 换电订单
///
[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;
}
///
/// 查询分页数据
///
///
[HttpPost("QueryPage")]
public async Task>> 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 ().ReverseMap());
IMapper mapper = config.CreateMapper();
List swapOrderBattery = mapper.Map>(batteryList);
//将电池数据添加到换电订单数据中
for (int i = 0; i < swapOrderResp.Rows.Count; i++)
{
swapOrderResp.Rows[i].BatteryList = new List();
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().ReverseMap());
IMapper mapper2 = config2.CreateMapper();
List swapOrderStep = mapper2.Map>(stepList);
//将步序数据添加到换电订单数据中
for (int i = 0; i < swapOrderResp.Rows.Count; i++)
{
swapOrderResp.Rows[i].StepList = new List();
swapOrderResp.Rows[i].StepList
.AddRange(swapOrderStep.Where(step => step.SwapOrderSn == swapOrderResp.Rows[i].Sn).ToList());
}
return Result>.Success(swapOrderResp);
}
///
/// 新增
///
///
[HttpPost("Add")]
public async Task> Add([FromBody] AddSwapOrderReq req)
{
//映射数据
var config = new MapperConfiguration(cfg => cfg.CreateMap().ReverseMap());
IMapper mapper = config.CreateMapper();
SwapOrder swapOrder = mapper.Map(req);
var swap = swapOrderService.QueryByClause(u => u.Sn == req.Sn);
if (swap != null)
{
return Result.Fail("新增失败!订单号重复");
}
else
{
swapOrderService.Insert(swapOrder);
return Result.Success(true,"新增成功");
}
}
///
/// 修改换电订单
///
///
///
[HttpPost("Modify")]
public async Task> Modify([FromBody] ModifySwapOrderReq req)
{
//映射数据
var config = new MapperConfiguration(cfg => cfg.CreateMap().ReverseMap());
IMapper mapper = config.CreateMapper();
SwapOrder swapOrder = mapper.Map(req);
if (swapOrderService.Update(swapOrder))
{
return Result.Success(true,"更改成功");
}
else
{
return Result.Fail("更改失败");
}
}
///
/// 删除
///
/// ids id列表
///
[HttpPost("DeleteByIds")]
public async Task> DeleteByIds([FromBody] List ids)
{
if (swapOrderService.DeleteByIds(ids))
{
return Result.Success(true,"删除成功");
}
else
{
return Result.Fail("删除失败");
}
}
///
/// 换电订单上报云端
///
///
[HttpGet("UploadCloud/{id}")]
public Result UploadCloud(int id)
{
return swapOrderService.UploadCloud(id);
}
}