Merge remote-tracking branch 'origin/test' into test

This commit is contained in:
张松
2025-11-26 09:32:57 +08:00
7 changed files with 125 additions and 108 deletions

View File

@@ -7,6 +7,7 @@ import com.czg.order.service.ShopProdStatisticService;
import com.czg.order.vo.SaleSummaryCountVo;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -62,4 +63,15 @@ public class SaleSummaryController {
return CzgResult.success(list);
}
@ResponseExcel(name = "销售统计明细")
@GetMapping("/export")
public List<ShopProdStatistic> summaryExport(SaleSummaryCountParam param) {
Long shopId = StpKit.USER.getShopId();
if (param.getShopId() == null) {
param.setShopId(shopId);
}
return prodStatisticService.getArchiveTradeData(
param.getShopId(),param.getProductName(), param.getRangeType(), param.getBeginDate(), param.getEndDate());
}
}

View File

@@ -1,5 +1,8 @@
package com.czg.order.entity;
import cn.idev.excel.annotation.ExcelIgnore;
import cn.idev.excel.annotation.ExcelProperty;
import cn.idev.excel.annotation.write.style.ColumnWidth;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
@@ -20,7 +23,6 @@ import java.util.Date;
* @since 2025-03-07
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table("tb_shop_prod_statistic")
@@ -30,48 +32,83 @@ public class ShopProdStatistic implements Serializable {
private static final long serialVersionUID = 1L;
@Id(keyType = KeyType.Auto)
@ExcelIgnore
private Long id;
/**
* 商品id
*/
@ExcelIgnore
private Long prodId;
/**
* 商品名称
*/
@Column(ignore = true)
@ExcelProperty("商品名称")
@ColumnWidth(30)
private String productName;
/**
* 销售数量
*/
@ExcelProperty("销量")
@ColumnWidth(5)
private BigDecimal saleCount;
/**
* 销售金额
*/
@ExcelProperty("销售金额")
@ColumnWidth(7)
private BigDecimal saleAmount;
/**
* 退单量
*/
@ExcelProperty("退单量")
@ColumnWidth(5)
private BigDecimal refundCount;
/**
* 退单金额
*/
@ExcelProperty("退款金额")
@ColumnWidth(7)
private BigDecimal refundAmount;
/**
* 实际销售数量(过滤掉退单后的数量)
*/
@ExcelProperty("实际销量")
@ColumnWidth(5)
private BigDecimal validSaleCount;
/**
* 实际销售金额(过滤掉退单后的金额)
*/
@ExcelProperty("实际销售额")
@ColumnWidth(7)
private BigDecimal validSaleAmount;
/**
* 店铺id
*/
@ExcelIgnore
private Long shopId;
/**
* 创建时间
*/
@ExcelIgnore
private Date createDay;
public void initValidData() {
// 初始化实际销售数量和金额(过滤退单数据)
this.validSaleCount = this.saleCount.subtract(this.refundCount);
this.validSaleAmount = this.saleAmount.subtract(this.refundAmount);
}
// 在 ShopProdStatistic.java 中添加以下方法
/**
* 判断当前统计数据是否有效销售数量、金额退单数量、金额均有值且大于0

View File

@@ -33,8 +33,8 @@ public class TableSummaryExportVo implements Serializable {
/**
* 台桌+日期
*/
@ExcelIgnore
private String tableConcatDate;
// @ExcelIgnore
// private String tableConcatDate;
/**
* 台桌
*/
@@ -80,27 +80,27 @@ public class TableSummaryExportVo implements Serializable {
/**
* 单价
*/
@ExcelProperty("单价")
@ColumnWidth(10)
private BigDecimal unitPrice;
/**
* 金额
*/
@ExcelProperty("金额")
@ColumnWidth(10)
private BigDecimal amount;
// @ExcelProperty("单价")
// @ColumnWidth(10)
// private BigDecimal unitPrice;
// /**
// * 金额
// */
// @ExcelProperty("金额")
// @ColumnWidth(10)
// private BigDecimal amount;
/**
* 销售额
*/
@ExcelProperty("销售额")
@ColumnWidth(10)
private BigDecimal salesAmount;
/**
* 总销售额
*/
@ExcelProperty("总销售额")
@ColumnWidth(15)
private BigDecimal totalSalesAmount;
// /**
// * 总销售额
// */
// @ExcelProperty("总销售额")
// @ColumnWidth(15)
// private BigDecimal totalSalesAmount;
/**
* 退单量
*/

View File

@@ -107,11 +107,6 @@ public class ShopProdStatisticServiceImpl extends ServiceImpl<ShopProdStatisticM
@Override
public void statisticAndInsert(Long shopId, LocalDate day) {
List<ShopProdStatistic> realTimeData = getRealTimeDataByDay(shopId, day, null);
if (CollUtil.isNotEmpty(realTimeData)) {
// 过滤掉没有有效数据的记录
realTimeData = realTimeData.stream()
.filter(ShopProdStatistic::isValid)
.toList();
if (CollUtil.isNotEmpty(realTimeData)) {
boolean exists = exists(QueryWrapper.create().eq(ShopProdStatistic::getShopId, shopId).eq(ShopProdStatistic::getCreateDay, day));
if (exists) {
@@ -120,11 +115,16 @@ public class ShopProdStatisticServiceImpl extends ServiceImpl<ShopProdStatisticM
saveBatch(realTimeData);
}
}
}
@Override
public List<ShopProdStatistic> getRealTimeDataByDay(Long shopId, LocalDate day, String productName) {
return mapper.selectProStatByDay(shopId, day, productName);
List<ShopProdStatistic> shopProdStatistics = mapper.selectProStatByDay(shopId, day, productName);
// 过滤掉没有有效数据的记录
shopProdStatistics = shopProdStatistics.stream()
.filter(ShopProdStatistic::isValid)
.peek(ShopProdStatistic::initValidData)
.toList();
return shopProdStatistics;
}
@Override

View File

@@ -11,6 +11,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -51,24 +52,6 @@ public class TableSummaryServiceImpl implements TableSummaryService {
if (CollUtil.isEmpty(list)) {
return List.of();
}
Map<String, BigDecimal> totalSalesAmountMap = list.stream().collect(
Collectors.groupingBy(
TableSummaryExportVo::getTableName, Collectors.reducing(BigDecimal.ZERO,
TableSummaryExportVo::getAmount,
BigDecimal::add)
)
);
Map<String, BigDecimal> salesAmountMap = list.stream().collect(
Collectors.groupingBy(
TableSummaryExportVo::getTableConcatDate, Collectors.reducing(BigDecimal.ZERO,
TableSummaryExportVo::getAmount,
BigDecimal::add)
)
);
list.parallelStream().forEach(data -> {
data.setSalesAmount(salesAmountMap.get(data.getTableConcatDate()));
data.setTotalSalesAmount(totalSalesAmountMap.get(data.getTableName()));
});
// 追加个空行用于处理表格样式
TableSummaryExportVo nullVo = new TableSummaryExportVo();
list.add(nullVo);

View File

@@ -23,7 +23,7 @@
AND `order`.trade_day = #{day}
AND `order`.paid_time IS NOT NULL
<if test="productName != null and productName != ''">
AND detail.name LIKE CONCAT('%',#{productName},'%')
AND detail.product_name LIKE CONCAT('%',#{productName},'%')
</if>
GROUP BY prodId
</select>
@@ -85,20 +85,21 @@
AND `order`.trade_day = #{day}
AND `order`.paid_time IS NOT NULL
<if test="productName != null and productName != ''">
AND detail.name LIKE CONCAT('%',#{productName},'%')
AND detail.product_name LIKE CONCAT('%',#{productName},'%')
</if>
</select>
<select id="summaryCountSingleDate" resultType="com.czg.order.vo.SaleSummaryCountVo">
SELECT
sum(tb_shop_prod_statistic.sale_count) AS saleCount,
sum(tb_shop_prod_statistic.sale_amount) AS saleAmount,
sum(tb_shop_prod_statistic.refund_count) AS refundCount,
sum(tb_shop_prod_statistic.refund_amount) AS refundAmount
sum(statistic.sale_count) AS saleCount,
sum(statistic.sale_amount) AS saleAmount,
sum(statistic.refund_count) AS refundCount,
sum(statistic.refund_amount) AS refundAmount
FROM
tb_shop_prod_statistic
tb_shop_prod_statistic statistic
INNER JOIN tb_product prod ON tb_shop_prod_statistic.prod_id = prod.id
WHERE
tb_shop_prod_statistic.shop_id = #{shopId}
AND tb_shop_prod_statistic.create_day = #{day}
statistic.shop_id = #{shopId}
AND statistic.create_day = #{day}
<if test="productName != null and productName != ''">
AND prod.name LIKE CONCAT('%',#{productName},'%')
</if>
@@ -106,16 +107,17 @@
<select id="summaryCountDateRange" resultType="com.czg.order.vo.SaleSummaryCountVo">
SELECT
sum(tb_shop_prod_statistic.sale_count) AS saleCount,
sum(tb_shop_prod_statistic.sale_amount) AS totalAmount,
sum(tb_shop_prod_statistic.refund_count) AS refundCount,
sum(tb_shop_prod_statistic.refund_amount) AS refundAmount
sum(statistic.sale_count) AS saleCount,
sum(statistic.sale_amount) AS totalAmount,
sum(statistic.refund_count) AS refundCount,
sum(statistic.refund_amount) AS refundAmount
FROM
tb_shop_prod_statistic
tb_shop_prod_statistic statistic
INNER JOIN tb_product prod ON statistic.prod_id = prod.id
WHERE
tb_shop_prod_statistic.shop_id = #{shopId}
AND tb_shop_prod_statistic.create_day &gt;= #{start}
AND tb_shop_prod_statistic.create_day &lt;= #{end}
statistic.shop_id = #{shopId}
AND statistic.create_day &gt;= #{start}
AND statistic.create_day &lt;= #{end}
<if test="productName != null and productName != ''">
AND prod.name LIKE CONCAT('%',#{productName},'%')
</if>

View File

@@ -5,49 +5,32 @@
<mapper namespace="com.czg.service.order.mapper.ShopTableOrderStatisticMapper">
<select id="findSummaryExportList" resultType="com.czg.order.vo.TableSummaryExportVo">
SELECT
t.product_name,
date_format(t.create_time, '%Y-%m-%d') as create_date,
if(t.table_name = 'NONE','银收客',t.table_name) as table_name,
concat(if(t.table_name = 'NONE','银收客',t.table_name),'-',date_format(t.create_time, '%Y-%m-%d')) as tableConcatDate,
t.category_name,
t.unit_name,
group_concat(distinct t.sku_name SEPARATOR ';') as sku_name,
sum(t.num) as num,
avg(t.unit_price) as unit_price,
sum(t.num*t.unit_price) as amount,
sum(-t.refund_num) as refund_num,
sum(-t.refund_num*t.unit_price) as refund_amount
FROM
(
SELECT
t1.product_id,
t2.table_code,
IF(t2.table_code is null or t2.table_code = '' or t6.NAME is null, 'NONE', t6.NAME) AS table_name,
t1.create_time,
t4.NAME AS category_name,
t3.name as product_name,
t5.NAME AS unit_name,
t1.sku_name,
t1.num,
t1.unit_price,
t1.refund_num as refund_num
FROM
tb_order_detail t1
LEFT JOIN tb_order_info t2 ON t1.order_id = t2.id
LEFT JOIN tb_product t3 ON t1.product_id = t3.id
LEFT JOIN tb_shop_prod_category t4 ON t3.category_id = t4.id
LEFT JOIN tb_shop_prod_unit t5 ON t3.unit_id = t5.id
LEFT JOIN tb_shop_table t6 ON t2.table_code = t6.table_code AND t1.shop_id = t6.shop_id and t6.table_code != '' and t6.table_code is not null
WHERE t1.shop_id = #{shopId}
<if test="beginDate != null and beginDate != ''">
AND t1.create_time >= str_to_date(#{beginDate}, '%Y-%m-%d %H:%i:%s')
</if>
<if test="endDate != null and endDate != ''">
and t1.create_time &lt;= str_to_date(#{endDate}, '%Y-%m-%d %H:%i:%s')
</if>
) t
group by t.product_id,date_format(t.create_time, '%Y-%m-%d'),t.table_name
order by t.table_name,t.table_code,date_format(t.create_time, '%Y-%m-%d'),t.category_name,t.product_id
SELECT `order`.table_code,
`order`.trade_day as create_date,
IF(`order`.table_code IS NULL OR `order`.table_code = '' OR `table`.NAME IS NULL, '吧台',
`table`.NAME) AS table_name,
category.`name` AS category_name,
product.`name` AS product_name,
unit.`name` AS unit_name,
detail.sku_name,
sum(detail.num - detail.return_num) AS num,
sum(detail.pay_amount) AS salesAmount,
sum(detail.refund_num) AS refund_num,
sum(detail.return_amount) AS refund_amount
FROM tb_order_info `order`
INNER JOIN tb_order_detail detail ON detail.order_id = `order`.id
AND detail.is_temporary = 0
LEFT JOIN tb_product product ON detail.product_id = product.id
LEFT JOIN tb_shop_prod_category category ON product.category_id = category.id
LEFT JOIN tb_shop_prod_unit unit ON product.unit_id = unit.id
LEFT JOIN tb_shop_table `table`
ON `order`.table_code = `table`.table_code AND `order`.shop_id = `table`.shop_id
AND `table`.table_code != '' AND `table`.table_code IS NOT NULL
WHERE `order`.shop_id = #{shopId}
AND `order`.paid_time IS NOT NULL
AND `order`.pay_mode != 'no-table'
AND `order`.trade_day >= #{beginDate}
AND `order`.trade_day &lt;= #{endDate}
group by `order`.trade_day, `order`.table_code, detail.product_id
</select>
</mapper>