|
@@ -0,0 +1,119 @@
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.ruoyi.common.core.domain.BaseIdPo;
|
|
|
+import com.sd.SdApplication;
|
|
|
+import com.sd.business.entity.bom.po.BomSpec;
|
|
|
+import com.sd.business.entity.department.po.Department;
|
|
|
+import com.sd.business.entity.sku.po.SkuSpec;
|
|
|
+import com.sd.business.entity.sku.po.SkuSpecLink;
|
|
|
+import com.sd.business.service.bom.BomSpecService;
|
|
|
+import com.sd.business.service.department.DepartmentService;
|
|
|
+import com.sd.business.service.sku.SkuSpecLinkService;
|
|
|
+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.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@SpringBootTest(classes = SdApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
|
|
+public class A4_SyncSkuLinkTest {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SkuSpecService skuSpecService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SkuSpecLinkService skuSpecLinkService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BomSpecService bomSpecService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DepartmentService departmentService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DataSource dataSource;
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String sql = "SELECT\n" +
|
|
|
+ "\tt1.id oldId,\n" +
|
|
|
+ "\tt2.spec_code skuSpecCode,\n" +
|
|
|
+ "\tt1.bom_color_id bomSpecId,\n" +
|
|
|
+ "\tifnull(t1.subsidiary_id,100) departmentId,\n" +
|
|
|
+ "\t'1' type,\n" +
|
|
|
+ "\tquantity quantity\n" +
|
|
|
+ "FROM\n" +
|
|
|
+ "\tt_sd_product_color_bom_mountings t1\n" +
|
|
|
+ "\tINNER JOIN t_sd_product_color t2 ON t1.product_color_id = t2.id\n";
|
|
|
+ System.out.println(sql);
|
|
|
+ }
|
|
|
+
|
|
|
+ @DSTransactional
|
|
|
+ @Test
|
|
|
+ public void test() {
|
|
|
+
|
|
|
+ skuSpecLinkService.remove(q -> q.isNotNull(SkuSpecLink::getOldId));
|
|
|
+
|
|
|
+ Map<String, Long> departmentIdMap = departmentService.mapKV(Department::getOldId, BaseIdPo::getId, null);
|
|
|
+ Map<String, SkuSpec> skuSpecMap = skuSpecService.mapKEntity(SkuSpec::getCode, null);
|
|
|
+ Map<String, Long> bomSpecMap = bomSpecService.mapKV(BomSpec::getOldId, BaseIdPo::getId, null);
|
|
|
+
|
|
|
+ DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
|
|
|
+ DataSource sdDataSource = dynamicRoutingDataSource.getDataSource("business");
|
|
|
+
|
|
|
+ String sql = "SELECT\n" +
|
|
|
+ "\tt1.id oldId,\n" +
|
|
|
+ "\tt2.spec_code skuSpecCode,\n" +
|
|
|
+ "\tt1.bom_color_id bomSpecId,\n" +
|
|
|
+ "\tifnull(t1.subsidiary_id,100) departmentId,\n" +
|
|
|
+ "\t'1' type,\n" +
|
|
|
+ "\tquantity quantity\n" +
|
|
|
+ "FROM\n" +
|
|
|
+ "\tt_sd_product_color_bom_mountings t1\n" +
|
|
|
+ "\tINNER JOIN t_sd_product_color t2 ON t1.product_color_id = t2.id\n" +
|
|
|
+ "union all\n" +
|
|
|
+ "SELECT\n" +
|
|
|
+ "\tt1.id oldId,\n" +
|
|
|
+ "\tt2.spec_code skuSpecCode,\n" +
|
|
|
+ "\tt1.bom_color_id bomSpecId,\n" +
|
|
|
+ "\tifnull(t1.subsidiary_id,100) departmentId,\n" +
|
|
|
+ "\t'2' type,\n" +
|
|
|
+ "\tquantity quantity\n" +
|
|
|
+ "FROM\n" +
|
|
|
+ "\tt_sd_product_color_bom_express_packaging t1\n" +
|
|
|
+ "\tINNER JOIN t_sd_product_color t2 ON t1.product_color_id = t2.id";
|
|
|
+
|
|
|
+ List<Map<String, Object>> list = new JdbcTemplate(sdDataSource).queryForList(sql);
|
|
|
+
|
|
|
+
|
|
|
+ List<SkuSpecLink> collect = list.stream()
|
|
|
+ .peek(item -> item.put("departmentId", departmentIdMap.get(item.get("departmentId").toString())))
|
|
|
+ .peek(item -> {
|
|
|
+ SkuSpec skuSpec = skuSpecMap.get(item.get("skuSpecCode").toString());
|
|
|
+ if (skuSpec == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ item.put("skuId", skuSpec.getSkuId());
|
|
|
+ item.put("skuSpecId", skuSpec.getId());
|
|
|
+ })
|
|
|
+ .peek(item -> item.put("bomSpecId", bomSpecMap.get(item.get("bomSpecId").toString())))
|
|
|
+ .map(item -> BeanUtil.toBean(item, SkuSpecLink.class))
|
|
|
+ .filter(item -> ObjectUtil.isNotNull(item.getBomSpecId()))
|
|
|
+ .filter(item -> ObjectUtil.isNotNull(item.getDepartmentId()))
|
|
|
+ .filter(item -> ObjectUtil.isNotNull(item.getSkuId()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ skuSpecLinkService.saveBatch(collect);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|