using System.Collections.Generic; using System.Transactions; using AutoMapper; using Entity.Api.Req; using Entity.Api.Resp; using Entity.Constant; using Entity.DbModel.Station; using Entity.Dto; using HybirdFrameworkCore.Entity; using log4net; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.Mvc; using Repository.Station; using Service.Init; using Service.Station; using Swapping.Business.Common; namespace WebStarter.Controllers; /// /// 换电订单 /// [ApiController] [Route("api/[controller]")] public class SwapOrderController : ControllerBase { private static readonly ILog Log = LogManager.GetLogger(typeof(SwapOrderController)); private readonly SwapOrderService swapOrderService; private readonly SwapOrderBatteryService swapOrderBatteryService; private readonly SwapOrderStepService swapOrderStepService; private readonly SwapOrderReportCloudRepository swapOrderReportCloudRepository; private readonly VehicleService vehicleService; private readonly BaseVehicleRepository _baseVehicleRepository; public SwapOrderController(SwapOrderService swapOrderService, SwapOrderBatteryService swapOrderBatteryService, SwapOrderStepService swapOrderStepService, SwapOrderReportCloudRepository swapOrderReportCloudRepository, BaseVehicleRepository baseVehicleRepository ) { this.swapOrderService = swapOrderService; this.swapOrderBatteryService = swapOrderBatteryService; this.swapOrderStepService = swapOrderStepService; this.swapOrderReportCloudRepository = swapOrderReportCloudRepository; _baseVehicleRepository = baseVehicleRepository; } /// /// 查询分页数据 /// /// [HttpPost("QueryPage")] public async Task>> QueryPage([FromBody] QuerySwapOrderPageReq req) { Log.Info($"SwapOrderController QueryPage req={req}"); if (!string.IsNullOrEmpty(req.DownBatteryNo) || !string.IsNullOrEmpty(req.UpBatteryNo)) { var result = BatteryCodeList(req); return Result>.Success(result); } Log.Info($"查询换电订单 SwapOrderController start QueryPage swapOrderService.QuerySwapOrder req={req}"); var swapOrderResp = swapOrderService.QuerySwapOrder(req); Log.Info($"查询换电订单 SwapOrderController end QueryPage swapOrderService.QuerySwapOrder req={req}"); PackageSwapOrder(swapOrderResp); List? list = swapOrderResp.Rows; List vehicleNoList = list?.Select(resp => resp.VehicleNo).ToList() ?? new List(); // 查询车辆的部门 List vehicleList = await _baseVehicleRepository .Queryable() .In(vehicle => vehicle.VehicleNo, vehicleNoList) .ToListAsync(); // 给换电订单赋值车的公司和部门 foreach (var swapOrder in list) { var matchingVehicle = vehicleList.FirstOrDefault(v => v.VehicleNo == swapOrder.VehicleNo); if (matchingVehicle != null) { swapOrder.Company = matchingVehicle.Company; swapOrder.Departments = matchingVehicle.Departments; } if (swapOrder.BatteryList != null && swapOrder.BatteryList.Count > 0) { SwapOrderBatteryResp swapOrderBatteryResp = swapOrder.BatteryList[0]; swapOrder.UpBatteryBinNo = swapOrderBatteryResp.UpBatteryBinNo; swapOrder.UpBatteryNo = swapOrderBatteryResp.UpBatteryNo; swapOrder.UpBatterySoc = swapOrderBatteryResp.UpBatterySoc; swapOrder.DownBatteryBinNo = swapOrderBatteryResp.DownBatteryBinNo; swapOrder.UpBatteryNo = swapOrderBatteryResp.UpBatteryNo; swapOrder.DownBatteryNo = swapOrderBatteryResp.DownBatteryNo; swapOrder.DownBatterySoc = swapOrderBatteryResp.DownBatterySoc; try { swapOrder.DownBatteryElectricQuantity = ((double)swapOrder.DownBatterySoc * 3.75).ToString(); swapOrder.UpBatteryElectricQuantity = ((double)swapOrder.UpBatterySoc * 3.75).ToString(); double ElectricQuantity = ((float)(swapOrder.UpBatterySoc - swapOrder.DownBatterySoc)) * 3.75; swapOrder.ElectricQuantity = ElectricQuantity.ToString(); } catch (Exception e) { } } if (swapOrder.StepList != null && swapOrder.StepList.Count > 0) { List swapOrderStepResps = swapOrder.StepList; swapOrder.ManualStep = string.Join(",", swapOrderStepResps .Where(step => step.StepType == (int)SwapConstant.StepType.MANUAL) .Select(step => step.StepDesc) .ToList()); } } return Result>.Success(swapOrderResp); } /// /// app查询换电订单 /// /// /// [HttpPost("AppSwapOrder")] public async Task>> AppSwapOrder([FromBody] QuerySwapOrderPageReq req) { Log.Info($"SwapOrderController QueryPage req={req}"); Log.Info($"查询换电订单 SwapOrderController start QueryPage swapOrderService.QuerySwapOrder req={req}"); var swapOrderResp = swapOrderService.AppQuerySwapOrder(req); Log.Info($"查询换电订单 SwapOrderController end QueryPage swapOrderService.QuerySwapOrder req={req}"); PackageSwapOrder(swapOrderResp); return Result>.Success(swapOrderResp); } private void PackageSwapOrder(PageResult swapOrderResp) { //获取所有订单号 var orderSns = swapOrderResp.Rows.Select(row => row.Sn).ToList(); //查询上报云平台次数 //Dictionary queryReportCloudNumBySn = swapOrderService.QueryReportCloudNumBySn(orderSns); ////查询订单电池数据 List 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); List swapOrderReportClouds = swapOrderReportCloudRepository.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn)); //将电池数据添加到换电订单数据中 foreach (var row in swapOrderResp.Rows) { row.BatteryList = swapOrderBattery.Where(battery => battery.SwapOrderSn == row.Sn).ToList(); var matchingReport = swapOrderReportClouds.FirstOrDefault(r => r.SwapOrderSn == row.Sn); if (matchingReport != null) { foreach (var battery in row.BatteryList) { battery.Vtm = matchingReport.Vtm; } } } //查询订单步序数据 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(); var swapOrderStepResps = swapOrderStep.Where(step => step.SwapOrderSn == swapOrderResp.Rows[i].Sn).ToList(); swapOrderResp.Rows[i].StepList .AddRange(swapOrderStepResps); swapOrderResp.Rows[i].ManualStep = String.Join(",", swapOrderStepResps.Where(i => i.StepType == (int)SwapConstant.StepType.MANUAL).Select(i => i.StepDesc) .ToList()); } } private PageResult BatteryCodeList(QuerySwapOrderPageReq req) { PageResult batteryCodeList = swapOrderBatteryService.QuerySwapOrderBatteryList(req); var batteryOrderSn = batteryCodeList.Rows.Select(row => row.SwapOrderSn).ToList(); List swapOrderRespList = swapOrderService.QueryListByClause(u => batteryOrderSn.Contains(u.Sn)); //获取所有订单号 var orderSns = swapOrderRespList.Select(row => row.Sn).ToList(); ////查询订单电池数据 List batteryList = swapOrderBatteryService.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn)); var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); cfg.CreateMap().ReverseMap(); }); //映射 IMapper mapper = config.CreateMapper(); List swapOrderBattery = mapper.Map>(batteryList); List swapOrderResp = mapper.Map>(swapOrderRespList); List swapOrderReportClouds = swapOrderReportCloudRepository.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn)); //将电池数据添加到换电订单数据中 foreach (var row in swapOrderResp) { row.BatteryList = swapOrderBattery.Where(battery => battery.SwapOrderSn == row.Sn).ToList(); var matchingReport = swapOrderReportClouds.FirstOrDefault(r => r.SwapOrderSn == row.Sn); if (matchingReport != null) { foreach (var battery in row.BatteryList) { battery.Vtm = matchingReport.Vtm; } } } //查询订单步序数据 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.Count; i++) { swapOrderResp[i].StepList = new List(); swapOrderResp[i].StepList .AddRange(swapOrderStep.Where(step => step.SwapOrderSn == swapOrderResp[i].Sn).ToList()); } PageResult result = new PageResult() { PageNum = req.PageNum, PageSize = req.PageSize, ToTal = batteryCodeList.ToTal, Rows = swapOrderResp }; return result; } /// /// 新增 /// /// [HttpPost("Add")] public async Task> Add([FromBody] AddSwapOrderReq req) { using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { // 映射数据 var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); cfg.CreateMap().ReverseMap(); }); IMapper mapper = config.CreateMapper(); // 转换换电订单 SwapOrder swapOrder = mapper.Map(req); swapOrder.Sn = SwapOrderNoGenerator.GenerateOrderNo(StaticStationInfo.StationNo); swapOrder.CloudReportStatus = 0; SwapOrder order = await swapOrderService.InsertAsync(swapOrder); // 绑定电池 var swapOrderBattery = new SwapOrderBattery { SwapOrderSn = swapOrder.Sn, UpBatterySoc = req.UpBatterySoc, UpNominalEnergy = req.UpNominalEnergy, DownBatterySoc = req.DownBatterySoc, DownNominalEnergy = req.DownNominalEnergy, SettleAnAccountFlag = 0 }; SwapOrderBattery swapOrderBatteryResult = await swapOrderBatteryService.InsertAsync(swapOrderBattery); if (order != null && swapOrderBatteryResult !=null) { // 提交 transactionScope.Complete(); return Result.Success(true, "新增成功"); } return Result.Fail(false, "新增失败"); } } /// /// 修改换电订单 /// /// /// [HttpPost("Modify")] public async Task> 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.Fail("订单号已存在"); } // 映射数据 var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); cfg.CreateMap().ReverseMap(); }); IMapper mapper = config.CreateMapper(); SwapOrder swapOrder = mapper.Map(req); bool orderUpdated = swapOrderService.Update(swapOrder); bool batteriesUpdated = true; if (req.batteryList.Any()) { // 绑定订单 List swapOrderBatteries = mapper.Map>(req.batteryList); foreach (var swapOrderBattery in swapOrderBatteries) { swapOrderBattery.SwapOrderSn = swapOrder.Sn; } // 更新电池订单 batteriesUpdated = await swapOrderBatteryService.UpdateAsync(swapOrderBatteries); } if (orderUpdated && batteriesUpdated) { // 提交 transactionScope.Complete(); return Result.Success(true, "更改成功"); } 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("删除失败"); } } /// /// 换电订单导出 /// /// /// [HttpPost] [Route("export")] public async Task ExportSwapOrder([FromBody] QuerySwapOrderPageReq req) { // 获取请求头中的语言信息 var language = Request.Headers["Accept-Language"].ToString().ToLower(); List orderList = await swapOrderService.QuerySwapOrderListAsync(req); // 获取所有订单号 var orderSns = orderList.Select(row => row.Sn).ToList(); // 查询订单电池数据 List batteryList = swapOrderBatteryService.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn)); // 根据语言设置不同的映射配置 var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); cfg.CreateMap().ReverseMap(); cfg.CreateMap().ReverseMap(); cfg.CreateMap().ReverseMap(); cfg.CreateMap().ReverseMap(); cfg.CreateMap().ReverseMap(); cfg.CreateMap().ReverseMap(); }); IMapper mapper = config.CreateMapper(); List swapOrderBattery = mapper.Map>(batteryList); // 换电订单 List swapOrderResp = mapper.Map>(orderList); foreach (var orderResp in swapOrderResp) { if (orderResp.ServiceTotalFee.HasValue) { orderResp.ServiceTotalFee = Math.Round(orderResp.ServiceTotalFee.Value / 100m, 2); } if (orderResp.ElectricityTotalFee.HasValue) { orderResp.ElectricityTotalFee = Math.Round(orderResp.ElectricityTotalFee.Value / 100m, 2); } if (orderResp.AppendTotalFee.HasValue) { orderResp.AppendTotalFee = Math.Round(orderResp.AppendTotalFee.Value / 100m, 2); } if (orderResp.TotalFee.HasValue) { orderResp.TotalFee = Math.Round(orderResp.TotalFee.Value / 100m, 2); } } List vehicleNoList = swapOrderResp.Select(resp => resp.VehicleNo).ToList() ?? new List(); // 查询车辆的部门 List vehicleList = await _baseVehicleRepository .Queryable() .In(vehicle => vehicle.VehicleNo, vehicleNoList) .ToListAsync(); var stepList = swapOrderStepService.QueryListByClause(u => orderSns.Contains(u.SwapOrderSn)); stepList = stepList.OrderBy(s => s.Sort).ToList(); List swapOrderStep = mapper.Map>(stepList); //将步序数据添加到换电订单数据中 // 给换电订单赋值车的公司和部门 for (int i = 0; i < swapOrderResp.Count; i++) { // 初始化 StepList swapOrderResp[i].StepList = new List(); // 获取与当前订单匹配的步骤信息 var swapOrderStepResps = swapOrderStep.Where(step => step.SwapOrderSn == swapOrderResp[i].Sn).ToList(); swapOrderResp[i].StepList.AddRange(swapOrderStepResps); // 设置 ManualStep 字段 swapOrderResp[i].ManualStep = string.Join(",", swapOrderStepResps.Where(step => step.StepType == (int)SwapConstant.StepType.MANUAL) .Select(step => step.StepDesc) .ToList()); // 查找匹配的车辆信息并赋值公司和部门字段 var matchingVehicle = vehicleList.FirstOrDefault(v => v.VehicleNo == swapOrderResp[i].VehicleNo); if (matchingVehicle != null) { swapOrderResp[i].Company = matchingVehicle.Company; swapOrderResp[i].Departments = matchingVehicle.Departments; } } if (req.SwapResult == 0 || req.SwapResult == 1) { List list = mapper.Map>(swapOrderResp); List list2 = mapper.Map>(swapOrderResp); // 将电池数据添加到换电订单数据中 for (int i = 0; i < list.Count; i++) { if (language == "en") { SwapOrderDto2 dto = list2[i]; SwapOrderBatteryResp? swapOrderBatteryResp = swapOrderBattery.FirstOrDefault(battery => battery.SwapOrderSn == dto.Sn); if (swapOrderBatteryResp != null) { dto.DownBatterySoc = swapOrderBatteryResp.DownBatterySoc; dto.DownBatteryNo = swapOrderBatteryResp.DownBatteryNo; dto.UpBatteryNo = swapOrderBatteryResp.UpBatteryNo; dto.UpBatterySoc = swapOrderBatteryResp.UpBatterySoc; dto.DownBatteryBinNo = swapOrderBatteryResp.DownBatteryBinNo; dto.UpBatteryBinNo = swapOrderBatteryResp.UpBatteryBinNo; try { dto.DownBatteryElectricQuantity = ((double)dto.DownBatterySoc * 3.75).ToString(); dto.UpBatteryElectricQuantity = ((double)dto.UpBatterySoc * 3.75).ToString(); double ElectricQuantity = ((float)(dto.UpBatterySoc - dto.DownBatterySoc)) * 3.75; dto.ElectricQuantity = ElectricQuantity.ToString(); } catch (Exception e) { } } } else { SwapOrderDto dto = list[i]; SwapOrderBatteryResp? swapOrderBatteryResp = swapOrderBattery.FirstOrDefault(battery => battery.SwapOrderSn == dto.Sn); if (swapOrderBatteryResp != null) { dto.DownBatterySoc = swapOrderBatteryResp.DownBatterySoc; dto.DownBatteryNo = swapOrderBatteryResp.DownBatteryNo; dto.UpBatteryNo = swapOrderBatteryResp.UpBatteryNo; dto.UpBatterySoc = swapOrderBatteryResp.UpBatterySoc; dto.DownBatteryBinNo = swapOrderBatteryResp.DownBatteryBinNo; dto.UpBatteryBinNo = swapOrderBatteryResp.UpBatteryBinNo; try { dto.DownBatteryElectricQuantity = ((double)dto.DownBatterySoc * 3.75).ToString(); dto.UpBatteryElectricQuantity = ((double)dto.UpBatterySoc * 3.75).ToString(); double ElectricQuantity = ((float)(dto.UpBatterySoc - dto.DownBatterySoc)) * 3.75; dto.ElectricQuantity = ElectricQuantity.ToString(); } catch (Exception e) { } } } } IExcelExporter excelExporter = new ExcelExporter(); var res = language == "en" ? await excelExporter.ExportAsByteArray(list2) : await excelExporter.ExportAsByteArray(list); return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + (language == "en" ? "_Swap_Orders.xlsx" : "2号站换电记录.xlsx") }; } else { List list = mapper.Map>(swapOrderResp); List list2 = mapper.Map>(swapOrderResp); // 将电池数据添加到换电订单数据中 for (int i = 0; i < list.Count; i++) { if (language == "en") { SwapLoseOrderDto2 dto = list2[i]; SwapOrderBatteryResp? swapOrderBatteryResp = swapOrderBattery.FirstOrDefault(battery => battery.SwapOrderSn == dto.Sn); if (swapOrderBatteryResp != null) { dto.DownBatteryBinNo = swapOrderBatteryResp.DownBatteryBinNo; dto.UpBatteryBinNo = swapOrderBatteryResp.UpBatteryBinNo; } } else { SwapLoseOrderDto dto = list[i]; SwapOrderBatteryResp? swapOrderBatteryResp = swapOrderBattery.FirstOrDefault(battery => battery.SwapOrderSn == dto.Sn); if (swapOrderBatteryResp != null) { dto.DownBatteryBinNo = swapOrderBatteryResp.DownBatteryBinNo; dto.UpBatteryBinNo = swapOrderBatteryResp.UpBatteryBinNo; } } } IExcelExporter excelExporter = new ExcelExporter(); var res = language == "en" ? await excelExporter.ExportAsByteArray(list2) : await excelExporter.ExportAsByteArray(list); return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + (language == "en" ? "_Swap_Lose_Orders.xlsx" : "2号站换电失败记录.xlsx") }; } } }