liu
18 小时以前 79c416ea8a72aa9469deb14272135cca8dc01aac
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package cn.iocoder.yudao.module.crm.service.refund;
 
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.crm.controller.admin.refund.vo.CrmRefundPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.refund.vo.CrmRefundRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.refund.vo.CrmRefundSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.refund.CrmRefundDO;
import cn.iocoder.yudao.module.crm.dal.mysql.customer.CrmCustomerMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.refund.CrmRefundMapper;
import cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum;
import cn.iocoder.yudao.module.system.api.approval.ApprovalConfigApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Set;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
 
@Service
@Validated
@Slf4j
public class CrmRefundServiceImpl implements CrmRefundService {
 
    /**
     * CRM 退费单审批的业务类型编码
     *
     * 与 system_approval_config.biz_type 对应,审批人在「系统管理 - 审批配置」中维护。
     */
    private static final String REFUND_APPROVE_BIZ_TYPE = "refund_approve";
 
    @Resource
    private CrmRefundMapper refundMapper;
    @Resource
    private CrmCustomerMapper customerMapper;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private ApprovalConfigApi approvalConfigApi;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createRefund(CrmRefundSaveReqVO createReqVO) {
        if (StrUtil.isBlank(createReqVO.getRefundNo())) {
            createReqVO.setRefundNo(generateRefundNo());
        } else if (refundMapper.selectByRefundNo(createReqVO.getRefundNo()) != null) {
            throw exception(ErrorCodeConstants.CRM_REFUND_NO_DUPLICATE);
        }
        CrmCustomerDO customer = customerMapper.selectById(createReqVO.getCustomerId());
        if (customer == null) {
            throw exception(ErrorCodeConstants.CUSTOMER_NOT_EXISTS);
        }
        if (StrUtil.isBlank(createReqVO.getCustomerName())) {
            createReqVO.setCustomerName(customer.getName());
        }
        if (createReqVO.getApplyTime() == null) {
            createReqVO.setApplyTime(LocalDateTime.now());
        }
        createReqVO.setAuditStatus(CrmAuditStatusEnum.DRAFT.getStatus()); // 默认为未提交状态
        CrmRefundDO entity = BeanUtils.toBean(createReqVO, CrmRefundDO.class);
        refundMapper.insert(entity);
        return entity.getId();
    }
 
    @Override
    public void updateRefund(CrmRefundSaveReqVO updateReqVO) {
        // 1. 校验存在 + 审核通过的退费单不允许修改
        CrmRefundDO refund = validateRefundExists(updateReqVO.getId());
        if (ObjUtil.equal(refund.getAuditStatus(), CrmAuditStatusEnum.APPROVE.getStatus())) {
            throw exception(ErrorCodeConstants.CRM_REFUND_UPDATE_FAIL_APPROVE);
        }
        // 2. 更新退费单
        CrmRefundDO entity = BeanUtils.toBean(updateReqVO, CrmRefundDO.class);
        entity.setAuditStatus(null); // 审批状态只能由提交/审核变更
        refundMapper.updateById(entity);
    }
 
    @Override
    public void deleteRefund(Long id) {
        // 校验存在 + 审核通过的退费单不允许删除
        CrmRefundDO refund = validateRefundExists(id);
        if (ObjUtil.equal(refund.getAuditStatus(), CrmAuditStatusEnum.APPROVE.getStatus())) {
            throw exception(ErrorCodeConstants.CRM_REFUND_DELETE_FAIL_APPROVE);
        }
        refundMapper.deleteById(id);
    }
 
    @Override
    public CrmRefundRespVO getRefund(Long id) {
        CrmRefundDO entity = refundMapper.selectById(id);
        return entity == null ? null : BeanUtils.toBean(entity, CrmRefundRespVO.class);
    }
 
    @Override
    public PageResult<CrmRefundRespVO> getRefundPage(CrmRefundPageReqVO pageReqVO) {
        return BeanUtils.toBean(refundMapper.selectPage(pageReqVO), CrmRefundRespVO.class);
    }
 
    @Override
    public List<CrmRefundRespVO> getRefundList(CrmRefundPageReqVO reqVO) {
        return BeanUtils.toBean(refundMapper.selectList(reqVO), CrmRefundRespVO.class);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitRefund(Long id) {
        // 1. 校验存在 + 可提交状态(未提交 / 审核不通过)
        CrmRefundDO refund = validateRefundExists(id);
        if (ObjUtil.notEqual(refund.getAuditStatus(), CrmAuditStatusEnum.DRAFT.getStatus())
                && ObjUtil.notEqual(refund.getAuditStatus(), CrmAuditStatusEnum.REJECT.getStatus())) {
            throw exception(ErrorCodeConstants.CRM_REFUND_SUBMIT_FAIL_NOT_EDITABLE);
        }
 
        // 2. 校验审批配置已启用且配置了有效审批人(未配置时抛出带明确提示的业务异常)
        approvalConfigApi.validateApprovalEnabledAndGetApprovers(REFUND_APPROVE_BIZ_TYPE);
 
        // 3. 状态置为审批中,并清空上一轮审核结果,避免残留「审核不通过」的原因
        refundMapper.submitAndResetAuditInfo(id, CrmAuditStatusEnum.PROCESS.getStatus());
    }
 
    @Override
    public Set<Long> getRefundApproverUserIds() {
        return approvalConfigApi.getApproverUserIds(REFUND_APPROVE_BIZ_TYPE);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditRefund(Long id, Boolean pass, String reviewRemark) {
        // 1. 校验存在
        CrmRefundDO refund = validateRefundExists(id);
        // 2. 校验处于审批中状态
        if (ObjUtil.notEqual(refund.getAuditStatus(), CrmAuditStatusEnum.PROCESS.getStatus())) {
            throw exception(ErrorCodeConstants.CRM_REFUND_AUDIT_FAIL_NOT_PROCESS);
        }
        // 3. 校验当前登录用户是该业务类型的审批人(或签:任一人均可审核)
        Long userId = getLoginUserId();
        approvalConfigApi.validateApprover(REFUND_APPROVE_BIZ_TYPE, userId);
        // 4. 审核不通过:必须填写原因,状态置为审核不通过,退回提交人修改后可重新提交
        if (!Boolean.TRUE.equals(pass)) {
            if (StrUtil.isBlank(reviewRemark)) {
                throw exception(ErrorCodeConstants.CRM_REFUND_AUDIT_REJECT_REASON_REQUIRED);
            }
            refundMapper.auditRefund(id, CrmAuditStatusEnum.REJECT.getStatus(),
                    userId, getUserNickname(userId), reviewRemark);
            return;
        }
        // 5. 审核通过:记录审核人与审核意见,状态置为审核通过
        refundMapper.auditRefund(id, CrmAuditStatusEnum.APPROVE.getStatus(),
                userId, getUserNickname(userId), reviewRemark);
    }
 
    private CrmRefundDO validateRefundExists(Long id) {
        CrmRefundDO refund = refundMapper.selectById(id);
        if (refund == null) {
            throw exception(ErrorCodeConstants.CRM_REFUND_NOT_EXISTS);
        }
        return refund;
    }
 
    /**
     * 获取用户昵称
     *
     * @param userId 用户编号
     * @return 昵称,用户不存在时返回 null
     */
    private String getUserNickname(Long userId) {
        if (userId == null) {
            return null;
        }
        AdminUserRespDTO user = adminUserApi.getUser(userId);
        return user == null ? null : user.getNickname();
    }
 
    private String generateRefundNo() {
        return "RF" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
                + RandomUtil.randomNumbers(4);
    }
 
}