云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package cn.iocoder.yudao.module.erp.dal.mysql.finance;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.payment.ErpFinancePaymentPageReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinancePaymentDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinancePaymentItemDO;
import cn.iocoder.yudao.module.erp.enums.ErpAuditStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
 
/**
 * ERP 付款单 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface ErpFinancePaymentMapper extends BaseMapperX<ErpFinancePaymentDO> {
 
    default PageResult<ErpFinancePaymentDO> selectPage(ErpFinancePaymentPageReqVO reqVO) {
        MPJLambdaWrapperX<ErpFinancePaymentDO> query = new MPJLambdaWrapperX<ErpFinancePaymentDO>()
                .likeIfPresent(ErpFinancePaymentDO::getNo, reqVO.getNo())
                .betweenIfPresent(ErpFinancePaymentDO::getPaymentTime, reqVO.getPaymentTime())
                .eqIfPresent(ErpFinancePaymentDO::getSupplierId, reqVO.getSupplierId())
                .eqIfPresent(ErpFinancePaymentDO::getCreator, reqVO.getCreator())
                .eqIfPresent(ErpFinancePaymentDO::getFinanceUserId, reqVO.getFinanceUserId())
                .eqIfPresent(ErpFinancePaymentDO::getAccountId, reqVO.getAccountId())
                .eqIfPresent(ErpFinancePaymentDO::getStatus, reqVO.getStatus())
                .likeIfPresent(ErpFinancePaymentDO::getRemark, reqVO.getRemark())
                .orderByDesc(ErpFinancePaymentDO::getId);
        if (reqVO.getBizNo() != null) {
            query.leftJoin(ErpFinancePaymentItemDO.class, ErpFinancePaymentItemDO::getPaymentId, ErpFinancePaymentDO::getId)
                    .eq(reqVO.getBizNo() != null, ErpFinancePaymentItemDO::getBizNo, reqVO.getBizNo())
                    .groupBy(ErpFinancePaymentDO::getId); // 避免 1 对多查询,产生相同的 1
        }
        return selectJoinPage(reqVO, ErpFinancePaymentDO.class, query);
    }
 
    default int updateByIdAndStatus(Long id, Integer status, ErpFinancePaymentDO updateObj) {
        return update(updateObj, new LambdaUpdateWrapper<ErpFinancePaymentDO>()
                .eq(ErpFinancePaymentDO::getId, id).eq(ErpFinancePaymentDO::getStatus, status));
    }
 
    default ErpFinancePaymentDO selectByNo(String no) {
        return selectOne(ErpFinancePaymentDO::getNo, no);
    }
 
    default List<ErpFinancePaymentDO> selectListByInvoiceIdAndStatus(Long invoiceId, Collection<Integer> statuses) {
        return selectList(new LambdaQueryWrapperX<ErpFinancePaymentDO>()
                .eq(ErpFinancePaymentDO::getInvoiceId, invoiceId)
                .in(ErpFinancePaymentDO::getStatus, statuses));
    }
 
    /**
     * 统计每张来票已付款金额(未审核 + 审批中 + 已审核,排除作废),用于校验剩余可付金额
     *
     * @param invoiceIds 来票编号列表
     * @return key 为来票编号,value 为已付款金额
     */
    default Map<Long, BigDecimal> selectPaymentPriceMapByInvoiceId(Collection<Long> invoiceIds) {
        if (CollUtil.isEmpty(invoiceIds)) {
            return Collections.emptyMap();
        }
        // SQL sum 查询
        List<Map<String, Object>> result = selectMaps(new QueryWrapper<ErpFinancePaymentDO>()
                .select("invoice_id, SUM(payment_price) AS total_payment_price")
                .in("status", ErpAuditStatus.PROCESS.getStatus(), // 未审核 + 审批中 + 已审核
                        ErpAuditStatus.APPROVING.getStatus(), ErpAuditStatus.APPROVE.getStatus())
                .groupBy("invoice_id")
                .in("invoice_id", invoiceIds));
        // 获得金额
        return convertMap(result, obj -> (Long) obj.get("invoice_id"), obj -> (BigDecimal) obj.get("total_payment_price"));
    }
 
    /**
     * 提交审批:置为审批中,并清空上一轮审核结果
     *
     * 用 UpdateWrapper 而非 updateById:MyBatis-Plus 默认更新策略忽略 null,
     * 直接 updateById 无法把审核不通过后残留的审核人/审核时间/审核意见清空。
     */
    default int submitAndResetAuditInfo(Long id, Integer status) {
        return update(null, new LambdaUpdateWrapper<ErpFinancePaymentDO>()
                .eq(ErpFinancePaymentDO::getId, id)
                .eq(ErpFinancePaymentDO::getStatus, ErpAuditStatus.PROCESS.getStatus())
                .set(ErpFinancePaymentDO::getStatus, status)
                .set(ErpFinancePaymentDO::getReviewerId, null)
                .set(ErpFinancePaymentDO::getReviewerName, null)
                .set(ErpFinancePaymentDO::getReviewTime, null)
                .set(ErpFinancePaymentDO::getReviewRemark, null));
    }
 
    /**
     * 审核(通过 / 不通过):写入审核结果、审核人与审核意见
     */
    default int auditFinancePayment(Long id, Integer status, Long reviewerId,
                                    String reviewerName, String reviewRemark) {
        return update(null, new LambdaUpdateWrapper<ErpFinancePaymentDO>()
                .eq(ErpFinancePaymentDO::getId, id)
                .eq(ErpFinancePaymentDO::getStatus, ErpAuditStatus.APPROVING.getStatus())
                .set(ErpFinancePaymentDO::getStatus, status)
                .set(ErpFinancePaymentDO::getReviewerId, reviewerId)
                .set(ErpFinancePaymentDO::getReviewerName, reviewerName)
                .set(ErpFinancePaymentDO::getReviewTime, LocalDateTime.now())
                .set(ErpFinancePaymentDO::getReviewRemark, reviewRemark));
    }
 
    /**
     * BPM 审批结束后回写付款单状态
     *
     * 仅当付款单仍处于「审批中」时更新,避免与旧审批路径重复覆盖。
     *
     * @param id           编号
     * @param status       目标状态(已审核 / 未审核)
     * @param reviewRemark 审核意见
     * @return 影响行数
     */
    default int updateAuditStatusByBpm(Long id, Integer status, String reviewRemark) {
        return update(null, new LambdaUpdateWrapper<ErpFinancePaymentDO>()
                .eq(ErpFinancePaymentDO::getId, id)
                .eq(ErpFinancePaymentDO::getStatus, ErpAuditStatus.APPROVING.getStatus())
                .set(ErpFinancePaymentDO::getStatus, status)
                .set(ErpFinancePaymentDO::getReviewTime, LocalDateTime.now())
                .set(ErpFinancePaymentDO::getReviewRemark, reviewRemark));
    }
 
}