|
@@ -9,6 +9,7 @@ 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.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
import com.fjhx.file.entity.FileInfo;
|
|
|
import com.fjhx.file.service.FileInfoService;
|
|
|
import com.obs.services.ObsClient;
|
|
@@ -17,6 +18,7 @@ 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.entity.bom.po.BomSpec;
|
|
|
import com.sd.business.service.bom.BomService;
|
|
|
import com.sd.business.service.bom.BomSpecService;
|
|
|
import org.junit.Test;
|
|
@@ -36,11 +38,8 @@ 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/";
|
|
|
-
|
|
|
+ private static final String oldUrlPrefix = "http://www.printmat.cn:8181/file/";
|
|
|
static {
|
|
|
classifyIdMap.put("1001", 1682221249268375554L);
|
|
|
classifyIdMap.put("1002", 1682221303530086402L);
|
|
@@ -59,14 +58,6 @@ public class SyncBomBootTest {
|
|
|
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}")
|
|
@@ -82,35 +73,76 @@ public class SyncBomBootTest {
|
|
|
@Value("${ruoyi.name}")
|
|
|
private String name;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private BomService bomService;
|
|
|
+ @Autowired
|
|
|
+ private BomSpecService bomSpecService;
|
|
|
+ @Autowired
|
|
|
+ private DataSource dataSource;
|
|
|
+ @Autowired
|
|
|
+ private FileInfoService fileInfoService;
|
|
|
+
|
|
|
@DSTransactional
|
|
|
@Test
|
|
|
public void test() {
|
|
|
DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
|
|
|
DataSource sdDataSource = dynamicRoutingDataSource.getDataSource("business");
|
|
|
- List<Bom> bomList = saveBom(sdDataSource);
|
|
|
|
|
|
+ List<Bom> bomList = saveBom(sdDataSource);
|
|
|
+ saveBomSpec(sdDataSource, bomList);
|
|
|
|
|
|
System.out.println();
|
|
|
}
|
|
|
|
|
|
+ private void saveBomSpec(DataSource sdDataSource, List<Bom> bomList) {
|
|
|
+ LogicHolder.setLogicHolder(new LogicHolder());
|
|
|
+ List<BomSpec> list = bomSpecService.list(q -> q.isNotNull(BomSpec::getOldId));
|
|
|
+ List<Long> bomSpecIdList = list.stream().map(BaseIdPo::getId).collect(Collectors.toList());
|
|
|
+ bomSpecService.removeBatchByIds(bomSpecIdList);
|
|
|
+ removeFile(bomSpecIdList);
|
|
|
+ LogicHolder.clear();
|
|
|
+
|
|
|
+ String sql = "SELECT\n" +
|
|
|
+ "\tid oldId,\n" +
|
|
|
+ "\tbom_id oldBomId,\n" +
|
|
|
+ "\tmag_path mainImgUrl,\n" +
|
|
|
+ "\tname_chinese name,\n" +
|
|
|
+ "\tspec_code code,\n" +
|
|
|
+ "\tcolor colour,\n" +
|
|
|
+ "\tcost_price costPrice,\n" +
|
|
|
+ "\tprice internalSellingPrice,\n" +
|
|
|
+ "\tforeign_price externalSellingPrice,\n" +
|
|
|
+ "\tsafety_stock safetyStock,\n" +
|
|
|
+ "\tproduct_long length,\n" +
|
|
|
+ "\tproduct_wide width,\n" +
|
|
|
+ "\tproduct_high height,\n" +
|
|
|
+ "\tnet_weight netWeight \n" +
|
|
|
+ "FROM\n" +
|
|
|
+ "\tt_sd_bom_color";
|
|
|
+
|
|
|
+ List<Map<String, Object>> bomSpecMapList = new JdbcTemplate(sdDataSource).queryForList(sql);
|
|
|
+ List<BomSpec> bomSpecList = BeanUtil.copyToList(bomSpecMapList, BomSpec.class);
|
|
|
+
|
|
|
+ Map<String, Long> bomMap = bomList.stream().collect(Collectors.toMap(Bom::getOldId, BaseIdPo::getId));
|
|
|
+
|
|
|
+ for (BomSpec bomSpec : bomSpecList) {
|
|
|
+ long id = IdWorker.getId();
|
|
|
+ bomSpec.setId(id);
|
|
|
+ bomSpec.setBomId(bomMap.get(bomSpec.getOldBomId()));
|
|
|
+
|
|
|
+ String fileUrl = uploadFile(bomSpec.getMainImgUrl(), id);
|
|
|
+ bomSpec.setMainImgUrl(fileUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ bomSpecService.saveBatch(bomSpecList);
|
|
|
+ }
|
|
|
+
|
|
|
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();
|
|
|
- }
|
|
|
+ removeFile(bomIdList);
|
|
|
LogicHolder.clear();
|
|
|
|
|
|
String sql = "SELECT\n" +
|
|
@@ -147,32 +179,15 @@ public class SyncBomBootTest {
|
|
|
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);
|
|
|
-
|
|
|
+ String fileUrl = uploadFile(mainImgUrl, id);
|
|
|
+ map.put("mainImgUrl", fileUrl);
|
|
|
}
|
|
|
|
|
|
List<Bom> bomList = BeanUtil.copyToList(bomMapList, Bom.class);
|
|
@@ -181,27 +196,53 @@ public class SyncBomBootTest {
|
|
|
return bomList;
|
|
|
}
|
|
|
|
|
|
- private String getFileName(String fileUrl) {
|
|
|
+ private String uploadFile(String fileUrl, Long businessId) {
|
|
|
+ if (StrUtil.isBlank(fileUrl)) {
|
|
|
+ return StringPool.EMPTY;
|
|
|
+ }
|
|
|
|
|
|
- String fileName = FileUtil.getName(fileUrl);
|
|
|
+ String objectKey = getFileName(fileUrl);
|
|
|
+ byte[] bytes = HttpUtil.downloadBytes(oldUrlPrefix + fileUrl);
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(bytes);
|
|
|
+ getObsClient().putObject(bucketName, objectKey, inputStream);
|
|
|
+
|
|
|
+ FileInfo fileInfo = new FileInfo();
|
|
|
+ fileInfo.setFileUrl(objectKey);
|
|
|
+ fileInfo.setFileName(FileUtil.getName(fileUrl));
|
|
|
+ fileInfo.setBusinessId(businessId);
|
|
|
+ fileInfo.setBusinessType(0);
|
|
|
+ fileInfoService.save(fileInfo);
|
|
|
+ return url + objectKey;
|
|
|
+ }
|
|
|
|
|
|
- // 文件后缀名
|
|
|
- String suffix = FileUtil.getSuffix(fileName);
|
|
|
+ private void removeFile(List<Long> businessIdList) {
|
|
|
+ if (ObjectUtil.isEmpty(businessIdList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ DynamicDataSourceContextHolder.push(BaseSourceConstant.BASE);
|
|
|
+ List<FileInfo> fileInfoList = fileInfoService.list(q -> q.in(FileInfo::getBusinessId, businessIdList));
|
|
|
+ if (fileInfoList.size() == 0) {
|
|
|
+ DynamicDataSourceContextHolder.poll();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ fileInfoList.forEach(item -> {
|
|
|
+ getObsClient().deleteObject(bucketName, item.getFileUrl());
|
|
|
+ fileInfoService.removeById(item.getId());
|
|
|
+ });
|
|
|
+ DynamicDataSourceContextHolder.poll();
|
|
|
+ }
|
|
|
|
|
|
- // 文件路径
|
|
|
- String objectKey = new StringJoiner("/")
|
|
|
+ private String getFileName(String fileUrl) {
|
|
|
+ String fileName = FileUtil.getName(fileUrl);
|
|
|
+ String suffix = FileUtil.getSuffix(fileName);
|
|
|
+ return 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);
|
|
|
}
|