package com.slodon.b2b2c.model.goods;

import com.alibaba.fastjson.JSON;
import com.slodon.b2b2c.core.constant.GoodsConst;
import com.slodon.b2b2c.core.constant.GoodsPlatformConst;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.uid.GoodsIdGenerator;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.dao.read.goods.*;
import com.slodon.b2b2c.dao.write.goods.*;
import com.slodon.b2b2c.goods.dto.GoodsPlatformDTO;
import com.slodon.b2b2c.goods.dto.GoodsPlatformInsertDTO;
import com.slodon.b2b2c.goods.dto.GoodsPublishFrontParamDTO;
import com.slodon.b2b2c.goods.example.*;
import com.slodon.b2b2c.goods.pojo.*;
import com.slodon.b2b2c.system.pojo.Admin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 平台库商品表model
 */
@Component
@Slf4j
public class GoodsPlatformModel {

    @Resource
    private GoodsPlatformReadMapper goodsPlatformReadMapper;
    @Resource
    private GoodsPlatformWriteMapper goodsPlatformWriteMapper;
    @Resource
    private ProductPlatformReadMapper productPlatformReadMapper;
    @Resource
    private ProductPlatformWriteMapper productPlatformWriteMapper;
    @Resource
    private GoodsPlatformPictureWriteMapper goodsPlatformPictureWriteMapper;
    @Resource
    private GoodsPlatformBindAttributeValueWriteMapper goodsPlatformBindAttributeValueWriteMapper;
    @Resource
    private GoodsPlatformBindReserveNameWriteMapper goodsPlatformBindReserveNameWriteMapper;
    @Resource
    private GoodsReadMapper goodsReadMapper;
    @Resource
    private GoodsExtendReadMapper goodsExtendReadMapper;
    @Resource
    private GoodsPictureReadMapper goodsPictureReadMapper;
    @Resource
    private ProductReadMapper productReadMapper;
    @Resource
    private GoodsCategoryReadMapper goodsCategoryReadMapper;
    @Resource
    private GoodsBrandReadMapper goodsBrandReadMapper;
    @Resource
    private GoodsBindAttributeValueReadMapper goodsBindAttributeValueReadMapper;
    @Resource
    private GoodsBindReserveNameReadMapper goodsBindReserveNameReadMapper;
    @Resource
    private GoodsSpecReadMapper goodsSpecReadMapper;
    @Resource
    private GoodsSpecWriteMapper goodsSpecWriteMapper;
    @Resource
    private GoodsSpecValueReadMapper goodsSpecValueReadMapper;
    @Resource
    private GoodsSpecValueWriteMapper goodsSpecValueWriteMapper;

    //region 发布商品

    /**
     * 平台发布商品
     * -保存商品信息
     * -保存货品信息
     * -保存商品属性绑定表（检索属性）
     * -保存商品图片表
     *
     * @param insertDTO 商品信息
     * @param adminId   管理员id
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveGoods(GoodsPlatformInsertDTO insertDTO, Integer adminId) {
        //货品列表
        List<GoodsPlatformDTO.ProductPlatformInfo> productList = insertDTO.getProductList();

        //-校验sku是否重复
        this.checkSku(productList);

        //-保存商品信息
        GoodsPlatform goodsPlatform = this.insertGoodsPlatform(insertDTO, adminId);

        //-保存货品信息
        this.insertProductPlatform(productList, goodsPlatform);

        //-保存商品属性绑定表（检索属性）
        this.insertGoodsBindAttribute(insertDTO.getAttributeAndParameter(), goodsPlatform.getPlatformGoodsId());

        //-保存商品图片表
        this.insertGoodsPicture(insertDTO.getSpecInfoList(), insertDTO.getImageList(), goodsPlatform, adminId);

        //-保存虚拟商品用户预留信息名称表
        this.insertGoodsBindReserveName(insertDTO.getReserveInfoList(), goodsPlatform.getGoodsId(), insertDTO.getIsVirtualGoods());
    }

    /**
     * 校验条形码是否重复
     *
     * @param productList
     */
    private void checkSku(List<GoodsPlatformDTO.ProductPlatformInfo> productList) {
        productList.forEach(productInfo -> {
            if (!StringUtils.isEmpty(productInfo.getBarCode())) {
                ProductPlatformExample example = new ProductPlatformExample();
                example.setBarCode(productInfo.getBarCode());
                int count = productPlatformReadMapper.countByExample(example);
                AssertUtil.isTrue(count > 0, "商品条形码[" + productInfo.getBarCode() + "]已存在，请重新输入");
            }
        });
    }

    /**
     * 发布商品-插入商品
     *
     * @param insertDTO 商品信息
     * @param adminId   管理员id
     * @return
     */
    private GoodsPlatform insertGoodsPlatform(GoodsPlatformInsertDTO insertDTO, Integer adminId) {
        //平台库商品id
        long platformGoodsId = GoodsIdGenerator.goodsIdGenerator();
        //生成一个默认货品id
        long defaultProductId = GoodsIdGenerator.productIdGenerator();
        //默认货品
        GoodsPlatformInsertDTO.ProductPlatformInsertInfo defaultProduct = insertDTO.getDefaultProduct();

        //查询分类信息
        GoodsCategory goodsCategory3 = goodsCategoryReadMapper.getByPrimaryKey(insertDTO.getCategoryId3());
        AssertUtil.notNull(goodsCategory3, "分类有误");
        GoodsCategory goodsCategory2 = goodsCategoryReadMapper.getByPrimaryKey(goodsCategory3.getPid());
        GoodsCategory goodsCategory1 = goodsCategoryReadMapper.getByPrimaryKey(goodsCategory2.getPid());

        GoodsPlatform goodsPlatform = new GoodsPlatform();
        goodsPlatform.setPlatformGoodsId(platformGoodsId);
        goodsPlatform.setGoodsName(insertDTO.getGoodsName());
        goodsPlatform.setGoodsBrief(insertDTO.getGoodsBrief());
        if (!StringUtil.isNullOrZero(insertDTO.getBrandId())) {
            //查询品牌信息
            GoodsBrand goodsBrand = goodsBrandReadMapper.getByPrimaryKey(insertDTO.getBrandId());
            AssertUtil.notNull(goodsBrand, "品牌有误");
            goodsPlatform.setBrandId(insertDTO.getBrandId());
            goodsPlatform.setBrandName(goodsBrand.getBrandName());
        }
        goodsPlatform.setCategoryId1(goodsCategory1.getCategoryId());
        goodsPlatform.setCategoryId2(goodsCategory2.getCategoryId());
        goodsPlatform.setCategoryId3(goodsCategory3.getCategoryId());
        goodsPlatform.setCategoryPath(goodsCategory1.getCategoryName() + "->" + goodsCategory2.getCategoryName() + "->" + goodsCategory3.getCategoryName());
        goodsPlatform.setMarketPrice(defaultProduct.getMarketPrice());
        goodsPlatform.setGoodsPrice(defaultProduct.getProductPrice());
        goodsPlatform.setIsSpec(CollectionUtils.isEmpty(insertDTO.getSpecInfoList()) ? GoodsConst.IS_SPEC_NO : GoodsConst.IS_SPEC_YES);
        goodsPlatform.setMainSpecId(insertDTO.getMainSpecId());
        goodsPlatform.setState(insertDTO.getState());
        goodsPlatform.setStoreId(0L);
        goodsPlatform.setMainImage(defaultProduct.getMainImage());
        goodsPlatform.setGoodsVideo(insertDTO.getGoodsVideo());
        goodsPlatform.setBarCode(defaultProduct.getBarCode());
        goodsPlatform.setDefaultProductId(defaultProductId);
        goodsPlatform.setIsVirtualGoods(insertDTO.getIsVirtualGoods());
        goodsPlatform.setAfterSaleService(insertDTO.getAfterSaleService());
        goodsPlatform.setCreateAdminId(adminId);
        goodsPlatform.setCreateTime(new Date());
        goodsPlatform.setUpdateTime(new Date());
        goodsPlatform.setGoodsDetails(insertDTO.getGoodsDetails());
        if (!CollectionUtils.isEmpty(insertDTO.getSpecInfoList())) {
            goodsPlatform.setSpecJson(JSON.toJSONString(insertDTO.getSpecInfoList()));
        }
        if (insertDTO.getAttributeAndParameter() != null) {
            if (!CollectionUtils.isEmpty(insertDTO.getAttributeAndParameter().getAttributeList())) {
                //检索属性
                goodsPlatform.setAttributeJson(JSON.toJSONString(insertDTO.getAttributeAndParameter().getAttributeList()));
            }
            if (insertDTO.getAttributeAndParameter().getParameterGroup() != null) {
                //自定义参数
                goodsPlatform.setGoodsParameter(JSON.toJSONString(insertDTO.getAttributeAndParameter().getParameterGroup()));
            }
        }
        this.saveGoodsPlatform(goodsPlatform);
        return goodsPlatform;
    }

    /**
     * 发布商品-插入货品信息
     *
     * @param productList   货品列表
     * @param goodsPlatform 商品信息
     */
    private void insertProductPlatform(List<GoodsPlatformDTO.ProductPlatformInfo> productList, GoodsPlatform goodsPlatform) {
        productList.forEach(productInfo -> {
            GoodsPlatformInsertDTO.ProductPlatformInsertInfo productInsertInfo = (GoodsPlatformInsertDTO.ProductPlatformInsertInfo) productInfo;
            this.doInsertProductPlatform(goodsPlatform, productInsertInfo);
        });
    }

    /**
     * 执行插入货品操作
     *
     * @param goodsPlatform
     * @param productInsertInfo
     */
    private void doInsertProductPlatform(GoodsPlatform goodsPlatform, GoodsPlatformInsertDTO.ProductPlatformInsertInfo productInsertInfo) {
        ProductPlatform productPlatform = new ProductPlatform();
        if (productInsertInfo.getIsDefault() == GoodsConst.PRODUCT_IS_DEFAULT_YES) {
            //默认货品
            productPlatform.setPlatformProductId(goodsPlatform.getDefaultProductId());
        } else {
            //非默认货品，生成平台库货品id
            productPlatform.setPlatformProductId(GoodsIdGenerator.productIdGenerator());
        }
        productPlatform.setPlatformGoodsId(goodsPlatform.getPlatformGoodsId());
        productPlatform.setGoodsName(goodsPlatform.getGoodsName());
        productPlatform.setSpecValues(productInsertInfo.getSpecValues());
        productPlatform.setSpecValueIds(productInsertInfo.getSpecValueIds());
        productPlatform.setBrandId(goodsPlatform.getBrandId());
        productPlatform.setBrandName(goodsPlatform.getBrandName());
        productPlatform.setStoreId(goodsPlatform.getStoreId());
        productPlatform.setCategoryId1(goodsPlatform.getCategoryId1());
        productPlatform.setCategoryId2(goodsPlatform.getCategoryId2());
        productPlatform.setCategoryId3(goodsPlatform.getCategoryId3());
        productPlatform.setCategoryPath(goodsPlatform.getCategoryPath());
        productPlatform.setProductPrice(productInsertInfo.getProductPrice());
        productPlatform.setMarketPrice(productInsertInfo.getMarketPrice());
        productPlatform.setBarCode(productInsertInfo.getBarCode());
        productPlatform.setWeight(productInsertInfo.getWeight());
        productPlatform.setLength(productInsertInfo.getLength());
        productPlatform.setWidth(productInsertInfo.getWidth());
        productPlatform.setHeight(productInsertInfo.getHeight());
        productPlatform.setMainImage(productInsertInfo.getMainImage());
        productPlatform.setState(productInsertInfo.getState());
        productPlatform.setIsDefault(productInsertInfo.getIsDefault());
        int count = productPlatformWriteMapper.insert(productPlatform);
        AssertUtil.isTrue(count == 0, "添加平台库货品表失败，请重试");
    }

    /**
     * 发布商品-保存商品属性绑定表（检索属性）
     *
     * @param attributeAndParameter
     * @param platformGoodsId
     */
    private void insertGoodsBindAttribute(GoodsPublishFrontParamDTO.AttributeAndParameter attributeAndParameter, Long platformGoodsId) {
        if (attributeAndParameter == null) {
            return;
        }
        if (CollectionUtils.isEmpty(attributeAndParameter.getAttributeList())) {
            return;
        }
        attributeAndParameter.getAttributeList().forEach(attribute -> {
            GoodsPlatformBindAttributeValue goodsBindAttributeValue = new GoodsPlatformBindAttributeValue();
            goodsBindAttributeValue.setPlatformGoodsId(platformGoodsId);
            goodsBindAttributeValue.setAttributeId(attribute.getAttributeId());
            goodsBindAttributeValue.setAttributeName(attribute.getAttributeName());
            goodsBindAttributeValue.setAttributeValueId(attribute.getAttributeValueId());
            goodsBindAttributeValue.setAttributeValue(attribute.getAttributeValue());
            int count = goodsPlatformBindAttributeValueWriteMapper.insert(goodsBindAttributeValue);
            AssertUtil.isTrue(count == 0, "添加平台库商品属性表失败，请重试");
        });
    }

    /**
     * 发布商品-保存商品图片
     *
     * @param specInfoList  规格信息
     * @param imageInfoList 图片信息
     * @param goodsPlatform 平台库商品信息
     * @param adminId       管理员id
     */
    private void insertGoodsPicture(List<GoodsPublishFrontParamDTO.SpecInfo> specInfoList, List<GoodsPublishFrontParamDTO.ImageInfo> imageInfoList,
                                    GoodsPlatform goodsPlatform, Integer adminId) {
        if (StringUtil.isNullOrZero(goodsPlatform.getMainSpecId())) {
            //没有主规格
            imageInfoList.forEach(imageInfo -> {
                this.insertPicture(imageInfo, goodsPlatform.getPlatformGoodsId(), adminId, 0);
            });
        } else {
            //有主规格
            specInfoList.forEach(specInfo -> {
                if (specInfo.getIsMainSpec() == GoodsConst.IS_MAIN_SPEC_YES) {
                    specInfo.getSpecValueList().forEach(specValueInfo -> {
                        specValueInfo.getImageList().forEach(imageInfo -> {
                            this.insertPicture(imageInfo, goodsPlatform.getPlatformGoodsId(), adminId, specValueInfo.getSpecValueId());
                        });
                    });
                }
            });
        }
    }

    /**
     * 保存图片
     *
     * @param imageInfo       图片信息
     * @param platformGoodsId 平台库商品id
     * @param adminId         管理员id
     * @param specValueId     规格值id
     */
    private void insertPicture(GoodsPublishFrontParamDTO.ImageInfo imageInfo, Long platformGoodsId, Integer adminId, Integer specValueId) {
        GoodsPlatformPicture goodsPlatformPicture = new GoodsPlatformPicture();
        goodsPlatformPicture.setPlatformGoodsId(platformGoodsId);
        goodsPlatformPicture.setImagePath(imageInfo.getImage());
        goodsPlatformPicture.setIsMain(imageInfo.getIsMain());
        goodsPlatformPicture.setSpecValueId(specValueId);
        goodsPlatformPicture.setCreateAdminId(adminId);
        goodsPlatformPicture.setCreateTime(new Date());
        int count = goodsPlatformPictureWriteMapper.insert(goodsPlatformPicture);
        AssertUtil.isTrue(count == 0, "添加平台库商品图片表失败，请重试");
    }

    /**
     * 发布商品-保存虚拟商品用户预留信息名称表
     *
     * @param reserveInfoList 用户预留信息名称列表
     * @param platformGoodsId 平台库商品id
     * @param isVirtualGoods  是否是虚拟商品：1-实物商品；2-虚拟商品
     */
    private void insertGoodsBindReserveName(List<GoodsPublishFrontParamDTO.GoodsReserveNameInfo> reserveInfoList,
                                            Long platformGoodsId, Integer isVirtualGoods) {
        if (isVirtualGoods == GoodsConst.IS_VIRTUAL_GOODS_1 || CollectionUtils.isEmpty(reserveInfoList)) {
            return;
        }
        reserveInfoList.forEach(reserveNameInfo -> {
            GoodsPlatformBindReserveName goodsBindReserveName = new GoodsPlatformBindReserveName();
            goodsBindReserveName.setReserveName(reserveNameInfo.getReserveName());
            goodsBindReserveName.setReserveType(reserveNameInfo.getReserveType());
            goodsBindReserveName.setIsRequired(reserveNameInfo.getIsRequired());
            goodsBindReserveName.setPlatformGoodsId(platformGoodsId);
            int count = goodsPlatformBindReserveNameWriteMapper.insert(goodsBindReserveName);
            AssertUtil.isTrue(count == 0, "添加平台库虚拟商品用户预留信息名称表失败，请重试");
        });
    }

    //endregion

    //region 编辑商品

    /**
     * 编辑商品
     * -对比货品列表信息，构造要增、删、改的货品列表
     * -更新商品信息
     * -更新货品信息
     * -保存新的商品属性绑定表（检索属性）
     * -保存新的商品图片表
     * -保存新的虚拟商品用户预留信息
     *
     * @param insertDTO
     * @param goodsPlatformDb
     * @param adminId
     */
    @Transactional(rollbackFor = Exception.class)
    public void editGoods(GoodsPlatformInsertDTO insertDTO, GoodsPlatform goodsPlatformDb, Integer adminId) {
        //前端传来的货品列表
        List<GoodsPlatformDTO.ProductPlatformInfo> productList = insertDTO.getProductList();
        //要新增的货品
        List<GoodsPlatformInsertDTO.ProductPlatformInsertInfo> addProductList = new ArrayList<>();
        //要删除的货品
        List<GoodsPlatformInsertDTO.ProductPlatformInsertInfo> updateProductList = new ArrayList<>();
        //要删除的货品（伪删除）
        List<ProductPlatform> deleteProductList = new ArrayList<>();

        //-对比货品列表信息，构造要增、删、改的货品列表
        this.compareSpecWithDb(goodsPlatformDb.getPlatformGoodsId(), productList, addProductList, updateProductList, deleteProductList);

        //-更新商品信息
        GoodsPlatform updateGoods = this.updateGoodsPlatform(insertDTO);

        //-更新货品信息
        this.updateProductPlatform(updateGoods, addProductList, updateProductList, deleteProductList);

        //-保存新的商品属性绑定表（检索属性）
        this.updateGoodsBindAttribute(insertDTO.getAttributeAndParameter(), updateGoods.getPlatformGoodsId());

        //-保存新的商品图片表
        this.updateGoodsPicture(insertDTO.getSpecInfoList(), insertDTO.getImageList(), updateGoods, adminId);

        //-保存虚拟商品用户预留信息名称表
        this.updateGoodsBindReserveName(insertDTO.getReserveInfoList(), updateGoods.getPlatformGoodsId(), insertDTO.getIsVirtualGoods());
    }

    /**
     * 编辑后的货品列表与数据库中的货品列表做规格比对，构造新增、编辑、删除操作的货品列表
     *
     * @param platformGoodsId   平台库商品id
     * @param paramProductList  前端传来的货品列表
     * @param addProductList    最终要新增的货品
     * @param updateProductList 最终要编辑的货品
     * @param deleteProductList 最终要伪删除的货品
     */
    private void compareSpecWithDb(Long platformGoodsId, List<GoodsPlatformDTO.ProductPlatformInfo> paramProductList,
                                   List<GoodsPlatformInsertDTO.ProductPlatformInsertInfo> addProductList,
                                   List<GoodsPlatformInsertDTO.ProductPlatformInsertInfo> updateProductList,
                                   List<ProductPlatform> deleteProductList) {
        //数据库中的货品列表
        ProductPlatformExample example = new ProductPlatformExample();
        example.setPlatformGoodsId(platformGoodsId);
        List<ProductPlatform> dbProductList = productPlatformReadMapper.listByExample(example);
        AssertUtil.notEmpty(dbProductList, "商品信息有误");

        //构造新增和更新的货品
        paramProductList.forEach(paramProduct -> {
            GoodsPlatformInsertDTO.ProductPlatformInsertInfo paramInsertProduct = (GoodsPlatformInsertDTO.ProductPlatformInsertInfo) paramProduct;
            boolean compared = false;//数据库中是否有此货品
            for (ProductPlatform dbProduct : dbProductList) {
                if (compareSpec(paramInsertProduct, dbProduct)) {
                    //匹配成功，更新此货品
                    paramInsertProduct.setPlatformProductId(dbProduct.getPlatformProductId());
                    updateProductList.add(paramInsertProduct);
                    compared = true;
                    break;
                }
            }
            if (!compared) {
                //未匹配成功，添加此货品
                addProductList.add(paramInsertProduct);
            }
        });

        //构造要删除的列表
        dbProductList.forEach(dbProduct -> {
            boolean delete = true;//是否要删除
            for (GoodsPlatformInsertDTO.ProductPlatformInsertInfo updateProduct : updateProductList) {
                if (dbProduct.getPlatformProductId().equals(updateProduct.getPlatformProductId())) {
                    //匹配成功，不需要删除
                    delete = false;
                    break;
                }
            }
            if (delete) {
                ProductPlatform deleteProduct = new ProductPlatform();
                deleteProduct.setPlatformProductId(dbProduct.getPlatformProductId());
                //删除状态
                deleteProduct.setState(GoodsConst.PRODUCT_STATE_0);
                //改为非默认
                deleteProduct.setIsDefault(GoodsConst.PRODUCT_IS_DEFAULT_NO);
                deleteProductList.add(deleteProduct);
            }
        });
    }

    /**
     * 前端货品与数据或货品比对
     *
     * @param paramProduct
     * @param dbProduct
     * @return
     */
    private boolean compareSpec(GoodsPlatformInsertDTO.ProductPlatformInsertInfo paramProduct, ProductPlatform dbProduct) {
        String s1 = paramProduct.getSpecValueIds();
        String s2 = dbProduct.getSpecValueIds();
        //数组元素的格式为  规格id:规格值id
        String[] arr1 = s1 == null ? "".split(",") : s1.split(",");
        String[] arr2 = s2 == null ? "".split(",") : s2.split(",");
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        return Arrays.equals(arr1, arr2);
    }

    /**
     * 编辑商品-更新商品信息
     *
     * @param insertDTO 商品信息
     * @return
     */
    private GoodsPlatform updateGoodsPlatform(GoodsPlatformInsertDTO insertDTO) {
        GoodsPlatformInsertDTO.ProductPlatformInsertInfo defaultProduct = insertDTO.getDefaultProduct();
        //构造默认货品id
        long defaultProductId = StringUtil.isNullOrZero(defaultProduct.getPlatformProductId())
                ? GoodsIdGenerator.productIdGenerator() : defaultProduct.getPlatformProductId();
        //查询分类信息
        GoodsCategory goodsCategory3 = goodsCategoryReadMapper.getByPrimaryKey(insertDTO.getCategoryId3());
        AssertUtil.notNull(goodsCategory3, "分类有误");
        GoodsCategory goodsCategory2 = goodsCategoryReadMapper.getByPrimaryKey(goodsCategory3.getPid());
        GoodsCategory goodsCategory1 = goodsCategoryReadMapper.getByPrimaryKey(goodsCategory2.getPid());

        GoodsPlatform goodsPlatform = new GoodsPlatform();
        goodsPlatform.setPlatformGoodsId(insertDTO.getPlatformGoodsId());
        goodsPlatform.setGoodsName(insertDTO.getGoodsName());
        //为传商品广告时，要清除原数据
        goodsPlatform.setGoodsBrief(StringUtils.isEmpty(insertDTO.getGoodsBrief()) ? "" : insertDTO.getGoodsBrief());
        if (!StringUtil.isNullOrZero(insertDTO.getBrandId())) {
            //查询品牌信息
            GoodsBrand goodsBrand = goodsBrandReadMapper.getByPrimaryKey(insertDTO.getBrandId());
            AssertUtil.notNull(goodsBrand, "品牌有误");
            goodsPlatform.setBrandId(insertDTO.getBrandId());
            goodsPlatform.setBrandName(goodsBrand.getBrandName());
        } else {
            //未选择品牌，要把之前的品牌信息清除
            goodsPlatform.setBrandId(0);
            goodsPlatform.setBrandName("");
        }
        goodsPlatform.setCategoryId1(goodsCategory1.getCategoryId());
        goodsPlatform.setCategoryId2(goodsCategory2.getCategoryId());
        goodsPlatform.setCategoryId3(goodsCategory3.getCategoryId());
        goodsPlatform.setCategoryPath(goodsCategory1.getCategoryName() + "->" + goodsCategory2.getCategoryName() + "->" + goodsCategory3.getCategoryName());
        goodsPlatform.setMarketPrice(StringUtil.isNullOrZero(defaultProduct.getMarketPrice()) ? new BigDecimal("0.00") : defaultProduct.getMarketPrice());
        goodsPlatform.setGoodsPrice(defaultProduct.getProductPrice());
        goodsPlatform.setIsSpec(CollectionUtils.isEmpty(insertDTO.getSpecInfoList()) ? GoodsConst.IS_SPEC_NO : GoodsConst.IS_SPEC_YES);
        goodsPlatform.setMainSpecId(insertDTO.getMainSpecId());
        goodsPlatform.setState(insertDTO.getState());
        goodsPlatform.setStoreId(0L);
        goodsPlatform.setMainImage(defaultProduct.getMainImage());
        goodsPlatform.setGoodsVideo(StringUtils.isEmpty(insertDTO.getGoodsVideo()) ? "" : insertDTO.getGoodsVideo());
        goodsPlatform.setBarCode(StringUtils.isEmpty(defaultProduct.getBarCode()) ? "" : defaultProduct.getBarCode());
        goodsPlatform.setDefaultProductId(defaultProductId);
        goodsPlatform.setAfterSaleService(insertDTO.getAfterSaleService());
        goodsPlatform.setUpdateTime(new Date());
        goodsPlatform.setGoodsDetails(insertDTO.getGoodsDetails());
        if (!CollectionUtils.isEmpty(insertDTO.getSpecInfoList())) {
            goodsPlatform.setSpecJson(JSON.toJSONString(insertDTO.getSpecInfoList()));
        } else {
            goodsPlatform.setSpecJson("");
        }
        if (insertDTO.getAttributeAndParameter() != null) {
            if (!CollectionUtils.isEmpty(insertDTO.getAttributeAndParameter().getAttributeList())) {
                //检索属性
                goodsPlatform.setAttributeJson(JSON.toJSONString(insertDTO.getAttributeAndParameter().getAttributeList()));
            } else {
                //未选择属性时，清除原有属性信息
                goodsPlatform.setAttributeJson("");
            }
            if (insertDTO.getAttributeAndParameter().getParameterGroup() != null) {
                //自定义参数
                goodsPlatform.setGoodsParameter(JSON.toJSONString(insertDTO.getAttributeAndParameter().getParameterGroup()));
            } else {
                goodsPlatform.setGoodsParameter("");
            }
        } else {
            goodsPlatform.setAttributeJson("");
            goodsPlatform.setGoodsParameter("");
        }
        this.updateGoodsPlatform(goodsPlatform);
        return goodsPlatform;
    }

    /**
     * 编辑商品-更新货品信息
     *
     * @param updateGoods       平台库商品信息
     * @param addProductList    新增货品信息
     * @param updateProductList 编辑货品信息
     * @param deleteProductList 删除货品信息
     */
    private void updateProductPlatform(GoodsPlatform updateGoods, List<GoodsPlatformInsertDTO.ProductPlatformInsertInfo> addProductList,
                                       List<GoodsPlatformInsertDTO.ProductPlatformInsertInfo> updateProductList,
                                       List<ProductPlatform> deleteProductList) {
        //新增
        this.updateProductAdd(updateGoods, addProductList);
        //编辑
        this.updateProductUpdate(updateGoods, updateProductList);
        //删除
        this.updateProductDelete(deleteProductList);
    }

    /**
     * 编辑商品-更新货品信息-新增货品
     *
     * @param updateGoods
     * @param addProductList
     */
    private void updateProductAdd(GoodsPlatform updateGoods, List<GoodsPlatformInsertDTO.ProductPlatformInsertInfo> addProductList) {
        if (CollectionUtils.isEmpty(addProductList)) {
            return;
        }
        addProductList.forEach(productInsertInfo -> {
            this.doInsertProductPlatform(updateGoods, productInsertInfo);
        });
    }

    /**
     * 编辑商品-更新货品信息-更新货品
     *
     * @param updateGoods
     * @param updateProductList
     */
    private void updateProductUpdate(GoodsPlatform updateGoods, List<GoodsPlatformInsertDTO.ProductPlatformInsertInfo> updateProductList) {
        if (CollectionUtils.isEmpty(updateProductList)) {
            return;
        }
        updateProductList.forEach(productInsertInfo -> {
            ProductPlatform productPlatform = new ProductPlatform();
            productPlatform.setPlatformProductId(productInsertInfo.getPlatformProductId());
            productPlatform.setGoodsName(updateGoods.getGoodsName());
            productPlatform.setSpecValues(productInsertInfo.getSpecValues());
            productPlatform.setSpecValueIds(productInsertInfo.getSpecValueIds());
            productPlatform.setBrandId(StringUtil.isNullOrZero(updateGoods.getBrandId()) ? 0 : updateGoods.getBrandId());
            productPlatform.setBrandName(StringUtil.isNullOrZero(updateGoods.getBrandId()) ? "" : updateGoods.getBrandName());
            productPlatform.setCategoryId1(updateGoods.getCategoryId1());
            productPlatform.setCategoryId2(updateGoods.getCategoryId2());
            productPlatform.setCategoryId3(updateGoods.getCategoryId3());
            productPlatform.setCategoryPath(updateGoods.getCategoryPath());
            productPlatform.setProductPrice(productInsertInfo.getProductPrice());
            productPlatform.setMarketPrice(StringUtil.isNullOrZero(productInsertInfo.getMarketPrice()) ? new BigDecimal("0.00") : productInsertInfo.getMarketPrice());
            productPlatform.setBarCode(StringUtils.isEmpty(productInsertInfo.getBarCode()) ? "" : productInsertInfo.getBarCode());
            productPlatform.setWeight(StringUtil.isNullOrZero(productInsertInfo.getWeight()) ? BigDecimal.ZERO : productInsertInfo.getWeight());
            productPlatform.setLength(StringUtil.isNullOrZero(productInsertInfo.getLength()) ? BigDecimal.ZERO : productInsertInfo.getLength());
            productPlatform.setWidth(StringUtil.isNullOrZero(productInsertInfo.getWidth()) ? BigDecimal.ZERO : productInsertInfo.getWidth());
            productPlatform.setHeight(StringUtil.isNullOrZero(productInsertInfo.getHeight()) ? BigDecimal.ZERO : productInsertInfo.getHeight());
            productPlatform.setMainImage(productInsertInfo.getMainImage());
            productPlatform.setState(productInsertInfo.getState());
            productPlatform.setIsDefault(productInsertInfo.getIsDefault());
            int count = productPlatformWriteMapper.updateByPrimaryKeySelective(productPlatform);
            AssertUtil.isTrue(count == 0, "更新平台库货品表失败,请重试");
        });
    }

    /**
     * 编辑商品-更新货品信息-删除货品
     *
     * @param deleteProductList
     */
    private void updateProductDelete(List<ProductPlatform> deleteProductList) {
        if (CollectionUtils.isEmpty(deleteProductList)){
            return;
        }
        deleteProductList.forEach(product -> {
            int count = productPlatformWriteMapper.updateByPrimaryKeySelective(product);
            AssertUtil.isTrue(count == 0, "更新平台库货品表失败,请重试");
        });
    }

    /**
     * 编辑商品-更新商品属性绑定表
     *
     * @param attributeAndParameter
     * @param platformGoodsId
     */
    private void updateGoodsBindAttribute(GoodsPublishFrontParamDTO.AttributeAndParameter attributeAndParameter, Long platformGoodsId) {
        //删除原有的绑定关系
        GoodsPlatformBindAttributeValueExample example = new GoodsPlatformBindAttributeValueExample();
        example.setPlatformGoodsId(platformGoodsId);
        goodsPlatformBindAttributeValueWriteMapper.deleteByExample(example);
        //新增绑定关系
        this.insertGoodsBindAttribute(attributeAndParameter, platformGoodsId);
    }

    /**
     * 编辑商品-更新商品图片
     *
     * @param specInfoList  规格列表
     * @param imageInfoList 图片列表
     * @param goodsPlatform 平台库商品信息
     * @param adminId       管理员id
     */
    private void updateGoodsPicture(List<GoodsPublishFrontParamDTO.SpecInfo> specInfoList, List<GoodsPublishFrontParamDTO.ImageInfo> imageInfoList,
                                    GoodsPlatform goodsPlatform, Integer adminId) {
        //删除原有图片
        GoodsPlatformPictureExample example = new GoodsPlatformPictureExample();
        example.setPlatformGoodsId(goodsPlatform.getPlatformGoodsId());
        goodsPlatformPictureWriteMapper.deleteByExample(example);
        //新增图片
        this.insertGoodsPicture(specInfoList, imageInfoList, goodsPlatform, adminId);
    }

    /**
     * 编辑商品-更新虚拟商品用户预留信息名称
     *
     * @param reserveInfoList 用户预留信息名称列表
     * @param platformGoodsId 平台库商品id
     * @param isVirtualGoods  是否是虚拟商品：1-实物商品；2-虚拟商品
     */
    private void updateGoodsBindReserveName(List<GoodsPublishFrontParamDTO.GoodsReserveNameInfo> reserveInfoList,
                                            Long platformGoodsId, Integer isVirtualGoods) {
        //删除原有绑定关系
        GoodsPlatformBindReserveNameExample example = new GoodsPlatformBindReserveNameExample();
        example.setPlatformGoodsId(platformGoodsId);
        goodsPlatformBindReserveNameWriteMapper.deleteByExample(example);

        //新增绑定关系
        this.insertGoodsBindReserveName(reserveInfoList, platformGoodsId, isVirtualGoods);
    }

    //endregion

    /**
     * 新增平台库商品表
     *
     * @param goodsPlatform
     * @return
     */
    public Integer saveGoodsPlatform(GoodsPlatform goodsPlatform) {
        int count = goodsPlatformWriteMapper.insert(goodsPlatform);
        if (count == 0) {
            throw new MallException("添加平台库商品表失败，请重试");
        }
        return count;
    }

    /**
     * 根据platformGoodsId删除平台库商品表
     *
     * @param platformGoodsId platformGoodsId
     * @return
     */
    public Integer deleteGoodsPlatform(Long platformGoodsId) {
        if (StringUtils.isEmpty(platformGoodsId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = goodsPlatformWriteMapper.deleteByPrimaryKey(platformGoodsId);
        if (count == 0) {
            log.error("根据platformGoodsId：" + platformGoodsId + "删除平台库商品表失败");
            throw new MallException("删除平台库商品表失败,请重试");
        }
        return count;
    }

    /**
     * 根据platformGoodsId更新平台库商品表
     *
     * @param goodsPlatform
     * @return
     */
    public Integer updateGoodsPlatform(GoodsPlatform goodsPlatform) {
        if (StringUtils.isEmpty(goodsPlatform.getPlatformGoodsId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = goodsPlatformWriteMapper.updateByPrimaryKeySelective(goodsPlatform);
        if (count == 0) {
            log.error("根据platformGoodsId：" + goodsPlatform.getPlatformGoodsId() + "更新平台库商品表失败");
            throw new MallException("更新平台库商品表失败,请重试");
        }
        return count;
    }

    /**
     * 根据platformGoodsId更新平台库商品表
     *
     * @param goodsPlatform 修改商品信息
     * @param example       查询条件信息
     * @return
     */
    public Integer updateGoodsPlatformByExample(GoodsPlatform goodsPlatform, GoodsPlatformExample example) {
        return goodsPlatformWriteMapper.updateByExampleSelective(goodsPlatform, example);
    }

    /**
     * 根据platformGoodsId获取平台库商品表详情
     *
     * @param platformGoodsId platformGoodsId
     * @return
     */
    public GoodsPlatform getGoodsPlatformByPlatformGoodsId(Long platformGoodsId) {
        return goodsPlatformReadMapper.getByPrimaryKey(platformGoodsId);
    }

    /**
     * 根据条件获取平台库商品表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<GoodsPlatform> getGoodsPlatformList(GoodsPlatformExample example, PagerInfo pager) {
        List<GoodsPlatform> goodsPlatformList;
        if (pager != null) {
            pager.setRowsCount(goodsPlatformReadMapper.countByExample(example));
            goodsPlatformList = goodsPlatformReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            goodsPlatformList = goodsPlatformReadMapper.listByExample(example);
        }
        return goodsPlatformList;
    }

    /**
     * 店铺商品导入
     *
     * @param goodsIds 商品id集合，用英文逗号隔开
     * @param admin    操作人信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void storeGoodsImport(String goodsIds, Admin admin) {
        String[] goodsIdArray = goodsIds.split(",");
        for (String goodsId : goodsIdArray) {
            Goods goods = goodsReadMapper.getByPrimaryKey(goodsId);
            AssertUtil.notNull(goods, "商品不存在");
            //-校验商品是否重复
            this.checkGoods(goods);

            //-导入商品信息
            GoodsPlatform goodsPlatform = this.importStoreGoods(goods, admin);

            //-导入货品信息
            this.importStoreProduct(goodsPlatform.getPlatformGoodsId(), goods.getGoodsId());

            //-导入商品属性绑定表（检索属性）
            this.importGoodsBindAttribute(goodsPlatform.getPlatformGoodsId(), goods.getGoodsId());

            //-导入商品图片表
            this.importGoodsPicture(goodsPlatform.getPlatformGoodsId(), goods.getGoodsId(), admin.getAdminId());

            //-导入虚拟商品用户预留信息名称表
            this.importGoodsBindReserveName(goodsPlatform.getPlatformGoodsId(), goods.getGoodsId(), goods.getIsVirtualGoods());
        }
    }

    /**
     * 校验商品是否重复
     *
     * @param goods 商品信息
     */
    private void checkGoods(Goods goods) {
        GoodsPlatformExample example = new GoodsPlatformExample();
        example.setGoodsId(goods.getGoodsId());
        example.setBarCode(goods.getBarCode());
        example.setStateNotEquals(GoodsPlatformConst.GOODS_STATE_4);
        int count = goodsPlatformReadMapper.countByExample(example);
        AssertUtil.isTrue(count > 0, "商品资料库商品[" + goods.getGoodsName() + "]已采集过，请重新选择");
    }

    /**
     * 导入店铺商品
     *
     * @param goods 商品信息
     * @param admin 管理员信息
     * @return
     */
    private GoodsPlatform importStoreGoods(Goods goods, Admin admin) {
        //平台库商品id
        long platformGoodsId = GoodsIdGenerator.goodsIdGenerator();

        GoodsPlatform goodsPlatform = new GoodsPlatform();
        goodsPlatform.setPlatformGoodsId(platformGoodsId);
        goodsPlatform.setGoodsId(goods.getGoodsId());
        goodsPlatform.setGoodsName(goods.getGoodsName());
        goodsPlatform.setGoodsBrief(goods.getGoodsBrief());
        goodsPlatform.setMarketPrice(goods.getMarketPrice());
        goodsPlatform.setGoodsPrice(goods.getGoodsPrice());
        goodsPlatform.setBrandId(goods.getBrandId());
        goodsPlatform.setBrandName(goods.getBrandName());
        goodsPlatform.setCategoryId1(goods.getCategoryId1());
        goodsPlatform.setCategoryId2(goods.getCategoryId2());
        goodsPlatform.setCategoryId3(goods.getCategoryId3());
        goodsPlatform.setCategoryPath(goods.getCategoryPath());
        goodsPlatform.setIsSpec(goods.getIsSpec());
        goodsPlatform.setMainSpecId(goods.getMainSpecId());
        goodsPlatform.setStoreId(goods.getStoreId());
        goodsPlatform.setState(GoodsPlatformConst.GOODS_STATE_1);
        goodsPlatform.setIsVirtualGoods(goods.getIsVirtualGoods());
        goodsPlatform.setMainImage(goods.getMainImage());
        goodsPlatform.setGoodsVideo(goods.getGoodsVideo());
        goodsPlatform.setBarCode(goods.getBarCode());
        goodsPlatform.setDefaultProductId(goods.getDefaultProductId());
        goodsPlatform.setAfterSaleService(goods.getAfterSaleService());
        goodsPlatform.setCreateAdminId(admin.getAdminId());
        goodsPlatform.setCreateTime(new Date());
        goodsPlatform.setUpdateTime(new Date());

        //查询商品扩展表信息
        GoodsExtendExample example = new GoodsExtendExample();
        example.setGoodsId(goods.getGoodsId());
        List<GoodsExtend> list = goodsExtendReadMapper.listByExample(example);
        AssertUtil.notEmpty(list, "商品不存在，请重试！");
        GoodsExtend goodsExtend = list.get(0);
        goodsPlatform.setGoodsDetails(goodsExtend.getGoodsDetails());
        goodsPlatform.setGoodsParameter(goodsExtend.getGoodsParameter());
        goodsPlatform.setSpecJson(this.dealSpecJson(goodsExtend.getSpecJson(), admin));
        goodsPlatform.setAttributeJson(goodsExtend.getAttributeJson());

        this.saveGoodsPlatform(goodsPlatform);
        return goodsPlatform;
    }

    /**
     * 处理规格信息
     *
     * @param specJson 规格json信息
     * @param admin    管理员信息
     * @return
     */
    public String dealSpecJson(String specJson, Admin admin) {
        if (StringUtil.isEmpty(specJson)) {
            return null;
        }
        //原始数据
        List<GoodsPublishFrontParamDTO.SpecInfo> dbSpecInfoList = JSON.parseArray(specJson, GoodsPublishFrontParamDTO.SpecInfo.class);
        //重新组装规格信息
        List<GoodsPublishFrontParamDTO.SpecInfo> specInfoList = new ArrayList<>();
        for (GoodsPublishFrontParamDTO.SpecInfo specInfo : dbSpecInfoList) {
            //平台是否有此规格,没有则插入，返回规格id
            Integer specId = this.importGoodsSpec(specInfo.getSpecName(), admin);
            GoodsPublishFrontParamDTO.SpecInfo info = new GoodsPublishFrontParamDTO.SpecInfo();
            info.setSpecId(specId);
            info.setSpecName(specInfo.getSpecName());
            info.setIsMainSpec(specInfo.getIsMainSpec());
            List<GoodsPublishFrontParamDTO.SpecInfo.SpecValueInfo> specValueList = new ArrayList<>();
            for (GoodsPublishFrontParamDTO.SpecInfo.SpecValueInfo specValueInfo : specInfo.getSpecValueList()) {
                //平台是否有此规格值,没有则插入，返回规格值id
                Integer specValueId = this.importGoodsSpecValue(specId, specValueInfo.getSpecValue(), admin.getAdminId().longValue());
                GoodsPublishFrontParamDTO.SpecInfo.SpecValueInfo valueInfo = new GoodsPublishFrontParamDTO.SpecInfo.SpecValueInfo();
                valueInfo.setSpecValueId(specValueId);
                valueInfo.setSpecValue(specValueInfo.getSpecValue());
                valueInfo.setImageList(specValueInfo.getImageList());
                specValueList.add(valueInfo);
            }
            info.setSpecValueList(specValueList);
            specInfoList.add(info);
        }
        return JSON.toJSONString(specInfoList);
    }

    /**
     * 导入规格信息
     *
     * @param specName 规格名称
     * @param admin    管理员信息
     * @return
     */
    public Integer importGoodsSpec(String specName, Admin admin) {
        //判断属性名称是否重复
        GoodsSpecExample example = new GoodsSpecExample();
        example.setStoreId(0L);
        example.setSpecName(specName);
        List<GoodsSpec> list = goodsSpecReadMapper.listByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0).getSpecId();
        } else {
            //插入规格
            GoodsSpec goodsSpec = new GoodsSpec();
            goodsSpec.setSpecName(specName);
            goodsSpec.setSpecType(GoodsConst.SPEC_TYPE_1);
            goodsSpec.setStoreId(0L);
            goodsSpec.setCreateId(admin.getAdminId().longValue());
            goodsSpec.setCreateName(admin.getAdminName());
            goodsSpec.setCreateTime(new Date());
            goodsSpec.setUpdateId(admin.getAdminId().longValue());
            goodsSpec.setUpdateName(admin.getAdminName());
            goodsSpec.setUpdateTime(new Date());
            goodsSpec.setState(GoodsConst.IS_SPEC_YES);
            goodsSpec.setSort(1);
            int count = goodsSpecWriteMapper.insert(goodsSpec);
            AssertUtil.isTrue(count == 0, "添加规格失败，请重试");
            return goodsSpec.getSpecId();
        }
    }

    /**
     * 导入规格值信息
     *
     * @param specId    规格id
     * @param specValue 规格值
     * @param creatId   创建人id
     * @return
     */
    public Integer importGoodsSpecValue(Integer specId, String specValue, Long creatId) {
        //判断规则值是否重复
        GoodsSpecValueExample example = new GoodsSpecValueExample();
        example.setSpecId(specId);
        example.setSpecValue(specValue);
        List<GoodsSpecValue> list = goodsSpecValueReadMapper.listByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0).getSpecValueId();
        } else {
            //插入规格值
            GoodsSpecValue goodsSpecValue = new GoodsSpecValue();
            goodsSpecValue.setSpecValue(specValue);
            goodsSpecValue.setSpecId(specId);
            goodsSpecValue.setStoreId(0L);
            goodsSpecValue.setCreateId(creatId);
            goodsSpecValue.setCreateTime(new Date());
            int count = goodsSpecValueWriteMapper.insert(goodsSpecValue);
            AssertUtil.isTrue(count == 0, "添加规格值失败，请重试");
            return goodsSpecValue.getSpecValueId();
        }
    }

    /**
     * 导入店铺货品
     *
     * @param platformGoodsId 平台库商品id
     * @param goodsId         店铺商品id
     */
    private void importStoreProduct(Long platformGoodsId, Long goodsId) {
        //查询货品列表
        ProductExample example = new ProductExample();
        example.setGoodsId(goodsId);
        example.setStateNotEquals(GoodsConst.PRODUCT_STATE_0);
        List<Product> list = productReadMapper.listByExample(example);
        AssertUtil.notEmpty(list, "商品不存在");
        list.forEach(product -> {
            ProductPlatform productPlatform = new ProductPlatform();
            productPlatform.setPlatformProductId(GoodsIdGenerator.productIdGenerator());
            productPlatform.setPlatformGoodsId(platformGoodsId);
            productPlatform.setProductId(product.getProductId());
            productPlatform.setGoodsId(product.getGoodsId());
            productPlatform.setGoodsName(product.getGoodsName());
            productPlatform.setSpecValues(product.getSpecValues());
            productPlatform.setSpecValueIds(product.getSpecValueIds());
            productPlatform.setBrandId(product.getBrandId());
            productPlatform.setBrandName(product.getBrandName());
            productPlatform.setStoreId(product.getStoreId());
            productPlatform.setCategoryId1(product.getCategoryId1());
            productPlatform.setCategoryId2(product.getCategoryId2());
            productPlatform.setCategoryId3(product.getCategoryId3());
            productPlatform.setCategoryPath(product.getCategoryPath());
            productPlatform.setProductPrice(product.getProductPrice());
            productPlatform.setMarketPrice(product.getMarketPrice());
            productPlatform.setMainImage(product.getMainImage());
            productPlatform.setState(GoodsConst.PRODUCT_STATE_1);
            productPlatform.setIsDefault(product.getIsDefault());
            productPlatform.setBarCode(product.getBarCode());
            productPlatform.setWeight(product.getWeight());
            productPlatform.setLength(product.getLength());
            productPlatform.setWidth(product.getWidth());
            productPlatform.setHeight(product.getHeight());
            int count = productPlatformWriteMapper.insert(productPlatform);
            AssertUtil.isTrue(count == 0, "添加平台库货品表失败，请重试");
        });
    }

    /**
     * 发布商品-保存商品属性绑定表（检索属性）
     *
     * @param platformGoodsId 平台库商品id
     * @param goodsId         店铺商品id
     */
    private void importGoodsBindAttribute(Long platformGoodsId, Long goodsId) {
        GoodsBindAttributeValueExample example = new GoodsBindAttributeValueExample();
        example.setGoodsId(goodsId);
        List<GoodsBindAttributeValue> list = goodsBindAttributeValueReadMapper.listByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        list.forEach(attribute -> {
            GoodsPlatformBindAttributeValue goodsBindAttributeValue = new GoodsPlatformBindAttributeValue();
            goodsBindAttributeValue.setPlatformGoodsId(platformGoodsId);
            goodsBindAttributeValue.setAttributeId(attribute.getAttributeId());
            goodsBindAttributeValue.setAttributeName(attribute.getAttributeName());
            goodsBindAttributeValue.setAttributeValueId(attribute.getAttributeValueId());
            goodsBindAttributeValue.setAttributeValue(attribute.getAttributeValue());
            int count = goodsPlatformBindAttributeValueWriteMapper.insert(goodsBindAttributeValue);
            AssertUtil.isTrue(count == 0, "添加平台库商品属性表失败，请重试");
        });
    }

    /**
     * 导入商品图片
     *
     * @param platformGoodsId 平台库商品id
     * @param goodsId         店铺商品id
     * @param adminId         操作人id
     */
    private void importGoodsPicture(Long platformGoodsId, Long goodsId, Integer adminId) {
        GoodsPictureExample example = new GoodsPictureExample();
        example.setGoodsId(goodsId);
        example.setOrderBy("picture_id asc");
        List<GoodsPicture> list = goodsPictureReadMapper.listByExample(example);
        AssertUtil.notEmpty(list, "商城的商品图片不存在");
        list.forEach(goodsPicture -> {
            GoodsPlatformPicture goodsPlatformPicture = new GoodsPlatformPicture();
            goodsPlatformPicture.setPlatformGoodsId(platformGoodsId);
            goodsPlatformPicture.setImagePath(goodsPicture.getImagePath());
            goodsPlatformPicture.setIsMain(goodsPicture.getIsMain());
            goodsPlatformPicture.setSpecValueId(goodsPicture.getSpecValueId());
            goodsPlatformPicture.setCreateAdminId(adminId);
            goodsPlatformPicture.setCreateTime(new Date());
            int count = goodsPlatformPictureWriteMapper.insert(goodsPlatformPicture);
            AssertUtil.isTrue(count == 0, "添加平台库商品图片表失败，请重试");
        });
    }

    /**
     * 导入虚拟商品用户预留信息名称
     *
     * @param platformGoodsId 平台库商品id
     * @param goodsId         店铺商品id
     * @param isVirtualGoods  是否是虚拟商品：1-实物商品；2-虚拟商品
     */
    private void importGoodsBindReserveName(Long platformGoodsId, Long goodsId, Integer isVirtualGoods) {
        if (isVirtualGoods == GoodsConst.IS_VIRTUAL_GOODS_1) {
            return;
        }
        GoodsBindReserveNameExample example = new GoodsBindReserveNameExample();
        example.setGoodsId(goodsId);
        example.setOrderBy("reserve_name_id asc");
        List<GoodsBindReserveName> list = goodsBindReserveNameReadMapper.listByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        list.forEach(reserveNameInfo -> {
            GoodsPlatformBindReserveName goodsBindReserveName = new GoodsPlatformBindReserveName();
            goodsBindReserveName.setReserveName(reserveNameInfo.getReserveName());
            goodsBindReserveName.setReserveType(reserveNameInfo.getReserveType());
            goodsBindReserveName.setIsRequired(reserveNameInfo.getIsRequired());
            goodsBindReserveName.setPlatformGoodsId(platformGoodsId);
            int count = goodsPlatformBindReserveNameWriteMapper.insert(goodsBindReserveName);
            AssertUtil.isTrue(count == 0, "添加平台库虚拟商品用户预留信息名称表失败，请重试");
        });
    }
}