|
@@ -0,0 +1,209 @@
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
+import com.fjhx.file.entity.FileInfo;
|
|
|
+import com.fjhx.file.service.FileInfoService;
|
|
|
+import com.obs.services.ObsClient;
|
|
|
+import com.ruoyi.common.constant.BaseSourceConstant;
|
|
|
+import com.ruoyi.common.core.domain.BaseIdPo;
|
|
|
+import com.ruoyi.framework.mybatis.holder.LogicHolder;
|
|
|
+import com.sd.SdApplication;
|
|
|
+import com.sd.business.entity.bom.po.Bom;
|
|
|
+import com.sd.business.service.bom.BomService;
|
|
|
+import com.sd.business.service.bom.BomSpecService;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@SpringBootTest(classes = SdApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
|
|
+public class SyncBomBootTest {
|
|
|
+
|
|
|
+ private static final Map<String, Long> classifyIdMap = new HashMap<>();
|
|
|
+
|
|
|
+ private static String oldUrlPrefix = "http://www.printmat.cn:8181/file/";
|
|
|
+
|
|
|
+ static {
|
|
|
+ classifyIdMap.put("1001", 1682221249268375554L);
|
|
|
+ classifyIdMap.put("1002", 1682221303530086402L);
|
|
|
+ classifyIdMap.put("1003", 1681220168933445634L);
|
|
|
+ classifyIdMap.put("1004", 1682221651040755714L);
|
|
|
+ classifyIdMap.put("1005", 1682221201491058690L);
|
|
|
+ classifyIdMap.put("1006", 1682221712801882114L);
|
|
|
+ classifyIdMap.put("1008", 1682221581453058049L);
|
|
|
+ classifyIdMap.put("1009", 1682221453145104386L);
|
|
|
+ classifyIdMap.put("1010", 1682221356147630081L);
|
|
|
+ classifyIdMap.put("1011", 1682221409847304194L);
|
|
|
+ classifyIdMap.put("1609478435019689985", 1682221078014943234L);
|
|
|
+ classifyIdMap.put("1609485052385693697", 1682221128782798850L);
|
|
|
+ classifyIdMap.put("1615229747095150593", 1682221528948760578L);
|
|
|
+ classifyIdMap.put("1636175301891383297", 1682221760906354690L);
|
|
|
+ classifyIdMap.put("1636175375388172289", 1682221804409675778L);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BomService bomService;
|
|
|
+ @Autowired
|
|
|
+ private BomSpecService bomSpecService;
|
|
|
+ @Autowired
|
|
|
+ private DataSource dataSource;
|
|
|
+ @Autowired
|
|
|
+ private FileInfoService fileInfoService;
|
|
|
+ @Value("${obs.ak}")
|
|
|
+ private String ak;
|
|
|
+ @Value("${obs.sk}")
|
|
|
+ private String sk;
|
|
|
+ @Value("${obs.endPoint}")
|
|
|
+ private String endPoint;
|
|
|
+ @Value("${obs.url}")
|
|
|
+ private String url;
|
|
|
+ @Value("${obs.bucketName}")
|
|
|
+ private String bucketName;
|
|
|
+ @Value("${spring.profiles.active}")
|
|
|
+ private String active;
|
|
|
+ @Value("${ruoyi.name}")
|
|
|
+ private String name;
|
|
|
+
|
|
|
+ @DSTransactional
|
|
|
+ @Test
|
|
|
+ public void test() {
|
|
|
+ DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
|
|
|
+ DataSource sdDataSource = dynamicRoutingDataSource.getDataSource("business");
|
|
|
+ List<Bom> bomList = saveBom(sdDataSource);
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println();
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Bom> saveBom(DataSource sdDataSource) {
|
|
|
+ LogicHolder.setLogicHolder(new LogicHolder());
|
|
|
+ List<Bom> list = bomService.list(q -> q.isNotNull(Bom::getOldId));
|
|
|
+ List<Long> bomIdList = list.stream().map(BaseIdPo::getId).collect(Collectors.toList());
|
|
|
+ bomService.removeBatchByIds(bomIdList);
|
|
|
+
|
|
|
+
|
|
|
+ if (bomIdList.size() > 0) {
|
|
|
+ DynamicDataSourceContextHolder.push(BaseSourceConstant.BASE);
|
|
|
+ List<FileInfo> fileInfoList = fileInfoService.list(q -> q.in(FileInfo::getBusinessId, bomIdList));
|
|
|
+ if (fileInfoList.size() > 0) {
|
|
|
+ fileInfoList.forEach(item -> {
|
|
|
+ getObsClient().deleteObject(bucketName, item.getFileUrl());
|
|
|
+ fileInfoService.removeById(item.getId());
|
|
|
+ });
|
|
|
+ }
|
|
|
+ DynamicDataSourceContextHolder.poll();
|
|
|
+ }
|
|
|
+ LogicHolder.clear();
|
|
|
+
|
|
|
+ String sql = "SELECT\n" +
|
|
|
+ "\tid oldId,\n" +
|
|
|
+ "\tcategory_id bomClassifyId,\n" +
|
|
|
+ "\tcode,\n" +
|
|
|
+ "\tname_chinese name,\n" +
|
|
|
+ "\tproject_type itemSubclass,\n" +
|
|
|
+ "\tmaterial_chinese material,\n" +
|
|
|
+ "\tfrontal_grain frontGrain,\n" +
|
|
|
+ "\tcompany unit,\n" +
|
|
|
+ "\tchromatogram chromatophore,\n" +
|
|
|
+ "\tangle angle,\n" +
|
|
|
+ "\tformula embossingProcess,\n" +
|
|
|
+ "\tgeneral_category species,\n" +
|
|
|
+ "\tproduct_model_chinese modelNumber,\n" +
|
|
|
+ "\treverse_grain reverseGrain,\n" +
|
|
|
+ "\tlogo ,\n" +
|
|
|
+ "\tgrade sellingPriceSystem,\n" +
|
|
|
+ "\tembossing formula,\n" +
|
|
|
+ "\tpic mainImgUrl,\n" +
|
|
|
+ "\tremarks detailText\n" +
|
|
|
+ "FROM\n" +
|
|
|
+ "\tt_sd_bom b\n" +
|
|
|
+ "ORDER BY b.create_time asc,b.id desc";
|
|
|
+
|
|
|
+ List<Map<String, Object>> bomMapList = new JdbcTemplate(sdDataSource).queryForList(sql);
|
|
|
+
|
|
|
+ for (Map<String, Object> map : bomMapList) {
|
|
|
+ long id = IdWorker.getId();
|
|
|
+ map.put("id", id);
|
|
|
+
|
|
|
+ String bomClassifyId = map.get("bomClassifyId").toString();
|
|
|
+ map.put("bomClassifyId", classifyIdMap.get(bomClassifyId));
|
|
|
+
|
|
|
+ Object mainImgUrlObj = map.get("mainImgUrl");
|
|
|
+
|
|
|
+ if (mainImgUrlObj == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String mainImgUrl = mainImgUrlObj.toString();
|
|
|
+ if (StrUtil.isBlank(mainImgUrl)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String objectKey = getFileName(mainImgUrl);
|
|
|
+
|
|
|
+ byte[] bytes = HttpUtil.downloadBytes(oldUrlPrefix + mainImgUrl);
|
|
|
+ InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
|
|
|
+ getObsClient().putObject(bucketName, objectKey, byteArrayInputStream);
|
|
|
+
|
|
|
+ // 保存文件
|
|
|
+ FileInfo fileInfo = new FileInfo();
|
|
|
+ fileInfo.setFileUrl(objectKey);
|
|
|
+ fileInfo.setFileName(FileUtil.getName(mainImgUrl));
|
|
|
+ fileInfo.setBusinessId(id);
|
|
|
+ fileInfo.setBusinessType(0);
|
|
|
+ fileInfoService.save(fileInfo);
|
|
|
+
|
|
|
+ map.put("mainImgUrl", url + objectKey);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Bom> bomList = BeanUtil.copyToList(bomMapList, Bom.class);
|
|
|
+ bomService.saveBatch(bomList);
|
|
|
+
|
|
|
+ return bomList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getFileName(String fileUrl) {
|
|
|
+
|
|
|
+ String fileName = FileUtil.getName(fileUrl);
|
|
|
+
|
|
|
+ // 文件后缀名
|
|
|
+ String suffix = FileUtil.getSuffix(fileName);
|
|
|
+
|
|
|
+ // 文件路径
|
|
|
+ String objectKey = new StringJoiner("/")
|
|
|
+ .add(name)
|
|
|
+ .add(active)
|
|
|
+ .add(DateUtil.format(new Date(), "yyyy/MM/dd"))
|
|
|
+ .add(IdUtil.fastSimpleUUID() + (ObjectUtil.isEmpty(suffix) ? "" : "." + suffix))
|
|
|
+ .toString();
|
|
|
+
|
|
|
+ return objectKey;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取oss链接客户端
|
|
|
+ */
|
|
|
+ private ObsClient getObsClient() {
|
|
|
+ return new ObsClient(ak, sk, endPoint);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|