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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ProductMsgConstants;
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.ext.BdProductExt;
import com.hzs.common.domain.sale.product.BdProduct;
import com.hzs.common.domain.sale.product.BdProductBas;
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.core.utils.TransactionUtils;
import com.hzs.sale.product.controller.manage.params.*;
import com.hzs.sale.product.service.*;
import com.hzs.sale.product.vo.ProductSpecsVo;
import com.hzs.sale.product.vo.ProductVo;
import com.hzs.sale.wares.service.IBdWaresDetailService;
import com.hzs.system.sys.dto.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description:
 * @Author: yuhui
 * @Time: 2022/9/1 14:14
 * @Classname: BdProductController
 * @PackageName: com.hzs.sale.product.controller.manage
 */
@Slf4j
@RestController
@RequestMapping("/manage/product")
public class BdProductController extends BaseController {

    @Autowired
    private IBdWaresDetailService waresDetailService;
    @Autowired
    private IBdClassifyService classifyService;
    @Autowired
    private IBdProductService productService;
    @Autowired
    private IBdProductSpecsService productSpecsService;
    @Autowired
    private IBdSpecsTypeService specsTypeService;
    @Autowired
    private IBdSpecsService specsService;
    @Autowired
    private IBdProductExtendService productExtendService;
    @Autowired
    private ITransactionCommonService transactionCommonService;
    @Autowired
    private UserTokenService userTokenService;

    /**
     * 查询商品列表
     *
     * @return
     */
    @Log(module = EOperationModule.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);
            }
        }
        // 获取需要翻译的枚举翻译
        Map<String, String> transactionMap = transactionCommonService.exportEnumTransaction(EShippingChannel.values(),ETransportType.values());
        startPage();
        productParams.setPkCountry(SecurityUtils.getPkCountry());
        List<ProductVo> list = productService.selectByQueryList(productParams);
        for (ProductVo productVo : list) {
            productVo.setPkProduct(productVo.getPkId());
            BdClassify classify = classifyService.getClassify(productVo.getPkClassify());
            if (classify != null) {
                productVo.setClassifyName(classify.getName());
                productVo.setClassifyPkTransaction(classify.getPkTransactionKey());
            }
            List<ProductSpecsVo> productSpecsList = productSpecsService.queryListByProductId(productVo.getPkId(),SecurityUtils.getPkCountry());
            if (productSpecsList.size() > 0){
                productVo.setProductSpecType(productSpecsList.get(0).getPkTransactionKey());
            }
            //   取值翻译  拼接海陆空运  到 typeShippingMerge
            StringBuilder str = new StringBuilder();
            // 海运
            if (EYesNo.YES.getIntValue() == productVo.getIsOceanTrans()) {
                str.append(transactionMap.get(ETransportType.OCEAN.getKey())).append("/");
            }
            //空运
            if (EYesNo.YES.getIntValue() == productVo.getIsAirTrans()) {
                str.append(transactionMap.get(ETransportType.AIR.getKey())).append("/");
            }
            // 陆运
            if (EYesNo.YES.getIntValue() == productVo.getIsLandTrans()) {
                str.append(transactionMap.get(ETransportType.LAND.getKey()));
            }
            productVo.setTypeShippingMerge(str.toString());
            if (productVo.getShippingChannel() != null){
                productVo.setShippingChannelVal(transactionMap.get(EShippingChannel.getEnumByValue(productVo.getShippingChannel()).getKey()));
            }

            //     transactionServiceApi.readTransactionByCache(ETransactionKey.PRODUCT,product.getPkTransactionKey());

        }
        return getDataTable(list);
    }

    /**
     * 新增商品信息
     */
    @Log(module = EOperationModule.PRODUCT_SAVE, method = EOperationMethod.INSERT)
    @PostMapping("save")
    public AjaxResult save(@RequestBody ProductParams productParams) {
//        QueryWrapper<BdProduct> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("PRODUCT_CODE",productParams.getProductCode());
//        int count = productService.count(queryWrapper);
//        if (count > 0){
//            return AjaxResult.error(ProductMsgConstants.PRODUCT_NUMBER_ALREADY_EXISTS);
//        }
        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();
//       if (loginUser == null){
//            loginUser = new LoginUser();
//           loginUser.setUserId(1l);
//           loginUser.setDataCountry(1);
//       }
        // 排序重复校验
        QueryWrapper<BdProductExtend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SORT",productParams.getSort());
        int count = productExtendService.count(queryWrapper);
        if (count > 0){
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.SORT_REPEAT));
        }
        // 校验编号重复
        QueryWrapper<BdProduct> qw = new QueryWrapper<>();
        qw.eq("PRODUCT_CODE",productParams.getProductCode());
        int productCount = productService.count(qw);
        if (productCount > 0){
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_NUMBER_ALREADY_EXISTS));
        }
        // 校验productBas 每个国家单个只能设置一次
        if (productParams.getPkProductBas() != null){
            QueryWrapper<BdProduct> qe = new QueryWrapper<>();
            qe.eq("PK_PRODUCT_BAS",productParams.getPkProductBas());
            qe.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
            int pCount = productService.count(qe);
            if (pCount > 0){
                return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_OCCUPIED));
            }
        }

        return AjaxResult.success(productService.saveProduct(productParams, loginUser));
     //   return AjaxResult.success(productService.saveProductModifyRecord(productParams, loginUser,EModifyRecordType.PRODUCT_ADD.getValue()));
    }

    /**
     * 修改用户信息
     *
     * @param productParams
     * @return
     */
    @Log(module = EOperationModule.PRODUCT_UPDATE, method = EOperationMethod.UPDATE)
    @PostMapping("update")
    public AjaxResult update(@RequestBody ProductParams productParams) {
        // 查询订单编号是否重复
        BdProduct product = productService.getById(productParams.getPkId());
        if (!product.getProductCode().equals(productParams.getProductCode())) {
            QueryWrapper<BdProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PRODUCT_CODE", productParams.getProductCode());
            int count = productService.count(queryWrapper);
            if (count > 0) {
                return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_NUMBER_ALREADY_EXISTS));
            }
        }
        // 排序重复校验
        QueryWrapper<BdProductExtend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SORT",productParams.getSort());
        queryWrapper.notIn("PK_PRODUCT",productParams.getPkId());
        int count = productExtendService.count(queryWrapper);
        if (count > 0){
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.SORT_REPEAT));
        }
        // 暂时去掉 后期还要用
//        QueryWrapper<BdWaresDetail> qw = new QueryWrapper<BdWaresDetail>();
//        qw.eq("PK_PRODUCT",productParams.getPkId());
//        int waresCount = waresDetailService.count(qw);
//        if (waresCount > 0){
//            return AjaxResult.error(ProductMsgConstants.PRODUCT_WARES_IS_USER);
//        }
        LoginUser loginUser = userTokenService.getLoginUser();
        // 校验编号重复
        QueryWrapper<BdProduct> qe = new QueryWrapper<>();
        qe.eq("PRODUCT_CODE",productParams.getProductCode());
        qe.notIn("PK_ID",productParams.getPkId());
        int productCount = productService.count(qe);
        if (productCount > 0){
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_NUMBER_ALREADY_EXISTS));
        }
        // 校验productBas 每个国家单个只能设置一次
        if (productParams.getPkProductBas() != null){
            QueryWrapper<BdProduct> qw = new QueryWrapper<>();
            qw.eq("PK_PRODUCT_BAS",productParams.getPkProductBas());
            qw.notIn("PK_ID",productParams.getPkId());
            qw.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
            int pCount = productService.count(qw);
            if (pCount >= 1){
                return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_OCCUPIED));
            }
        }

        // 修改商品信息
        productService.updateProduct(productParams, loginUser);
    //    productService.saveProductModifyRecord(productParams, loginUser,EModifyRecordType.PRODUCT_ADD.getValue());
        return AjaxResult.success(productParams.getProductCode());
    }

    /**
     * 根据商品id 删除数据
     *
     * @param pkId
     * @return
     */
    @Log(module = EOperationModule.PRODUCT_DELETE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Integer pkId) {
        QueryWrapper<BdWaresDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_PRODUCT",pkId);
        int count = waresDetailService.count(queryWrapper);
        if (count > 0){
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_OCCUPIED));
        }
        productService.removeProduct(pkId);
        return AjaxResult.success();
    }

    /**
     * 修改商品启用禁用状态
     *
     * @param pkId
     * @param enable
     * @return
     */
    @Log(module = EOperationModule.PRODUCT_UPDATE_ENABLE, method = EOperationMethod.UPDATE)
    @PostMapping("/updateEnable/{pkId}/{enable}")
    public AjaxResult updateEnable(@PathVariable Integer pkId, @PathVariable Integer enable) {
        BdProduct product = new BdProduct();
        product.setPkId(pkId);
        product.setEnableState(enable);
        productService.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 = productService.getProduct(productId);
            if (product != null) {
                productSpecsInfo.setProductId(productId);
                productSpecsInfo.setProductName(product.getProductName());
                productSpecsInfo.setProductCode(product.getProductCode());
                productSpecsInfo.setProductCover(product.getCover());
                // 查询商品的规格类型
                HashSet<Integer> set = new LinkedHashSet<>();
                QueryWrapper<BdProductSpecs> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PK_PRODUCT", productId);
                //根据规格id 正序排列
                queryWrapper.orderByAsc("PK_ID");
                List<BdProductSpecs> list = productSpecsService.list(queryWrapper);
                for (BdProductSpecs productSpecs : list) {
                    set.add(productSpecs.getPkSpecsType());
                }
                List<SpecsTypeParam> specsTypeParamList = new ArrayList<>();
                for (Integer specsTypeId : set) {
                    BdSpecsType specsType = specsTypeService.getSpecsType(specsTypeId);
                    if (specsType != null) {
                        // 根据规格类型查询规格属性
                        SpecsTypeParam specsTypeParam = new SpecsTypeParam();
                        specsTypeParam.setPkId(specsType.getPkId());
                        specsTypeParam.setTypeName(specsType.getTypeName());
                        QueryWrapper<BdSpecs> specsWrapper = new QueryWrapper<>();
                        specsWrapper.eq("PK_SPECS_TYPE", specsTypeId);
                        List<BdSpecs> specsList = specsService.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();
            QueryWrapper<BdProduct> qw = new QueryWrapper<>();
            qw.eq("PRODUCT_CODE",productCode);
            BdProduct product = productService.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<>();
                QueryWrapper<BdProductSpecs> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PK_PRODUCT", product.getPkId());
                List<BdProductSpecs> list = productSpecsService.list(queryWrapper);
                for (BdProductSpecs productSpecs : list) {
                    set.add(productSpecs.getPkSpecsType());
                }
                List<SpecsTypeParam> specsTypeParamList = new ArrayList<>();
                for (Integer specsTypeId : set) {
                    BdSpecsType specsType = specsTypeService.getSpecsType(specsTypeId);
                    if (specsType != null) {
                        // 根据规格类型查询规格属性
                        SpecsTypeParam specsTypeParam = new SpecsTypeParam();
                        specsTypeParam.setPkId(specsType.getPkId());
                        specsTypeParam.setTypeName(specsType.getTypeName());
                        QueryWrapper<BdSpecs> specsWrapper = new QueryWrapper<>();
                        specsWrapper.eq("PK_SPECS_TYPE", specsTypeId);
                        List<BdSpecs> specsList = specsService.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 = productService.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<>();
                QueryWrapper<BdProductSpecs> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PK_PRODUCT", productId);
                //根据规格id 正序排列
                queryWrapper.orderByAsc("PK_ID");
                List<BdProductSpecs> list = productSpecsService.list(queryWrapper);
                for (BdProductSpecs productSpecs : list) {
                    set.add(productSpecs.getPkSpecsType());
                }
                List<SpecsTypeParam> specsTypeParamList = new ArrayList<>();
                for (Integer specsTypeId : set) {
                    BdSpecsType specsType = specsTypeService.getSpecsType(specsTypeId);
                    if (specsType != null) {
                        // 根据规格类型查询规格属性
                        SpecsTypeParam specsTypeParam = new SpecsTypeParam();
                        specsTypeParam.setPkId(specsType.getPkId());
                        specsTypeParam.setTypeName(specsType.getTypeName());
                        QueryWrapper<BdSpecs> specsWrapper = new QueryWrapper<>();
                        specsWrapper.eq("PK_SPECS_TYPE", specsTypeId);
                        List<BdSpecs> specsList = specsService.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 = productService.getByProductId(productId);
        if (product == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_NOT_REQUIRED));
        }
        ProductParams productParams = BeanUtil.copyProperties(product, ProductParams.class);
        BdProductExtend productExtend = productExtendService.getProductExtend(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());
        QueryWrapper<BdProductSpecs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_PRODUCT", productId);
        List<Integer> pkSpecsType = new ArrayList<>();
        List<BdProductSpecs> productSpecList = productSpecsService.list(queryWrapper);
        for (BdProductSpecs bdProductSpecs : productSpecList) {
            pkSpecsType.add(bdProductSpecs.getPkSpecsType());
        }
        productParams.setPkSpecsType(pkSpecsType);
        productParams.setProductBasName(product.getProductBasName());
        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);
            }
        }
        Map<String, String> transactionMap = transactionCommonService.exportEnumTransaction(ETransportType.values(), EYesNo.values(), EShippingChannel.values());

        productParams.setPkCountry(SecurityUtils.getPkCountry());
        List<ProductVo> list = productService.selectByQueryList(productParams);
        for (ProductVo productVo : list) {
            BdClassify classify = classifyService.getClassify(productVo.getPkClassify());
            if (classify != null) {
                productVo.setClassifyName(classify.getName());
            }

            // 发货类型翻译
            productVo.setShippingChannelVal(transactionMap.get(EShippingChannel.getEnumByValue(productVo.getShippingChannel()).getKey()));
            // 单位是配置的，不能走枚举翻译
//            //单位翻译翻译
//            productVo.setUnitVal(transactionMap.get(EUnit.getEnumByValue(productVo.getUnit()).getKey()));
            // 产品属性翻译
            if (productVo.getProductAttr() != null) {
                productVo.setProductAttrVal(transactionMap.get(EProductAttrType.getEnumByValue(productVo.getProductAttr()).getKey()));
            }
            // 礼包翻译
            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(transactionMap.get(ETransportType.OCEAN.getKey())).append("/");
            }
            //空运
            if (EYesNo.YES.getIntValue() == productVo.getIsAirTrans()) {
                str.append(transactionMap.get(ETransportType.AIR.getKey())).append("/");
            }
            // 陆运
            if (EYesNo.YES.getIntValue() == productVo.getIsLandTrans()) {
                str.append(transactionMap.get(ETransportType.LAND.getKey()));
            }
            productVo.setTypeShippingMerge(str.toString());
            //     transactionServiceApi.readTransactionByCache(ETransactionKey.PRODUCT,product.getPkTransactionKey());

        }
        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) {
        productService.productMove(productId, productMoveType);
        return AjaxResult.success();
    }

    /**
     * 置顶
     *
     * @param productId
     * @return
     */
    @GetMapping("floating-roof")
    public AjaxResult floatingRoof(Integer productId) {
        productService.floatingRoof(productId);
        return AjaxResult.success();
    }


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

    /**
     * 查询全部产品
     *
     * @return
     */
    @GetMapping("/find-all")
    public TableDataInfo findAll(Integer pkCountry,String query) {
        if (null == pkCountry) {
            pkCountry = SecurityUtils.getPkCountry();
        }
        List<BdProductExt> bdProductExts = productService.queryProductExistSpecs(pkCountry, query);
        for (BdProductExt bdProductExt : bdProductExts) {
            bdProductExt.setPkProduct(bdProductExt.getPkId());
        }
        return getDataTable(bdProductExts);
    }
    /**
     * 查询产品规格
     * @param pkProduct
     * @return
     */
    @GetMapping("get-product-specs")
    public AjaxResult getProductSpecs(Integer pkProduct){

        List<ProductSpecsVo> productSpecsList = productSpecsService.selectByList(pkProduct);
        for (ProductSpecsVo productSpecsVo : productSpecsList) {
            QueryWrapper<BdSpecs> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PK_SPECS_TYPE",productSpecsVo.getPkSpecsType());
            List<BdSpecs> specsList = specsService.list(queryWrapper);
            productSpecsVo.setSpecsList(specsList);
        }

        return AjaxResult.success(productSpecsList);
    }

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

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

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



    /**
     * 根据产品编号查询产品信息
     * @param productCode
     * @return
     */
    @GetMapping("get-product-info")
    public AjaxResult getProductInfo(String productCode){

        return AjaxResult.success(productService.getByProductCodeInfo(productCode, SecurityUtils.getPkCountry()));
    }
}
