package com.gk.firework.Scheduls.DL; import com.gk.firework.Domain.Enterprise; import com.gk.firework.Service.EnterpriseService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component @EnableScheduling public class DeviceIdRobot { private static final int deviceSize = 32; @Value("${com.gk.firework.schedules.single.switch}") private Boolean switchBtn; @Autowired private EnterpriseService enterpriseService; @Scheduled(cron = "0/3 * * * * ?") //每隔3秒执行一次 public void deviceIdRobot(){ if (!switchBtn) return; List result = enterpriseService.selectAllDlCompanyCodeIsNotNull(); List updates = null; if (result.size() > 0) { updates = new ArrayList<>(); for (Enterprise e : result) { String enterprisenumber = e.getEnterprisenumber(); String dlcompanycode = e.getDlcompanycode(); String strs = dlcompanycode + enterprisenumber; int length = strs.length(); if ( length < deviceSize) { String deviceId = addZeroForNum(strs); e.setDeviceid(deviceId); }else{ String deviceId = strs.substring(0, deviceSize); e.setDeviceid(deviceId); } updates.add(e); } } if (updates != null && updates.size() > 0) { enterpriseService.updateBatchById(updates); } } private static String addZeroForNum(String str) { int strLen = str.length(); if (strLen < DeviceIdRobot.deviceSize) { StringBuilder sb = new StringBuilder(); sb.append(str); while (sb.toString().length() < DeviceIdRobot.deviceSize) { sb.append("0"); str = sb.toString(); } } return str; } }