|
|
package com.yoho.ufo.service.impl;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.common.utils.DateUtil;
|
|
|
import com.yoho.core.dal.datasource.annotation.Database;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.order.dal.SellerOrderGoodsMapper;
|
|
|
import com.yoho.ufo.dal.ProductSortMapper;
|
|
|
import com.yoho.ufo.dal.StorageMapper;
|
|
|
import com.yoho.ufo.dal.UfoSizeMapper;
|
|
|
import com.yoho.ufo.model.brand.BrandSeries;
|
|
|
import com.yoho.ufo.model.commoditybasicrole.category.ProductSort;
|
|
|
import com.yoho.ufo.model.commoditybasicrole.size.Size;
|
|
|
import com.yoho.ufo.service.ISizeService;
|
|
|
import com.yoho.ufo.util.CollectionUtil;
|
|
|
import com.yoho.ufo.util.OrikaUtils;
|
|
|
import com.yohobuy.ufo.model.common.ApiResponse;
|
|
|
import com.yohobuy.ufo.model.common.PageModel;
|
|
|
import com.yohobuy.ufo.model.common.PageResponseBO;
|
|
|
import com.yohobuy.ufo.model.request.size.SizeRequestBo;
|
|
|
import com.yohobuy.ufo.model.request.size.SizeResponseBo;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author kun.wang
|
...
|
...
|
@@ -33,37 +45,87 @@ public class SizeServiceImpl implements ISizeService { |
|
|
|
|
|
@Autowired
|
|
|
private UfoSizeMapper ufoSizeMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private ProductSortMapper productSortMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private StorageMapper storageMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private ISizeService sizeService;
|
|
|
|
|
|
@Override
|
|
|
public int saveOrUpdateSize(SizeRequestBo sizeRequestBo) {
|
|
|
LOGGER.info("saveOrUpdateSize param = {}", sizeRequestBo);
|
|
|
Size size = OrikaUtils.map(sizeRequestBo, Size.class);
|
|
|
if (size.getId() == null || size.getId() == 0) {
|
|
|
// 判断排序值是否已存在
|
|
|
List<Size> sizeList = ufoSizeMapper.getOrderBySortLevel2Id(size.getSortId());
|
|
|
List<Integer> orderBys = sizeList.stream().map(size1 -> size1.getOrderBy()).collect(Collectors.toList());
|
|
|
if (orderBys.contains(size.getOrderBy())) {
|
|
|
throw new ServiceException(201, "排序值" + size.getOrderBy() + "已存在!");
|
|
|
}
|
|
|
size.setCreateTime(DateUtil.currentTimeSeconds());
|
|
|
size.setUpdateTime(size.getCreateTime());
|
|
|
size.setDel(0);
|
|
|
size.setUpdateTime(0);
|
|
|
// 新增
|
|
|
return ufoSizeMapper.insertSize(size);
|
|
|
} else {
|
|
|
List<Size> sizeList = ufoSizeMapper.getOrderBySortLevel2Id(size.getSortId());
|
|
|
for (Size size1 : sizeList) {
|
|
|
if (!size1.getId().equals(size.getId())) {
|
|
|
if (size1.getOrderBy().equals(size.getOrderBy())) {
|
|
|
throw new ServiceException(201, "排序值" + size.getOrderBy() + "已存在!");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 更新
|
|
|
size.setUpdateTime(DateUtil.currentTimeSeconds());
|
|
|
return ufoSizeMapper.updateSize(size);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int addOrUpdateBind(SizeRequestBo sizeRequestBo) {
|
|
|
LOGGER.info("addOrUpdateBind param = {}", sizeRequestBo);
|
|
|
Integer bindId = sizeRequestBo.getId();
|
|
|
Integer sizeId = sizeRequestBo.getSizeId();
|
|
|
Integer sortId = sizeRequestBo.getSortId();
|
|
|
Integer orderBy = sizeRequestBo.getOrderBy();
|
|
|
if (orderBy == null || orderBy < 1) {
|
|
|
throw new ServiceException(400, "请填写排序值");
|
|
|
}
|
|
|
|
|
|
Size size = OrikaUtils.map(sizeRequestBo, Size.class);
|
|
|
if (bindId != null && bindId > 0) {
|
|
|
Size binded = ufoSizeMapper.getBindById(bindId);
|
|
|
if (binded == null) {
|
|
|
throw new ServiceException(400, "该绑定关系不存在");
|
|
|
}
|
|
|
size.setSizeId(null);
|
|
|
size.setSortId(null);
|
|
|
size.setUpdateTime(DateUtil.currentTimeSeconds());
|
|
|
return ufoSizeMapper.updateBind(size);
|
|
|
} else {
|
|
|
if (sizeId == null || sizeId < 1) {
|
|
|
throw new ServiceException(400, "请选择尺码");
|
|
|
}
|
|
|
if (sortId == null || sortId < 1) {
|
|
|
throw new ServiceException(400, "请选择品类");
|
|
|
}
|
|
|
Size binded = ufoSizeMapper.getBySizeIdAndSortId(sizeId, sortId);
|
|
|
if (binded != null) {
|
|
|
throw new ServiceException(400, "该绑定关系已经存在");
|
|
|
}
|
|
|
size.setCreateTime(DateUtil.currentTimeSeconds());
|
|
|
size.setUpdateTime(0);
|
|
|
return ufoSizeMapper.addBind(size);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int removeBind(SizeRequestBo sizeRequestBo) {
|
|
|
LOGGER.info("removeBind param = {}", sizeRequestBo);
|
|
|
Size size = OrikaUtils.map(sizeRequestBo, Size.class);
|
|
|
Size binded = ufoSizeMapper.getBindById(sizeRequestBo.getId());
|
|
|
if (binded != null) {
|
|
|
size.setDel(1);
|
|
|
size.setUpdateTime(DateUtil.currentTimeSeconds());
|
|
|
return ufoSizeMapper.updateBind(size);
|
|
|
} else {
|
|
|
throw new ServiceException(400, "该绑定关系不存在");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SizeResponseBo getSizeById(Integer id) {
|
...
|
...
|
@@ -72,26 +134,36 @@ public class SizeServiceImpl implements ISizeService { |
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public PageResponseBO<SizeResponseBo> getSizePageList(SizeRequestBo sizeRequestBo) {
|
|
|
LOGGER.info("getSizePageList param = {}", sizeRequestBo);
|
|
|
Size size = OrikaUtils.map(sizeRequestBo, Size.class);
|
|
|
PageModel pageModel = sizeRequestBo.toPageModel();
|
|
|
int count = ufoSizeMapper.selectSizeCount(size);
|
|
|
if (count == 0) {
|
|
|
LOGGER.warn("getSizePageList count is 0. param = {}", sizeRequestBo);
|
|
|
return new PageResponseBO<>();
|
|
|
}
|
|
|
List<Size> sizeList = ufoSizeMapper.getSizePageList(size, pageModel);
|
|
|
List<SizeResponseBo> responseBos = OrikaUtils.mapToList(sizeList, SizeResponseBo.class);
|
|
|
return new PageResponseBO<>(count, responseBos, pageModel.getCurrentPage(), pageModel.getPageSize());
|
|
|
}
|
|
|
public PageResponseBO<SizeResponseBo> getSizePageList(SizeRequestBo sizeRequestBo) {
|
|
|
LOGGER.info("getSizePageList param = {}", sizeRequestBo);
|
|
|
Size size = OrikaUtils.map(sizeRequestBo, Size.class);
|
|
|
PageModel pageModel = sizeRequestBo.toPageModel();
|
|
|
List<Integer> sortIdList = null;
|
|
|
if (sizeRequestBo.getSortId() != null && sizeRequestBo.getSortId() > 0) {
|
|
|
ProductSort sort = productSortMapper.selectOneById(sizeRequestBo.getSortId());
|
|
|
if (sort != null) {
|
|
|
sortIdList = new ArrayList<>();
|
|
|
sortIdList.add(sizeRequestBo.getSortId());
|
|
|
if (sort.getParentId() != null && sort.getParentId() > 0) {
|
|
|
sortIdList.add(sort.getParentId());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
int count = ufoSizeMapper.selectSizeCount(size, sortIdList);
|
|
|
if (count == 0) {
|
|
|
LOGGER.warn("getSizePageList count is 0. param = {}", sizeRequestBo);
|
|
|
return new PageResponseBO<>();
|
|
|
}
|
|
|
List<Size> sizeList = ufoSizeMapper.getSizePageList(size, sortIdList, pageModel);
|
|
|
List<SizeResponseBo> responseBos = OrikaUtils.mapToList(sizeList, SizeResponseBo.class);
|
|
|
return new PageResponseBO<>(count, responseBos, pageModel.getCurrentPage(), pageModel.getPageSize());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询所有的尺码,下拉框使用
|
|
|
* @return
|
|
|
*/
|
|
|
public List<JSONObject> queryAllSize4Goods(Integer sortId) {
|
|
|
// TODO 可以添加缓存
|
|
|
List<Size> sizes = ufoSizeMapper.selectAllSizeBySortId(sortId);
|
|
|
List<JSONObject> result = new ArrayList<>();
|
|
|
JSONObject jsonObject;
|
...
|
...
|
@@ -104,6 +176,117 @@ public class SizeServiceImpl implements ISizeService { |
|
|
revealEmptyItem(result);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询所有的尺码,下拉框使用
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
public List<JSONObject> queryAllSize() {
|
|
|
List<Size> sizes = ufoSizeMapper.selectAllSize();
|
|
|
List<JSONObject> result = new ArrayList<>();
|
|
|
JSONObject jsonObject;
|
|
|
for (Size item : sizes) {
|
|
|
jsonObject = new JSONObject();
|
|
|
jsonObject.put("id", item.getId());
|
|
|
jsonObject.put("text", item.getSizeName());
|
|
|
result.add(jsonObject);
|
|
|
}
|
|
|
revealEmptyItem(result);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 查询所有的尺码,下拉框使用
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
public List<JSONObject> queryAllSecondSize4Goods(Integer sortId) {
|
|
|
List<JSONObject> result = new ArrayList<>();
|
|
|
ProductSort sort = productSortMapper.selectOneById(sortId);
|
|
|
List<Size> sizes;
|
|
|
if (sort != null) {
|
|
|
List<Integer> idList = new ArrayList<>();
|
|
|
idList.add(sortId);
|
|
|
if (sort.getParentId() != null && sort.getParentId() > 0) {
|
|
|
idList.add(sort.getParentId());
|
|
|
}
|
|
|
sizes = ufoSizeMapper.selectAllSizeBySortIdList(idList);
|
|
|
} else {
|
|
|
sizes = new ArrayList<>();
|
|
|
}
|
|
|
JSONObject jsonObject;
|
|
|
for (Size item : sizes) {
|
|
|
jsonObject = new JSONObject();
|
|
|
jsonObject.put("id", item.getId());
|
|
|
jsonObject.put("text", item.getSizeName());
|
|
|
result.add(jsonObject);
|
|
|
}
|
|
|
revealEmptyItem(result);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public PageResponseBO<SizeResponseBo> getBindInfo(SizeRequestBo sizeRequestBo) {
|
|
|
LOGGER.info("getBindInfo param = {}", sizeRequestBo);
|
|
|
Size size = OrikaUtils.map(sizeRequestBo, Size.class);
|
|
|
PageModel pageModel = sizeRequestBo.toPageModel();
|
|
|
List<Integer> sortIdList = null;
|
|
|
if (sizeRequestBo.getSortId2() != null && sizeRequestBo.getSortId2() > 0) {
|
|
|
sortIdList = new ArrayList<>();
|
|
|
sortIdList.add(sizeRequestBo.getSortId2());
|
|
|
} else if (sizeRequestBo.getSortId() != null && sizeRequestBo.getSortId() > 0) {
|
|
|
ProductSort sort = productSortMapper.selectOneById(sizeRequestBo.getSortId());
|
|
|
if (sort != null) {
|
|
|
sortIdList = new ArrayList<>();
|
|
|
sortIdList.add(sizeRequestBo.getSortId());
|
|
|
List<ProductSort> children = productSortMapper.getChildren(sort.getId());
|
|
|
sortIdList.addAll(CollectionUtil.map(children, ProductSort::getId));
|
|
|
}
|
|
|
}
|
|
|
int count = ufoSizeMapper.getBindCount(size, sortIdList);
|
|
|
if (count == 0) {
|
|
|
LOGGER.warn("getBindInfo count is 0. param = {}", sizeRequestBo);
|
|
|
return new PageResponseBO<>();
|
|
|
}
|
|
|
List<Size> sizeList = ufoSizeMapper.getBindPageList(size, sortIdList, pageModel);
|
|
|
List<Integer> rSizeIdList = CollectionUtil.map(sizeList, Size::getSizeId);
|
|
|
List<Integer> rSortIdList = CollectionUtil.map(sizeList, Size::getSortId);
|
|
|
List<ProductSort> sorts = productSortMapper.selectSortByIdList(rSortIdList);
|
|
|
List<Size> sizes = ufoSizeMapper.selectByIdList(rSizeIdList);
|
|
|
Map<Integer, Size> sizeMap = CollectionUtil.extractMap(sizes, Size::getId);
|
|
|
Map<Integer, ProductSort> sortMap = CollectionUtil.extractMap(sorts, ProductSort::getId);
|
|
|
List<SizeResponseBo> responseBos = new ArrayList<>();
|
|
|
for (Size s : sizeList) {
|
|
|
Size s0 = sizeMap.get(s.getSizeId());
|
|
|
ProductSort ps0 = sortMap.get(s.getSortId());
|
|
|
SizeResponseBo bo = new SizeResponseBo();
|
|
|
bo.setId(s.getId());
|
|
|
bo.setOrderBy(s.getOrderBy());
|
|
|
bo.setSortId(s.getSortId());
|
|
|
bo.setSizeId(s.getSizeId());
|
|
|
if (s0 == null || StringUtils.isBlank(s0.getSizeName())) {
|
|
|
bo.setSizeName("");
|
|
|
} else {
|
|
|
bo.setSizeName(s0.getSizeName());
|
|
|
}
|
|
|
if (ps0 == null || StringUtils.isBlank(ps0.getSortName())) {
|
|
|
bo.setSortName("");
|
|
|
} else {
|
|
|
String sortName = ps0.getSortName();
|
|
|
if (ps0.getParentId() != null && ps0.getParentId() > 0) {
|
|
|
ProductSort ps1 = sortMap.get(ps0.getParentId());
|
|
|
if (ps1 != null && StringUtils.isNotBlank(ps1.getSortName())) {
|
|
|
sortName = ps1.getSortName() + "/" + sortName;
|
|
|
}
|
|
|
}
|
|
|
bo.setSortName(sortName);
|
|
|
}
|
|
|
responseBos.add(bo);
|
|
|
}
|
|
|
return new PageResponseBO<>(count, responseBos, pageModel.getCurrentPage(), pageModel.getPageSize());
|
|
|
}
|
|
|
|
|
|
private void revealEmptyItem(List<JSONObject> jsonObjectList) {
|
|
|
JSONObject jsonObject;
|
...
|
...
|
@@ -112,4 +295,98 @@ public class SizeServiceImpl implements ISizeService { |
|
|
jsonObjectList.add(jsonObject);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional
|
|
|
@Database(DataSource = "ufo_product")
|
|
|
public ApiResponse<Object> exchangeData() {
|
|
|
List<Size> allSize = ufoSizeMapper.selectAllSize();
|
|
|
LOGGER.info("开始切换品类尺码数据, 总数 {}", allSize.size());
|
|
|
Map<String, List<Size>> bindMap = CollectionUtil.groupingBy(allSize, Size::getSizeName);
|
|
|
LOGGER.info("开始切换品类尺码数据, bindMap {}", bindMap);
|
|
|
int time = DateUtil.currentTimeSeconds();
|
|
|
try {
|
|
|
Map<Integer, List<Integer>> idReplace = new HashMap<>();
|
|
|
List<String> allSizeName = new ArrayList<>(bindMap.keySet());
|
|
|
Collections.sort(allSizeName);
|
|
|
for (String sName : allSizeName) {
|
|
|
LOGGER.info("切换品类尺码数据, 处理尺码:{}", sName);
|
|
|
List<Size> binded = bindMap.get(sName);
|
|
|
|
|
|
Size s = new Size();
|
|
|
s.setSizeName(sName);
|
|
|
s.setSortId(0);
|
|
|
Integer orderBy = 0;
|
|
|
if (CollectionUtils.isNotEmpty(binded)) {
|
|
|
orderBy = binded.get(0).getOrderBy();
|
|
|
if (orderBy == null) {
|
|
|
orderBy = 0;
|
|
|
} else {
|
|
|
orderBy *= 100;
|
|
|
if(StringUtils.equals("S", sName)) {
|
|
|
orderBy += 50;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
s.setOrderBy(orderBy);
|
|
|
s.setCreateTime(time);
|
|
|
s.setUpdateTime(0);
|
|
|
LOGGER.info("切换品类尺码数据, 构建新尺码:{}", s);
|
|
|
ufoSizeMapper.insertSize(s);
|
|
|
LOGGER.info("切换品类尺码数据, 保存新尺码成功:{}", s);
|
|
|
|
|
|
LOGGER.info("切换品类尺码数据, 获取 {} 绑定的类别", sName);
|
|
|
if (CollectionUtils.isNotEmpty(binded)) {
|
|
|
idReplace.put(s.getId(), CollectionUtil.distinct(binded, Size::getId));
|
|
|
|
|
|
LOGGER.info("切换品类尺码数据, 尺码名称:{},品类总数:{}, 明细:{}", sName, binded.size(), binded);
|
|
|
for (Size b : binded) {
|
|
|
if (b.getSortId() == null || b.getSortId() < 1) {
|
|
|
LOGGER.info("切换品类尺码数据, 尺码名称:{},品类id无效跳过:{}", sName, b.getSortId());
|
|
|
}
|
|
|
LOGGER.info("切换品类尺码数据, 尺码名称:{},构建品类关联:{}", sName, b);
|
|
|
Size ss = new Size();
|
|
|
ss.setSizeId(s.getId());
|
|
|
ss.setSortId(b.getSortId());
|
|
|
ss.setOrderBy(b.getOrderBy());
|
|
|
ss.setDel(0);
|
|
|
ss.setCreateTime(time);
|
|
|
ss.setUpdateTime(0);
|
|
|
LOGGER.info("切换品类尺码数据, 尺码名称:{},构建品类关联结果:{}", sName, ss);
|
|
|
ufoSizeMapper.addBind(ss);
|
|
|
LOGGER.info("切换品类尺码数据, 尺码名称:{},保存品类关联成功:{}", sName, ss);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for (Map.Entry<Integer, List<Integer>> entry : idReplace.entrySet()) {
|
|
|
Integer newId = entry.getKey();
|
|
|
List<Integer> oldList = entry.getValue();
|
|
|
LOGGER.info("切换品类尺码数据, sku老尺码信息换成新的:new:{}, old:{}", newId, oldList);
|
|
|
LOGGER.info("切换品类尺码数据, sku老尺码信息换成新的 rollbackSql :update ufo_product.storage set size_id={} where size_id = {}", oldList.get(0), newId);
|
|
|
int exCount = storageMapper.updateSizeId(newId, oldList);
|
|
|
LOGGER.info("切换品类尺码数据, sku老尺码信息换成新的:new:{}, 修改数:{}", newId, exCount);
|
|
|
}
|
|
|
|
|
|
sizeService.replaceOrderOldSizeId(idReplace);
|
|
|
} catch (Exception e) {
|
|
|
LOGGER.error("切换类别尺码数据错误!", e);
|
|
|
throw new ServiceException(200, "执行失败:" + e.getMessage());
|
|
|
}
|
|
|
return new ApiResponse<>(200, "执行成功!");
|
|
|
}
|
|
|
|
|
|
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
|
|
@Database(DataSource = "ufo_order")
|
|
|
public void replaceOrderOldSizeId(Map<Integer, List<Integer>> idReplace) {
|
|
|
for (Map.Entry<Integer, List<Integer>> entry : idReplace.entrySet()) {
|
|
|
Integer newId = entry.getKey();
|
|
|
List<Integer> oldList = entry.getValue();
|
|
|
|
|
|
LOGGER.info("切换品类尺码数据, order老尺码信息换成新的:new:{}, old:{}", newId, oldList);
|
|
|
LOGGER.info("切换品类尺码数据, sku老尺码信息换成新的 rollbackSql :update ufo_order.seller_order_goods set size_id={} where size_id = {}", oldList.get(0), newId);
|
|
|
int exCount = sellerOrderGoodsMapper.updateSizeId(newId, oldList);
|
|
|
LOGGER.info("切换品类尺码数据, order老尺码信息换成新的:new:{}, 修改数:{}", newId, exCount);
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|