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);
|
}
|
|
}
|