package com.gkhy.firework.schedule; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.gkhy.firework.master.domain.CustomerSlice1; import com.gkhy.firework.master.domain.Enterprise; import com.gkhy.firework.master.domain.SaleorderSlice1; import com.gkhy.firework.master.domain.SaleorderdetailSlice1; import com.gkhy.firework.service.*; import com.gkhy.firework.slave.domain.TLsXjlstm; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import java.util.ArrayList; import java.util.Date; import java.util.List; @Slf4j @Configuration @EnableScheduling public class OrderSyncTask { @Autowired private EnterpriseService enterpriseService; @Autowired private SaleorderSlice1Service saleorderSlice1Service; @Autowired private SaleorderdetailSlice1Service saleorderdetailSlice1Service; @Autowired private CustomerSlice1Service customerSlice1Service; @Autowired private TLsXjlstmService tLsXjlstmService; //@Scheduled(cron = "0 42 9 * * ?") //每天凌晨两点执行一次 @Scheduled(cron = "0 0 */1 * * ?") //每隔一小时 public void syncDLTask(){ Integer pageSize=100; Integer pageIndex=1; List tLsXjlstmList = new ArrayList<>(); List orderIds=new ArrayList<>(); log.info("丹灵同步开始:{}",new Date()); while(true) { log.info("enterprise pageIndex={}",pageIndex); List enterpriseList = enterpriseService.list(Wrappers.lambdaQuery() .select(Enterprise::getId,Enterprise::getDeviceid,Enterprise::getEnterprisenumber,Enterprise::getDlcompanycode) .eq(Enterprise::getValidflag, 1) .eq(Enterprise::getEnterprisestatus, "ON") .isNotNull(Enterprise::getDlcompanycode) .isNotNull(Enterprise::getDeviceid) .last("limit " + (pageIndex - 1) * pageSize + "," + pageSize)); if(enterpriseList.size()==0){ break; } for (Enterprise enterprise : enterpriseList) { if (StrUtil.isBlank(enterprise.getDlcompanycode()) || StrUtil.isBlank(enterprise.getDeviceid())) { continue; } //3.上传数据 Integer orderPageSize = 100; while (true) { List orderInfos = saleorderSlice1Service.list(Wrappers.lambdaQuery() .select(SaleorderSlice1::getId,SaleorderSlice1::getIdcardnum,SaleorderSlice1::getCode,SaleorderSlice1::getCreatedat) .eq(SaleorderSlice1::getIsupload, 0) .eq(SaleorderSlice1::getCompanynumber, enterprise.getEnterprisenumber()) .last(" limit " + orderPageSize)); if (orderInfos == null || orderInfos.size() == 0) { break; } for (SaleorderSlice1 saleorderSlice1 : orderInfos) { Long count = saleorderdetailSlice1Service.count(Wrappers.lambdaQuery() .eq(SaleorderdetailSlice1::getOrdercode, saleorderSlice1.getCode())); TLsXjlstm tLsXjlstm = new TLsXjlstm(); tLsXjlstm.setJysl(count); tLsXjlstm.setCjsbid(enterprise.getDeviceid()); tLsXjlstm.setJylx("10"); tLsXjlstm.setDwdm(enterprise.getDlcompanycode()); CustomerSlice1 customerSlice = customerSlice1Service.getOne(Wrappers.lambdaQuery() .select(CustomerSlice1::getId,CustomerSlice1::getName) .eq(CustomerSlice1::getIdcardnum, saleorderSlice1.getIdcardnum())); if(customerSlice!=null){ tLsXjlstm.setGmrxm(customerSlice.getName()); }else{ tLsXjlstm.setGmrxm("未知"); } tLsXjlstm.setGmrzjhm(saleorderSlice1.getIdcardnum()); tLsXjlstm.setSjscsj(saleorderSlice1.getCreatedat()); tLsXjlstm.setMytimestamp(new Date()); tLsXjlstmList.add(tLsXjlstm); orderIds.add(saleorderSlice1.getId()); } if (tLsXjlstmList.size() > 100) { tLsXjlstmService.saveBatch(tLsXjlstmList); tLsXjlstmList.clear(); } if(orderIds.size()>0){ saleorderSlice1Service.update(Wrappers.lambdaUpdate() .set(SaleorderSlice1::getIsupload,(byte)1) .set(SaleorderSlice1::getUploadat,new Date()) .in(SaleorderSlice1::getId,orderIds)); orderIds.clear(); } } } pageIndex = pageIndex + 1; } if(tLsXjlstmList.size()>0){ tLsXjlstmService.saveBatch(tLsXjlstmList); tLsXjlstmList.clear(); } if(orderIds.size()>0){ saleorderSlice1Service.update(Wrappers.lambdaUpdate() .set(SaleorderSlice1::getIsupload,(byte)1) .set(SaleorderSlice1::getUploadat,new Date()) .in(SaleorderSlice1::getId,orderIds)); orderIds.clear(); } log.info("丹灵同步结束:{}",new Date()); } }