U8CustomerService.java 2.96 KB
package com.huaheng.pc.U8.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.framework.datasource.DynamicDataSource;
import com.huaheng.pc.U8.domain.U8Customer;
import com.huaheng.pc.U8.mapper.U8CustomerMapper;
import com.huaheng.pc.config.customer.domain.Customer;
import com.huaheng.pc.config.customer.service.CustomerServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class U8CustomerService extends ServiceImpl<U8CustomerMapper, U8Customer> {
    @Resource
    private CustomerServiceImpl service;

    public List getU8CustomerList() {
        // 切换到从库
        DynamicDataSource.slave();
        List<U8Customer> list = this.list();
        return list;
    }

    public List sync() throws ParseException {
        List<U8Customer> list = this.getU8CustomerList();
        DynamicDataSource.master();
        List<Customer> lst = service.list();
        ArrayList<Customer> list1 = new ArrayList<>();
        ArrayList<Customer> list2 = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (U8Customer customer : list) {
            for (Customer cus : lst) {
                if (Objects.equals(customer.getCode(), cus.getCode())) {
                    if (!sdf.format(customer.getMaintenanceDate()).equals(sdf.format(cus.getMaintenanceDate()))) {
                        Customer customer1 = new Customer();
                        customer1.setId(cus.getId());
                        customer1.setName(customer.getName());
                        customer1.setPhoneNum(customer.getPhoneNum());
                        customer1.setAttentionTo(customer.getAttentionTo());
                        customer1.setMaintenanceDate(customer.getMaintenanceDate());
                        list1.add(customer1);
                    }
                }
            }
            if (lst.stream().noneMatch(w -> String.valueOf(w.getCode()).equals(customer.getCode()))) {
                Customer customer1 = new Customer();
                customer1.setCode(customer.getCode());
                customer1.setName(customer.getName());
                customer1.setPhoneNum(customer.getPhoneNum());
                customer1.setAttentionTo(customer.getAttentionTo());
                customer1.setMaintenanceDate(sdf.parse(sdf.format(customer.getMaintenanceDate())));
                customer1.setWarehouseCode(QuantityConstant.WAREHOUSECODE);
                customer1.setCompanyCode(QuantityConstant.COMPANYCODE);
                list2.add(customer1);
            }
        }
        if (!list1.isEmpty()){
            service.updateBatchById(list1);
        }
        if (!list2.isEmpty()){
            service.saveBatch(list2);
        }
        return lst;
    }
}