123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package com.fjhx.sale.flow;
- import cn.hutool.core.util.ObjectUtil;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
- import com.fjhx.common.enums.FlowStatusEnum;
- import com.fjhx.flow.core.FlowDelegate;
- import com.fjhx.sale.entity.contract.dto.ContractDto;
- import com.fjhx.sale.entity.contract.po.Contract;
- import com.fjhx.sale.entity.contract.po.ContractProduct;
- import com.fjhx.sale.service.contract.ContractProductService;
- import com.fjhx.sale.service.contract.ContractService;
- import com.ruoyi.common.core.domain.BaseIdPo;
- import com.ruoyi.common.exception.ServiceException;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import java.math.BigDecimal;
- import java.util.List;
- import java.util.Map;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- /**
- * 合同变更流程
- */
- @Component
- public class ContractUpdateFlow extends FlowDelegate {
- @Autowired
- private ContractFlow contractFlow;
- @Autowired
- private ContractService contractService;
- @Autowired
- private ContractProductService contractProductService;
- @Override
- public String getFlowKey() {
- return "contract_update_flow";
- }
- /**
- * 发起流程
- *
- * @param flowId 流程ID
- * @param submitData 采购付款数据
- * @return
- */
- @Override
- public Long start(Long flowId, JSONObject submitData) {
- ContractDto contract = submitData.toJavaObject(ContractDto.class);
- // 原合同id不能为空
- Long oldContractId = contract.getOldContractId();
- if (oldContractId == null) {
- throw new ServiceException("原合同id不能为空");
- }
- List<ContractProduct> list = contractProductService.list(q -> q.eq(ContractProduct::getContractId, oldContractId));
- // 赋值待处理数量
- if (CollectionUtils.isNotEmpty(list)) {
- List<ContractProduct> contractProductList = contract.getContractProductList();
- if (ObjectUtil.isEmpty(contractProductList)) {
- throw new ServiceException("没有合同产品");
- }
- Map<Long, ContractProduct> contractProductMap = contractProductList
- .stream()
- .peek(item -> {
- if (item.getId() == null) {
- throw new ServiceException("合同产品id不能为空");
- }
- })
- .collect(Collectors.toMap(BaseIdPo::getId, Function.identity()));
- for (ContractProduct item : list) {
- ContractProduct contractProduct = contractProductMap.get(item.getId());
- if (contractProduct == null) {
- throw new ServiceException("产品id为" + item.getId() + "未上传");
- }
- BigDecimal expendQuantity = item.getExpendQuantity().subtract(item.getQuantity().subtract(contractProduct.getQuantity()));
- contractProduct.setExpendQuantity(expendQuantity);
- }
- }
- return contractFlow.start(contract);
- }
- /**
- * 结束流程
- *
- * @param flowId 流程ID
- * @param businessId 业务ID
- * @param submitData 数据
- */
- @Override
- public void end(Long flowId, Long businessId, JSONObject submitData) {
- contractFlow.end(flowId, businessId, submitData);
- // 通过业务id查询合同数据
- Contract contract = contractService.getById(businessId);
- // 原合同改为作废状态
- Long oldContractId = contract.getOldContractId();
- Contract oldContract = contractService.getById(oldContractId);
- if (oldContract == null) {
- throw new ServiceException("原合同不存在");
- }
- oldContract.setStatus(FlowStatusEnum.UPDATE.getKey());
- oldContract.setIsChange("1");
- contractService.updateById(oldContract);
- }
- }
|