package com.storlead.sales.service.impl.sku;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.storlead.framework.common.constant.DSConstants;
import com.storlead.framework.common.util.DateUtil;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.util.sku.constants.SsdProductConstants;
import com.storlead.sales.util.sku.constants.SsdSkuConstants;
import com.storlead.sales.pojo.entity.sku.SsdSku;
import com.storlead.sales.pojo.vo.sku.*;
import com.storlead.sales.config.SsdSkuConfigProperties;
import com.storlead.sales.mapper.sku.CurrencyRateMapper;
import com.storlead.sales.mapper.sku.SsdSkuMapper;
import com.storlead.sales.service.sku.SsdSkuService;
import com.storlead.sales.util.SsdSkuPriceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@DS(DSConstants.DATASOURCE_MANAGEMENT)
@Slf4j
public class SsdSkuServiceImpl extends ServiceImpl<SsdSkuMapper, SsdSku> implements SsdSkuService {

    @Resource
    private CurrencyRateMapper currencyRateMapper;
    @Resource
    private SsdSkuConfigProperties ssdSkuConfigProperties;
    @Override
    public Result list(SsdSkuVO ssdSku) {

        if (StringUtils.isBlank(ssdSku.getSsdSkuCode())) {
            ssdSku.setSsdSkuCode(null);
        }
        if (StringUtils.isBlank(ssdSku.getConnectorType())) {
            ssdSku.setConnectorType(null);
        }
        if (StringUtils.isBlank(ssdSku.getProductSize())) {
            ssdSku.setProductSize(null);
        }
        if (StringUtils.isBlank(ssdSku.getConnectorProtocol())) {
            ssdSku.setConnectorProtocol(null);
        }
        if (StringUtils.isBlank(ssdSku.getCoreChipType())) {
            ssdSku.setCoreChipType(null);
        }
        if (StringUtils.isBlank(ssdSku.getProductLevel())) {
            ssdSku.setProductLevel(null);
        }
        if (StringUtils.isBlank(ssdSku.getFlashChipType())) {
            ssdSku.setFlashChipType(null);
        }
        if (StringUtils.isBlank(ssdSku.getCapacity())) {
            ssdSku.setCapacity(null);
        }

        Page<SsdSkuVO> page = new Page<>(ssdSku.getPageIndex(), ssdSku.getPageSize());
        ssdSku.setPageIndex((ssdSku.getPageIndex() - 1) * ssdSku.getPageSize());
        List<SsdSkuVO> ssdSkuVOList = baseMapper.list(ssdSku);
        for (SsdSkuVO ssdSkuVO : ssdSkuVOList){
            ssdSkuVO.setForceOpenPhysicalDestroyTemp(ssdSkuVO.getForceOpenPhysicalDestroy());
            ssdSkuVO.setForceOpenLogicDestroyTemp(ssdSkuVO.getForceOpenLogicDestroy());
            ssdSkuVO.setForceOpenAesEncryptTemp(ssdSkuVO.getForceOpenAesEncrypt());
            ssdSkuVO.setForceOpenRapidBackupTemp(ssdSkuVO.getForceOpenRapidBackup());
        }
        page.setRecords(ssdSkuVOList);
        page.setTotal(baseMapper.total(ssdSku));
        return Result.result(page);
    }

    @Override
    public Result detail(SsdSkuVO ssdSkuVO) {

        return Result.result(baseMapper.detail(ssdSkuVO));
    }

    @Override
    public Result configSsdSkuJson() {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("connectorType", baseMapper.queryDataDictCodeList(SsdProductConstants.SSD_PRODUCT_CONNECTOR_TYPE));
        jsonObject.put("productSize", baseMapper.queryDataDictCodeList(SsdProductConstants.SSD_PRODUCT_SIZE));
        jsonObject.put("connectorProtocol", baseMapper.queryDataDictCodeList(SsdProductConstants.SSD_PRODUCT_CONNECTOR_PROTOCOL));
        jsonObject.put("coreChipType", baseMapper.queryDataDictCodeList(SsdProductConstants.SSD_PRODUCT_CORE_CHIP_TYPE));
        jsonObject.put("productLevel", baseMapper.queryDataDictCodeList(SsdProductConstants.SSD_PRODUCT_LEVEL));
        jsonObject.put("flashChipType", baseMapper.queryDataDictCodeList(SsdProductConstants.SSD_PRODUCT_FLASH_CHIP_TYPE));
        jsonObject.put("capacity", baseMapper.queryDataDictCodeList(SsdProductConstants.SSD_PRODUCT_CAPACITY));
        return Result.result(jsonObject);

    }

    @Override
    public BigDecimal getRate() {
        BigDecimal uSDToCNYRate = currencyRateMapper.queryCurrencyRateUSDToCNY();
        return uSDToCNYRate;
    }

    @Override
    public Result ssdStepOneGetSkuDetail(MakeQuotationSkuDetailCNENVO skuDetailParams){

        log.info(">>>>> MakeQuotationSkuDetailCNENVO start ");
        long startTime = System.currentTimeMillis();

        if (skuDetailParams.getForceOpenAesEncrypt()){
            skuDetailParams.setNewOpenAesEncrypt(1);
        }else {
            skuDetailParams.setNewOpenAesEncrypt(2);
        }
        if (skuDetailParams.getForceOpenPhysicalDestroy()){
            skuDetailParams.setNewOpenPhysicalDestroy(1);
        }else {
            skuDetailParams.setNewOpenPhysicalDestroy(2);
        }
        if (skuDetailParams.getForceOpenLogicDestroy()){
            skuDetailParams.setNewOpenLogicDestroy(1);
        }else {
            skuDetailParams.setNewOpenLogicDestroy(2);
        }
        if (skuDetailParams.getForceOpenRapidBackup()){
            skuDetailParams.setNewOpenRapidBackup(1);
        }else {
            skuDetailParams.setNewOpenRapidBackup(2);
        }

//        skuDetailParams.setOpenAesEncrypt(skuDetailParams.getForceOpenAesEncrypt());
//        skuDetailParams.setOpenPhysicalDestroy(skuDetailParams.getOpenPhysicalDestroy());
//        skuDetailParams.setOpenRapidBackup(skuDetailParams.getOpenRapidBackup());
//        skuDetailParams.setOpenLogicDestroy(skuDetailParams.getOpenLogicDestroy());

        MakeOrderSkuDetailVO skuDetail = this.getMakeOrderSkuDetail(skuDetailParams);
        BigDecimal productCount = BigDecimal.valueOf(skuDetailParams.getProductCount());

        if (skuDetail == null) {
            return Result.error("输入数量超出sku数量配置范围，如有疑问，请找sku管理人员处理。");
        }
        if (skuDetailParams.getOpenPhysicalDestroy() != null && skuDetailParams.getOpenPhysicalDestroy().intValue() == SsdSkuConstants.OPEN_PHYSICALDESTROY) {
            // 表示选择了开通物理销毁的功能
            if (skuDetail.getOpenPhysicalDestroy().intValue() == SsdSkuConstants.NOT_OPEN_PHYSICALDESTROY) {
                return Result.error("该配置不支持物理销毁功能");
            }
        }
        // 逻辑销毁
        if (skuDetailParams.getOpenLogicDestroy() != null && skuDetailParams.getOpenLogicDestroy().intValue() == SsdSkuConstants.OPEN_LOGICDESTROY) {
            // 表示选择了开通物理销毁的功能
            if (skuDetail.getOpenLogicDestroy().intValue() == SsdSkuConstants.NOT_OPEN_LOGICDESTROY) {
                return Result.error("该配置不支持逻辑销毁功能");
            }

        }
        // AES加密
        if (skuDetailParams.getOpenAesEncrypt() != null && skuDetailParams.getOpenAesEncrypt().intValue() == SsdSkuConstants.OPEN_AESENCRYPT) {
            // 表示选择了开通物理销毁的功能
            if (skuDetail.getOpenAesEncrypt().intValue() == SsdSkuConstants.NOT_OPEN_AESENCRYPT) {
                return Result.error("该配置不支持AES加密功能");
            }

        }
        // r-Backup断电保护
        if (skuDetailParams.getOpenRapidBackup() != null && skuDetailParams.getOpenRapidBackup().intValue() == SsdSkuConstants.OPEN_RAPIDBACKUP) {
            // 表示选择了开通物理销毁的功能
            if (skuDetail.getOpenRapidBackup().intValue() == SsdSkuConstants.NOT_OPEN_RAPIDBACKUP) {
                return Result.error("该配置不支持r-Backup断电保护功能");
            }
        }

        Map<String, Object> resultMap = new HashMap<>(20);
        //RATE
        BigDecimal USDToCNYRate = currencyRateMapper.queryCurrencyRateUSDToCNY();

        // 总订单金额 不含税
        BigDecimal orderAmt = skuDetail.getSkuPrice();

        if (orderAmt != null && USDToCNYRate != null) {

            //汇率
            resultMap.put("rate", USDToCNYRate);

            //单价CNY含税
            BigDecimal unitPriceCNY = SsdSkuPriceUtil.getSsdSkuTaxUnitPrice(skuDetail,
                    skuDetailParams.getNewOpenPhysicalDestroy(),
                    skuDetailParams.getNewOpenLogicDestroy(),
                    skuDetailParams.getNewOpenAesEncrypt(),
                    skuDetailParams.getNewOpenRapidBackup(), ssdSkuConfigProperties);
            //单价CNY不含税
            BigDecimal skuPriceCNYWithNoRate = skuDetail.getSkuunitPrice();

            //单价USD不含税 CNY / USDtoCNY RATE = 不含税USD
            BigDecimal unitPriceUSD = skuPriceCNYWithNoRate.divide(USDToCNYRate, 2, BigDecimal.ROUND_HALF_UP);

//            //单价CNY含税
//            BigDecimal unitPriceCNY = skuDetailParams.getUnitPriceCNYActual();
//            //单价CNY不含税
//            BigDecimal skuPriceCNYWithNoRate = skuDetailParams.getSkuPriceCNYWithNoRateActual();
//
//            //单价USD不含税 CNY / USDtoCNY RATE = 不含税USD
//            BigDecimal unitPriceUSD = skuDetailParams.getUnitPriceUSDActual();

            //总价 CNY含税
            resultMap.put("skuPriceCNY", unitPriceCNY.multiply(productCount));
            //总价 CNY不含税
            resultMap.put("skuPriceCNYWithNoRate", skuPriceCNYWithNoRate.multiply(productCount));

//            //总价 CNY含税
//            resultMap.put("skuPriceCNY", skuDetailParams.getSkuPriceCNYActual());
//            //总价 CNY不含税
//            resultMap.put("skuPriceCNYWithNoRate", skuDetailParams.getSkuPriceCNYWithNoRateActual());

            resultMap.put("additionalSurcharges", skuDetail.getAdditionalSurcharges());

            // 总订单金额USD = CNY / USDtoCNY RATE = 不含税USD
            BigDecimal orderAmtUSD = unitPriceUSD.multiply(productCount);
//            BigDecimal orderAmtUSD = skuDetailParams.getSkuPriceUSD();
            resultMap.put("skuPriceUSD", orderAmtUSD);

            //单价CNY含税
            resultMap.put("unitPriceCNY", unitPriceCNY);
            //单价CNY不含税
            resultMap.put("unitPriceCNYWithNoRate", skuPriceCNYWithNoRate);
            //单价USD不含税
            resultMap.put("unitPriceUSD", unitPriceUSD);

            // 计算定金金额
            BigDecimal earnestAmt = orderAmt.multiply(BigDecimal.valueOf(0.3)).setScale(2, BigDecimal.ROUND_HALF_UP);
            resultMap.put("earnestAmt", earnestAmt);
            resultMap.put("leftAmt", orderAmt.subtract(earnestAmt));
        }

        resultMap.put("deliveryDays", skuDetail.getEstimateDay());
        resultMap.put("ssdSkuId", skuDetail.getSsdSkuId());
        resultMap.put("ssdSkuCode", skuDetail.getSsdSkuCode());
        resultMap.put("connectorType", skuDetail.getConnectorType());
        resultMap.put("productSize", skuDetail.getProductSize());
        resultMap.put("connectorProtocol", skuDetail.getConnectorProtocol());
        resultMap.put("openPhysicalDestroy", skuDetailParams.getOpenPhysicalDestroy());
        resultMap.put("openLogicDestroy", skuDetailParams.getOpenLogicDestroy());
        resultMap.put("openAesEncrypt", skuDetailParams.getOpenAesEncrypt());
        resultMap.put("openRapidBackup", skuDetailParams.getOpenRapidBackup());

//        resultMap.put("forceOpenPhysicalDestroy", skuDetailParams.getForceOpenPhysicalDestroy());
//        resultMap.put("forceOpenLogicDestroy", skuDetailParams.getForceOpenLogicDestroy());
//        resultMap.put("forceOpenAesEncrypt", skuDetailParams.getForceOpenAesEncrypt());
//        resultMap.put("forceOpenRapidBackup", skuDetailParams.getForceOpenRapidBackup());

        resultMap.put("datasheetName", skuDetail.getDatasheetName());
        resultMap.put("datasheetUrl", skuDetail.getDatasheetUrl());
        String zhCNTaxPriceRate = ssdSkuConfigProperties.getZhCNTaxPriceRate().multiply(BigDecimal.valueOf(100)).stripTrailingZeros().toString();
        resultMap.put("zhCNTaxPriceRate", zhCNTaxPriceRate+"%");

        resultMap.put("randomId", skuDetailParams.getRandomId());
        resultMap.put("functionRemarks", skuDetailParams.getFunctionRemarks());
        resultMap.put("specificationRemarks", skuDetailParams.getSpecificationRemarks());

        long endTime = System.currentTimeMillis();
        log.info(">>>>> MakeQuotationSkuDetailCNENVO end run time :{} ms", (endTime - startTime));
        return Result.result(resultMap);
    }

    public Result ssdStepOneGetSkuDetailDownload(MakeQuotationSkuDetailCNENVO skuDetailParams){

        log.info(">>>>> MakeQuotationSkuDetailCNENVO start ");
        long startTime = System.currentTimeMillis();

        if (skuDetailParams.getForceOpenAesEncrypt()){
            skuDetailParams.setNewOpenAesEncrypt(1);
        }else {
            skuDetailParams.setNewOpenAesEncrypt(2);
        }
        if (skuDetailParams.getForceOpenPhysicalDestroy()){
            skuDetailParams.setNewOpenPhysicalDestroy(1);
        }else {
            skuDetailParams.setNewOpenPhysicalDestroy(2);
        }
        if (skuDetailParams.getForceOpenLogicDestroy()){
            skuDetailParams.setNewOpenLogicDestroy(1);
        }else {
            skuDetailParams.setNewOpenLogicDestroy(2);
        }
        if (skuDetailParams.getForceOpenRapidBackup()){
            skuDetailParams.setNewOpenRapidBackup(1);
        }else {
            skuDetailParams.setNewOpenRapidBackup(2);
        }

        MakeOrderSkuDetailVO skuDetail = this.getMakeOrderSkuDetail(skuDetailParams);
        BigDecimal productCount = BigDecimal.valueOf(skuDetailParams.getProductCount());

        if (skuDetail == null) {
            return Result.error("输入数量超出sku数量配置范围，如有疑问，请找sku管理人员处理。");
        }
        if (skuDetailParams.getOpenPhysicalDestroy() != null && skuDetailParams.getOpenPhysicalDestroy().intValue() == SsdSkuConstants.OPEN_PHYSICALDESTROY) {
            // 表示选择了开通物理销毁的功能
            if (skuDetail.getOpenPhysicalDestroy().intValue() == SsdSkuConstants.NOT_OPEN_PHYSICALDESTROY) {
                return Result.error("该配置不支持物理销毁功能");
            }
        }
        // 逻辑销毁
        if (skuDetailParams.getOpenLogicDestroy() != null && skuDetailParams.getOpenLogicDestroy().intValue() == SsdSkuConstants.OPEN_LOGICDESTROY) {
            // 表示选择了开通物理销毁的功能
            if (skuDetail.getOpenLogicDestroy().intValue() == SsdSkuConstants.NOT_OPEN_LOGICDESTROY) {
                return Result.error("该配置不支持逻辑销毁功能");
            }

        }
        // AES加密
        if (skuDetailParams.getOpenAesEncrypt() != null && skuDetailParams.getOpenAesEncrypt().intValue() == SsdSkuConstants.OPEN_AESENCRYPT) {
            // 表示选择了开通物理销毁的功能
            if (skuDetail.getOpenAesEncrypt().intValue() == SsdSkuConstants.NOT_OPEN_AESENCRYPT) {
                return Result.error("该配置不支持AES加密功能");
            }

        }
        // r-Backup断电保护
        if (skuDetailParams.getOpenRapidBackup() != null && skuDetailParams.getOpenRapidBackup().intValue() == SsdSkuConstants.OPEN_RAPIDBACKUP) {
            // 表示选择了开通物理销毁的功能
            if (skuDetail.getOpenRapidBackup().intValue() == SsdSkuConstants.NOT_OPEN_RAPIDBACKUP) {
                return Result.error("该配置不支持r-Backup断电保护功能");
            }
        }

        Map<String, Object> resultMap = new HashMap<>(20);
        //RATE
        BigDecimal USDToCNYRate = currencyRateMapper.queryCurrencyRateUSDToCNY();

        // 总订单金额 不含税
        BigDecimal orderAmt = skuDetail.getSkuPrice();

        if (orderAmt != null && USDToCNYRate != null) {

            //汇率
            resultMap.put("rate", USDToCNYRate);


            //单价CNY含税
            BigDecimal unitPriceCNY = skuDetailParams.getUnitPriceCNYActual();
            //单价CNY不含税
            BigDecimal unitPriceCNYWithNoRateActual = skuDetailParams.getUnitPriceCNYWithNoRateActual();

            //单价USD不含税 CNY / USDtoCNY RATE = 不含税USD
            BigDecimal unitPriceUSD = skuDetailParams.getUnitPriceUSDActual();


            //总价 CNY含税
            resultMap.put("skuPriceCNY", skuDetailParams.getSkuPriceCNYActual());
            //总价 CNY不含税
            resultMap.put("skuPriceCNYWithNoRate", skuDetailParams.getSkuPriceCNYWithNoRateActual());
            resultMap.put("skuPriceCNYWithNoRateActual", skuDetailParams.getSkuPriceCNYWithNoRateActual());

            resultMap.put("additionalSurcharges", skuDetail.getAdditionalSurcharges());

            // 总订单金额USD = CNY / USDtoCNY RATE = 不含税USD
//            BigDecimal orderAmtUSD = skuDetailParams.getSkuPriceUSD();
            BigDecimal orderAmtUSD = skuDetailParams.getSkuPriceUSDActual();
            resultMap.put("skuPriceUSD", orderAmtUSD);

            //单价CNY含税
            resultMap.put("unitPriceCNY", unitPriceCNY);
            //单价CNY不含税
            resultMap.put("unitPriceCNYWithNoRate", unitPriceCNYWithNoRateActual);
            resultMap.put("unitPriceCNYWithNoRateActual", unitPriceCNYWithNoRateActual);
            //单价USD不含税
            resultMap.put("unitPriceUSD", unitPriceUSD);

            // 计算定金金额
            BigDecimal earnestAmt = orderAmt.multiply(BigDecimal.valueOf(0.3)).setScale(2, BigDecimal.ROUND_HALF_UP);
            resultMap.put("earnestAmt", earnestAmt);
            resultMap.put("leftAmt", orderAmt.subtract(earnestAmt));
        }

        resultMap.put("deliveryDays", skuDetail.getEstimateDay());
        resultMap.put("ssdSkuId", skuDetail.getSsdSkuId());
        resultMap.put("ssdSkuCode", skuDetail.getSsdSkuCode());
        resultMap.put("connectorType", skuDetail.getConnectorType());
        resultMap.put("productSize", skuDetail.getProductSize());
        resultMap.put("connectorProtocol", skuDetail.getConnectorProtocol());
        resultMap.put("openPhysicalDestroy", skuDetailParams.getOpenPhysicalDestroy());
        resultMap.put("openLogicDestroy", skuDetailParams.getOpenLogicDestroy());
        resultMap.put("openAesEncrypt", skuDetailParams.getOpenAesEncrypt());
        resultMap.put("openRapidBackup", skuDetailParams.getOpenRapidBackup());

//        resultMap.put("forceOpenPhysicalDestroy", skuDetailParams.getForceOpenPhysicalDestroy());
//        resultMap.put("forceOpenLogicDestroy", skuDetailParams.getForceOpenLogicDestroy());
//        resultMap.put("forceOpenAesEncrypt", skuDetailParams.getForceOpenAesEncrypt());
//        resultMap.put("forceOpenRapidBackup", skuDetailParams.getForceOpenRapidBackup());

        resultMap.put("datasheetName", skuDetail.getDatasheetName());
        resultMap.put("datasheetUrl", skuDetail.getDatasheetUrl());
        String zhCNTaxPriceRate = ssdSkuConfigProperties.getZhCNTaxPriceRate().multiply(BigDecimal.valueOf(100)).stripTrailingZeros().toString();
        resultMap.put("zhCNTaxPriceRate", zhCNTaxPriceRate+"%");

        resultMap.put("randomId", skuDetailParams.getRandomId());
        resultMap.put("functionRemarks", skuDetailParams.getFunctionRemarks());
        resultMap.put("specificationRemarks", skuDetailParams.getSpecificationRemarks());

        long endTime = System.currentTimeMillis();
        log.info(">>>>> MakeQuotationSkuDetailCNENVO end run time :{} ms", (endTime - startTime));
        return Result.result(resultMap);
    }

    @Override
    public MakeOrderSkuDetailVO getMakeOrderSkuDetail(MakeQuotationSkuDetailCNENVO skuDetailVO) {
        MakeOrderSkuDetailVO resultSkuDetailVO = baseMapper.queryMakeOrderSkuDetailBy(skuDetailVO);

        MakeOrderSkuDetailVO resultSkuDetailVOA = setMakeOrderSkuDetailVOAmt(skuDetailVO, resultSkuDetailVO);

        return resultSkuDetailVO;
    }

    @Override
    public Result downloadQuotationFile(MakeQuotationSkuDetailCNENVO skuDetailParams) {
        // 生成报价单

        //报价单的价格
//        Result result = this.ssdStepOneGetSkuDetail(skuDetailParams);
        Result result = this.ssdStepOneGetSkuDetailDownload(skuDetailParams);
        if (result == null) {
            return Result.error("查询不到对应的报价单");
        }

        Map<String, Object> priceMap = (Map<String, Object>) result.getResult();


        GenSsdOrderContractDataVO quotationFile = new GenSsdOrderContractDataVO();
        quotationFile.setYear(DateUtil.getCurrentDateYear());
        quotationFile.setMonth(DateUtil.getCurrentDateMonth());
        quotationFile.setDay(DateUtil.getCurrentDateDay());

        //产品型号
        quotationFile.setSkuCode(String.valueOf(priceMap.get("ssdSkuCode")));
        quotationFile.setSsdSkuCode(String.valueOf(priceMap.get("ssdSkuCode")));
        //产品数量
        quotationFile.setProductCount(String.valueOf(skuDetailParams.getProductCount()));
        //接口
        quotationFile.setConnectorType(String.valueOf(priceMap.get("connectorType")));
        //协议
        quotationFile.setConnectorProtocol(String.valueOf(priceMap.get("connectorProtocol")));
        //主控
        quotationFile.setCoreChipType(skuDetailParams.getCoreChipType());
        //芯片设计
        quotationFile.setFlashChipType(skuDetailParams.getFlashChipType());
        //容量
        quotationFile.setCapacity(skuDetailParams.getCapacity());
        //创建时间
        quotationFile.setCreatedTime(DateUtil.getStringByFormat(new Date(), "yyyy/MM/dd"));
        //物理销毁
        quotationFile.setOpenPhysicalDestroy((Integer) priceMap.get("openPhysicalDestroy"));
        //逻辑销毁
        quotationFile.setOpenLogicDestroy((Integer) priceMap.get("openLogicDestroy"));
        //AES加密
        quotationFile.setOpenAesEncrypt((Integer) priceMap.get("openAesEncrypt"));
        //R备份
        quotationFile.setOpenRapidBackup((Integer) priceMap.get("openRapidBackup"));
        quotationFile.setFunctionRemarks(String.valueOf(priceMap.get("functionRemarks")));
        quotationFile.setSpecificationRemarks(String.valueOf(priceMap.get("specificationRemarks")));
        // 增值税
        String zhCNTaxPriceRate = ssdSkuConfigProperties.getZhCNTaxPriceRate().multiply(BigDecimal.valueOf(100)).stripTrailingZeros().toString()+"%";
        quotationFile.setZhCNTaxPriceRate(zhCNTaxPriceRate);

        // CN VERSION
        if (skuDetailParams.getLanguageType().equals(1)) {

            //总价 CNY 含税
            quotationFile.setOrderAmt(String.valueOf(priceMap.get("skuPriceCNY")));
            //单价 CNY 含税
            quotationFile.setUnitPrice(String.valueOf(priceMap.get("unitPriceCNY")));

            //总价 CNY 不含税
            quotationFile.setSkuPriceCNYWithNoRateActual(String.valueOf(priceMap.get("skuPriceCNYWithNoRateActual")));
            //单价 CNY 不含税
            quotationFile.setUnitPriceCNYWithNoRateActual(String.valueOf(priceMap.get("unitPriceCNYWithNoRateActual")));

            //尺寸
            quotationFile.setProductSize(String.valueOf(priceMap.get("productSize")));
            //级别
            quotationFile.setProductLevel(skuDetailParams.getProductLevel());
            String openPLAR = "";
            String openPLAREn = "";
            //物理销毁
            if (skuDetailParams.getForceOpenPhysicalDestroy()) {
                quotationFile.setOpenP("开通");
                openPLAR = openPLAR + "物理销毁,";
                openPLAREn = openPLAREn + "Physical Destruction,";
            } else {
                quotationFile.setOpenP("不开通");
            }
            //逻辑销毁
            if (skuDetailParams.getForceOpenLogicDestroy()) {
                quotationFile.setOpenL("开通");
                openPLAR = openPLAR + "逻辑销毁,";
                openPLAREn = openPLAREn + "Logical Destruction,";
            } else {
                quotationFile.setOpenL("不开通");
            }
            //AES加密
            if (skuDetailParams.getForceOpenAesEncrypt()) {
                quotationFile.setOpenA("开通");
                openPLAR = openPLAR + "AES加密,";
                openPLAREn = openPLAREn + "AES Encryption,";
            } else {
                quotationFile.setOpenA("不开通");
            }
            //R备份
            if (skuDetailParams.getForceOpenRapidBackup()) {
                quotationFile.setOpenR("开通");
                openPLAR = openPLAR + "R备份,";
                openPLAREn = openPLAREn + "rBackup,";
            } else {
                quotationFile.setOpenR("不开通");
            }
            if (!ObjectUtils.isEmpty(openPLAR)){
                openPLAR =  openPLAR.substring(0,openPLAR.length()-1);
            }
            if (!ObjectUtils.isEmpty(openPLAREn)){
                openPLAREn =  openPLAREn.substring(0,openPLAREn.length()-1);
            }
            quotationFile.setOpenPLAR(openPLAR);
            quotationFile.setOpenPLAREn(openPLAREn);

            // EN VERSION
        } else if (skuDetailParams.getLanguageType().equals(2)) {
            if (quotationFile.getSkuCode().equals("外部产品")){
                quotationFile.setSkuCode("External Products");
            }
            if (quotationFile.getSkuCode().equals("其他费用")){
                quotationFile.setSkuCode("Other Expenses");
            }

            quotationFile.setOrderAmt(String.valueOf(priceMap.get("skuPriceUSD")));
            // 总价 USD
            quotationFile.setEarnestAmt(String.valueOf(priceMap.get("skuPriceUSD")));
            //单价 USD
            quotationFile.setUnitPrice(String.valueOf(priceMap.get("unitPriceUSD")));
            //尺寸
            quotationFile.setProductSize(priceMap.get("productSize").toString().replace("寸", "inch"));

            //级别
            String productLevel = skuDetailParams.getProductLevel();
            quotationFile.setProductLevel(skuDetailParams.getProductLevel());


            if (productLevel.equals("商业")) {
                quotationFile.setProductLevel("Commercial");
            } else if (productLevel.equals("工业")) {
                quotationFile.setProductLevel("Industrial");
            } else if (productLevel.equals("军工")) {
                quotationFile.setProductLevel("Military");
            }
            String openPLAREn = "";
            //物理销毁
            if (quotationFile.getOpenPhysicalDestroy() == 1) {
                quotationFile.setOpenP("YES");
                openPLAREn = openPLAREn + "Physical Destruction,";
            } else {
                quotationFile.setOpenP("NO");
            }
            //逻辑销毁
            if (quotationFile.getOpenLogicDestroy() == 1) {
                quotationFile.setOpenL("YES");
                openPLAREn = openPLAREn + "Logical Destruction,";
            } else {
                quotationFile.setOpenL("NO");
            }
            //AES加密
            if (quotationFile.getOpenAesEncrypt() == 1) {
                quotationFile.setOpenA("YES");
                openPLAREn = openPLAREn + "AES Encryption,";
            } else {
                quotationFile.setOpenA("NO");
            }
            //R备份
            if (quotationFile.getOpenRapidBackup() == 1) {
                quotationFile.setOpenR("YES");
                openPLAREn = openPLAREn + "rBackup,";
            } else {
                quotationFile.setOpenR("NO");
            }
            if (!ObjectUtils.isEmpty(openPLAREn)){
                openPLAREn =  openPLAREn.substring(0,openPLAREn.length()-1);
            }
            quotationFile.setOpenPLAREn(openPLAREn);
        }

        // 生成报价单
        // sku文件下载
        return Result.result(quotationFile);
    }

    /**
     * @param skuDetailParam  查询sku价格的参数
     * @param resultSkuDetail 返回当时sku的最终价格
     * @Author: YPZ
     * @Date: 2024/08/02 18:19
     * @Description: 设置sku 的价格
     */
    public MakeOrderSkuDetailVO setMakeOrderSkuDetailVOAmt(MakeQuotationSkuDetailCNENVO skuDetailParam, MakeOrderSkuDetailVO resultSkuDetail) {
        if (resultSkuDetail == null) {
            return null;
        }

        // (工人费用成本 + 物料费用成本 + 主控成本 + Flash 成本 + 可选功能成本 ) / (1 - 毛利率)
        BigDecimal orderAmt = BigDecimal.valueOf(0);
        BigDecimal productCount = BigDecimal.valueOf(skuDetailParam.getProductCount());

        // 计算成本
        // 计算sku 价格
        BigDecimal unitPrice = SsdSkuPriceUtil.getSsdSkuUnitPrice(
                resultSkuDetail,
                skuDetailParam.getNewOpenPhysicalDestroy(),
                skuDetailParam.getNewOpenLogicDestroy(),
                skuDetailParam.getNewOpenAesEncrypt(),
                skuDetailParam.getNewOpenRapidBackup()
        );

        // 计算生产天数
        int dayCount = SsdSkuPriceUtil.getSsdSkuEstimateDay(
                resultSkuDetail,
                skuDetailParam.getNewOpenPhysicalDestroy(),
                skuDetailParam.getNewOpenLogicDestroy(),
                skuDetailParam.getNewOpenAesEncrypt(),
                skuDetailParam.getNewOpenRapidBackup(),
                ssdSkuConfigProperties
        );


        int i = resultSkuDetail.getAdditionalSurcharges().compareTo(BigDecimal.ZERO);

        if(i == 1) {
            BigDecimal b = unitPrice.multiply(BigDecimal.valueOf(skuDetailParam.getProductCount()));
            BigDecimal c = b.add(resultSkuDetail.getAdditionalSurcharges());
            resultSkuDetail.setSkuPrice(c);
            resultSkuDetail.setSkuunitPrice(resultSkuDetail.getSkuPrice().divide(productCount, 2, BigDecimal.ROUND_HALF_UP));
        } else {
            resultSkuDetail.setSkuunitPrice(unitPrice);
            resultSkuDetail.setSkuPrice(unitPrice.multiply(BigDecimal.valueOf(skuDetailParam.getProductCount())));
        }
//        resultSkuDetail.setSkuPrice(unitPrice.multiply(BigDecimal.valueOf(skuDetailParam.getProductCount())));
        resultSkuDetail.setEstimateDay(dayCount);

        return resultSkuDetail;
    }

}
