迁移 商品规格
This commit is contained in:
parent
89b2712adf
commit
e94fb5f44c
6
pom.xml
6
pom.xml
|
|
@ -84,6 +84,12 @@
|
|||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.35</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>2.0.54</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ public class CodeGen {
|
|||
//设置表前缀和只生成哪些表,setGenerateTable 未配置时,生成所有表
|
||||
globalConfig.getStrategyConfig()
|
||||
.setTablePrefix("tb_")
|
||||
.setGenerateTable("tb_shop_prod_unit");
|
||||
.setGenerateTable("tb_shop_prod_spec");
|
||||
|
||||
EntityConfig entityConfig = globalConfig.getEntityConfig();
|
||||
if (isOldVersion) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.czg.mergedata.controller;
|
||||
|
||||
import com.czg.mergedata.common.resp.CzgResult;
|
||||
import com.czg.mergedata.cur.service.CurShopProdSpecService;
|
||||
import com.czg.mergedata.cur.service.CurShopProdUnitService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
|
@ -16,9 +17,17 @@ public class ProductController {
|
|||
@Resource
|
||||
private CurShopProdUnitService curShopProdUnitService;
|
||||
|
||||
@Resource
|
||||
private CurShopProdSpecService curShopProdSpecService;
|
||||
|
||||
@RequestMapping("/mergeUnit")
|
||||
public CzgResult<String> mergeUnit() {
|
||||
return curShopProdUnitService.mergeProductUnit();
|
||||
}
|
||||
|
||||
@RequestMapping("/mergeSpec")
|
||||
public CzgResult<String> mergeSpec() {
|
||||
return curShopProdSpecService.mergeProductSpec();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,91 @@
|
|||
package com.czg.mergedata.cur.entity;
|
||||
|
||||
import com.mybatisflex.annotation.Column;
|
||||
import com.mybatisflex.annotation.Id;
|
||||
import com.mybatisflex.annotation.KeyType;
|
||||
import com.mybatisflex.annotation.Table;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 商品规格 实体类。
|
||||
*
|
||||
* @author mac
|
||||
* @since 2025-02-17
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table("tb_shop_prod_spec")
|
||||
public class CurShopProdSpec implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@Id(keyType = KeyType.Auto)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 规格名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 完整规格名称
|
||||
*/
|
||||
private String fullName;
|
||||
|
||||
/**
|
||||
* 规格级别
|
||||
*/
|
||||
private Integer level;
|
||||
|
||||
/**
|
||||
* 排序
|
||||
*/
|
||||
private Integer sort;
|
||||
|
||||
/**
|
||||
* 上级id
|
||||
*/
|
||||
private Long pid;
|
||||
|
||||
/**
|
||||
* 所有上级id,用逗号分隔
|
||||
*/
|
||||
private String pids;
|
||||
|
||||
/**
|
||||
* 店铺id
|
||||
*/
|
||||
private Long shopId;
|
||||
|
||||
/**
|
||||
* 状态 0-禁用 1-启用
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Column(onInsertValue = "now()")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@Column(onInsertValue = "now()", onUpdateValue = "now()")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package com.czg.mergedata.cur.mapper;
|
||||
|
||||
import com.mybatisflex.annotation.UseDataSource;
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
import com.czg.mergedata.cur.entity.CurShopProdSpec;
|
||||
|
||||
/**
|
||||
* 商品规格 映射层。
|
||||
*
|
||||
* @author mac
|
||||
* @since 2025-02-17
|
||||
*/
|
||||
@UseDataSource("ds1")
|
||||
public interface CurShopProdSpecMapper extends BaseMapper<CurShopProdSpec> {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package com.czg.mergedata.cur.service;
|
||||
|
||||
import com.czg.mergedata.common.resp.CzgResult;
|
||||
import com.mybatisflex.core.service.IService;
|
||||
import com.czg.mergedata.cur.entity.CurShopProdSpec;
|
||||
|
||||
/**
|
||||
* 商品规格 服务层。
|
||||
*
|
||||
* @author mac
|
||||
* @since 2025-02-17
|
||||
*/
|
||||
public interface CurShopProdSpecService extends IService<CurShopProdSpec> {
|
||||
CzgResult<String> mergeProductSpec();
|
||||
}
|
||||
|
|
@ -0,0 +1,159 @@
|
|||
package com.czg.mergedata.cur.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.czg.mergedata.common.resp.CzgResult;
|
||||
import com.czg.mergedata.common.utils.PageUtils;
|
||||
import com.czg.mergedata.cur.service.CurShopIdRelationService;
|
||||
import com.czg.mergedata.old.entity.OldProductSpec;
|
||||
import com.czg.mergedata.old.service.OldProductSpecService;
|
||||
import com.mybatisflex.core.paginate.Page;
|
||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||
import com.czg.mergedata.cur.entity.CurShopProdSpec;
|
||||
import com.czg.mergedata.cur.mapper.CurShopProdSpecMapper;
|
||||
import com.czg.mergedata.cur.service.CurShopProdSpecService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 商品规格 服务层实现。
|
||||
*
|
||||
* @author mac
|
||||
* @since 2025-02-17
|
||||
*/
|
||||
@Service
|
||||
public class CurShopProdSpecServiceImpl extends ServiceImpl<CurShopProdSpecMapper, CurShopProdSpec> implements CurShopProdSpecService{
|
||||
|
||||
@Resource
|
||||
private CurShopIdRelationService curShopIdRelationService;
|
||||
|
||||
@Resource
|
||||
private OldProductSpecService oldProductSpecService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public CzgResult<String> mergeProductSpec() {
|
||||
Map<Long, Long> oldAndCurShopIdMap = curShopIdRelationService.getOldShopIdRelation();
|
||||
|
||||
mergeProductSpec(oldAndCurShopIdMap);
|
||||
return CzgResult.success("迁移成功");
|
||||
}
|
||||
|
||||
private void mergeProductSpec(Map<Long, Long> oldAndCurShopIdMap) {
|
||||
Page<OldProductSpec> page = oldProductSpecService.page(PageUtils.buildPage());
|
||||
|
||||
while (page.hasNext() || page.getPageNumber() == 1) {
|
||||
saveProdSpec(page.getRecords(), oldAndCurShopIdMap);
|
||||
|
||||
page = oldProductSpecService.page(PageUtils.buildPage(page.getPageNumber() + 1));
|
||||
}
|
||||
}
|
||||
|
||||
private void saveProdSpec(List<OldProductSpec> oldProductSpecs, Map<Long, Long> oldAndCurShopIdMap) {
|
||||
List<CurShopProdSpec> firstSpecs = new ArrayList<>();
|
||||
|
||||
// 保存一级规格
|
||||
List<Long> parentIds = List.of(0L);
|
||||
String pids = parentIds.stream()
|
||||
.map(Object::toString)
|
||||
.collect(Collectors.joining(",")) + ",";
|
||||
for (OldProductSpec oldProductSpec : oldProductSpecs) {
|
||||
CurShopProdSpec curShopProdSpec = new CurShopProdSpec();
|
||||
|
||||
Long curShopId = oldAndCurShopIdMap.get(Long.valueOf(oldProductSpec.getShopId()));
|
||||
curShopProdSpec.setShopId(curShopId == null ? 1L : curShopId);
|
||||
curShopProdSpec.setLevel(1);
|
||||
curShopProdSpec.setPid(0L);
|
||||
curShopProdSpec.setPids(pids);
|
||||
curShopProdSpec.setStatus(1);
|
||||
curShopProdSpec.setName(oldProductSpec.getName());
|
||||
curShopProdSpec.setFullName(oldProductSpec.getName());
|
||||
curShopProdSpec.setSort(oldProductSpec.getSort() == null ? 1 : oldProductSpec.getSort());
|
||||
|
||||
firstSpecs.add(curShopProdSpec);
|
||||
}
|
||||
|
||||
saveBatch(firstSpecs);
|
||||
|
||||
Map<Long, CurShopProdSpec> oldAndCurSpecIdMap = new HashMap<>();
|
||||
for (OldProductSpec oldProductSpec : oldProductSpecs) {
|
||||
for (CurShopProdSpec firstSpec : firstSpecs) {
|
||||
if (oldProductSpec.getName().equals(firstSpec.getName())) {
|
||||
oldAndCurSpecIdMap.put(Long.valueOf(oldProductSpec.getShopId()), firstSpec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 保存二级规格
|
||||
for (OldProductSpec oldProductSpec : oldProductSpecs) {
|
||||
if (StrUtil.isBlank(oldProductSpec.getSpecList())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
JSONArray jsonArray = JSONArray.parse(oldProductSpec.getSpecList());
|
||||
if (jsonArray.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
CurShopProdSpec first = oldAndCurSpecIdMap.get(Long.valueOf(oldProductSpec.getShopId()));
|
||||
|
||||
List<Long> pids2 = List.of(0L, first.getId());
|
||||
String pids2Str = pids2.stream()
|
||||
.map(Object::toString)
|
||||
.collect(Collectors.joining(",")) + ",";
|
||||
for (Object item : jsonArray) {
|
||||
JSONObject jsonItem = (JSONObject) item;
|
||||
|
||||
CurShopProdSpec curShopProdSpec = new CurShopProdSpec();
|
||||
curShopProdSpec.setName(jsonItem.getString("name"));
|
||||
curShopProdSpec.setFullName(String.format("%s-%s", first.getFullName(), curShopProdSpec.getName()));
|
||||
curShopProdSpec.setLevel(2);
|
||||
curShopProdSpec.setSort(1);
|
||||
curShopProdSpec.setPid(first.getPid());
|
||||
curShopProdSpec.setPids(pids2Str);
|
||||
curShopProdSpec.setShopId(first.getShopId());
|
||||
curShopProdSpec.setStatus(1);
|
||||
|
||||
save(curShopProdSpec);
|
||||
|
||||
String value = jsonItem.getString("value");
|
||||
if (StrUtil.isBlank(value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
JSONArray array = JSONArray.parse(value);
|
||||
if (array.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
List<CurShopProdSpec> thirdSpecs = new ArrayList<>();
|
||||
List<Long> pids3 = List.of(0L, first.getId(), curShopProdSpec.getId());
|
||||
String pids3Str = pids3.stream()
|
||||
.map(Object::toString)
|
||||
.collect(Collectors.joining(",")) + ",";
|
||||
for (Object item2 : array) {
|
||||
String name = (String) item2;
|
||||
CurShopProdSpec curShopProdSpec2 = new CurShopProdSpec();
|
||||
curShopProdSpec2.setName(name);
|
||||
curShopProdSpec2.setFullName(String.format("%s-%s", curShopProdSpec.getFullName(), name));
|
||||
curShopProdSpec2.setLevel(3);
|
||||
curShopProdSpec2.setSort(1);
|
||||
curShopProdSpec2.setPid(curShopProdSpec.getPid());
|
||||
curShopProdSpec2.setPids(pids3Str);
|
||||
curShopProdSpec2.setShopId(first.getShopId());
|
||||
curShopProdSpec2.setStatus(1);
|
||||
|
||||
thirdSpecs.add(curShopProdSpec2);
|
||||
}
|
||||
|
||||
saveBatch(thirdSpecs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package com.czg.mergedata.old.entity;
|
||||
|
||||
import com.mybatisflex.annotation.Id;
|
||||
import com.mybatisflex.annotation.KeyType;
|
||||
import com.mybatisflex.annotation.Table;
|
||||
import java.io.Serializable;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 商品规格 实体类。
|
||||
*
|
||||
* @author mac
|
||||
* @since 2025-02-17
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table("tb_product_spec")
|
||||
public class OldProductSpec implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@Id(keyType = KeyType.Auto)
|
||||
private Integer id;
|
||||
|
||||
private String shopId;
|
||||
|
||||
/**
|
||||
* 商品属性名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 规格属性
|
||||
*/
|
||||
private String specTag;
|
||||
|
||||
/**
|
||||
* 属性介绍
|
||||
*/
|
||||
private String specTagDetail;
|
||||
|
||||
private String specList;
|
||||
|
||||
/**
|
||||
* 排序
|
||||
*/
|
||||
private Integer sort;
|
||||
|
||||
private Long createdAt;
|
||||
|
||||
private Long updatedAt;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package com.czg.mergedata.old.mapper;
|
||||
|
||||
import com.mybatisflex.annotation.UseDataSource;
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
import com.czg.mergedata.old.entity.OldProductSpec;
|
||||
|
||||
/**
|
||||
* 商品规格 映射层。
|
||||
*
|
||||
* @author mac
|
||||
* @since 2025-02-17
|
||||
*/
|
||||
@UseDataSource("ds2")
|
||||
public interface OldProductSpecMapper extends BaseMapper<OldProductSpec> {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package com.czg.mergedata.old.service;
|
||||
|
||||
import com.mybatisflex.core.service.IService;
|
||||
import com.czg.mergedata.old.entity.OldProductSpec;
|
||||
|
||||
/**
|
||||
* 商品规格 服务层。
|
||||
*
|
||||
* @author mac
|
||||
* @since 2025-02-17
|
||||
*/
|
||||
public interface OldProductSpecService extends IService<OldProductSpec> {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package com.czg.mergedata.old.service.impl;
|
||||
|
||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||
import com.czg.mergedata.old.entity.OldProductSpec;
|
||||
import com.czg.mergedata.old.mapper.OldProductSpecMapper;
|
||||
import com.czg.mergedata.old.service.OldProductSpecService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 商品规格 服务层实现。
|
||||
*
|
||||
* @author mac
|
||||
* @since 2025-02-17
|
||||
*/
|
||||
@Service
|
||||
public class OldProductSpecServiceImpl extends ServiceImpl<OldProductSpecMapper, OldProductSpec> implements OldProductSpecService{
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.czg.mergedata.cur.mapper.CurShopProdSpecMapper">
|
||||
|
||||
</mapper>
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.czg.mergedata.old.mapper.OldProductSpecMapper">
|
||||
|
||||
</mapper>
|
||||
Loading…
Reference in New Issue