SupplierSyncService.java 6.36 KB
package com.huaheng.pc.config.supplier.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.bean.BeanUtils;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.customer.domain.Customer;
import com.huaheng.pc.config.customer.domain.CustomerErp;
import com.huaheng.pc.config.customer.service.CustomerErpService;
import com.huaheng.pc.config.customer.service.CustomerServiceImpl;
import com.huaheng.pc.config.supplier.domain.Supplier;
import com.huaheng.pc.config.supplier.mapper.SupplierMapper;
import com.huaheng.pc.config.syncTime.domain.SyncTime;
import com.huaheng.pc.config.syncTime.service.SyncTimeService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.framework.aspectj.lang.annotation.Ds;
import com.huaheng.framework.aspectj.lang.constant.DataSourceName;
import com.huaheng.pc.config.supplier.domain.SupplierErp;
import com.huaheng.pc.config.supplier.mapper.SupplierErpMapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 定时任务同步erp的供应商、客户
 */

@Service
public class SupplierSyncService extends ServiceImpl<SupplierMapper, Supplier>{




    @Resource
    private SupplierService supplierService;
    @Resource
    private CustomerServiceImpl customerService;
    @Resource
    private SupplierErpService supplierErpService;
    @Resource
    private SyncTimeService syncTimeService;
    @Resource
    private AddressService addressService;
    @Resource
    private CustomerErpService customerErpService;


    public void syncSupplierData(Integer supplierTime){
        String syncName="supplier";
        Date date=new Date();
        Boolean tag=syncTimeService.isSync(syncName,supplierTime);
        if(!tag){
            return;
        }
        SyncTime syncTime=getTimeByName(syncName);
        Address address=addressService.selectAddressByParam("ERP", QuantityConstant.WAREHOUSE_CS);
        List<SupplierErp> list=supplierErpService.findSupplierData(supplierTime,address,syncTime.getSyncTimes());
        System.out.println("=======供应商======="+date+"==========="+list.size());
        saveAndUpdateData(list);
        syncTimeService.updateStatus("supplier",1,date);
    }

    public void syncCustomerData(Integer customerTime){
        String syncName="customer";
        Date date=new Date();
        Boolean tag=syncTimeService.isSync(syncName,customerTime);
        if(!tag){
            return;
        }
        SyncTime syncTime=getTimeByName(syncName);
        Address address=addressService.selectAddressByParam("ERP", QuantityConstant.WAREHOUSE_CS);
        List<CustomerErp> list=customerErpService.findCustomerData(customerTime,address,syncTime.getSyncTimes());
        System.out.println("======客户数据========"+list.size());
        saveAndUpdateDataCustomer(list);
        syncTimeService.updateStatus("customer",1,date);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveAndUpdateData(List<SupplierErp> list){
        if(list==null||list.size()==0){
            return ;
        }
        List<Supplier> lists=new ArrayList<>();
        for (int a=0;a<list.size();a++){
            SupplierErp supplierErp=list.get(a);
            Supplier supplier=new Supplier();
            String code=supplierErp.getPmaa001();
            //查找数据库有无该供应商,有则跳过,无则新增
            Supplier supplier1 = supplierService.getSuppierByCode(code);
            if(supplier1!=null){
//                LambdaQueryWrapper<Supplier> lambda = new LambdaQueryWrapper<>();
//                lambda.eq(Supplier::getCode,code);
//                lambda.ne(Supplier::getId,supplier1.getId());
//                supplierService.remove(lambda);
//                continue;
                if(StringUtils.isNotEmpty(supplierErp.getPmaal())&&!supplierErp.getPmaal().equals(supplier1.getName())){
                    supplier.setId(supplier1.getId());
                }else{
                    continue;
                }
            }
            supplier.setCode(code);
            supplier.setName(supplierErp.getPmaal());
            supplier.setDeleted(false);
            if(StringUtils.isEmpty(supplierErp.getPmaal())){
                supplier.setName(code);
            }
            lists.add(supplier);
        }
        if(lists!=null&&lists.size()>0){
            supplierService.saveOrUpdateBatch(lists);
//            containerSyncService.updateSyncById(ids);
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveAndUpdateDataCustomer(List<CustomerErp> list){
        if(list==null||list.size()==0){
            return ;
        }
        List<Customer> lists=new ArrayList<>();
        for (int a=0;a<list.size();a++){
            CustomerErp supplierErp=list.get(a);
            Customer customer=new Customer();
            String code=supplierErp.getPmaa001();
            Customer customer1 = customerService.getCustomerByCode(code,null);
            if(customer1!=null){
                if(StringUtils.isNotEmpty(supplierErp.getPmaal())&&!supplierErp.getPmaal().equals(customer1.getName())){
                    customer.setId(customer1.getId());
                }else{
                    continue;
                }
            }
            customer.setCode(code);
            customer.setName(supplierErp.getPmaal());
            customer.setDeleted(false);
            if(StringUtils.isEmpty(supplierErp.getPmaal())){
                customer.setName(code);
            }
            lists.add(customer);
        }
        if(lists!=null&&lists.size()>0){
            customerService.saveOrUpdateBatch(lists);
        }
    }

    public SyncTime getTimeByName(String syncName){
        LambdaQueryWrapper<SyncTime> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SyncTime::getSyncName,syncName);
        wrapper.last("limit 1");
        SyncTime syncTime= syncTimeService.getOne(wrapper);
        return syncTime;
    }
}