using BPA.KitChen.GroupMealOrder.Application.BaseDto; using BPA.KitChen.GroupMealOrder.Application.Service.TestService; using BPA.KitChen.GroupMealOrder.Core.Common.Const; using BPA.KitChen.StoreManagement.Application.Service.OrderManage.Dtos; using Furion.DatabaseAccessor; using Furion; using Furion.DependencyInjection; using Furion.DynamicApiController; using Microsoft.AspNetCore.Mvc; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BPA.KitChen.GroupMealOrder.SqlSugar; using BPA.KitChen.GroupMealOrder.Core.Entity; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using BPA.KitChen.GroupMealOrder.Application.Service.Order.Dtos; using Org.BouncyCastle.Crypto; namespace BPA.KitChen.GroupMealOrder.Application.Service.Order { [ApiDescriptionSettings("订单管理", Tag = "订单管理", SplitCamelCase = false)] public class OrderService : IDynamicApiController, ITransient, IOrderService { /// /// 核心对象:拥有完整的SqlSugar全部功能 /// private readonly SqlSugarScope db; private readonly string CustomerIds = "omwji4jSSh6o-I0UPLGrxKEio73Y,omwji4lFXTPD6SoCQLMexKoWdWY4"; private readonly string StoreId = "3b007e32-f1cc-4021-b4e0-f4764fa90f12"; IWebHostEnvironment _hostingEnvironment; /// /// construct /// /// public OrderService(IWebHostEnvironment hostingEnvironment) { db = SqlSugarDb.Db; _hostingEnvironment=hostingEnvironment; } /// /// 订单明细 /// /// /// [HttpPost("/api/Order/PostDetail")] public PageUtil PostDetail(OrderQueryInputDto dto) { //根据对应的条件查询和排序 string strWhere = string.Empty; int total = 0; List res = new List(); //if (!string.IsNullOrEmpty(dto.StoreTypeId))//店铺分类 //{ // strWhere += $" and d.Id='{dto.StoreTypeId}'"; //} if (dto.StoreId != null && dto.StoreId.Length > 0)//店铺名称 { strWhere += $" and a.StoreId in ("; for (int i = 0; i < dto.StoreId.Length; i++) { strWhere += $"'{dto.StoreId[i]}',"; } strWhere = strWhere.Substring(0, strWhere.LastIndexOf(',')); strWhere += ")"; } if (dto.GoodsId != null && dto.GoodsId.Length > 0)//商品名称 { strWhere += $" and a.GoodId in ("; for (int i = 0; i < dto.GoodsId.Length; i++) { strWhere += $"'{dto.GoodsId[i]}',"; } strWhere = strWhere.Substring(0, strWhere.LastIndexOf(',')); strWhere += ")"; } if (!dto.isreport) { if (!string.IsNullOrWhiteSpace(CustomerIds))//屏蔽查询人 { string key = ""; string[] CustomerId = CustomerIds.Split(','); for (int i = 0; i < CustomerId.Length; i++) { key += $"'{CustomerId[i]}',"; } key = key.Substring(0, key.LastIndexOf(',')); strWhere += $" and a.Customer_ID not in (" + key + ")"; } strWhere += $" and a.StoreId not in ('" + StoreId + "')"; } //if (!string.IsNullOrEmpty(dto.Customer_Tel)) //{ // strWhere += $" and g.Phone='{dto.Customer_Tel}'"; //} if (dto.GoodsTypeId != null && dto.GoodsTypeId.Length > 0)//商品类型 { strWhere += $" and e.Goods_TypeID in ("; for (int i = 0; i < dto.GoodsTypeId.Length; i++) { strWhere += $"'{dto.GoodsTypeId[i]}',"; } strWhere = strWhere.Substring(0, strWhere.LastIndexOf(',')); strWhere += ")"; } //默认当天时间 if (string.IsNullOrEmpty(dto.Order_CreateTime.ToString())) { dto.Order_CreateTime = DateTime.Now; } if (string.IsNullOrEmpty(dto.Order_EndTime.ToString())) { dto.Order_EndTime = DateTime.Now; } var GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value; if (!string.IsNullOrWhiteSpace(GroupId)) { strWhere += $" and a.GroupId = '" + GroupId + "'"; } if (dto.OrderStatus != null) { strWhere += $" and a.Order_Status = " + dto.OrderStatus; } var temp1 = Convert.ToDateTime(dto.Order_CreateTime.Value.ToString("yyyy-MM-dd")); var temp2 = Convert.ToDateTime(dto.Order_EndTime.Value.AddDays(1).ToString("yyyy-MM-dd")); if (!dto.isreport) { var lastDay = DateTime.Now.AddDays(1 - DateTime.Now.Day).AddMonths(-1).Date; if (temp1 < lastDay) { temp1 = lastDay; } if (temp2 < lastDay) { temp2 = lastDay; } } string sql = @"SELECT UUID() as orderFlowId, a.OrderInfo_Id AS Id, a.RefundStatus, a.StoreId, a.StoreName, (case when a.RefundStatus<0 then a.Order_RealMoney else 0 end ) AS Order_RealMoney, (case when a.RefundStatus<0 then a.Order_OriginalMoney else 0 end ) AS Order_OriginalMoney, (case when a.RefundStatus>=0 then a.Order_RealMoney else 0 end ) AS RefundRealMoney, (case when a.RefundStatus>=0 then a.Order_OriginalMoney else 0 end ) AS RefundOriginalMoney, a.Transaction_ID AS Order_Number, a.Order_CreateTime AS Order_CreateTime, a.PlaceMode AS PayMode, a.Order_Status AS Order_Status, a.Customer_ID AS Customer_Name, a.Customer_ID AS Customer_Tel, 1 as order_Count FROM (SELECT c.`OrderInfo_Id` AS `OrderInfo_Id`, c.`Order_Status` AS `Order_Status`, c.`Order_CreateTime` AS `Order_CreateTime`, c.`StoreId` AS `StoreId`, c.`StoreName` AS `StoreName`, c.`Order_OriginalMoney` AS `Order_OriginalMoney`, c.`Order_RealMoney` AS `Order_RealMoney`, c.`PlaceMode` AS `PlaceMode`, c.`Customer_ID` AS `Customer_ID`, c.`Transaction_ID` AS `Transaction_ID`, c.`RefundStatus` AS `RefundStatus`, c.`GroupId` AS `GroupId`, c.`order_Count` AS `order_Count` FROM ( SELECT `a`.`Id` AS `OrderInfo_Id`, `a`.`OrderStatus` AS `Order_Status`, `a`.`CreatedAt` AS `Order_CreateTime`, `a`.`OrgId` AS `StoreId`, `a`.`OrgId` AS `StoreName`, `a`.`OrderOriginalMoney` AS `Order_OriginalMoney`, `a`.`OrderRealMoney` AS `Order_RealMoney`,(((( `a`.`OrderRealMoney` + ifnull( `a`.`DiscountMoney`, 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.CouponPrice' ), 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.ActivityPrice' ), 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.Vip' ), 0 )) AS `OrderRealMoney`, `a`.`OrderType` AS `PlaceMode`, `a`.`CustomerId` AS `Customer_ID`, `a`.`OrderCount` AS `order_Count`, `a`.`TransactionId` AS `Transaction_ID`,-( 1 ) AS `RefundStatus`, `a`.`GroupId` AS `GroupId` FROM `bpa_order` `a` WHERE ( length( `a`.`TransactionId` ) > 1 )) AS c UNION ALL SELECT `a`.`OrderInfo_Id` AS `OrderInfo_Id`, `a`.`Order_Status` AS `Order_Status`, `b`.`RefundTime` AS `Order_CreateTime`, `a`.`StoreId` AS `StoreId`, `a`.`StoreName` AS `StoreName`, `a`.`Order_OriginalMoney` AS `Order_OriginalMoney`, `a`.`Order_RealMoney` AS `Order_RealMoney`, `a`.`PlaceMode` AS `PlaceMode`, `a`.`Customer_ID` AS `Customer_ID`, `a`.`Transaction_ID` AS `Transaction_ID`, `b`.`RefundResult` AS `RefundStatus`, `a`.`GroupId` AS `GroupId`, `a`.`order_Count` AS `order_Count` FROM ( `bpa_orderrefundlog` `b` LEFT JOIN ( SELECT `a`.`Id` AS `OrderInfo_Id`, `a`.`OrderStatus` AS `Order_Status`, `a`.`CreatedAt` AS `Order_CreateTime`, `a`.`OrgId` AS `StoreId`, `a`.`OrgId` AS `StoreName`, `a`.`OrderOriginalMoney` AS `Order_OriginalMoney`, `a`.`OrderRealMoney` AS `Order_RealMoney`,(((( `a`.`OrderRealMoney` + ifnull( `a`.`DiscountMoney`, 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.CouponPrice' ), 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.ActivityPrice' ), 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.Vip' ), 0 )) AS `OrderRealMoney`, `a`.`OrderType` AS `PlaceMode`, `a`.`CustomerId` AS `Customer_ID`, `a`.`OrderCount` AS `order_Count`, `a`.`TransactionId` AS `Transaction_ID`,-( 1 ) AS `RefundStatus`, `a`.`GroupId` AS `GroupId` FROM `bpa_order` `a` WHERE ( length( `a`.`TransactionId` ) > 1 )) `a` ON (( `a`.`OrderInfo_Id` = `b`.`OrderId` ))) WHERE ( `b`.`Stutas` = 202)) a where a.RefundStatus is not NULL and a.Order_CreateTime >='" + temp1.ToString("yyyy-MM-dd HH:mm:ss") + @"' and a.Order_CreateTime <'" + temp2.ToString("yyyy-MM-dd HH:mm:ss") + @"' " + strWhere + ""; res = db.SqlQueryable(sql) .WhereIF(dto.PayMode.HasValue, (b) => b.PayMode == dto.PayMode) .OrderBy(a => a.Order_CreateTime, OrderByType.Desc).ToPageList(dto.Current, dto.PageSize, ref total); var ids = res.Select(x => x.Id).ToList(); var orders = db.Queryable().Where(x => ids.Contains(x.Id)).ToList(); foreach (var item in res) { item.PayMode = orders.FirstOrDefault(x => x.Id == item.Id)?.PayMode ?? 0; } PageUtil unit = new PageUtil() { Data = res, Total = total, }; return unit; } [HttpPost("/api/Order/GetFlowExport"), ApiDescriptionSettings(SplitCamelCase = false), AllowAnonymous, NonUnify] public ResultEntity OrderDetailFlowExport([FromBody] OrderQueryInputDto dto) { List orderFlows = OrderDetailFlowExportData(dto); return new ResultEntity() { Data = orderFlows, IsSuccess = orderFlows.Count > 0, }; string fielName = DateTime.Now.ToString("yyyyMMddHHmmss"); var currentUseId = "OrderFlow"; var rootPath = _hostingEnvironment.ContentRootPath + "orderExcels\\"; if (System.IO.Directory.Exists(rootPath) == false) System.IO.Directory.CreateDirectory(rootPath); var newFile = rootPath + "\\" + fielName + ".xlsx"; if (System.IO.File.Exists(newFile)) { System.IO.File.Delete(newFile); } using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write)) { IWorkbook workbook = new XSSFWorkbook(); var sheet = workbook.CreateSheet("orders"); var header = sheet.CreateRow(0); header.CreateCell(1).SetCellValue("店铺名称 "); header.CreateCell(2).SetCellValue("交易单号"); header.CreateCell(3).SetCellValue("应付金额"); header.CreateCell(4).SetCellValue("实付金额 "); header.CreateCell(5).SetCellValue("应退金额"); header.CreateCell(6).SetCellValue("实退金额"); header.CreateCell(7).SetCellValue("菜品数量"); header.CreateCell(8).SetCellValue("下单时间"); var rowIndex = 1; foreach (var item in orderFlows) { var datarow = sheet.CreateRow(rowIndex); datarow.CreateCell(0).SetCellValue(item.StoreName); datarow.CreateCell(1).SetCellValue(item.Order_Number); datarow.CreateCell(2).SetCellValue(item.Order_OriginalMoney.Value.ToString("#0.00")); datarow.CreateCell(3).SetCellValue(item.Order_RealMoney.Value.ToString("#0.00")); datarow.CreateCell(4).SetCellValue(item.RefundOriginalMoney.Value.ToString("#0.00")); datarow.CreateCell(5).SetCellValue(item.RefundRealMoney.Value.ToString("#0.00")); datarow.CreateCell(6).SetCellValue(item.Order_Count); datarow.CreateCell(7).SetCellValue(item.Order_CreateTime.ToString("yyyy-MM-dd HH:mm:ss")); rowIndex++; } workbook.Write(fs); } var memory = new MemoryStream(); using (var stream = new FileStream(newFile, FileMode.Open)) { stream.CopyTo(memory); } memory.Position = 0; var result = new FileStreamResult(memory, "application/octet-stream") { FileDownloadName = fielName + ".xlsx" }; // return result; } private List OrderDetailFlowExportData(OrderQueryInputDto dto) { //根据对应的条件查询和排序 string strWhere = string.Empty; int total = 0; List res = new List(); if (!string.IsNullOrEmpty(dto.StoreTypeId))//店铺分类 { strWhere += $" and d.Id='{dto.StoreTypeId}'"; } if (dto.StoreId != null && dto.StoreId.Length > 0)//店铺名称 { strWhere += $" and a.StoreId in ("; for (int i = 0; i < dto.StoreId.Length; i++) { strWhere += $"'{dto.StoreId[i]}',"; } strWhere = strWhere.Substring(0, strWhere.LastIndexOf(',')); strWhere += ")"; } if (dto.GoodsId != null && dto.GoodsId.Length > 0)//商品名称 { strWhere += $" and a.GoodId in ("; for (int i = 0; i < dto.GoodsId.Length; i++) { strWhere += $"'{dto.GoodsId[i]}',"; } strWhere = strWhere.Substring(0, strWhere.LastIndexOf(',')); strWhere += ")"; } if (!dto.isreport) { if (!string.IsNullOrWhiteSpace(CustomerIds))//屏蔽查询人 { string key = ""; string[] CustomerId = CustomerIds.Split(','); for (int i = 0; i < CustomerId.Length; i++) { key += $"'{CustomerId[i]}',"; } key = key.Substring(0, key.LastIndexOf(',')); strWhere += $" and a.Customer_ID not in (" + key + ")"; } strWhere += $" and a.StoreId not in ('" + StoreId + "')"; } if (!string.IsNullOrEmpty(dto.Customer_Tel)) { strWhere += $" and g.Phone='{dto.Customer_Tel}'"; } if (dto.GoodsTypeId != null && dto.GoodsTypeId.Length > 0)//商品类型 { strWhere += $" and e.Goods_TypeID in ("; for (int i = 0; i < dto.GoodsTypeId.Length; i++) { strWhere += $"'{dto.GoodsTypeId[i]}',"; } strWhere = strWhere.Substring(0, strWhere.LastIndexOf(',')); strWhere += ")"; } //默认当天时间 if (string.IsNullOrEmpty(dto.Order_CreateTime.ToString())) { dto.Order_CreateTime = DateTime.Now; } if (string.IsNullOrEmpty(dto.Order_EndTime.ToString())) { dto.Order_EndTime = DateTime.Now; } var GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value; if (!string.IsNullOrWhiteSpace(GroupId)) { strWhere += $" and a.GroupId = '" + GroupId + "'"; } if (dto.OrderStatus != null) { strWhere += $" and a.OrderStatus = " + dto.OrderStatus; } var temp1 = Convert.ToDateTime(dto.Order_CreateTime.Value.ToString("yyyy-MM-dd")); var temp2 = Convert.ToDateTime(dto.Order_EndTime.Value.AddDays(1).ToString("yyyy-MM-dd")); if (!dto.isreport) { var lastDay = DateTime.Now.AddDays(1 - DateTime.Now.Day).AddMonths(-1).Date; if (temp1 < lastDay) { temp1 = lastDay; } if (temp2 < lastDay) { temp2 = lastDay; } } string sql = @"SELECT a.OrderInfo_Id AS Id, a.RefundStatus, a.StoreId, a.StoreName, (case when a.RefundStatus<0 then a.Order_RealMoney else 0 end ) AS Order_RealMoney, (case when a.RefundStatus<0 then a.Order_OriginalMoney else 0 end ) AS Order_OriginalMoney, (case when a.RefundStatus>=0 then a.Order_RealMoney else 0 end ) AS RefundRealMoney, (case when a.RefundStatus>=0 then a.Order_OriginalMoney else 0 end ) AS RefundOriginalMoney, a.Transaction_ID AS Order_Number, a.Order_CreateTime AS Order_CreateTime, a.PlaceMode AS PayMode, a.Order_Status AS Order_Status, '' AS Customer_Name, '' AS Customer_Tel, 1 as order_Count FROM (SELECT c.`OrderInfo_Id` AS `OrderInfo_Id`, c.`Order_Status` AS `Order_Status`, c.`Order_CreateTime` AS `Order_CreateTime`, c.`StoreId` AS `StoreId`, c.`StoreName` AS `StoreName`, c.`Order_OriginalMoney` AS `Order_OriginalMoney`, c.`Order_RealMoney` AS `Order_RealMoney`, c.`PlaceMode` AS `PlaceMode`, c.`Customer_ID` AS `Customer_ID`, c.`Transaction_ID` AS `Transaction_ID`, c.`RefundStatus` AS `RefundStatus`, c.`GroupId` AS `GroupId`, c.`order_Count` AS `order_Count` FROM ( SELECT `a`.`Id` AS `OrderInfo_Id`, `a`.`OrderStatus` AS `Order_Status`, `a`.`CreatedAt` AS `Order_CreateTime`, `a`.`OrgId` AS `StoreId`, `a`.`OrgId` AS `StoreName`, `a`.`OrderOriginalMoney` AS `Order_OriginalMoney`, `a`.`OrderRealMoney` AS `Order_RealMoney`,(((( `a`.`OrderRealMoney` + ifnull( `a`.`DiscountMoney`, 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.CouponPrice' ), 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.ActivityPrice' ), 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.Vip' ), 0 )) AS `OrderRealMoney`, `a`.`OrderType` AS `PlaceMode`, `a`.`CustomerId` AS `Customer_ID`, `a`.`OrderCount` AS `order_Count`, `a`.`TransactionId` AS `Transaction_ID`,-( 1 ) AS `RefundStatus`, `a`.`GroupId` AS `GroupId` FROM `bpa_order` `a` WHERE ( length( `a`.`TransactionId` ) > 1 )) AS c UNION ALL SELECT `a`.`OrderInfo_Id` AS `OrderInfo_Id`, `a`.`Order_Status` AS `Order_Status`, `b`.`RefundTime` AS `Order_CreateTime`, `a`.`StoreId` AS `StoreId`, `a`.`StoreName` AS `StoreName`, `a`.`Order_OriginalMoney` AS `Order_OriginalMoney`, `a`.`Order_RealMoney` AS `Order_RealMoney`, `a`.`PlaceMode` AS `PlaceMode`, `a`.`Customer_ID` AS `Customer_ID`, `a`.`Transaction_ID` AS `Transaction_ID`, `b`.`RefundResult` AS `RefundStatus`, `a`.`GroupId` AS `GroupId`, `a`.`order_Count` AS `order_Count` FROM ( `bpa_orderrefundlog` `b` LEFT JOIN ( SELECT `a`.`Id` AS `OrderInfo_Id`, `a`.`OrderStatus` AS `Order_Status`, `a`.`CreatedAt` AS `Order_CreateTime`, `a`.`OrgId` AS `StoreId`, `a`.`OrgId` AS `StoreName`, `a`.`OrderOriginalMoney` AS `Order_OriginalMoney`, `a`.`OrderRealMoney` AS `Order_RealMoney`,(((( `a`.`OrderRealMoney` + ifnull( `a`.`DiscountMoney`, 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.CouponPrice' ), 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.ActivityPrice' ), 0 )) + ifnull( json_extract( `a`.`PriceCollection`, '$.Vip' ), 0 )) AS `OrderRealMoney`, `a`.`OrderType` AS `PlaceMode`, `a`.`CustomerId` AS `Customer_ID`, `a`.`OrderCount` AS `order_Count`, `a`.`TransactionId` AS `Transaction_ID`,-( 1 ) AS `RefundStatus`, `a`.`GroupId` AS `GroupId` FROM `bpa_order` `a` WHERE ( length( `a`.`TransactionId` ) > 1 )) `a` ON (( `a`.`OrderInfo_Id` = `b`.`OrderId` ))) WHERE ( `b`.`Stutas` = 202)) a where a.RefundStatus is not NULL and a.Order_CreateTime >='" + temp1.ToString("yyyy-MM-dd HH:mm:ss") + @"' and a.Order_CreateTime <'" + temp2.ToString("yyyy-MM-dd HH:mm:ss") + @"' " + strWhere + ""; res = db.SqlQueryable(sql) .WhereIF(dto.PayMode.HasValue, (b) => b.PayMode == dto.PayMode) .OrderBy(a => a.Order_CreateTime, OrderByType.Desc).ToList(); return res; } #region 第三方订单 /// /// 获取第三方 /// /// /// [HttpPost("/api/Order/ThirdOrderPage")] public async Task ThirdOrderPageAsync(ThirdOrderInputDto inputDto) { var total = new RefAsync(); var data = await db.Queryable() .OrderBy(a => a.CreateAt, OrderByType.Desc) .WhereIF(!string.IsNullOrEmpty(inputDto.OrderNum),x=>x.OrderNum.Contains(inputDto.OrderNum)) .Select(x=>new ThirdOrderDto() { Id = x.Id.SelectAll() }) .ToPageListAsync(inputDto.Current, inputDto.PageSize, total); var ids = data.Select(x => x.Id).ToList(); var thirdOrderInfos = await db.Queryable() .Where(x=>ids.Contains(x.ThirdOrderId)) .ToListAsync(); foreach (var item in data) { var thisData = thirdOrderInfos.Where(x => x.ThirdOrderId == item.Id).ToList(); item.ThirdOrderInfos= thisData; } return new PageUtil() { Data = data, Total = total }; } /// /// 获取第三方订单详情 /// /// /// [HttpGet("/api/Order/GetThirdOrderInfo")] public async Task GetThirdOrderInfo(string thirdOrderId) { var data = await db.Queryable() .Where(x=>x.Id==thirdOrderId) .OrderBy(a => a.CreateAt, OrderByType.Desc) .Select(x => new ThirdOrderDto() { Id = x.Id.SelectAll() }).FirstAsync(); var thirdOrderInfos = await db.Queryable() .Where(x => x.ThirdOrderId==thirdOrderId) .ToListAsync(); data.ThirdOrderInfos = thirdOrderInfos; return data; } #endregion } }