xiaoyi
3 天以前 abf1c0a35d85d38239ff5d2ed746a4e4b797c9d1
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
package cn.iocoder.yudao.module.crm.service.contract;
 
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.contract.vo.command.CrmContractCommandPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.command.CrmContractCommandRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.command.CrmContractCommandSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractCommandDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractDO;
import cn.iocoder.yudao.module.crm.dal.mysql.contract.CrmContractCommandMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.contract.CrmContractMapper;
import cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.mes.api.workorder.MesProWorkOrderApi;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDateTime;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 
@Service
@Validated
public class CrmContractCommandServiceImpl implements CrmContractCommandService {
 
    private static final Integer STATUS_WAIT = 0;
    private static final Integer STATUS_DISPATCHED = 10;
    private static final Integer STATUS_DONE = 20;
    private static final Integer STATUS_CANCELED = 30;
 
    /** 目标模块:MES 生产工单(有真实落地单据,执行时确认工单) */
    private static final String TARGET_MODULE_MES_WORK_ORDER = "mes-work-order";
 
    @Resource
    private CrmContractCommandMapper commandMapper;
    @Resource
    private CrmContractMapper contractMapper;
    @Resource
    private MesProWorkOrderApi workOrderApi;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCommand(CrmContractCommandSaveReqVO createReqVO) {
        CrmContractDO contract = contractMapper.selectById(createReqVO.getContractId());
        if (contract == null) {
            throw exception(ErrorCodeConstants.CONTRACT_NOT_EXISTS);
        }
        if (StrUtil.isBlank(createReqVO.getContractNo())) {
            createReqVO.setContractNo(contract.getNo());
        }
        if (createReqVO.getOperateTime() == null) {
            createReqVO.setOperateTime(LocalDateTime.now());
        }
        if (createReqVO.getStatus() == null) {
            createReqVO.setStatus(STATUS_WAIT);
        }
        CrmContractCommandDO entity = BeanUtils.toBean(createReqVO, CrmContractCommandDO.class);
        commandMapper.insert(entity);
        return entity.getId();
    }
 
    @Override
    public void updateCommand(CrmContractCommandSaveReqVO updateReqVO) {
        CrmContractCommandDO exists = commandMapper.selectById(updateReqVO.getId());
        if (exists == null) {
            throw exception(ErrorCodeConstants.CRM_CONTRACT_COMMAND_NOT_EXISTS);
        }
        CrmContractCommandDO entity = BeanUtils.toBean(updateReqVO, CrmContractCommandDO.class);
        commandMapper.updateById(entity);
    }
 
    @Override
    public void deleteCommand(Long id) {
        CrmContractCommandDO exists = commandMapper.selectById(id);
        if (exists == null) {
            throw exception(ErrorCodeConstants.CRM_CONTRACT_COMMAND_NOT_EXISTS);
        }
        commandMapper.deleteById(id);
    }
 
    @Override
    public CrmContractCommandRespVO getCommand(Long id) {
        CrmContractCommandDO entity = commandMapper.selectById(id);
        return entity == null ? null : BeanUtils.toBean(entity, CrmContractCommandRespVO.class);
    }
 
    @Override
    public PageResult<CrmContractCommandRespVO> getCommandPage(CrmContractCommandPageReqVO pageReqVO) {
        return BeanUtils.toBean(commandMapper.selectPage(pageReqVO), CrmContractCommandRespVO.class);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void executeCommand(Long id) {
        CrmContractCommandDO exists = commandMapper.selectById(id);
        if (exists == null) {
            throw exception(ErrorCodeConstants.CRM_CONTRACT_COMMAND_NOT_EXISTS);
        }
        if (!STATUS_WAIT.equals(exists.getStatus()) && !STATUS_DISPATCHED.equals(exists.getStatus())) {
            throw exception(ErrorCodeConstants.CRM_COMMAND_EXECUTE_FAIL_STATUS);
        }
        // 跨模块真实下发:mes-work-order 目标模块,确认指令关联的工单
        if (TARGET_MODULE_MES_WORK_ORDER.equals(exists.getTargetModule())) {
            if (exists.getTargetId() == null) {
                throw exception(ErrorCodeConstants.CRM_COMMAND_DISPATCH_TARGET_MISSING);
            }
            workOrderApi.confirmWorkOrder(exists.getTargetId());
        }
        // 其余目标模块(mes-metering/srm-service)暂无真实落地单据,仅记录状态流转
        CrmContractCommandDO update = new CrmContractCommandDO();
        update.setId(id);
        update.setStatus(STATUS_DONE);
        commandMapper.updateById(update);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelCommand(Long id) {
        CrmContractCommandDO exists = commandMapper.selectById(id);
        if (exists == null) {
            throw exception(ErrorCodeConstants.CRM_CONTRACT_COMMAND_NOT_EXISTS);
        }
        if (!STATUS_WAIT.equals(exists.getStatus()) && !STATUS_DISPATCHED.equals(exists.getStatus())) {
            throw exception(ErrorCodeConstants.CRM_COMMAND_EXECUTE_FAIL_STATUS);
        }
        // 跨模块联动取消:已下发的 MES 工单同步取消
        if (TARGET_MODULE_MES_WORK_ORDER.equals(exists.getTargetModule())
                && exists.getTargetId() != null) {
            workOrderApi.cancelWorkOrder(exists.getTargetId());
        }
        CrmContractCommandDO update = new CrmContractCommandDO();
        update.setId(id);
        update.setStatus(STATUS_CANCELED);
        commandMapper.updateById(update);
    }
 
    @Override
    public Long countByContractId(Long contractId) {
        List<CrmContractCommandDO> list = commandMapper.selectListByContractId(contractId);
        return (long) list.size();
    }
 
}