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.

384 lines
16 KiB

using System.Transactions;
7 months ago
using AutoMapper;
using Entity.Api.Req;
using Entity.Api.Resp;
7 months ago
using Entity.DbModel.Station;
using Entity.Dto;
using HybirdFrameworkCore.Entity;
using log4net;
using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Mvc;
6 months ago
using Repository.Station;
using Service.Init;
7 months ago
using Service.Station;
using Swapping.Business.Common;
namespace WebStarter.Controllers;
7 months ago
/// <summary>
/// 换电订单
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class SwapOrderController : ControllerBase
{
private static readonly ILog Log = LogManager.GetLogger(typeof(SwapOrderController));
7 months ago
private readonly SwapOrderService swapOrderService;
7 months ago
private readonly SwapOrderBatteryService swapOrderBatteryService;
private readonly SwapOrderStepService swapOrderStepService;
6 months ago
private readonly SwapOrderReportCloudRepository swapOrderReportCloudRepository;
public SwapOrderController(SwapOrderService swapOrderService, SwapOrderBatteryService swapOrderBatteryService,
6 months ago
SwapOrderStepService swapOrderStepService,SwapOrderReportCloudRepository swapOrderReportCloudRepository)
7 months ago
{
this.swapOrderService = swapOrderService;
7 months ago
this.swapOrderBatteryService = swapOrderBatteryService;
this.swapOrderStepService = swapOrderStepService;
6 months ago
this.swapOrderReportCloudRepository = swapOrderReportCloudRepository;
7 months ago
}
7 months ago
/// <summary>
7 months ago
/// 查询分页数据
7 months ago
/// </summary>
/// <returns></returns>
[HttpPost("QueryPage")]
public async Task<Result<PageResult<SwapOrderResp>>> QueryPage([FromBody] QuerySwapOrderPageReq req)
{
Log.Info($"SwapOrderController QueryPage req={req}");
6 months ago
if (!string.IsNullOrEmpty(req.DownBatteryNo)||!string.IsNullOrEmpty(req.UpBatteryNo))
{
var result = BatteryCodeList(req);
return Result<PageResult<SwapOrderResp>>.Success(result);
}
Log.Info($"查询换电订单 SwapOrderController start QueryPage swapOrderService.QuerySwapOrder req={req}");
6 months ago
var swapOrderResp = swapOrderService.QuerySwapOrder(req);
Log.Info($"查询换电订单 SwapOrderController end QueryPage swapOrderService.QuerySwapOrder req={req}");
6 months ago
7 months ago
//获取所有订单号
var orderSns = swapOrderResp.Rows.Select(row => row.Sn).ToList();
Log.Info($"查询上报云平台次数 SwapOrderController start QueryPage swapOrderService.QueryReportCloudNumBySn req={orderSns}");
6 months ago
//查询上报云平台次数
Dictionary<string, int?> queryReportCloudNumBySn = swapOrderService.QueryReportCloudNumBySn(orderSns);
Log.Info($"查询上报云平台次数 SwapOrderController end QueryPage swapOrderService.QueryReportCloudNumBySn req={orderSns}");
Log.Info($"查询订单电池数据 SwapOrderController start QueryPage swapOrderBatteryService.QueryListByClause req={orderSns}");
6 months ago
////查询订单电池数据
List<SwapOrderBattery> batteryList =
swapOrderBatteryService.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn));
Log.Info($"查询订单电池数据 SwapOrderController end QueryPage swapOrderBatteryService.QueryListByClause req={orderSns}");
6 months ago
//映射
var config =
new MapperConfiguration(cfg => cfg.CreateMap<SwapOrderBatteryResp, SwapOrderBattery>().ReverseMap());
IMapper mapper = config.CreateMapper();
List<SwapOrderBatteryResp> swapOrderBattery = mapper.Map<List<SwapOrderBatteryResp>>(batteryList);
Log.Info($"查询公里数 SwapOrderController start QueryPage swapOrderReportCloudRepository.QueryListByClause req={orderSns}");
6 months ago
List<SwapOrderReportCloud> swapOrderReportClouds = swapOrderReportCloudRepository.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn));
Log.Info($"查询公里数 SwapOrderController end QueryPage swapOrderReportCloudRepository.QueryListByClause req={orderSns}");
6 months ago
//将电池数据添加到换电订单数据中
foreach (var row in swapOrderResp.Rows)
{
row.BatteryList = swapOrderBattery.Where(battery => battery.SwapOrderSn == row.Sn).ToList();
queryReportCloudNumBySn.TryGetValue(row.Sn, out int? num);
row.ReportCloudNum = num ?? 0;
var matchingReport = swapOrderReportClouds.FirstOrDefault(r => r.SwapOrderSn == row.Sn);
if (matchingReport != null)
{
foreach (var battery in row.BatteryList)
{
battery.Vtm = matchingReport.Vtm;
}
}
}
Log.Info($"查询订单步序数据 SwapOrderController start QueryPage swapOrderStepService.QueryListByClause req={orderSns}");
6 months ago
//查询订单步序数据
var stepList = swapOrderStepService.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn));
Log.Info($"查询订单步序数据 SwapOrderController end QueryPage swapOrderStepService.QueryListByClause req={orderSns}");
6 months ago
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);
}
private PageResult<SwapOrderResp> BatteryCodeList(QuerySwapOrderPageReq req)
{
PageResult<SwapOrderBatteryResp> batteryCodeList = swapOrderBatteryService.QuerySwapOrderBatteryList(req);
var batteryOrderSn = batteryCodeList.Rows.Select(row => row.SwapOrderSn).ToList();
7 months ago
6 months ago
List<SwapOrder> swapOrderRespList = swapOrderService.QueryListByClause(u => batteryOrderSn.Contains(u.Sn));
//获取所有订单号
var orderSns = swapOrderRespList.Select(row => row.Sn).ToList();
//查询上报云平台次数
Dictionary<string, int?> queryReportCloudNumBySn = swapOrderService.QueryReportCloudNumBySn(orderSns);
7 months ago
////查询订单电池数据
List<SwapOrderBattery> batteryList =
swapOrderBatteryService.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn));
6 months ago
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<SwapOrderResp, SwapOrder>().ReverseMap();
cfg.CreateMap<SwapOrderBatteryResp, SwapOrderBattery>().ReverseMap();
});
//映射
IMapper mapper = config.CreateMapper();
6 months ago
List<SwapOrderBatteryResp> swapOrderBattery = mapper.Map<List<SwapOrderBatteryResp>>(batteryList);
6 months ago
List<SwapOrderResp> swapOrderResp = mapper.Map<List<SwapOrderResp>>(swapOrderRespList);
List<SwapOrderReportCloud> swapOrderReportClouds = swapOrderReportCloudRepository.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn));
7 months ago
//将电池数据添加到换电订单数据中
6 months ago
foreach (var row in swapOrderResp)
{
6 months ago
row.BatteryList = swapOrderBattery.Where(battery => battery.SwapOrderSn == row.Sn).ToList();
queryReportCloudNumBySn.TryGetValue(row.Sn, out int? num);
row.ReportCloudNum = num ?? 0;
var matchingReport = swapOrderReportClouds.FirstOrDefault(r => r.SwapOrderSn == row.Sn);
if (matchingReport != null)
{
6 months ago
foreach (var battery in row.BatteryList)
{
battery.Vtm = matchingReport.Vtm;
}
}
}
7 months ago
//查询订单步序数据
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());
7 months ago
IMapper mapper2 = config2.CreateMapper();
List<SwapOrderStepResp> swapOrderStep = mapper2.Map<List<SwapOrderStepResp>>(stepList);
//将步序数据添加到换电订单数据中
6 months ago
for (int i = 0; i < swapOrderResp.Count; i++)
7 months ago
{
6 months ago
swapOrderResp[i].StepList = new List<SwapOrderStepResp>();
swapOrderResp[i].StepList
.AddRange(swapOrderStep.Where(step => step.SwapOrderSn == swapOrderResp[i].Sn).ToList());
7 months ago
}
6 months ago
PageResult<SwapOrderResp> result = new PageResult<SwapOrderResp>()
{
PageNum = req.PageNum,
PageSize = req.PageSize,
ToTal = batteryCodeList.ToTal,
Rows = swapOrderResp
};
return result;
7 months ago
}
7 months ago
/// <summary>
/// 新增
/// </summary>
/// <returns></returns>
[HttpPost("Add")]
public async Task<Result<bool>> Add([FromBody] AddSwapOrderReq req)
{
using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
7 months ago
{
// 映射数据
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.CloudReportStatus = 0;
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, "新增失败");
7 months ago
}
7 months ago
}
/// <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))
7 months ago
{
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, "更改成功");
}
7 months ago
return Result<bool>.Fail("更改失败");
}
7 months ago
}
/// <summary>
/// 删除
/// </summary>
7 months ago
/// <param name="ids">ids id列表</param>
7 months ago
/// <returns></returns>
[HttpPost("DeleteByIds")]
public async Task<Result<bool>> DeleteByIds([FromBody] List<long> ids)
{
7 months ago
if (swapOrderService.DeleteByIds(ids))
{
return Result<bool>.Success(true, "删除成功");
7 months ago
}
else
{
return Result<bool>.Fail("删除失败");
}
7 months ago
}
/// <summary>
/// 换电订单导出
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
[HttpPost]
[Route("export")]
public async Task<IActionResult> ExportSwapOrder([FromBody] QuerySwapOrderPageReq req)
{
List<SwapOrder> orderList = await swapOrderService.QuerySwapOrderListAsync(req);
//获取所有订单号
var orderSns = orderList.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();
cfg.CreateMap<SwapOrderDto, SwapOrder>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List<SwapOrderBatteryResp> swapOrderBattery = mapper.Map<List<SwapOrderBatteryResp>>(batteryList);
List<SwapOrderDto> list = mapper.Map<List<SwapOrderDto>>(orderList);
//将电池数据添加到换电订单数据中
for (int i = 0; i < list.Count; i++)
{
SwapOrderDto dto = list[i];
SwapOrderBatteryResp? swapOrderBatteryResp =
swapOrderBattery.FirstOrDefault(battery => battery.SwapOrderSn == dto.Sn);
if (swapOrderBatteryResp != null)
{
dto.DownBatterySoc = swapOrderBatteryResp.DownBatterySoc;
dto.DownBatteryRealSoc = swapOrderBatteryResp.DownBatteryRealSoc;
dto.DownBatteryLastSoc = swapOrderBatteryResp.DownBatteryLastSoc;
dto.DownBatteryNo = swapOrderBatteryResp.DownBatteryNo;
dto.UpBatteryNo = swapOrderBatteryResp.UpBatteryNo;
dto.UpBatterySoc = swapOrderBatteryResp.UpBatterySoc;
dto.UpBatteryRealSoc = swapOrderBatteryResp.UpBatteryRealSoc;
6 months ago
dto.DownBatteryBinNo = swapOrderBatteryResp.DownBatteryBinNo;
dto.UpBatteryBinNo = swapOrderBatteryResp.UpBatteryBinNo;
}
}
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(list);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "换电订单.xlsx" };
}
7 months ago
/// <summary>
/// 换电订单上报云端
/// </summary>
/// <returns></returns>
[HttpGet("UploadCloud/{id}")]
public Result<bool> UploadCloud(int id)
7 months ago
{
return swapOrderService.UploadCloud(id);
7 months ago
}
}