ContractRunner.java 4.06 KB
package vion.cron;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import vion.model.Contract;
import vion.model.Dictionary;
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;

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

        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 concat(t3.optnty_name,t1.hetongluru_name) as name, t1.hetongluru_name as contract_no,t1.hetongluru_char12 as type,t1.hetongluru_date01 as sign_date,t1.hetongluru_start_date as maintain_sdate,t1.hetongluru_end_date as maintain_edate,t1.hetongluru_dec01 as total_amount,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";
            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();
                        contract.setName(resultSet.getString("name"));
                        contract.setContractNo(contractNo);
                        contract.setType(contractTypeMap.getOrDefault(resultSet.getString("type"), 0));
                        contract.setSignDate(resultSet.getDate("sign_date"));
                        contract.setMaintainSdate(resultSet.getDate("maintain_sdate"));
                        contract.setMaintainEdate(resultSet.getDate("maintain_edate"));
                        contract.setStatus(1);
                        contract.setTotalAmount(resultSet.getBigDecimal("total_amount"));
                        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);

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