ContractRunner.java 9.5 KB
package vion.cron;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import vion.model.Contract;
import vion.model.ContractPayment;
import vion.model.Dictionary;
import vion.service.IContractPaymentService;
import vion.service.IContractService;
import vion.service.IDictionaryService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 从crm系统定时同步合同信息
 */
@Component
@Slf4j
public class ContractRunner {

    @Resource
    private IContractService contractService;
    @Resource
    private IDictionaryService dictionaryService;
    @Resource
    private IContractPaymentService contractPaymentService;

    @Scheduled(cron = "0 0 * * * *")
    public Boolean contractSync() {
        log.info("【开始】从crm系统同步合同信息");
        Set<String> existContractNoSet = contractService.list().stream().map(Contract::getContractNo).collect(Collectors.toSet());
        Map<String, Integer> contractTypeMap = dictionaryService.lambdaQuery().eq(Dictionary::getType, "contract_type").list()
                .stream().collect(Collectors.toMap(Dictionary::getValue, Dictionary::getKey));
        Map<String, Integer> payTypeMap = dictionaryService.lambdaQuery().eq(Dictionary::getType, "pay_type").list()
                .stream().collect(Collectors.toMap(Dictionary::getValue, Dictionary::getKey));

        List<Contract> insertContractList = new ArrayList<>();
        String url = "jdbc:sqlserver://47.92.144.255:1433;databaseName=UFDATA_001_2020;encrypt=false";
        String username = "vion-reader";
        String password = "vion-reader";
        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            String sql = "select t5.hetongpingshen_name as name,t1.hetongluru_name as contract_no,t1.hetongluru_char12 as type,t1.hetongluru_date01 as sign_date,t1.hetongluru_char01 as warranty_period,t1.hetongluru_start_date as maintain_sdate,t1.hetongluru_end_date as maintain_edate,t1.hetongluru_dec01 as total_amount,t5.hetongpingshen_dec02 as sign_ratio,t5.hetongpingshen_dec03 as arrive_ratio,t5.hetongpingshen_dec04 as system_check_ratio,t5.hetongpingshen_dec05 as project_check_ratio,t5.hetongpingshen_dec06 as warranty_ratio,t5.hetongpingshen_dec07 as maintain1_ratio,t5.hetongpingshen_dec09 as maintain2_ratio,t5.hetongpingshen_dec10 as maintain3_ratio,t2.cCusAbbName as customer_name,t4.cUser_Name as sale_name" +
                    " from tcu_hetongluru t1 left join Customer t2 on t1.account_id = t2.cCusCode left join tc_opportunity t3 on t1.hetongluru_char04 = t3.ufcode left join UA_User_Ex t4 on t1.owner_user_id = t4.cUser_Id left join tcu_hetongpingshen t5 on t1.hetongluru_name = t5.hetongpingshen_char05";
            try (Statement statement = connection.createStatement()) {
                ResultSet resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    String contractNo = resultSet.getString("contract_no");
                    if (!existContractNoSet.contains(contractNo)) {
                        Contract contract = new Contract();
                        String contractName = resultSet.getString("name");
                        if (StrUtil.isNotBlank(contractName)) {
                            contract.setName(resultSet.getString("name"));
                        } else {
                            contract.setName(contractNo);
                        }
                        contract.setContractNo(contractNo);
                        contract.setType(contractTypeMap.getOrDefault(resultSet.getString("type"), 0));
                        contract.setSignDate(resultSet.getDate("sign_date"));
                        String warrantyPeriod = resultSet.getString("warranty_period");
                        if (StrUtil.isNotBlank(warrantyPeriod) && warrantyPeriod.contains("个月")) {
                            String substring = warrantyPeriod.substring(0, (warrantyPeriod.length() - 2));
                            contract.setWarrantyPeriod(Integer.parseInt(substring));
                        }
                        contract.setMaintainSdate(resultSet.getDate("maintain_sdate"));
                        contract.setMaintainEdate(resultSet.getDate("maintain_edate"));
                        contract.setStatus(1);
                        contract.setTotalAmount(resultSet.getBigDecimal("total_amount"));
                        contract.setSignRatio(resultSet.getBigDecimal("sign_ratio"));
                        contract.setArriveRatio(resultSet.getBigDecimal("arrive_ratio"));
                        contract.setSystemCheckRatio(resultSet.getBigDecimal("system_check_ratio"));
                        contract.setProjectCheckRatio(resultSet.getBigDecimal("project_check_ratio"));
                        contract.setWarrantyRatio(resultSet.getBigDecimal("warranty_ratio"));
                        contract.setMaintainRatio1(resultSet.getBigDecimal("maintain1_ratio"));
                        contract.setMaintainRatio2(resultSet.getBigDecimal("maintain2_ratio"));
                        contract.setMaintainRatio3(resultSet.getBigDecimal("maintain3_ratio"));
                        contract.setPaidAmount(BigDecimal.ZERO);
                        contract.setReceivableAmount(BigDecimal.ZERO);
                        contract.setOutstandingAmount(resultSet.getBigDecimal("total_amount"));
                        contract.setSubject("北京文安智能技术股份有限公司");
                        contract.setCustomerName(resultSet.getString("customer_name"));
                        contract.setSaleName(resultSet.getString("sale_name"));
                        contract.setCreateUser(-1L);
                        contract.setModifyUser(-1L);



                        insertContractList.add(contract);
                    }
                }
            }
        } catch (SQLException e) {
            log.error("合同信息同步失败:", e);
            return false;
        }
        contractService.saveBatch(insertContractList, 500);

        // 合同付款比例
        List<ContractPayment> contractPaymentList = new ArrayList<>();
        for (Contract contract : insertContractList) {
            // 签订
            ContractPayment contractSignPayment = new ContractPayment();
            contractSignPayment.setContractId(contract.getId());
            contractSignPayment.setPaymentType(1);
            contractSignPayment.setPaymentRatio(null != contract.getSignRatio() ? contract.getSignRatio() : BigDecimal.ZERO);
            contractPaymentList.add(contractSignPayment);

            // 到货
            ContractPayment contractArrivePayment = new ContractPayment();
            contractArrivePayment.setContractId(contract.getId());
            contractArrivePayment.setPaymentType(2);
            contractArrivePayment.setPaymentRatio(null != contract.getArriveRatio() ? contract.getArriveRatio() : BigDecimal.ZERO);
            contractPaymentList.add(contractArrivePayment);

            // 系统验收
            ContractPayment contractSystemCheckPayment = new ContractPayment();
            contractSystemCheckPayment.setContractId(contract.getId());
            contractSystemCheckPayment.setPaymentType(3);
            contractSystemCheckPayment.setPaymentRatio(null != contract.getSystemCheckRatio() ? contract.getSystemCheckRatio() : BigDecimal.ZERO);
            contractPaymentList.add(contractSystemCheckPayment);

            // 项目验收
            ContractPayment contractProjectCheckPayment = new ContractPayment();
            contractProjectCheckPayment.setContractId(contract.getId());
            contractProjectCheckPayment.setPaymentType(4);
            contractProjectCheckPayment.setPaymentRatio(null != contract.getProjectCheckRatio() ? contract.getProjectCheckRatio() : BigDecimal.ZERO);
            contractPaymentList.add(contractProjectCheckPayment);

            // 质保
            ContractPayment contractWarrantyPayment = new ContractPayment();
            contractWarrantyPayment.setContractId(contract.getId());
            contractWarrantyPayment.setPaymentType(5);
            contractWarrantyPayment.setPaymentRatio(null != contract.getWarrantyRatio() ? contract.getWarrantyRatio() : BigDecimal.ZERO);
            contractPaymentList.add(contractWarrantyPayment);

            // 维保
            ContractPayment contractMaintainPayment = new ContractPayment();
            contractMaintainPayment.setContractId(contract.getId());
            contractMaintainPayment.setPaymentType(6);
            BigDecimal maintainPaymentRatio = BigDecimal.ZERO;
            if (null != contract.getMaintainRatio1()) {
                maintainPaymentRatio = maintainPaymentRatio.add(contract.getMaintainRatio1());
            }
            if (null != contract.getMaintainRatio2()) {
                maintainPaymentRatio = maintainPaymentRatio.add(contract.getMaintainRatio2());
            }
            if (null != contract.getMaintainRatio3()) {
                maintainPaymentRatio = maintainPaymentRatio.add(contract.getMaintainRatio3());
            }
            contractMaintainPayment.setPaymentRatio(maintainPaymentRatio);
            contractPaymentList.add(contractMaintainPayment);
        }

        contractPaymentService.saveBatch(contractPaymentList);


        log.info("【结束】从crm系统同步合同信息");
        return true;
    }
}