Pu Zhibing
2025-04-03 1f09f6daaf73bc83cceb4ae22b862b7b365635cf
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/controller/ShopBalanceStatementController.java
@@ -1,25 +1,38 @@
package com.ruoyi.other.controller;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.other.api.domain.Shop;
import com.ruoyi.other.api.domain.ShopBalanceStatement;
import com.ruoyi.other.api.domain.ShopBalanceStatementCopy;
import com.ruoyi.other.api.domain.ShopWithdraw;
import com.ruoyi.other.dto.ShopBalanceDto;
import com.ruoyi.other.mapper.ShopBalanceStatementCopyMapper;
import com.ruoyi.other.mapper.ShopBalanceStatementMapper;
import com.ruoyi.other.service.ShopBalanceStatementService;
import com.ruoyi.other.service.ShopService;
import com.ruoyi.other.service.ShopWithdrawService;
import com.ruoyi.other.vo.ShopBalanceStatementDetailPage;
import com.ruoyi.other.vo.ShopCommission;
import com.ruoyi.other.vo.ShopCommissionDetail;
import com.ruoyi.other.vo.ShopCommissionStatisticsVO;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@@ -27,13 +40,15 @@
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collection;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.List;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
 * <p>
 *  前端控制器
 * 前端控制器
 * </p>
 *
 * @author luodangjia
@@ -53,10 +68,11 @@
    private ShopService shopService;
    @Resource
    private SysUserClient sysUserClient;
    @Resource
    private ShopBalanceStatementCopyMapper shopBalanceStatementCopyMapper;
    @Resource
    private ShopWithdrawService shopWithdrawService;
    /**
     * 门店分佣统计
@@ -65,74 +81,93 @@
    @ApiOperation("门店分佣统计")
    public R<ShopCommissionStatisticsVO> shopCommissionStatistics(@ApiParam("页码") @RequestParam Integer pageNum,
                                                                  @ApiParam("每一页数据大小") Integer pageSize,
                                                                  ShopBalanceStatement shopBalanceStatement){
                                                                  ShopBalanceStatement shopBalanceStatement) {
        ShopCommissionStatisticsVO shopCommissionStatisticsVO = shopBalanceStatementService.shopCommissionStatistics(Page.of(pageNum, pageSize), shopBalanceStatement);
        return R.ok(shopCommissionStatisticsVO);
    }
    /**
     * 导出门店分佣统计
     */
    @GetMapping("/export")
    @ApiOperation("导出门店分佣统计")
    public void export(HttpServletResponse response , ShopBalanceStatement shopBalanceStatement){
    public void export(HttpServletResponse response, ShopBalanceStatement shopBalanceStatement) {
        ShopCommissionStatisticsVO shopCommissionStatisticsVO = shopBalanceStatementService.shopCommissionStatistics(Page.of(1, Integer.MAX_VALUE), shopBalanceStatement);
        IPage<ShopBalanceStatement> statementIPage = shopCommissionStatisticsVO.getStatementIPage();
        ExcelUtil<ShopBalanceStatement> util = new ExcelUtil<>(ShopBalanceStatement.class);
        util.exportExcel(response, statementIPage.getRecords(), "用户积分统计");
    }
    @GetMapping("/commissionStatistics/list")
    @ApiOperation(value = "门店余额明细", notes = "门店余额明细", tags = {"门店后台"})
    public R<Page<ShopBalanceStatement>> shopCommissionStatisticslist(@ApiParam("页码") @RequestParam Integer pageNum,
                                                                  @ApiParam("每一页数据大小") Integer pageSize,
                                                                  ShopBalanceStatement shopBalanceStatement){
                                                                      @ApiParam("每一页数据大小") Integer pageSize,
                                                                      ShopBalanceStatement shopBalanceStatement) {
        Long userid = tokenService.getLoginUser().getUserid();
        R<SysUser> sysUserR = sysUserClient.getSysUser(userid);
        SysUser sysUser = sysUserR.getData();
        Integer objectId = sysUser.getObjectId();
        Page<ShopBalanceStatement> page = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId, objectId).eq(shopBalanceStatement.getType() != null, ShopBalanceStatement::getType, shopBalanceStatement.getType())
                .orderByDesc(ShopBalanceStatement::getCreateTime)
                .page(Page.of(pageNum, pageSize));
        Page<ShopBalanceStatement> page = shopBalanceStatementService.shopCommissionStatisticslist(objectId, shopBalanceStatement.getType(), pageNum, pageSize);
        page.getRecords().forEach(s->s.setVariableAmount(s.getVariableAmount().multiply(new BigDecimal(s.getChangeDirection()))));
        return R.ok(page);
    }
    @GetMapping("/commissionStatistics/info")
    @ApiOperation(value = "门店余额明细上方数据", notes = "门店余额明细", tags = {"门店后台"})
    public R<ShopBalanceDto> shopCommissionStatisticsinfo(Integer type){
    public R<ShopBalanceDto> shopCommissionStatisticsinfo(Integer type) {
        Integer objectId = tokenService.getLoginUser().getSysUser().getObjectId();
        ShopBalanceDto dto = new ShopBalanceDto();
        Shop byId = shopService.getById(objectId);
        if (type==null){
            List<ShopBalanceStatement> list = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId,objectId).eq(ShopBalanceStatement::getType, 3).list();
            List<ShopBalanceStatement> list1 = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId,objectId).eq(ShopBalanceStatement::getType, 1).list();
        if (type == null) {
            List<ShopBalanceStatement> list = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId, objectId).eq(ShopBalanceStatement::getType, 3).list();
            List<ShopBalanceStatement> list1 = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId, objectId).eq(ShopBalanceStatement::getType, 1).list();
            List<ShopBalanceStatementCopy> list3 = shopBalanceStatementCopyMapper.selectList(new LambdaQueryWrapper<ShopBalanceStatementCopy>().eq(ShopBalanceStatementCopy::getShopId, objectId).eq(ShopBalanceStatementCopy::getType, 3));
            List<ShopBalanceStatementCopy> list4 = shopBalanceStatementCopyMapper.selectList(new LambdaQueryWrapper<ShopBalanceStatementCopy>().eq(ShopBalanceStatementCopy::getShopId, objectId).eq(ShopBalanceStatementCopy::getType, 1));
            BigDecimal bigDecimal1 = new BigDecimal(0);
            for (ShopBalanceStatement shopBalanceStatement : list) {
                bigDecimal1 = bigDecimal1.add(shopBalanceStatement.getVariableAmount());
            }
            for (ShopBalanceStatementCopy shopBalanceStatement : list3) {
                bigDecimal1 = bigDecimal1.add(shopBalanceStatement.getVariableAmount());
            }
            for (ShopBalanceStatement shopBalanceStatement : list1) {
                bigDecimal1 = bigDecimal1.add(shopBalanceStatement.getVariableAmount());
            }
            for (ShopBalanceStatementCopy shopBalanceStatement : list4) {
                bigDecimal1 = bigDecimal1.add(shopBalanceStatement.getVariableAmount());
            }
            dto.setBigDecimal1(bigDecimal1);
            BigDecimal bigDecimal2= new BigDecimal(0);
            BigDecimal bigDecimal2 = new BigDecimal(0);
            for (ShopBalanceStatement shopBalanceStatement : list) {
                bigDecimal2 = bigDecimal2.add(shopBalanceStatement.getVariableAmount());
            }
            for (ShopBalanceStatementCopy shopBalanceStatement : list3) {
                bigDecimal2 = bigDecimal2.add(shopBalanceStatement.getVariableAmount());
            }
            dto.setBigDecimal2(bigDecimal2);
            BigDecimal bigDecimal3= new BigDecimal(0);
            BigDecimal bigDecimal3 = new BigDecimal(0);
            for (ShopBalanceStatement shopBalanceStatement : list1) {
                bigDecimal3 = bigDecimal3.add(shopBalanceStatement.getVariableAmount());
            }
            for (ShopBalanceStatementCopy shopBalanceStatement : list4) {
                bigDecimal3 = bigDecimal3.add(shopBalanceStatement.getVariableAmount());
            }
            dto.setBigDecimal3(bigDecimal3);
            List<ShopBalanceStatement> list2 = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId,objectId).eq(ShopBalanceStatement::getType, 2).list();
            BigDecimal bigDecimal4= new BigDecimal(0);
            List<ShopBalanceStatement> list2 = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId, objectId).eq(ShopBalanceStatement::getType, 2).list();
            List<ShopBalanceStatementCopy> list5 = shopBalanceStatementCopyMapper.selectList(new LambdaQueryWrapper<ShopBalanceStatementCopy>().eq(ShopBalanceStatementCopy::getShopId, objectId).eq(ShopBalanceStatementCopy::getType, 2));
            BigDecimal bigDecimal4 = new BigDecimal(0);
            for (ShopBalanceStatement shopBalanceStatement : list2) {
                bigDecimal4 = bigDecimal4.add(shopBalanceStatement.getVariableAmount());
            }
            for (ShopBalanceStatementCopy shopBalanceStatement : list5) {
                bigDecimal4 = bigDecimal4.add(shopBalanceStatement.getVariableAmount());
            }
            dto.setBigDecimal4(bigDecimal4);
@@ -140,13 +175,17 @@
            dto.setBigDecimal5(byId.getCanWithdrawMoney());
            dto.setBigDecimal6(byId.getWithdrawMoney());
            return R.ok(dto);
        }else{
            switch (type){
        } else {
            switch (type) {
                case 1:
                    // 关联用户分拥
                    BigDecimal bigDecimal = new BigDecimal("0");
                    List<ShopBalanceStatement> list1 = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId,objectId).eq(ShopBalanceStatement::getType, 1).list();
                    List<ShopBalanceStatement> list1 = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId, objectId).eq(ShopBalanceStatement::getType, 1).list();
                    List<ShopBalanceStatementCopy> list4 = shopBalanceStatementCopyMapper.selectList(new LambdaQueryWrapper<ShopBalanceStatementCopy>().eq(ShopBalanceStatementCopy::getShopId, objectId).eq(ShopBalanceStatementCopy::getType, 1));
                    for (ShopBalanceStatement shopBalanceStatement : list1) {
                        bigDecimal = bigDecimal.add(shopBalanceStatement.getVariableAmount());
                    }
                    for (ShopBalanceStatementCopy shopBalanceStatement : list4) {
                        bigDecimal = bigDecimal.add(shopBalanceStatement.getVariableAmount());
                    }
                    dto.setBigDecimal3(bigDecimal);
@@ -154,9 +193,13 @@
                    break;
                case 2:
                    // 下级门店分佣
                    BigDecimal bigDecimal2= new BigDecimal(0);
                    List<ShopBalanceStatement> list2 = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId,objectId).eq(ShopBalanceStatement::getType, 2).list();
                    BigDecimal bigDecimal2 = new BigDecimal(0);
                    List<ShopBalanceStatement> list2 = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId, objectId).eq(ShopBalanceStatement::getType, 2).list();
                    List<ShopBalanceStatementCopy> list5 = shopBalanceStatementCopyMapper.selectList(new LambdaQueryWrapper<ShopBalanceStatementCopy>().eq(ShopBalanceStatementCopy::getShopId, objectId).eq(ShopBalanceStatementCopy::getType, 2));
                    for (ShopBalanceStatement shopBalanceStatement : list2) {
                        bigDecimal2 = bigDecimal2.add(shopBalanceStatement.getVariableAmount());
                    }
                    for (ShopBalanceStatementCopy shopBalanceStatement : list5) {
                        bigDecimal2 = bigDecimal2.add(shopBalanceStatement.getVariableAmount());
                    }
                    dto.setBigDecimal4(bigDecimal2);
@@ -164,9 +207,13 @@
                    break;
                case 3:
                    // 服务费分佣
                    List<ShopBalanceStatement> list = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId,objectId).eq(ShopBalanceStatement::getType, 3).list();
                    BigDecimal bigDecimal3= new BigDecimal(0);
                    List<ShopBalanceStatement> list = shopBalanceStatementService.lambdaQuery().eq(ShopBalanceStatement::getShopId, objectId).eq(ShopBalanceStatement::getType, 3).list();
                    List<ShopBalanceStatementCopy> list6 = shopBalanceStatementCopyMapper.selectList(new LambdaQueryWrapper<ShopBalanceStatementCopy>().eq(ShopBalanceStatementCopy::getShopId, objectId).eq(ShopBalanceStatementCopy::getType, 3));
                    BigDecimal bigDecimal3 = new BigDecimal(0);
                    for (ShopBalanceStatement shopBalanceStatement : list) {
                        bigDecimal3 = bigDecimal3.add(shopBalanceStatement.getVariableAmount());
                    }
                    for (ShopBalanceStatementCopy shopBalanceStatement : list6) {
                        bigDecimal3 = bigDecimal3.add(shopBalanceStatement.getVariableAmount());
                    }
                    dto.setBigDecimal2(bigDecimal3);
@@ -182,47 +229,255 @@
    }
    @PostMapping("/getList")
    public R<List<ShopBalanceStatement>> getList(@RequestBody ShopBalanceStatement shopBalanceStatement){
    public R<List<ShopBalanceStatement>> getList(@RequestBody ShopBalanceStatement shopBalanceStatement) {
        return R.ok(shopBalanceStatementMapper.selectShopBalanceStatementList(shopBalanceStatement));
    }
    /**
     * 保存门店余额流水
     *
     * @param shopBalanceStatement
     */
    @PostMapping("/saveShopBalanceStatement")
    public void saveShopBalanceStatement(@RequestBody ShopBalanceStatement shopBalanceStatement){
    public void saveShopBalanceStatement(@RequestBody ShopBalanceStatement shopBalanceStatement) {
        shopBalanceStatementService.save(shopBalanceStatement);
    }
    @PostMapping("/saveShopBalanceStatementCopy")
    public void saveShopBalanceStatementCopy(@RequestBody ShopBalanceStatementCopy shopBalanceStatementCopy) {
        shopBalanceStatementCopyMapper.insert(shopBalanceStatementCopy);
    }
    @DeleteMapping("/deleteShopBalanceStatementCopy")
    public R<Boolean> deleteShopBalanceStatementCopy(@RequestParam("orderId") Long orderId, @RequestParam("type") List<Integer> type) {
        shopBalanceStatementCopyMapper.delete(new LambdaQueryWrapper<ShopBalanceStatementCopy>().in(ShopBalanceStatementCopy::getType, type)
                .eq(ShopBalanceStatementCopy::getObjectId, orderId));
        return R.ok();
    }
    @DeleteMapping("/deleteShopBalanceStatementCopyByIds")
    public R<Boolean> deleteShopBalanceStatementCopyByIds(@RequestParam("ids") List<Long> ids){
        shopBalanceStatementCopyMapper.deleteBatchIds(ids);
        return R.ok();
    }
    @PostMapping("/getShopBalanceStatementCopy")
    public R<List<ShopBalanceStatementCopy>> getShopBalanceStatementCopy(@RequestParam("orderId") Long orderId, @RequestParam("type") List<Integer> type) {
        List<ShopBalanceStatementCopy> shopBalanceStatementCopies = shopBalanceStatementCopyMapper.selectList(new LambdaQueryWrapper<ShopBalanceStatementCopy>().in(ShopBalanceStatementCopy::getType, type)
                .eq(ShopBalanceStatementCopy::getObjectId, orderId));
        return R.ok(shopBalanceStatementCopies);
    }
    /**
     * 获取门店流水数据
     *
     * @param types
     * @param objectId
     * @return
     */
    @PostMapping("/getShopBalanceStatementList")
    public R<List<ShopBalanceStatement>> getShopBalanceStatementList(@RequestParam("types") Collection<Integer> types, @RequestParam(value = "objectId",required = false) Long objectId){
    public R<List<ShopBalanceStatement>> getShopBalanceStatementList(@RequestParam("types") Collection<Integer> types, @RequestParam(value = "objectId", required = false) Long objectId) {
        List<ShopBalanceStatement> list = shopBalanceStatementService.list(new LambdaQueryWrapper<ShopBalanceStatement>()
                .eq(objectId != null,ShopBalanceStatement::getObjectId, objectId)
                .in(types !=null && !types.isEmpty(),ShopBalanceStatement::getType, types));
                .eq(objectId != null, ShopBalanceStatement::getObjectId, objectId)
                .in(types != null && !types.isEmpty(), ShopBalanceStatement::getType, types));
        return R.ok(list);
    }
    @PostMapping("/getShopBalanceStatementList2")
    public R<List<ShopBalanceStatement>> getShopBalanceStatementList2(@RequestParam("type") Integer type,
                                                                      @RequestParam(value = "date") LocalDateTime date){
        List<ShopBalanceStatement> list = shopBalanceStatementService.list(new LambdaQueryWrapper<ShopBalanceStatement>()
    public R<List<ShopBalanceStatement>> getShopBalanceStatementList2(@RequestParam("type") Integer type, @RequestParam(value = "shopId") Integer shopId,
                                                                      @RequestParam(value = "date") LocalDateTime date) {
        LambdaQueryWrapper<ShopBalanceStatement> queryWrapper = new LambdaQueryWrapper<ShopBalanceStatement>()
                .eq(ShopBalanceStatement::getType, type)
                .between(ShopBalanceStatement::getCreateTime, date.with(LocalTime.MIN), date.with(LocalTime.MAX)));
                .between(ShopBalanceStatement::getCreateTime, date.with(LocalTime.MIN), date.with(LocalTime.MAX));
        if (null != shopId && 0 != shopId) {
            queryWrapper.eq(ShopBalanceStatement::getShopId, shopId);
        }else{
            List<Integer> shipIds = shopService.list(new LambdaQueryWrapper<Shop>().eq(Shop::getDelFlag, 0)).stream().map(Shop::getId).collect(Collectors.toList());
            if(shipIds.size() > 0){
                queryWrapper.in(ShopBalanceStatement::getShopId, shipIds);
            }else{
                queryWrapper.eq(ShopBalanceStatement::getShopId, -1);
            }
        }
        List<ShopBalanceStatement> list = shopBalanceStatementService.list(queryWrapper);
        for (ShopBalanceStatement shopBalanceStatement : list) {
            shopBalanceStatement.setVariableAmount(shopBalanceStatement.getVariableAmount().multiply(new BigDecimal(shopBalanceStatement.getChangeDirection())));
        }
        LambdaQueryWrapper<ShopBalanceStatementCopy> queryWrapper1 = new LambdaQueryWrapper<ShopBalanceStatementCopy>()
                .eq(ShopBalanceStatementCopy::getType, type)
                .between(ShopBalanceStatementCopy::getCreateTime, date.with(LocalTime.MIN), date.with(LocalTime.MAX));
        if (null != shopId && 0 != shopId) {
            queryWrapper1.eq(ShopBalanceStatementCopy::getShopId, shopId);
        }else{
            List<Integer> shipIds = shopService.list(new LambdaQueryWrapper<Shop>().eq(Shop::getDelFlag, 0)).stream().map(Shop::getId).collect(Collectors.toList());
            if(shipIds.size() > 0){
                queryWrapper1.in(ShopBalanceStatementCopy::getShopId, shipIds);
            }else{
                queryWrapper1.eq(ShopBalanceStatementCopy::getShopId, -1);
            }
        }
        List<ShopBalanceStatementCopy> list1 = shopBalanceStatementCopyMapper.selectList(queryWrapper1);
        for (ShopBalanceStatementCopy shopBalanceStatementCopy  : list1) {
            ShopBalanceStatement shopBalanceStatement = new ShopBalanceStatement();
            BeanUtils.copyProperties(shopBalanceStatementCopy, shopBalanceStatement);
            shopBalanceStatement.setVariableAmount(shopBalanceStatement.getVariableAmount().multiply(new BigDecimal(shopBalanceStatement.getChangeDirection())));
            list.add(shopBalanceStatement);
        }
        return R.ok(list);
    }
    /**
     * 获取门店佣金
     *
     * @param shopId
     * @return
     */
    @GetMapping("getShopCommission")
    @ApiOperation(value = "获取门店佣金", notes = "获取门店佣金", tags = {"小程序-个人中心-门店管理-门店钱包"})
    @Transactional
    public R<ShopCommission> getShopCommission(@RequestParam("shopId") Integer shopId) {
        // 合并查询逻辑
        List<ShopBalanceStatementCopy> copies = shopBalanceStatementCopyMapper.selectList(
                new LambdaQueryWrapper<ShopBalanceStatementCopy>()
                        .eq(ShopBalanceStatementCopy::getShopId, shopId));
        List<ShopBalanceStatement> statements = shopBalanceStatementService.list(
                new LambdaQueryWrapper<ShopBalanceStatement>()
                        .eq(ShopBalanceStatement::getShopId, shopId));
        // 合并计算
        Map<Integer, BigDecimal> result = Stream.concat(
                copies.stream().map(e -> Pair.of(e.getType(), e.getVariableAmount().multiply(new BigDecimal(e.getChangeDirection())))),
                statements.stream().map(e -> Pair.of(e.getType(), e.getVariableAmount().multiply(new BigDecimal(e.getChangeDirection()))))
        ).collect(Collectors.groupingBy(
                Pair::getKey,
                Collectors.mapping(Pair::getValue, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))
        ));
        // 待审核佣金
        BigDecimal waitingAudit = shopWithdrawService.list(new LambdaQueryWrapper<ShopWithdraw>()
                        .eq(ShopWithdraw::getShopId, shopId)
                        .eq(ShopWithdraw::getAuditStatus, 0))
                .stream()
                .map(ShopWithdraw::getMoney)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 店铺信息校验
        Shop shop = shopService.getById(shopId);
        if (shop == null) {
            throw new ServiceException("店铺不存在");
        }
        // 构建结果
        List<ShopWithdraw> list = shopWithdrawService.lambdaQuery().eq(ShopWithdraw::getShopId, shopId)
                .eq(ShopWithdraw::getAuditStatus, 0).list();
        BigDecimal withdrawAuditMoney = list.stream().map(ShopWithdraw::getMoney).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
        ShopCommission commission = new ShopCommission();
        commission.setCanWithdrawCommission(shop.getCanWithdrawMoney());
        commission.setWithdrawnCommission(shop.getWithdrawMoney().subtract(withdrawAuditMoney));
        commission.setCommissionAmount(
                result.getOrDefault(1, BigDecimal.ZERO)
                        .add(result.getOrDefault(2, BigDecimal.ZERO)
                                .add(result.getOrDefault(3, BigDecimal.ZERO))
                        ));
        commission.setWaitingAuditCommission(waitingAudit);
        commission.setTotalAmount(commission.getCommissionAmount());
        return R.ok(commission);
    }
    /**
     * 获取门店佣金明细
     */
    @GetMapping("/getshopCommissionStatisticsDetail")
    @ApiOperation(value = "门店返佣明细", notes = "门店返佣明细", tags = {"小程序-个人中心-门店管理-门店钱包"})
    public R<ShopBalanceStatementDetailPage> shopCommissionStatisticsDetail(@ApiParam("店铺id") Integer shopId,
                                                                            @ApiParam("类型") Integer type,
                                                                            @ApiParam("开始时间") String createTime,
                                                                            @ApiParam("页码") Integer pageNum,
                                                                            @ApiParam("每一页数据大小") Integer pageSize) {
        LocalDateTime startDate = null;
        LocalDateTime endDate = null;
        if (StringUtils.isNotEmpty(createTime)){
            startDate = LocalDateTime.parse(createTime + " 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            endDate = LocalDateTime.parse(createTime + " 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
        List<ShopBalanceStatementCopy> copies = shopBalanceStatementCopyMapper.selectList(
                new LambdaQueryWrapper<ShopBalanceStatementCopy>()
                        .eq(ShopBalanceStatementCopy::getShopId, shopId)
                        .eq(type != null, ShopBalanceStatementCopy::getType, type)
                        .between(startDate != null && endDate != null, ShopBalanceStatementCopy::getCreateTime,
                                startDate, endDate));
        List<ShopBalanceStatement> statements = shopBalanceStatementService.list(
                new LambdaQueryWrapper<ShopBalanceStatement>()
                        .eq(ShopBalanceStatement::getShopId, shopId)
                        .eq(type != null, ShopBalanceStatement::getType, type)
                        .between(startDate != null && endDate != null, ShopBalanceStatement::getCreateTime,
                                startDate, endDate));
        Map<Integer, String> itemMap = new HashMap<>();
        itemMap.put(1, "服务商分佣");
        itemMap.put(2, "高级服务商分佣");
        itemMap.put(3, "核销门店服务费");
        itemMap.put(4, "提现");
        List<ShopCommissionDetail> list = new ArrayList<>();
        for (ShopBalanceStatementCopy copy : copies) {
            ShopCommissionDetail detail = new ShopCommissionDetail();
            detail.setItem(itemMap.get(copy.getType()));
            detail.setTime(copy.getCreateTime());
            detail.setAmount(copy.getVariableAmount());
            if (copy.getChangeDirection() == -1) {
                detail.setType(2);
            } else {
                detail.setType(1);
            }
            list.add(detail);
        }
        for (ShopBalanceStatement statement : statements) {
            ShopCommissionDetail detail = new ShopCommissionDetail();
            detail.setItem(itemMap.get(statement.getType()));
            detail.setTime(statement.getCreateTime());
            detail.setAmount(statement.getVariableAmount());
            if (statement.getChangeDirection() == -1) {
                detail.setType(2);
            } else {
                detail.setType(1);
            }
            list.add(detail);
        }
        // 按时间排序加手动分页
        Integer total = list.size();
        list.sort(Comparator.comparing(ShopCommissionDetail::getTime).reversed());
        if (pageNum != null && pageSize != null) {
            int fromIndex = (pageNum - 1) * pageSize;
            int toIndex = Math.min(fromIndex + pageSize, list.size());
            list = list.subList(fromIndex, toIndex);
        }
        ShopBalanceStatementDetailPage page = new ShopBalanceStatementDetailPage();
        page.setRecords(list);
        page.setTotal(total);
        page.setPageSize(pageSize);
        page.setPageNum(pageNum);
        return R.ok(page);
    }
}