|
@@ -1,186 +0,0 @@
|
|
|
-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.StringPool;
|
|
|
-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.sd.SdApplication;
|
|
|
-import com.sd.business.entity.bom.po.BomSpec;
|
|
|
-import com.sd.business.entity.sku.po.SkuSpec;
|
|
|
-import com.sd.business.service.bom.BomSpecService;
|
|
|
-import com.sd.business.service.sku.SkuSpecService;
|
|
|
-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 SyncSkuBootTest {
|
|
|
- private static final Map<String, Long> classifyIdMap = new HashMap<>();
|
|
|
- private static final 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);
|
|
|
- }
|
|
|
-
|
|
|
- @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;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private DataSource dataSource;
|
|
|
- @Autowired
|
|
|
- private FileInfoService fileInfoService;
|
|
|
- @Autowired
|
|
|
- private SkuSpecService skuSpecService;
|
|
|
- @Autowired
|
|
|
- private BomSpecService bomSpecService;
|
|
|
-
|
|
|
- @DSTransactional
|
|
|
- @Test
|
|
|
- public void test() {
|
|
|
- DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
|
|
|
- DataSource sdDataSource = dynamicRoutingDataSource.getDataSource("business");
|
|
|
-
|
|
|
- Map<String, Long> bomSpecIdMap = bomSpecService.mapKV(BomSpec::getOldId, BaseIdPo::getId, null);
|
|
|
-
|
|
|
- Map<String, Long> stringLongMap = skuSpecService.mapKV(SkuSpec::getCode, BaseIdPo::getId, null);
|
|
|
-
|
|
|
- String sql = "SELECT\n" +
|
|
|
- "\tp.spec_code code,\n" +
|
|
|
- "\tp.weight netWeight,\n" +
|
|
|
- "\tp.length length,\n" +
|
|
|
- "\tp.width width,\n" +
|
|
|
- "\tp.height height,\n" +
|
|
|
- "\tp.processing_layout machinedPanel,\n" +
|
|
|
- "\ts.bom_color_id bomSpecId\n" +
|
|
|
- "FROM\n" +
|
|
|
- "\tt_sd_product_color p\n" +
|
|
|
- "\tLEFT JOIN t_sd_product_color_bom s on p.id = s.product_color_id";
|
|
|
-
|
|
|
- List<Map<String, Object>> list = new JdbcTemplate(sdDataSource).queryForList(sql);
|
|
|
-
|
|
|
- for (Map<String, Object> map : list) {
|
|
|
- Object bomSpecIdObj = map.get("bomSpecId");
|
|
|
- if (bomSpecIdObj == null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- String bomSpecId = bomSpecIdObj.toString();
|
|
|
- if (StrUtil.isBlank(bomSpecId)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- map.put("bomSpecId", bomSpecIdMap.get(bomSpecId));
|
|
|
- }
|
|
|
- List<SkuSpec> skuSpecList = BeanUtil.copyToList(list, SkuSpec.class);
|
|
|
-
|
|
|
-
|
|
|
- for (SkuSpec skuSpec : skuSpecList) {
|
|
|
- String code = skuSpec.getCode();
|
|
|
- Long id = stringLongMap.get(code);
|
|
|
- skuSpec.setId(id);
|
|
|
- }
|
|
|
-
|
|
|
- skuSpecList = skuSpecList.stream().filter(item -> ObjectUtil.isNotNull(item.getId())).collect(Collectors.toList());
|
|
|
-
|
|
|
- skuSpecService.updateBatchById(skuSpecList);
|
|
|
-
|
|
|
- System.out.println();
|
|
|
- }
|
|
|
-
|
|
|
- private String uploadFile(String fileUrl, Long businessId) {
|
|
|
- if (StrUtil.isBlank(fileUrl)) {
|
|
|
- return StringPool.EMPTY;
|
|
|
- }
|
|
|
-
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
- 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();
|
|
|
- }
|
|
|
-
|
|
|
- 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();
|
|
|
- }
|
|
|
-
|
|
|
- private ObsClient getObsClient() {
|
|
|
- return new ObsClient(ak, sk, endPoint);
|
|
|
- }
|
|
|
-
|
|
|
-}
|