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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.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();
        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 supplier = Customer.builder()
                                .id(cus.getId())
                                .name(customer.getName())
                                .phoneNum(customer.getPhoneNum())
                                .attentionTo(customer.getAttentionTo())
                                .maintenanceDate(customer.getMaintenanceDate())
                                .build();
                        service.updateById(supplier);
                    }
                }
            }
            if (lst.stream().noneMatch(w -> String.valueOf(w.getCode()).equals(customer.getCode()))) {
                Customer cus = Customer.builder().code(customer.getCode())
                        .name(customer.getName())
                        .phoneNum(customer.getPhoneNum())
                        .attentionTo(customer.getAttentionTo())
                        .maintenanceDate(sdf.parse(sdf.format(customer.getMaintenanceDate())))
                        .warehouseCode("CS0001").companyCode("BHF")
                        .build();
                service.save(cus);
            }
        }
        return lst;
    }
}