数据同步

This commit is contained in:
Tankaikai 2025-03-25 20:58:10 +08:00
parent 735e142a71
commit 24be50e34d
4 changed files with 114 additions and 125 deletions

View File

@ -32,7 +32,7 @@ public class CurProdGroup implements Serializable {
/**
* id
*/
@Id(keyType = KeyType.Auto)
@Id(keyType = KeyType.None)
private Long id;
/**

View File

@ -32,7 +32,7 @@ public class CurShopProdSpec implements Serializable {
/**
* id
*/
@Id(keyType = KeyType.Auto)
@Id(keyType = KeyType.None)
private Long id;
/**

View File

@ -1,6 +1,9 @@
package com.czg.mergedata.cur.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.czg.mergedata.common.resp.CzgResult;
import com.czg.mergedata.common.utils.PageUtils;
@ -41,7 +44,7 @@ public class CurProdGroupServiceImpl extends ServiceImpl<CurProdGroupMapper, Cur
private OldProductGroupService oldProductGroupService;
@Override
@Transactional
//@Transactional
public CzgResult<String> mergeData() {
getMapper().truncateTable();
curProdGroupRelationMapper.truncateTable();
@ -61,37 +64,52 @@ public class CurProdGroupServiceImpl extends ServiceImpl<CurProdGroupMapper, Cur
}
private void saveProdGroup(List<OldProductGroup> oldProductGroupList) {
List<CurProdGroup> curProdGroupList = new ArrayList<>();
List<CurProdGroupRelation> curProdGroupRelationList = new ArrayList<>();
for (OldProductGroup oldProductGroup : oldProductGroupList) {
CurProdGroup curProdGroup = new CurProdGroup();
curProdGroup.setId(Long.valueOf(oldProductGroup.getId()));
curProdGroup.setName(oldProductGroup.getName());
curProdGroup.setShopId(Long.valueOf(oldProductGroup.getId()));
curProdGroup.setPic(oldProductGroup.getPic());
curProdGroup.setStatus(oldProductGroup.getIsShow());
curProdGroup.setSort(oldProductGroup.getSort());
curProdGroup.setSortMode(oldProductGroup.getSortMode());
curProdGroup.setUseTime(oldProductGroup.getUseTime());
curProdGroup.setSaleStartTime(oldProductGroup.getSaleStartTime());
curProdGroup.setSaleEndTime(oldProductGroup.getSaleEndTime());
curProdGroup.setCreateTime(DateUtil.toLocalDateTime(oldProductGroup.getCreatedAt() == null ? new Date() : new Date(oldProductGroup.getCreatedAt())));
curProdGroupList.add(curProdGroup);
List<Long> list = JSONArray.parseArray(oldProductGroup.getProductIds(), Long.class);
for (OldProductGroup old : oldProductGroupList) {
Integer id = old.getId();
String name = old.getName();
Integer shopId = old.getShopId();
String pic = old.getPic();
Integer isShow = old.getIsShow();
Integer sort = old.getSort();
String sortMode = old.getSortMode();
String productIds = old.getProductIds();
Long createdAt = old.getCreatedAt();
Long updatedAt = old.getUpdatedAt();
Integer useTime = old.getUseTime();
String saleStartTime = old.getSaleStartTime();
String saleEndTime = old.getSaleEndTime();
CurProdGroup cur = new CurProdGroup();
cur.setId(id.longValue());
cur.setName(name);
cur.setShopId(Long.valueOf(shopId));
cur.setPic(pic);
cur.setStatus(isShow);
cur.setSort(sort);
cur.setSortMode(StrUtil.nullToDefault(sortMode, "0"));
cur.setUseTime(useTime);
if (StrUtil.isAllNotEmpty(saleStartTime, saleEndTime)) {
cur.setSaleStartTime(saleStartTime + ":00");
cur.setSaleEndTime(saleEndTime + ":59");
}
cur.setCreateTime(LocalDateTimeUtil.of(createdAt));
cur.setUpdateTime(LocalDateTimeUtil.of(updatedAt));
save(cur);
if (StrUtil.isEmpty(productIds)) {
continue;
}
List<Long> list = JSONArray.parseArray(productIds, Long.class);
if (CollUtil.isEmpty(list)) {
continue;
}
int index = 0;
for (Long productId : list) {
CurProdGroupRelation curProdGroupRelation = new CurProdGroupRelation();
curProdGroupRelation.setProductId(productId);
curProdGroupRelation.setProdGroupId(Long.valueOf(oldProductGroup.getId()));
curProdGroupRelation.setSort(1);
curProdGroupRelationList.add(curProdGroupRelation);
index++;
CurProdGroupRelation relation = new CurProdGroupRelation();
relation.setProductId(productId);
relation.setProdGroupId(cur.getId());
relation.setSort(index);
curProdGroupRelationService.save(relation);
}
}
saveBatch(curProdGroupList);
curProdGroupRelationService.saveBatch(curProdGroupRelationList);
}
}

View File

@ -1,6 +1,11 @@
package com.czg.mergedata.cur.service.impl;
import java.time.LocalDateTime;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.czg.mergedata.common.resp.CzgResult;
@ -29,13 +34,13 @@ import java.util.stream.Collectors;
* @since 2025-02-17
*/
@Service
public class CurShopProdSpecServiceImpl extends ServiceImpl<CurShopProdSpecMapper, CurShopProdSpec> implements CurShopProdSpecService{
public class CurShopProdSpecServiceImpl extends ServiceImpl<CurShopProdSpecMapper, CurShopProdSpec> implements CurShopProdSpecService {
@Resource
private OldProductSpecService oldProductSpecService;
@Override
@Transactional
//@Transactional
public CzgResult<String> mergeProductSpec() {
getMapper().truncateTable();
@ -54,105 +59,71 @@ public class CurShopProdSpecServiceImpl extends ServiceImpl<CurShopProdSpecMappe
}
private void saveProdSpec(List<OldProductSpec> oldProductSpecs) {
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();
Integer id = oldProductSpec.getId();
String shopId = oldProductSpec.getShopId();
String name = oldProductSpec.getName();
Integer sort = oldProductSpec.getSort();
Long createdAt = oldProductSpec.getCreatedAt();
Long updatedAt = oldProductSpec.getUpdatedAt();
CurShopProdSpec one = new CurShopProdSpec();
one.setId(id.longValue());
one.setName(name);
one.setFullName(name);
one.setLevel(1);
one.setSort(sort);
one.setPid(0L);
one.setPids("0,");
one.setShopId(Convert.toLong(shopId));
one.setStatus(1);
one.setCreateTime(LocalDateTimeUtil.of(createdAt));
one.setUpdateTime(LocalDateTimeUtil.of(updatedAt));
save(one);
Long curShopId = Long.valueOf(oldProductSpec.getShopId());
curShopProdSpec.setId(Long.valueOf(oldProductSpec.getId()));
curShopProdSpec.setShopId(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())) {
String specList = oldProductSpec.getSpecList();
if (StrUtil.isEmpty(specList)) {
continue;
}
JSONArray jsonArray = JSONArray.parse(oldProductSpec.getSpecList());
if (jsonArray.isEmpty()) {
JSONArray specListJson = JSON.parseArray(specList);
if (specListJson.isEmpty()) {
continue;
}
for (int i = 0; i < specListJson.size(); i++) {
JSONObject row = specListJson.getJSONObject(i);
String name2 = row.getString("name");
int index = i + 1;
CurShopProdSpec two = new CurShopProdSpec();
two.setId(Long.valueOf(one.getId() + "" + index));
two.setName(name2);
two.setFullName(one.getName() + " - " + name2);
two.setLevel(2);
two.setSort(index);
two.setPid(one.getId());
two.setPids(one.getPids() + Convert.toStr(one.getId()) + ",");
two.setShopId(Convert.toLong(shopId));
two.setStatus(1);
two.setCreateTime(one.getCreateTime());
two.setUpdateTime(one.getUpdateTime());
save(two);
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.getId());
curShopProdSpec.setPids(pids2Str);
curShopProdSpec.setShopId(first.getShopId());
curShopProdSpec.setStatus(1);
save(curShopProdSpec);
String value = jsonItem.getString("value");
if (StrUtil.isBlank(value)) {
continue;
JSONArray v = row.getJSONArray("value");
for (int j = 0; j < v.size(); j++) {
String name3 = v.getString(j);
int seq = j + 1;
CurShopProdSpec three = new CurShopProdSpec();
three.setId(Long.valueOf(two.getId() + "" + seq));
three.setName(name3);
three.setFullName(two.getName() + " - " + name3);
three.setLevel(3);
three.setSort(seq);
three.setPid(two.getId());
three.setPids(two.getPids() + Convert.toStr(two.getId()) + ",");
three.setShopId(Convert.toLong(shopId));
three.setStatus(1);
three.setCreateTime(two.getCreateTime());
three.setUpdateTime(two.getUpdateTime());
save(three);
}
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.getId());
curShopProdSpec2.setPids(pids3Str);
curShopProdSpec2.setShopId(first.getShopId());
curShopProdSpec2.setStatus(1);
thirdSpecs.add(curShopProdSpec2);
}
saveBatch(thirdSpecs);
}
}
}