package com.hzs.sale.product.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.HttpStatus;
import com.hzs.common.core.constant.msg.ProductMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.CreateNormsCodeUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.sale.classify.BdClassify;
import com.hzs.common.domain.sale.classify.BdSpecs;
import com.hzs.common.domain.sale.classify.BdSpecsType;
import com.hzs.common.domain.sale.product.BdProduct;
import com.hzs.common.domain.sale.product.BdProductExtend;
import com.hzs.common.domain.sale.product.BdProductSpecs;
import com.hzs.common.domain.sale.wares.BdWaresDetail;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.common.util.TransactionUtils;
import com.hzs.sale.product.controller.manage.params.*;
import com.hzs.sale.product.param.SupplierProductCensusParam;
import com.hzs.sale.product.service.*;
import com.hzs.sale.product.vo.ProductSpecsVo;
import com.hzs.sale.product.vo.ProductVo;
import com.hzs.sale.product.vo.SupplierProductCensusVo;
import com.hzs.sale.wares.service.IBdWaresDetailService;
import com.hzs.system.base.ISystemConfigServiceApi;
import com.hzs.system.base.dto.BdCubasdocDTO;
import com.hzs.system.sys.dto.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/manage/product")
public class BdProductController extends BaseController {

    @Autowired
    private IBdWaresDetailService iBdWaresDetailService;
    @Autowired
    private IBdClassifyService iBdClassifyService;
    @Autowired
    private IBdProductService iBdProductService;
    @Autowired
    private IBdProductSpecsService iBdProductSpecsService;
    @Autowired
    private IBdSpecsTypeService iBdSpecsTypeService;
    @Autowired
    private IBdSpecsService iBdSpecsService;
    @Autowired
    private IBdProductExtendService iBdProductExtendService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;
    @Autowired
    private UserTokenService userTokenService;

    @DubboReference
    ISystemConfigServiceApi iSystemConfigServiceApi;

    /**
     * 查询商品列表
     *
     * @return
     */
    @AccessPermissions("ProductList")
    @Log(module = EOperationModule.PRODUCT_LIST, business = EOperationBusiness.PRODUCT_LIST, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(ProductParams productParams) {
        if (productParams.getShippingType() != null) {
            if (productParams.getShippingType() == 1) {
                // 陆运
                productParams.setIsLandTrans(0);
            }
            if (productParams.getShippingType() == 2) {
                // 空运
                productParams.setIsAirTrans(0);
            }
            if (productParams.getShippingType() == 3) {
                // 海运
                productParams.setIsOceanTrans(0);
            }
        }

        productParams.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        List<ProductVo> list = iBdProductService.selectByQueryList(productParams);
        for (ProductVo productVo : list) {
            BdClassify classify = iBdClassifyService.getClassify(productVo.getPkClassify());
            if (classify != null) {
                productVo.setClassifyName(classify.getName());
            }
            productVo.setPkProduct(productVo.getPkId());
            //   取值翻译  拼接海陆空运  到 typeShippingMerge
            StringBuilder str = new StringBuilder();
            // 海运
            if (EYesNo.YES.getIntValue() == productVo.getIsOceanTrans()) {
                str.append(ETransportType.OCEAN.getLabel()).append("/");
            }
            //空运
            if (EYesNo.YES.getIntValue() == productVo.getIsAirTrans()) {
                str.append(ETransportType.AIR.getLabel()).append("/");
            }
            // 陆运
            if (EYesNo.YES.getIntValue() == productVo.getIsLandTrans()) {
                str.append(ETransportType.LAND.getLabel());
            }
            productVo.setTypeShippingMerge(str.toString());
            if (productVo.getShippingChannel() != null) {
                productVo.setShippingChannelVal(EShippingChannel.getEnumByValue(productVo.getShippingChannel()).getLabel());
            }
        }
        return getDataTable(list);
    }

    /**
     * 新增商品信息
     */
    @Log(module = EOperationModule.PRODUCT_SAVE, method = EOperationMethod.INSERT, business = EOperationBusiness.PRODUCT_RELEASE)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody ProductParams productParams) {
        if (StringUtils.isBlank(productParams.getProductName())) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_NAME_REQUIRED));
        }
        // 产品划线价
        if (productParams.getRetailPrice() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_PRICE_REQUIRED));
        }
        // 发货渠道
        if (productParams.getShippingChannel() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_SHIPPING_CHANNEL_REQUIRED));
        }
        // 保质期
        if (productParams.getShelfLife() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_SHELFLIFE_REQUIRED));
        }
        // 单位
        if (productParams.getUnit() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_UNIT_REQUIRED));
        }
        // 重量
        if (productParams.getWeight() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_WEIGHT_REQUIRED));
        }
        // 邮费重量
        if (productParams.getPostageWeight() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_POSTAGE_WEIGHT_REQUIRED));
        }
        // 产品属性
        if (productParams.getProductAttr() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_ATTR_REQUIRED));
        }
        // 产品分类
        if (productParams.getPkClassify() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_CLASSIFY_REQUIRED));
        }
        // 排序序号
        if (productParams.getSort() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_SORT_REQUIRED));
        }
        // 长
        if (productParams.getLength() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_LENGTH_REQUIRED));
        }
        // 宽
        if (productParams.getWidth() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_WIDTH_REQUIRED));
        }
        // 高
        if (productParams.getHeight() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_HEIGHT_REQUIRED));
        }
        // 产品主图
        if (StringUtils.isBlank(productParams.getCover())) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_COVER_REQUIRED));
        }
        // 产品相关规格
        if (productParams.getPkSpecsType().size() == 0) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_SPECSTYPE_REQUIRED));
        }

        // 新增商品信息
        LoginUser loginUser = userTokenService.getLoginUser();
        // 排序重复校验
        LambdaQueryWrapper<BdProductExtend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdProductExtend::getSort, productParams.getSort());
        int count = iBdProductExtendService.count(queryWrapper);
        if (count > 0) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.SORT_REPEAT));
        }
        // 校验编号重复
        LambdaQueryWrapper<BdProduct> qw = new LambdaQueryWrapper<>();
        qw.eq(BdProduct::getProductCode, productParams.getProductCode());
        int productCount = iBdProductService.count(qw);
        if (productCount > 0) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_NUMBER_ALREADY_EXISTS));
        }
        iBdProductService.saveProduct(productParams, loginUser);
        return AjaxResult.success();
    }

    /**
     * 修改用户信息
     *
     * @param productParams
     * @return
     */
    @Log(module = EOperationModule.PRODUCT_UPDATE, method = EOperationMethod.UPDATE, business = EOperationBusiness.PRODUCT_UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody ProductParams productParams) {
        // 查询订单编号是否重复
        BdProduct product = iBdProductService.getById(productParams.getPkId());
        if (!product.getProductCode().equals(productParams.getProductCode())) {
            LambdaQueryWrapper<BdProduct> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BdProduct::getProductCode, productParams.getProductCode());
            int count = iBdProductService.count(queryWrapper);
            if (count > 0) {
                return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_NUMBER_ALREADY_EXISTS));
            }
        }
        // 排序重复校验
        LambdaQueryWrapper<BdProductExtend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdProductExtend::getSort, productParams.getSort());
        queryWrapper.notIn(BdProductExtend::getPkProduct, productParams.getPkId());
        int count = iBdProductExtendService.count(queryWrapper);
        if (count > 0) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.SORT_REPEAT));
        }
        LoginUser loginUser = userTokenService.getLoginUser();
        // 校验编号重复
        LambdaQueryWrapper<BdProduct> qe = new LambdaQueryWrapper<>();
        qe.eq(BdProduct::getProductCode, productParams.getProductCode());
        qe.notIn(BdProduct::getPkId, productParams.getPkId());
        int productCount = iBdProductService.count(qe);
        if (productCount > 0) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_NUMBER_ALREADY_EXISTS));
        }
        // 修改商品信息
        iBdProductService.updateProduct(productParams, loginUser);
        return AjaxResult.success(productParams.getProductCode());
    }

    /**
     * 根据商品id 删除数据
     *
     * @param pkId
     * @return
     */
    @Log(module = EOperationModule.PRODUCT_DELETE, method = EOperationMethod.DELETE, business = EOperationBusiness.PRODUCT_DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Integer pkId) {
        LambdaQueryWrapper<BdWaresDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdWaresDetail::getPkProduct, pkId);
        int count = iBdWaresDetailService.count(queryWrapper);
        if (count > 0) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_OCCUPIED));
        }
        LoginUser loginUser = userTokenService.getLoginUser();
        iBdProductService.removeProduct(pkId, loginUser);
        return AjaxResult.success();
    }

    /**
     * 修改商品启用禁用状态
     *
     * @param pkId
     * @param enable
     * @return
     */
    @Log(module = EOperationModule.PRODUCT_UPDATE_ENABLE, method = EOperationMethod.UPDATE, business = EOperationBusiness.PRODUCT_UPDATE)
    @PostMapping("/updateEnable/{pkId}/{enable}")
    public AjaxResult updateEnable(@PathVariable Integer pkId, @PathVariable Integer enable) {
        BdProduct product = new BdProduct();
        product.setPkId(pkId);
        product.setEnableState(enable);
        iBdProductService.updateById(product);
        return AjaxResult.success();
    }

    /**
     * 查询产品规格
     */
    @PostMapping("/get-product-specs")
    public AjaxResult getProductSpecs(@RequestBody ProductParams productParams) {
        List<Integer> productIds = productParams.getProductIds();
        if (productIds.size() == 0) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PLEASE_SELECT_PRODUCT));
        }
        // 查询产品使用的所有规格
        List<ProductSpecsInfo> resultList = new ArrayList<>();
        for (Integer productId : productParams.getProductIds()) {
            ProductSpecsInfo productSpecsInfo = new ProductSpecsInfo();
            BdProduct product = iBdProductService.getProduct(productId);
            if (product != null) {
                productSpecsInfo.setProductId(productId);
                productSpecsInfo.setProductName(product.getProductName());
                productSpecsInfo.setProductCode(product.getProductCode());
                productSpecsInfo.setProductCover(product.getCover());
                productSpecsInfo.setRetailPriceTmp(product.getRetailPrice());
                // 查询商品的规格类型
                HashSet<Integer> set = new LinkedHashSet<>();
                LambdaQueryWrapper<BdProductSpecs> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(BdProductSpecs::getPkProduct, productId);
                //根据规格id 正序排列
                queryWrapper.orderByAsc(BdProductSpecs::getPkId);
                List<BdProductSpecs> list = iBdProductSpecsService.list(queryWrapper);
                for (BdProductSpecs productSpecs : list) {
                    set.add(productSpecs.getPkSpecsType());
                }
                List<SpecsTypeParam> specsTypeParamList = new ArrayList<>();
                for (Integer specsTypeId : set) {
                    BdSpecsType specsType = iBdSpecsTypeService.getSpecsType(specsTypeId);
                    if (specsType != null) {
                        // 根据规格类型查询规格属性
                        SpecsTypeParam specsTypeParam = new SpecsTypeParam();
                        specsTypeParam.setPkId(specsType.getPkId());
                        specsTypeParam.setTypeName(specsType.getTypeName());
                        LambdaQueryWrapper<BdSpecs> specsWrapper = new LambdaQueryWrapper<>();
                        specsWrapper.eq(BdSpecs::getPkSpecsType, specsTypeId);
                        List<BdSpecs> specsList = iBdSpecsService.list(specsWrapper);
                        List<SpecsParam> resultSpecsList = new ArrayList<>();
                        for (BdSpecs bdSpecs : specsList) {
                            SpecsParam specsParam = new SpecsParam();
                            specsParam.setSpecsId(bdSpecs.getPkId());
                            specsParam.setValue(bdSpecs.getSpecsName());
                            specsParam.setPkSpecsType(bdSpecs.getPkSpecsType());
                            resultSpecsList.add(specsParam);
                        }
                        specsTypeParam.setSpecsList(resultSpecsList);
                        specsTypeParamList.add(specsTypeParam);
                    }
                }
                productSpecsInfo.setSpecsTypeData(specsTypeParamList);
            }
            resultList.add(productSpecsInfo);
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 查询产品规格(带是否赠品)
     */
    @PostMapping("/get-product-specs-gift")
    public AjaxResult getProductSpecsGift(@RequestBody ProductParams productParams) {
        List<ProductGiftParam> productGiftList = productParams.getProductGiftList();
        if (productGiftList.size() == 0) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PLEASE_SELECT_PRODUCT));
        }
        // 查询产品使用的所有规格
        List<ProductSpecsInfo> resultList = new ArrayList<>();
        for (ProductGiftParam productGiftParam : productParams.getProductGiftList()) {
            ProductSpecsInfo productSpecsInfo = new ProductSpecsInfo();
            Integer productId = productGiftParam.getPkProduct();
            BdProduct product = iBdProductService.getProduct(productId);
            if (product != null) {
                productSpecsInfo.setProductId(productId);
                productSpecsInfo.setProductName(product.getProductName());
                productSpecsInfo.setProductCode(product.getProductCode());
                productSpecsInfo.setProductCover(product.getCover());
                productSpecsInfo.setRetailPriceTmp(product.getRetailPrice());
                productSpecsInfo.setQuantity(productGiftParam.getQuantity());
                productSpecsInfo.setIsGift(productGiftParam.getIsGift());
                // 查询商品的规格类型
                HashSet<Integer> set = new LinkedHashSet<>();
                LambdaQueryWrapper<BdProductSpecs> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(BdProductSpecs::getPkProduct, productId);
                //根据规格id 正序排列
                queryWrapper.orderByAsc(BdProductSpecs::getPkId);
                List<BdProductSpecs> list = iBdProductSpecsService.list(queryWrapper);
                for (BdProductSpecs productSpecs : list) {
                    set.add(productSpecs.getPkSpecsType());
                }
                List<SpecsTypeParam> specsTypeParamList = new ArrayList<>();
                for (Integer specsTypeId : set) {
                    BdSpecsType specsType = iBdSpecsTypeService.getSpecsType(specsTypeId);
                    if (specsType != null) {
                        // 根据规格类型查询规格属性
                        SpecsTypeParam specsTypeParam = new SpecsTypeParam();
                        specsTypeParam.setPkId(specsType.getPkId());
                        specsTypeParam.setTypeName(specsType.getTypeName());
                        LambdaQueryWrapper<BdSpecs> specsWrapper = new LambdaQueryWrapper<>();
                        specsWrapper.eq(BdSpecs::getPkSpecsType, specsTypeId);
                        List<BdSpecs> specsList = iBdSpecsService.list(specsWrapper);
                        List<SpecsParam> resultSpecsList = new ArrayList<>();
                        for (BdSpecs bdSpecs : specsList) {
                            SpecsParam specsParam = new SpecsParam();
                            specsParam.setSpecsId(bdSpecs.getPkId());
                            specsParam.setValue(bdSpecs.getSpecsName());
                            specsParam.setPkSpecsType(bdSpecs.getPkSpecsType());
                            resultSpecsList.add(specsParam);
                        }
                        specsTypeParam.setSpecsList(resultSpecsList);
                        specsTypeParamList.add(specsTypeParam);
                    }
                }
                productSpecsInfo.setSpecsTypeData(specsTypeParamList);
            }
            resultList.add(productSpecsInfo);
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 查询产品规格
     */
    @PostMapping("/get-product-code-specs")
    public AjaxResult getProductCodeSpecs(@RequestBody ProductParams productParams) {
        List<String> productCodes = productParams.getProductCodes();
        if (productCodes.size() == 0) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PLEASE_SELECT_PRODUCT));
        }
        // 查询产品使用的所有规格
        List<ProductSpecsInfo> resultList = new ArrayList<>();
        for (String productCode : productParams.getProductCodes()) {
            ProductSpecsInfo productSpecsInfo = new ProductSpecsInfo();
            LambdaQueryWrapper<BdProduct> qw = new LambdaQueryWrapper<>();
            qw.eq(BdProduct::getProductCode, productCode);
            BdProduct product = iBdProductService.getOne(qw);
            if (product != null) {
                productSpecsInfo.setProductId(product.getPkId());
                productSpecsInfo.setProductName(product.getProductName());
                productSpecsInfo.setProductCode(product.getProductCode());
                productSpecsInfo.setProductCover(product.getCover());
                // 查询商品的规格类型
                Set<Integer> set = new HashSet<>();
                LambdaQueryWrapper<BdProductSpecs> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(BdProductSpecs::getPkProduct, product.getPkId());
                queryWrapper.orderByAsc(BdProductSpecs::getPkId);
                List<BdProductSpecs> list = iBdProductSpecsService.list(queryWrapper);
                for (BdProductSpecs productSpecs : list) {
                    set.add(productSpecs.getPkSpecsType());
                }
                List<SpecsTypeParam> specsTypeParamList = new ArrayList<>();
                for (Integer specsTypeId : set) {
                    BdSpecsType specsType = iBdSpecsTypeService.getSpecsType(specsTypeId);
                    if (specsType != null) {
                        // 根据规格类型查询规格属性
                        SpecsTypeParam specsTypeParam = new SpecsTypeParam();
                        specsTypeParam.setPkId(specsType.getPkId());
                        specsTypeParam.setTypeName(specsType.getTypeName());
                        LambdaQueryWrapper<BdSpecs> specsWrapper = new LambdaQueryWrapper<>();
                        specsWrapper.eq(BdSpecs::getPkSpecsType, specsTypeId);
                        specsWrapper.orderByAsc(BdSpecs::getPkId);
                        List<BdSpecs> specsList = iBdSpecsService.list(specsWrapper);
                        List<SpecsParam> resultSpecsList = new ArrayList<>();
                        for (BdSpecs bdSpecs : specsList) {
                            SpecsParam specsParam = new SpecsParam();
                            specsParam.setSpecsId(bdSpecs.getPkId());
                            specsParam.setValue(bdSpecs.getSpecsName());
                            specsParam.setPkSpecsType(bdSpecs.getPkSpecsType());
                            resultSpecsList.add(specsParam);
                        }
                        specsTypeParam.setSpecsList(resultSpecsList);
                        specsTypeParamList.add(specsTypeParam);
                    }
                }
                productSpecsInfo.setSpecsTypeData(specsTypeParamList);
            }
            resultList.add(productSpecsInfo);
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 查询产品详情
     *
     * @return
     */
    @GetMapping("/detail")
    public AjaxResult detail(Integer productId) {
        ProductVo product = iBdProductService.getByProductId(productId);
        if (product == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_NOT_REQUIRED));
        }

        ProductParams productParams = BeanUtil.copyProperties(product, ProductParams.class);
        productParams.setProductBasName(product.getProductBasName());
        BdProductExtend productExtend = iBdProductExtendService.getOne(new LambdaQueryWrapper<BdProductExtend>().eq(BdProductExtend::getPkProduct, productId));
        productParams.setIsPickUp(productExtend.getIsPickUp());
        productParams.setIsPrize(productExtend.getIsPrize());
        productParams.setIsBarCode(productExtend.getIsBarCode());
        productParams.setRepurchaseType(productExtend.getRepurchaseType());
        productParams.setIsMerge(productExtend.getIsMerge());
        productParams.setIsGift(productExtend.getIsGift());
        productParams.setIsLandTrans(productExtend.getIsLandTrans());
        productParams.setIsAirTrans(productExtend.getIsAirTrans());
        productParams.setIsOceanTrans(productExtend.getIsOceanTrans());
        productParams.setSort(productExtend.getSort());
        productParams.setIsPutOn(productExtend.getIsPutOn());
        LambdaQueryWrapper<BdProductSpecs> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdProductSpecs::getPkProduct, productId);
        List<Integer> pkSpecsType = new ArrayList<>();
        List<BdProductSpecs> productSpecList = iBdProductSpecsService.list(queryWrapper);
        for (BdProductSpecs bdProductSpecs : productSpecList) {
            pkSpecsType.add(bdProductSpecs.getPkSpecsType());
        }
        productParams.setPkSpecsType(pkSpecsType);
        return AjaxResult.success(productParams);
    }

    /**
     * @return
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductParams productParams) {
        if (productParams.getShippingType() != null) {
            if (productParams.getShippingType() == 1) {
                // 陆运
                productParams.setIsLandTrans(0);
            }
            if (productParams.getShippingType() == 2) {
                // 空运
                productParams.setIsAirTrans(0);
            }
            if (productParams.getShippingType() == 3) {
                // 海运
                productParams.setIsOceanTrans(0);
            }
        }
        productParams.setPkCountry(SecurityUtils.getPkCountry());
        Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(ETransportType.values(), EYesNo.values(), EShippingChannel.values(), EUnit.values());
        List<ProductVo> list = iBdProductService.selectByQueryList(productParams);
        for (ProductVo productVo : list) {
            BdClassify classify = iBdClassifyService.getClassify(productVo.getPkClassify());
            if (classify != null) {
                productVo.setClassifyName(classify.getName());
            }

            // 发货类型翻译
            productVo.setShippingChannelVal(transactionMap.get(EShippingChannel.getEnumByValue(productVo.getShippingChannel()).getKey()));
            // 产品属性翻译
            if (productVo.getProductAttr() != null) {
                productVo.setProductAttrVal(EProductAttrType.getEnumByValue(productVo.getProductAttr()).getLabel());
            }
            // 礼包翻译
            productVo.setIsGiftVal(transactionMap.get(EYesNo.getEnumByValue(productVo.getIsGift()).getKey()));
            // 提货配置
            productVo.setIsPickUpVal(transactionMap.get(EYesNo.getEnumByValue(productVo.getIsPickUp()).getKey()));
            // 奖品配置
            productVo.setIsPrizeVal(transactionMap.get(EYesNo.getEnumByValue(productVo.getIsPrize()).getKey()));
            // 追溯编号 翻译
            productVo.setIsBarCodeVal(transactionMap.get(EYesNo.getEnumByValue(productVo.getIsBarCode()).getKey()));
            // 合并发货翻译
            productVo.setIsMergeVal(transactionMap.get(EYesNo.getEnumByValue(productVo.getIsMerge()).getKey()));
            // 冷链运输翻译
            productVo.setIsFreshVal(transactionMap.get(EYesNo.getEnumByValue(productVo.getIsFresh()).getKey()));

            //   取值翻译  拼接海陆空运  到 typeShippingMerge
            StringBuilder str = new StringBuilder();
            // 海运
            if (EYesNo.YES.getIntValue() == productVo.getIsOceanTrans()) {
                str.append(ETransportType.OCEAN.getLabel()).append("/");
            }
            //空运
            if (EYesNo.YES.getIntValue() == productVo.getIsAirTrans()) {
                str.append(ETransportType.AIR.getLabel()).append("/");
            }
            // 陆运
            if (EYesNo.YES.getIntValue() == productVo.getIsLandTrans()) {
                str.append(ETransportType.LAND.getLabel());
            }
            productVo.setTypeShippingMerge(str.toString());
        }
        ExcelUtil<ProductVo> util = new ExcelUtil<>(ProductVo.class);
        util.exportExcel(response, list, "产品信息导出");
    }

    /**
     * 产品移动（上移/下移）
     *
     * @param productId       产品id
     * @param productMoveType 产品移动类型 0 上移 1 下移
     * @return
     */
    @GetMapping("move")
    public AjaxResult move(Integer productId, Integer productMoveType) {
        // 新增商品信息
        LoginUser loginUser = userTokenService.getLoginUser();
        iBdProductService.productMove(productId, productMoveType, loginUser);
        return AjaxResult.success();
    }

    /**
     * 置顶
     *
     * @param productId
     * @return
     */
    @GetMapping("floating-roof")
    public AjaxResult floatingRoof(Integer productId) {
        LoginUser loginUser = userTokenService.getLoginUser();
        iBdProductService.floatingRoof(productId, loginUser);
        return AjaxResult.success();
    }


    /**
     * 查询全部产品
     *
     * @return
     */
    @GetMapping("/all")
    public AjaxResult all(Integer pkCountry) {
        if (null == pkCountry) {
            pkCountry = SecurityUtils.getPkCountry();
        }
        return AjaxResult.success(iBdProductService.queryProductExistSpecs(pkCountry));
    }

    /**
     * 查询产品规格
     *
     * @param pkProduct
     * @return
     */
    @GetMapping("/get-product-specs")
    public AjaxResult getProductSpecs(Integer pkProduct) {
        List<ProductSpecsVo> productSpecsList = iBdProductSpecsService.selectByList(pkProduct);
        for (ProductSpecsVo productSpecsVo : productSpecsList) {
            LambdaQueryWrapper<BdSpecs> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BdSpecs::getPkSpecsType, productSpecsVo.getPkSpecsType());
            queryWrapper.orderByAsc(BdSpecs::getPkId);
            List<BdSpecs> specsList = iBdSpecsService.list(queryWrapper);
            productSpecsVo.setSpecsList(specsList);
        }
        return AjaxResult.success(productSpecsList);
    }

    /**
     * 根据产品编号查询产品
     *
     * @param productCode
     * @return
     */
    @GetMapping("/get-product-code")
    public AjaxResult getProductCode(String productCode) {
        LambdaQueryWrapper<BdProduct> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(productCode)) {
            queryWrapper.eq(BdProduct::getProductCode, productCode);
            return AjaxResult.success(iBdProductService.getOne(queryWrapper));
        } else {
            return AjaxResult.success();
        }
    }

    /**
     * 生成产品编号
     *
     * @return
     */
    @GetMapping("/create-product-code")
    public AjaxResult createProductCode() {
        // 查询最末商品编号 生成商品编号
        String productCode = CreateNormsCodeUtils.createProductCode(iBdProductService.getLastProductCode());
        return AjaxResult.success(productCode);
    }

    /**
     * 查询最大排序
     *
     * @return
     */
    @GetMapping("/get-max-sort")
    public AjaxResult getMaxSort() {
        Integer sort = iBdProductExtendService.getMaxProductSort();
        if (sort == null) {
            sort = 0;
        }
        return AjaxResult.success(sort + 1);
    }

    /**
     * 供应商产品统计
     */
    @GetMapping("/supplier-product-census")
    public TableDataInfo supplierProductCensus(SupplierProductCensusParam supplierProductCensusParam) {
        startPage();
        //   下面代码需要使用 坐等随工提交此方法 取消注释
        R<BdCubasdocDTO> bdCubasdocDate = iSystemConfigServiceApi.queryBdCubasdocByUser(SecurityUtils.getUserId());
        Integer pkSupplier = null;
        if (bdCubasdocDate.getCode() == HttpStatus.SUCCESS) {
            BdCubasdocDTO bdCubasdoc = bdCubasdocDate.getData();
            if (bdCubasdoc != null) {
                pkSupplier = bdCubasdoc.getPkSupplier();
            }
        }
        supplierProductCensusParam.setPkSupplier(pkSupplier);
        List<SupplierProductCensusVo> supplierProductCensusVoList = iBdProductService.getSupplierProductCensus(supplierProductCensusParam);
        for (SupplierProductCensusVo supplierProductCensusVo : supplierProductCensusVoList) {
            BdClassify classify = iBdClassifyService.getClassify(supplierProductCensusVo.getPkClassify());
            if (classify != null) {
                supplierProductCensusVo.setClassifyName(classify.getName());
            }
            if (supplierProductCensusVo.getShippingChannel() != null) {
                supplierProductCensusVo.setShippingChannelVal(EShippingChannel.getEnumByValue(supplierProductCensusVo.getShippingChannel()).getLabel());
            }
        }

        return getDataTable(supplierProductCensusVoList);
    }

    /**
     * @param supplierProductCensusParam
     */
    @PostMapping("/supplier-product-census-export")
    public void supplierProductCensusExport(HttpServletResponse response, SupplierProductCensusParam supplierProductCensusParam) {
        //   下面代码需要使用 坐等随工提交此方法 取消注释
        R<BdCubasdocDTO> bdCubasdocDate = iSystemConfigServiceApi.queryBdCubasdocByUser(SecurityUtils.getUserId());
        Integer pkSupplier = null;
        if (bdCubasdocDate.getCode() == HttpStatus.SUCCESS) {
            BdCubasdocDTO bdCubasdoc = bdCubasdocDate.getData();
            if (bdCubasdoc != null) {
                pkSupplier = bdCubasdoc.getPkSupplier();
            }
        }
        supplierProductCensusParam.setPkSupplier(pkSupplier);
        List<SupplierProductCensusVo> supplierProductCensusVoList = iBdProductService.getSupplierProductCensus(supplierProductCensusParam);
        for (SupplierProductCensusVo supplierProductCensusVo : supplierProductCensusVoList) {
            BdClassify classify = iBdClassifyService.getClassify(supplierProductCensusVo.getPkClassify());
            if (classify != null) {
                supplierProductCensusVo.setClassifyName(classify.getName());
            }
            if (supplierProductCensusVo.getShippingChannel() != null) {
                supplierProductCensusVo.setShippingChannelVal(EShippingChannel.getEnumByValue(supplierProductCensusVo.getShippingChannel()).getLabel());
            }
        }
        ExcelUtil<SupplierProductCensusVo> util = new ExcelUtil<>(SupplierProductCensusVo.class);
        util.exportExcel(response, supplierProductCensusVoList, "供应商商品统计导出");
    }

}
