master:每日定时任务更新分类的排列顺序;

pull/1121/head
liujiang 2025-04-17 21:56:31 +08:00
parent 7a1d5d0a34
commit 2f1de57e17
6 changed files with 129 additions and 7 deletions

View File

@ -39,4 +39,10 @@ public class DailySaleController extends BaseController {
return R.ok();
}
@PostMapping("/cate-sort")
public R dailyCateSort(SysJob sysJob) {
task.categorySort();
return R.ok();
}
}

View File

@ -0,0 +1,22 @@
package com.ruoyi.quartz.dto;
import lombok.Data;
/**
* @author liujiang
* @version v1.0
* @date 2025/4/16 23:06
*/
@Data
public class WeekCateSaleDTO {
/**
* ID
*/
private Long prodCateId;
/**
*
*/
private Integer count;
}

View File

@ -0,0 +1,17 @@
package com.ruoyi.quartz.mapper;
import com.ruoyi.quartz.dto.WeekCateSaleDTO;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
import java.util.List;
/**
*
*
* @author ruoyi
*/
public interface WeekCateSaleMapper {
List<WeekCateSaleDTO> selectWeekCateSale(@Param("yesterday") Date yesterday, @Param("pastDate") Date pastDate);
}

View File

@ -3,15 +3,21 @@ package com.ruoyi.quartz.task;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysProductCategory;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.quartz.domain.DailySale;
import com.ruoyi.quartz.domain.DailySaleCustomer;
import com.ruoyi.quartz.domain.DailySaleProduct;
import com.ruoyi.quartz.dto.DailySaleCusDTO;
import com.ruoyi.quartz.dto.DailySaleDTO;
import com.ruoyi.quartz.dto.DailySaleProdDTO;
import com.ruoyi.quartz.dto.WeekCateSaleDTO;
import com.ruoyi.quartz.mapper.DailySaleCustomerMapper;
import com.ruoyi.quartz.mapper.DailySaleMapper;
import com.ruoyi.quartz.mapper.DailySaleProductMapper;
import com.ruoyi.quartz.mapper.WeekCateSaleMapper;
import com.ruoyi.system.mapper.SysProductCategoryMapper;
import com.ruoyi.xkt.mapper.StoreProductStorageMapper;
import com.ruoyi.xkt.mapper.StoreSaleDetailMapper;
import com.ruoyi.xkt.mapper.StoreSaleMapper;
@ -22,8 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -41,6 +46,8 @@ public class XktTask {
final StoreProductStorageMapper storageMapper;
final DailySaleCustomerMapper dailySaleCusMapper;
final DailySaleProductMapper dailySaleProdMapper;
final SysProductCategoryMapper prodCateMapper;
final WeekCateSaleMapper weekCateSaleMapper;
/**
* 1
@ -106,7 +113,55 @@ public class XktTask {
}
this.dailySaleProdMapper.insert(saleList.stream().map(x -> BeanUtil.toBean(x, DailySaleProduct.class)
.setVoucherDate(yesterday)).collect(Collectors.toList()));
}
@Transactional
public void categorySort() {
// 系统所有的商品分类
List<SysProductCategory> cateList = this.prodCateMapper.selectList(new LambdaQueryWrapper<SysProductCategory>()
.eq(SysProductCategory::getDelFlag, Constants.UNDELETED).eq(SysProductCategory::getStatus, Constants.UNDELETED));
if (CollectionUtils.isEmpty(cateList)) {
throw new ServiceException("商品分类不存在!", HttpStatus.ERROR);
}
// 根据LocalDate 获取当前日期前一天
final Date yesterday = Date.from(LocalDate.now().minusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
// 及当前日期前一天的前一周,并转为 Date 格式
final Date pastDate = Date.from(LocalDate.now().minusDays(8).atStartOfDay(ZoneId.systemDefault()).toInstant());
// 获取各项子分类最近一周的销售数量
List<WeekCateSaleDTO> weekCateSaleList = this.weekCateSaleMapper.selectWeekCateSale(yesterday, pastDate);
if (CollectionUtils.isEmpty(weekCateSaleList)) {
return;
}
// 将各个小项销售数量转化为map
Map<Long, Integer> itemCateCountMap = weekCateSaleList.stream().collect(Collectors.toMap(WeekCateSaleDTO::getProdCateId, WeekCateSaleDTO::getCount));
// 按照大类对应的各小类以此进行数量统计及排序
List<WeekCateSaleDTO> sortList = new ArrayList<>();
cateList.stream()
// 过滤掉父级为0的分类以及父级为1的分类父级为1的为子分类
.filter(x -> !Objects.equals(x.getParentId(), 0L) && !Objects.equals(x.getParentId(), 1L))
.collect(Collectors.groupingBy(SysProductCategory::getParentId))
.forEach((parentId, itemList) -> sortList.add(new WeekCateSaleDTO() {{
setCount(itemList.stream().mapToInt(x -> itemCateCountMap.getOrDefault(x.getId(), 0)).sum());
setProdCateId(parentId);
}}));
// 按照大类的数量倒序排列
sortList.sort(Comparator.comparing(WeekCateSaleDTO::getCount).reversed());
Map<Long, Integer> topCateSortMap = new LinkedHashMap<>();
// 按照sortList的顺序结合 topCateMap 依次更新SysProductCategory 的 sortNum的排序
for (int i = 0; i < sortList.size(); i++) {
topCateSortMap.put(sortList.get(i).getProdCateId(), i + 1);
}
// 顶级分类的数量
Integer topCateSize = Math.toIntExact(cateList.stream().filter(x -> Objects.equals(x.getParentId(), 1L)).count());
// 次级分类列表
List<SysProductCategory> updateList = cateList.stream().filter(x -> Objects.equals(x.getParentId(), 1L))
// 如果存在具体销售数量,则按照具体销售数量排序,否则将排序置为最大值
.peek(x -> x.setOrderNum(topCateSortMap.getOrDefault(x.getId(), topCateSize)))
.collect(Collectors.toList());
this.prodCateMapper.updateById(updateList);
}
}

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.quartz.mapper.WeekCateSaleMapper">
<select id="selectWeekCateSale" resultType="com.ruoyi.quartz.dto.WeekCateSaleDTO">
SELECT
sp.prod_cate_id,
COUNT( prod_cate_id ) AS count
FROM
store_sale_detail sdd
JOIN store_product sp ON sdd.store_prod_id = sp.id
WHERE
sdd.del_flag = 0
AND sdd.voucher_date BETWEEN #{pastDate} AND #{yesterday}
GROUP BY
sp.prod_cate_id
ORDER BY
COUNT( sp.prod_cate_id ) DESC
</select>
</mapper>

View File

@ -18,10 +18,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;
@ -110,8 +107,10 @@ public class SysProductCategoryServiceImpl implements ISysProductCategoryService
@Transactional(readOnly = true)
public List<ProdCateListResDTO> selectList(ProdCateListDTO listDTO) {
LambdaQueryWrapper<SysProductCategory> queryWrapper = new LambdaQueryWrapper<SysProductCategory>()
// 筛选第一级菜单
.ne(SysProductCategory::getParentId, 0L)
.eq(SysProductCategory::getDelFlag, Constants.UNDELETED)
.orderByAsc(SysProductCategory::getOrderNum);
.orderByAsc(Arrays.asList(SysProductCategory::getOrderNum, SysProductCategory::getId));
if (StringUtils.isNotBlank(listDTO.getName())) {
queryWrapper.like(SysProductCategory::getName, listDTO.getName());
}