MaterialSyncService.java 4.92 KB
package com.huaheng.pc.config.material.service;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
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.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.domain.MaterialErp;
import com.huaheng.pc.config.material.mapper.MaterialMapper;
import com.huaheng.pc.config.supplier.service.SupplierSyncService;
import com.huaheng.pc.config.syncTime.domain.SyncTime;
import com.huaheng.pc.config.syncTime.service.SyncTimeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.config.customer.domain.Customer;
import com.huaheng.pc.config.supplier.domain.Supplier;
import com.huaheng.pc.config.supplier.domain.SupplierErp;

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

@Service
public class MaterialSyncService extends ServiceImpl<MaterialMapper, Material>{

    @Resource
    private MaterialService  materialService;
    @Resource
    private MaterialErpService materialErpService;
    @Resource
    private SyncTimeService syncTimeService;
    @Resource
    private AddressService addressService;
    @Resource
    private SupplierSyncService supplierSyncService;

    /**
     * 获取erp物料数据,同步到wms数据库
     */
    public void syncMaterialData(Integer materialTime){
        String syncName="material";
        Date date=new Date();
        Boolean tag=syncTimeService.isSync(syncName,materialTime);
        if(!tag){
//            return;
        }
        SyncTime syncTime=supplierSyncService.getTimeByName(syncName);
        Date syncTimes=syncTime.getSyncTimes();
        //判断synctime如果超过2小时则设置结束时间,否则结束时间为当前时间
        if((date.getTime()-syncTimes.getTime())>1*24*60*60*1000){
            Instant instant = syncTimes.toInstant();
            // 将Instant往前推移10分钟
            Instant newInstant = instant.plus(1, ChronoUnit.DAYS);
            // 将新的Instant转换回Date
            Date newDate = Date.from(newInstant);
            syncTime.setEndTime(newDate);
            date=syncTime.getEndTime();
        }
        String codeStart=syncTime.getCodeStart();
        String codeEnd="";
        if(StringUtils.isNotEmpty(codeStart)) {
            long number = 0l;
            try {
                number = Long.parseLong(codeStart); // 将字符串转换为11位数字的长整型
                number += 100000000; // 加上100000
                codeEnd=String.valueOf(number);
//                System.out.println("转换后的数字是: " + number); // 输出结果
            } catch (NumberFormatException e) {
            }
        }
        Address address=addressService.selectAddressByParam("ERP", QuantityConstant.WAREHOUSE_CS);
        List<MaterialErp> list=materialErpService.findMaterialData(syncTime.getEndTime(),address,syncTime.getSyncTimes(),codeStart,codeEnd);
        System.out.println("=======物料======="+new Date()+"==========="+list.size());
        saveAndUpdateData(list);
        if(StringUtils.isNotEmpty(codeStart)){
            syncTimeService.updateCodeStart("material",codeEnd);
        }else{
            syncTimeService.updateStatus("material",1,date);
        }
    }




    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveAndUpdateData(List<MaterialErp> list){
        if(list==null||list.size()==0){
            return ;
        }
        List<Material> lists=new ArrayList<>();
        for (int a=0;a<list.size();a++){
            MaterialErp materialErp=list.get(a);
            Material material=new Material();
            String code=materialErp.getImaa001();
            //查找数据库有无该供应商,有则跳过,无则新增
            Material material1 = materialService.getMaterialByCode(code);
            if(material1!=null){
                material.setId(material1.getId());
//                continue;
            }else{
                material.setType(materialErp.getImaa004());
            }
            material.setCode(code);
            material.setName(materialErp.getImaal003());
            if(StringUtils.isEmpty(materialErp.getImaal003())){
                material.setName(code);
            }
            material.setSpec(materialErp.getImaal004());
            material.setUnit(materialErp.getImaa006());
            material.setUserDef4(materialErp.getOocal003());
            lists.add(material);
        }
        if(lists!=null&&lists.size()>0){
            materialService.saveOrUpdateBatch(lists);
        }
    }


}