package com.fs.store.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.common.BeanCopyUtils;
import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.company.cache.ICompanyCacheService;
import com.fs.statis.dto.ModifyMoreDTO;
import com.fs.store.domain.*;
import com.fs.store.dto.ProductArrtDTO;
import com.fs.store.dto.ProductAttrCountDto;
import com.fs.store.mapper.*;
import com.fs.store.param.FsStoreProductAddEditParam;
import com.fs.store.param.FsStoreProductQueryParam;
import com.fs.store.service.IFsStoreProductAttrValueService;
import com.fs.store.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.store.service.IFsStoreProductService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品Service业务层处理
 *
 * @author fs
 * @date 2022-03-15
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class FsStoreProductServiceImpl implements IFsStoreProductService
{
    @Autowired
    private FsStoreProductMapper fsStoreProductMapper;
    @Autowired
    private FsStoreProductAttrValueMapper fsStoreProductAttrValueMapper;
    @Autowired
    private FsStoreProductAttrMapper fsStoreProductAttrMapper;


    @Autowired
    private IFsStoreProductAttrValueService attrValueService;
    @Autowired
    private FsStoreProductPackageMapper fsStoreProductPackageMapper;
    @Autowired
    private FsStoreProductGroupMapper fsStoreProductGroupMapper;

    @Autowired
    private ICompanyCacheService companyCacheService;
    /**
     * 查询商品
     *
     * @param productId 商品ID
     * @return 商品
     */
    @Override
    public FsStoreProduct selectFsStoreProductById(Long productId)
    {
        return fsStoreProductMapper.selectFsStoreProductById(productId);
    }

    /**
     * 查询商品列表
     *
     * @param fsStoreProduct 商品
     * @return 商品
     */
    @Override
    public List<FsStoreProduct> selectFsStoreProductList(FsStoreProduct fsStoreProduct)
    {
        return fsStoreProductMapper.selectFsStoreProductList(fsStoreProduct);
    }

    /**
     * 新增商品
     *
     * @param fsStoreProduct 商品
     * @return 结果
     */
    @Override
    public int insertFsStoreProduct(FsStoreProduct fsStoreProduct)
    {
        fsStoreProduct.setCreateTime(DateUtils.getNowDate());
        return fsStoreProductMapper.insertFsStoreProduct(fsStoreProduct);
    }

    /**
     * 修改商品
     *
     * @param fsStoreProduct 商品
     * @return 结果
     */
    @Override
    public int updateFsStoreProduct(FsStoreProduct fsStoreProduct)
    {
        fsStoreProduct.setUpdateTime(DateUtils.getNowDate());
        return fsStoreProductMapper.updateFsStoreProduct(fsStoreProduct);
    }

    /**
     * 批量删除商品
     *
     * @param productIds 需要删除的商品ID
     * @return 结果
     */
    @Override
    public int deleteFsStoreProductByIds(Long[] productIds)
    {
        return fsStoreProductMapper.deleteFsStoreProductByIds(productIds);
    }

    /**
     * 删除商品信息
     *
     * @param productId 商品ID
     * @return 结果
     */
    @Override
    public int deleteFsStoreProductById(Long productId)
    {
        return fsStoreProductMapper.deleteFsStoreProductById(productId);
    }

    @Override
    public List<FsStoreProductListVO> selectFsStoreProductListVO(FsStoreProduct fsStoreProduct) {
        List<FsStoreProductListVO> fsStoreProductListVOS = fsStoreProductMapper.selectFsStoreProductListVO(fsStoreProduct);

        for (FsStoreProductListVO item : fsStoreProductListVOS) {
            if(StringUtils.isNotBlank(item.getCompanyIds())){
                List<String> companyNameList = new ArrayList<>();
                String[] split = item.getCompanyIds().split(",");

                for (String companyId : split) {
                    String companyName = companyCacheService.selectCompanyNameById(Long.valueOf(companyId));
                    companyNameList.add(companyName);
                }
                item.setCompanyName(String.join(",",companyNameList));
            }
        }
        return fsStoreProductListVOS;
    }

    @Override
    public List<FsStoreProductListVO> selectFsStoreProductBarCodeListVO(FsStoreProduct fsStoreProduct) {
        List<FsStoreProductListVO> fsStoreProductListVOS = fsStoreProductMapper.selectFsStoreProductBarCodeListVO(fsStoreProduct);

        for (FsStoreProductListVO item : fsStoreProductListVOS) {
            if(StringUtils.isNotBlank(item.getCompanyIds())){
                List<String> companyNameList = new ArrayList<>();
                String[] split = item.getCompanyIds().split(",");

                for (String companyId : split) {
                    String companyName = companyCacheService.selectCompanyNameById(Long.valueOf(companyId));
                    companyNameList.add(companyName);
                }
                item.setCompanyName(String.join(",",companyNameList));
            }
        }
        return fsStoreProductListVOS;
    }

    @Override
    public Map<String, Object> getFormatAttr(Long productId, String jsonStr) {
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        Map<String, Object> resultMap = new LinkedHashMap<>(3);
        if (jsonObject == null || jsonObject.get("attrs") == null || jsonObject.getJSONArray("attrs").isEmpty()) {
            resultMap.put("attr", new ArrayList<>());
            resultMap.put("value", new ArrayList<>());
            resultMap.put("header", new ArrayList<>());
            return resultMap;
        }
        List<ProductArrtDTO> fromatDetailDTOList = JSON.parseArray(jsonObject.get("attrs").toString(),
                ProductArrtDTO.class);
        DetailVO detailDto = this.attrFormat(fromatDetailDTOList);
        List<Map<String, Object>> headerMapList = null;
        List<Map<String, Object>> valueMapList = new ArrayList<>();
        String align = "center";
        Map<String, Object> headerMap = new LinkedHashMap<>();
        for (Map<String, Map<String, String>> map : detailDto.getRes()) {
            Map<String, String> detail = map.get("detail");
            String[] detailArr = detail.values().toArray(new String[]{});
            Arrays.sort(detailArr);

            String sku = String.join(",", detailArr);

            Map<String, Object> valueMap = new LinkedHashMap<>();

            List<String> detailKeys =
                    detail.entrySet()
                            .stream()
                            .map(Map.Entry::getKey)
                            .collect(Collectors.toList());

            int i = 0;
            headerMapList = new ArrayList<>();
            for (String title : detailKeys) {
                headerMap.put("title", title);
                headerMap.put("minWidth", "130");
                headerMap.put("align", align);
                headerMap.put("key", "value" + (i + 1));
                headerMap.put("slot", "value" + (i + 1));
                headerMapList.add(ObjectUtil.clone(headerMap));
                i++;
            }

            String[] detailValues = detail.values().toArray(new String[]{});
            for (int j = 0; j < detailValues.length; j++) {
                String key = "value" + (j + 1);
                valueMap.put(key, detailValues[j]);
            }

            valueMap.put("detail", detail);
            valueMap.put("sku", "");
            valueMap.put("image", "");
            valueMap.put("price", 0);
            valueMap.put("cost", 0);
            valueMap.put("otPrice", 0);
            valueMap.put("stock", 0);
            valueMap.put("barCode", "");
            valueMap.put("groupBarCode", "");
            valueMap.put("agentPrice", 0);
            valueMap.put("weight", 0);
            valueMap.put("volume", 0);
            valueMap.put("brokerage", 0);
            valueMap.put("brokerageTwo", 0);
            valueMap.put("brokerageThree", 0);
            valueMap.put("integral", 0);
            if (productId > 0) {
                FsStoreProductAttrValue attrValueMap=new FsStoreProductAttrValue();
                attrValueMap.setProductId(productId);
                attrValueMap.setSku(sku);
                List<FsStoreProductAttrValue> values = fsStoreProductAttrValueMapper.selectFsStoreProductAttrValueList(attrValueMap);
                if (values != null&&values.size()==1) {
                    valueMap.put("id",values.get(0).getId());
                    valueMap.put("sku",values.get(0).getSku());
                    valueMap.put("image", values.get(0).getImage());
                    valueMap.put("price", values.get(0).getPrice());
                    valueMap.put("cost", values.get(0).getCost());
                    valueMap.put("otPrice", values.get(0).getOtPrice());
                    valueMap.put("agentPrice", values.get(0).getAgentPrice());
                    valueMap.put("stock", values.get(0).getStock());
                    valueMap.put("barCode", values.get(0).getBarCode());
                    valueMap.put("groupBarCode", values.get(0).getGroupBarCode());
                    valueMap.put("weight", values.get(0).getWeight());
                    valueMap.put("volume", values.get(0).getVolume());
                    valueMap.put("brokerage", values.get(0).getBrokerage());
                    valueMap.put("brokerageTwo", values.get(0).getBrokerageTwo());
                    valueMap.put("brokerageThree", values.get(0).getBrokerageThree());
                    valueMap.put("integral", values.get(0).getIntegral());
                }
            }
            valueMapList.add(ObjectUtil.clone(valueMap));
        }
        this.addHeaderMap(headerMap, headerMapList, align);
        resultMap.put("attr", fromatDetailDTOList);
        resultMap.put("value", valueMapList);
        resultMap.put("header", headerMapList);
        return resultMap;
    }

    @Override
    @Transactional
    public R addOrEdit(FsStoreProductAddEditParam param) {
        ProductAttrCountDto countDto=computedProductCount(param.getValues());

        if(param.getProductId()>0){
            FsStoreProduct product=new FsStoreProduct();
            BeanUtils.copyProperties(param,product);
            product.setPrice(countDto.getMinPrice());
            product.setOtPrice(countDto.getMinOtPrice());
            product.setCost(countDto.getMinCost());
            product.setIntegral(Long.parseLong(countDto.getMinIntegral().toString()));
            product.setStock(Long.parseLong(countDto.getStock().toString()));
            product.setCompanyIds(param.getCompanyIds());
            product.setVideo(param.getVideo());
            fsStoreProductMapper.updateFsStoreProduct(product);
            if (param.getSpecType().equals(0)) {
                ProductArrtDTO fromatDetailDto = ProductArrtDTO.builder()
                        .value("规格")
                        .detail(ListUtil.toList("默认"))
                        .build();
                List<ProductArrtDTO> items=new ArrayList<>();
                items.add(fromatDetailDto);
                param.getValues().get(0).setSku("默认");
                addProductAttr(product.getProductId(),items,param.getValues());
            } else {
                addProductAttr(product.getProductId(),param.getItems(),param.getValues());
            }
        }
        else{
            FsStoreProduct product=new FsStoreProduct();
            BeanUtils.copyProperties(param,product);
            product.setPrice(countDto.getMinPrice());
            product.setOtPrice(countDto.getMinOtPrice());
            product.setCost(countDto.getMinCost());
            product.setIntegral(Long.parseLong(countDto.getMinIntegral().toString()));
            product.setStock(Long.parseLong(countDto.getStock().toString()));
            product.setCompanyIds(param.getCompanyIds());
            product.setVideo(param.getVideo());
            fsStoreProductMapper.insertFsStoreProduct(product);
            if (param.getSpecType().equals(0)) {
                ProductArrtDTO fromatDetailDto = ProductArrtDTO.builder()
                        .value("规格")
                        .detail(ListUtil.toList("默认"))
                        .build();
                List<ProductArrtDTO> items=new ArrayList<>();
                items.add(fromatDetailDto);
                param.getValues().get(0).setSku("默认");
                addProductAttr(product.getProductId(),items,param.getValues());
            } else {
                addProductAttr(product.getProductId(),param.getItems(),param.getValues());
            }
        }
        return R.ok();
    }


    private void addProductAttr(Long productId, List<ProductArrtDTO> items, List<FsStoreProductAttrValue> values){
        //清空attr
        fsStoreProductAttrMapper.clear(productId);
        //清空values
        //查出商品属性所有ID;
        List<FsStoreProductAttrValue> attrValues=fsStoreProductAttrValueMapper.selectFsStoreProductAttrValueByProductId(productId);
        fsStoreProductAttrValueMapper.deleteFsStoreProductAttrValueByProductId(productId);
        //写入attr
        for(ProductArrtDTO vo:items){
            FsStoreProductAttr attr=new FsStoreProductAttr();
            attr.setProductId(productId);
            attr.setAttrName(vo.getValue());
            attr.setAttrValues(StringUtils.join(vo.getDetail(), ","));
            fsStoreProductAttrMapper.insertFsStoreProductAttr(attr);
        }
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("attr",items);
        // map.put("value",values);

        for(FsStoreProductAttrValue val: values){
            //更新套餐商品属性ID  获取套餐
            Long id=val.getId();
            if(val.getDetail()!=null){
                List<String> stringList = new ArrayList<>(val.getDetail().values());
                Collections.sort(stringList);
                val.setSku(StrUtil.join(",",stringList));
            }
            val.setProductId(productId);
            fsStoreProductAttrValueMapper.insertFsStoreProductAttrValue(val);
            if(attrValues!=null&&attrValues.size()>0){
                for(FsStoreProductAttrValue attrValue:attrValues){
                    if(attrValue.getId().equals(id)){
                        fsStoreProductGroupMapper.updateProducts(attrValue.getId(),val.getId());
                        fsStoreProductPackageMapper.updateProducts(attrValue.getId(),val.getId());
                    }
                }
            }

        }
    }

    private DetailVO attrFormat(List<ProductArrtDTO> fromatDetailDTOList) {
        List<String> data = new ArrayList<>();
        List<Map<String, Map<String, String>>> res = new ArrayList<>();
        fromatDetailDTOList.stream()
                .map(ProductArrtDTO::getDetail)
                .forEach(i -> {
                    if (i == null || i.isEmpty()) {
                        throw new CustomException("请至少添加一个规格值哦");
                    }
                    String str = ArrayUtil.join(i.toArray(), ",");
                    if (str.contains("-")) {
                        throw new CustomException("规格值里包含'-',请重新添加");
                    }
                });

        if (fromatDetailDTOList.size() > 1) {
            for (int i = 0; i < fromatDetailDTOList.size() - 1; i++) {
                if (i == 0) {
                    data = fromatDetailDTOList.get(i).getDetail();
                }
                List<String> tmp = new LinkedList<>();
                for (String v : data) {
                    for (String g : fromatDetailDTOList.get(i + 1).getDetail()) {
                        String rep2 = "";
                        if (i == 0) {
                            rep2 = fromatDetailDTOList.get(i).getValue() + "_" + v + "-"
                                    + fromatDetailDTOList.get(i + 1).getValue() + "_" + g;
                        } else {
                            rep2 = v + "-"
                                    + fromatDetailDTOList.get(i + 1).getValue() + "_" + g;
                        }

                        tmp.add(rep2);

                        if (i == fromatDetailDTOList.size() - 2) {
                            Map<String, Map<String, String>> rep4 = new LinkedHashMap<>();
                            Map<String, String> reptemp = new LinkedHashMap<>();
                            for (String h : Arrays.asList(rep2.split("-"))) {
                                List<String> rep3 = Arrays.asList(h.split("_"));
                                if (rep3.size() > 1) {
                                    reptemp.put(rep3.get(0), rep3.get(1));
                                } else {
                                    reptemp.put(rep3.get(0), "");
                                }
                            }
                            rep4.put("detail", reptemp);
                            res.add(rep4);
                        }
                    }
                }
                if (!tmp.isEmpty()) {
                    data = tmp;
                }
            }
        } else {
            List<String> dataArr = new ArrayList<>();
            for (ProductArrtDTO fromatDetailDTO : fromatDetailDTOList) {
                for (String str : fromatDetailDTO.getDetail()) {
                    Map<String, Map<String, String>> map2 = new LinkedHashMap<>();
                    dataArr.add(fromatDetailDTO.getValue() + "_" + str);
                    Map<String, String> map1 = new LinkedHashMap<>();
                    map1.put(fromatDetailDTO.getValue(), str);
                    map2.put("detail", map1);
                    res.add(map2);
                }
            }
            String s = StrUtil.join("-", dataArr);
            data.add(s);
        }
        DetailVO detailDto = new DetailVO();
        detailDto.setData(data);
        detailDto.setRes(res);
        return detailDto;
    }


    /**
     * 增加表头
     */
    private void addHeaderMap(Map<String, Object> headerMap, List<Map<String, Object>> headerMapList, String align) {
        headerMap.put("title", "图片");
        headerMap.put("slot", "image");
        headerMap.put("align", align);
        headerMap.put("minWidth", 80);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "售价");
        headerMap.put("slot", "price");
        headerMap.put("align", align);
        headerMap.put("minWidth", 120);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "代理价");
        headerMap.put("slot", "agentPrice");
        headerMap.put("align", align);
        headerMap.put("minWidth", 120);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "成本价");
        headerMap.put("slot", "cost");
        headerMap.put("align", align);
        headerMap.put("minWidth", 140);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "原价");
        headerMap.put("slot", "otPrice");
        headerMap.put("align", align);
        headerMap.put("minWidth", 140);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "库存");
        headerMap.put("slot", "stock");
        headerMap.put("align", align);
        headerMap.put("minWidth", 140);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "商品编号");
        headerMap.put("slot", "barCode");
        headerMap.put("align", align);
        headerMap.put("minWidth", 140);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "组合编号");
        headerMap.put("slot", "groupBarCode");
        headerMap.put("align", align);
        headerMap.put("minWidth", 200);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "重量(KG)");
        headerMap.put("slot", "weight");
        headerMap.put("align", align);
        headerMap.put("minWidth", 140);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "体积(m³)");
        headerMap.put("slot", "volume");
        headerMap.put("align", align);
        headerMap.put("minWidth", 140);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "所需积分");
        headerMap.put("slot", "integral");
        headerMap.put("align", align);
        headerMap.put("minWidth", 140);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "一级返佣");
        headerMap.put("slot", "brokerage");
        headerMap.put("align", align);
        headerMap.put("minWidth", 140);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "二级返佣");
        headerMap.put("slot", "brokerageTwo");
        headerMap.put("align", align);
        headerMap.put("minWidth", 140);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "三级返佣");
        headerMap.put("slot", "brokerageThree");
        headerMap.put("align", align);
        headerMap.put("minWidth", 140);
        headerMapList.add(ObjectUtil.clone(headerMap));

        headerMap.put("title", "操作");
        headerMap.put("slot", "action");
        headerMap.put("align", align);
        headerMap.put("minWidth", 70);
        headerMapList.add(ObjectUtil.clone(headerMap));
    }



    private ProductAttrCountDto computedProductCount(List<FsStoreProductAttrValue> values) {
        BigDecimal val=new BigDecimal(0);
        //取最小价格
        BigDecimal minPrice = values
                .stream()
                .map(FsStoreProductAttrValue::getPrice)
                .min(Comparator.naturalOrder())
                .orElse(val);

        //取最小积分
        Integer minIntegral = values
                .stream()
                .map(FsStoreProductAttrValue::getIntegral)
                .min(Comparator.naturalOrder())
                .orElse(0);

        BigDecimal minOtPrice = values
                .stream()
                .map(FsStoreProductAttrValue::getOtPrice)
                .min(Comparator.naturalOrder())
                .orElse(val);

        BigDecimal minCost = values
                .stream()
                .map(FsStoreProductAttrValue::getCost)
                .min(Comparator.naturalOrder())
                .orElse(val);
        //计算库存
        Integer stock = values
                .stream()
                .map(FsStoreProductAttrValue::getStock)
                .reduce(Integer::sum)
                .orElse(0);

//        if (stock <= 0) {
//            throw new CustomException("库存不能低于0");
//        }

        return ProductAttrCountDto.builder()
                .minPrice(minPrice)
                .minOtPrice(minOtPrice)
                .minCost(minCost)
                .stock(stock)
                .minIntegral(minIntegral)
                .build();
    }

    @Override
    public List<FsStoreProductListQueryVO> selectFsStoreProductTuiQuery(int count) {
        return fsStoreProductMapper.selectFsStoreProductTuiQuery(count);
    }



    @Override
    public List<FsStoreProductListQueryVO> selectFsStoreProductListQuery(FsStoreProductQueryParam param) {
        return fsStoreProductMapper.selectFsStoreProductListQuery(param);
    }

    @Override
    public FsStoreProductQueryVO selectFsStoreProductByIdQuery(Long productId) {
        return fsStoreProductMapper.selectFsStoreProductByIdQuery(productId);
    }

    @Override
    public void decProductStock(Long productId, Long productAttrValueId, Integer cartNum) {
        //处理属性sku
        fsStoreProductAttrValueMapper.decProductAttrStock(productAttrValueId,cartNum);
        fsStoreProductMapper.decProductAttrStock(productId,cartNum);

    }

    @Override
    public void incProductStock(Long num, Long productId, Long productAttrValueId) {
        //处理属性sku
        if (productAttrValueId!=null) {
            attrValueService.incProductAttrStock(num, productId, productAttrValueId);
        }
        //先处理商品库存
        fsStoreProductMapper.incStockDecSales(num, productId);

    }

    @Override
    public List<FsStoreProductListQueryVO> selectFsStoreProductNewQuery(int count) {
        return fsStoreProductMapper.selectFsStoreProductNewQuery(count);
    }

    @Override
    public List<FsStoreProductListQueryVO> selectFsStoreProductHotQuery(int count) {
        return fsStoreProductMapper.selectFsStoreProductHotQuery(count);
    }

    @Override
    public List<FsStoreProductListQueryVO> selectFsStoreProductGoodQuery(int count) {
        return fsStoreProductMapper.selectFsStoreProductGoodQuery(count);
    }

    @Override
    public List<FsStoreProductListQueryVO> selectFsStoreProductTuiListQuery() {
        return fsStoreProductMapper.selectFsStoreProductTuiListQuery();
    }

    @Override
    public List<FsStoreProductListQueryVO> selectFsStoreProductGoodListQuery() {
        return fsStoreProductMapper.selectFsStoreProductGoodListQuery();
    }

    @Override
    public Long selectFsStoreProductCount(int type) {
        return fsStoreProductMapper.selectFsStoreProductCount(type);
    }

    @Override
    public Long selectFsStoreProductCount(int type, Long companyId) {
        return fsStoreProductMapper.selectFsStoreProductCompanyCount(type,companyId);
    }

    @Override
    public List<FsStoreProductActivityListVO> selectFsStoreProductByIds(String productIds) {
        return fsStoreProductMapper.selectFsStoreProductByIds(productIds);
    }

    @Override
    public int updateStock(Long productId) {
        return fsStoreProductMapper.updateStock(productId);
    }

    @Override
    public List<FsStoreProductExportVO> selectFsStoreProductExportList(FsStoreProduct fsStoreProduct) {
        return fsStoreProductMapper.selectFsStoreProductExportList(fsStoreProduct);
    }

    @Override
    public String importStoreProduct(List<FsStoreProductExportVO> list, boolean updateSupport) {
        if (com.fs.common.utils.StringUtils.isNull(list) || list.size() == 0)
        {
            throw new CustomException("导入商品数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (FsStoreProductExportVO productVO : list){
            try
            {
                FsStoreProduct product = BeanCopyUtils.copy(productVO, FsStoreProduct.class);
                if (product.getBarCode()==null || product.getBarCode()==""){
                    throw new CustomException("商品编号为空");
                }
                this.insertFsStoreProduct(product);
                ProductArrtDTO formatDetailDto = ProductArrtDTO.builder()
                        .value("规格")
                        .detail(ListUtil.toList("默认"))
                        .build();
                List<ProductArrtDTO> items=new ArrayList<>();
                items.add(formatDetailDto);
                FsStoreProductAttrValue p = new FsStoreProductAttrValue();
                p.setProductId(product.getProductId());
                p.setSku("默认");
                p.setStock(product.getStock().intValue());
                if (product.getStock()==null){
                    p.setStock(0);
                }
                p.setSales(product.getSales().intValue());
                if (product.getSales()==null){
                    p.setStock(0);
                }
                p.setPrice(product.getPrice());
                if (product.getPrice()==null){
                    p.setPrice(new BigDecimal(0));
                }
                p.setImage(product.getImage());
                p.setCost(product.getCost());
                if (product.getCost()==null){
                    p.setCost(new BigDecimal(0));
                }
                p.setBarCode(product.getBarCode());
                p.setGroupBarCode(product.getBarCode());
                p.setOtPrice(product.getOtPrice());
                if (product.getOtPrice()==null){
                    p.setOtPrice(new BigDecimal(0));
                }
                p.setWeight(productVO.getWeight());
                if (productVO.getWeight()==null){
                    p.setWeight(new BigDecimal(0));
                }

                p.setVolume(new BigDecimal(0));
                p.setBrokerage(new BigDecimal(0));
                p.setBrokerageTwo(new BigDecimal(0));
                p.setBrokerageThree(new BigDecimal(0));
                p.setAgentPrice(new BigDecimal(0));
                fsStoreProductAttrValueMapper.insertFsStoreProductAttrValue(p);

                fsStoreProductAttrMapper.clear(product.getProductId());
                FsStoreProductAttr attr = new FsStoreProductAttr();
                attr.setProductId(product.getProductId());
                attr.setAttrName("规格");
                attr.setAttrValues("默认");
                fsStoreProductAttrMapper.insertFsStoreProductAttr(attr);
                successNum++;
                successMsg.append("<br/>" + successNum + "、商品 " + product.getProductName() + " 导入成功");
            }
            catch (Exception e)
            {

                failureNum++;
                String msg = "<br/>" + failureNum + "、商品 " + productVO.getProductName()  + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }

        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public void batchModify(ModifyMoreDTO modifyMoreDTO) {
        fsStoreProductMapper.batchModify(modifyMoreDTO);
    }
}
