/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：商品选择相关共通方法
 *		2.功能描述：首页 促销商品选择,购物车页面,开单选商品,退还补选商品
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		于继渤		  2022-09-01	                1.00		    	商品选择相关共通方法(方便维护)      
 *******************************************************************************/
const app = getApp()
const api = require('./api.js');
const Constants = require('./Constants.js');
const common = require('./common.js');
const util = require('./util.js');



/**
  * @desc : 打开商品
  * @author : 于继渤
  * @date : 2022/7/2 20:16
  */
function openAddItemInfoCommon(e, _this, page, type) {

    _this.setData({
        goodsSkuList: []
    })
    let item = e.currentTarget.dataset.item
    //促销类型
    let promotionType = e.currentTarget.dataset.item.promotionType
    item['priceStandardTemp'] = item.priceStandard
    //开单/退还补选商品页面
    if (page == 'choose-goods') {
        item.itemQuantity = 1
        //促销价
        item.pricePromotion = e.currentTarget.dataset.item.pricePromotion
        item.activityId = e.currentTarget.dataset.item.activityId
        item.warehouseId = app.globalData.user.whId
        item.whId = app.globalData.user.whId
        item.warehouseName = app.globalData.user.whName
        // 组合促销
        if (promotionType == Constants.PROMOTION_TYPE.COMBINED) {
            getCombinedByPriceId(e, _this);
            _this.setData({
                show1: false,
                show2: false,
                show3: false,
                show4: true,
                show5: false
            })
        }
        else {
            //非标淋浴房  2定制
            if (item.flgNonStandardType == 1 || item.flgNonStandardType == 2) {
                item.nonStandardList = []
                item.nonStandardArea = 1
                //加载默认spu非标参数数据
                getFlgNonStandardList(item, _this)
                _this.setData({
                    show7: true
                })
            }
            // 根据spuId获取sku相关信息
            getSkuBySpuId(e, _this);
            _this.setData({
                show1: true,
                show2: false,
                show3: false,
                show4: false,
                show5: false
            })
        }
        _this.setData({
            currentChoosedItem: item,
            showAdd: true,
        })


    }


    //购物车页面
    if (page == 'shopping') {
        item.itemQuantity = 1
        //促销价
        item.pricePromotion = e.currentTarget.dataset.item.pricePromotion
        item.activityId = e.currentTarget.dataset.item.activityId
        item.warehouseId = app.globalData.user.whId
        item.whId = app.globalData.user.whId
        item.warehouseName = app.globalData.user.whName
        // 组合促销
        if (promotionType == Constants.PROMOTION_TYPE.COMBINED) {
            getCombinedByPriceId(e, _this, type);
            _this.setData({
                show1: false,
                show2: false,
                show3: false,
                show4: true,
                show5: false
            })
        }
        else {
            //非标淋浴房
            if (item.flgNonStandardType == 1 || item.flgNonStandardType == 2) {
                item.nonStandardArea = 1
                item.nonStandardList = []
                //加载默认spu非标参数数据
                getFlgNonStandardList(item, _this)
                _this.setData({
                    show7: true
                })
            }
            // 根据spuId获取sku相关信息
            getSkuBySpuId(e, _this);
            _this.setData({
                show1: true,
                show2: false,
                show3: false,
                show4: false,
                show5: false
            })
        }
        _this.setData({
            currentChoosedItem: item,
            showAdd: true,
        })
    }
    //其他出库页面
    if (page == 'otherOutbound') {
        item.itemQuantity = 1
        _this.setData({
            currentChoosedItem: item,
            showAdd: true,
        })
    }
    //其他入库页面
    if (page == 'choose-sku-product') {
        item.itemQuantity = 1
        //非标淋浴房  2定制
        if (item.flgNonStandardType == 1 || item.flgNonStandardType == 2) {
            item.nonStandardList = []
            item.nonStandardArea = 1
            //加载默认spu非标参数数据
           // getFlgNonStandardList(item, _this)
            _this.setData({
                show7: true
            })
        }
        // 根据spuId获取sku相关信息
        //getSkuBySpuIdProduct(e, _this);
        _this.setData({
            show1: false,
            show2: false,
            show3: false,
            show4: false,
            show5: false,
            show5: true
        })
        _this.setData({
            currentChoosedItem: item,
            showAdd: true,
        })
    }
}


/**
* @desc : 选择非标参数事件
* @author : 于继渤
* @date : 2022/8/24 13:16
*/
function nonStandardTap(e, _this) {

    let currentChoosedItem = _this.data.currentChoosedItem
    let nonStandardList = currentChoosedItem.nonStandardList
    let pindex = e.currentTarget.dataset.pindex //父索引
    let indext = e.currentTarget.dataset.indext //子索引
    let itemt = e.currentTarget.dataset.itemt //子对象
    console.log('选择非标参数事件', currentChoosedItem)
    nonStandardList[pindex].options.forEach(res => {
        res.infotype = 'default'
    })
    if (itemt.infotype == 'info') {
        return
    }


    nonStandardList[pindex].options[indext].infotype = itemt.infotype == 'info' ? 'default' : 'info'

    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
    //设置非标匹配参数
    setNonStandardMatchingParameters(_this)
}

/**
* @desc : 设置非标匹配参数
* @author : 于继渤
* @date : 2022/8/24 13:16
*/
function setNonStandardMatchingParameters(_this) {
    //当前选择的商品信息
    let currentChoosedItem = _this.data.currentChoosedItem
    //非标参数 数组(根据参数不同对应不同数据)
    let nonStandardList = currentChoosedItem.nonStandardList
    //非标参数保存用的 json 数据
    let nonGlassList = []
    let skuData = currentChoosedItem.skuData
    if (nonStandardList && nonStandardList.length > 0) {
        //判断哪些按钮状态为info 代表当前选中
        nonStandardList.forEach(res => {
            if (res.options && res.options.length > 0) {
                //遍历可选中的参数
                res.options.forEach(item => {
                    if (item.infotype == 'info') {
                        //选中的数据
                        //编辑非标JSON
                        nonGlassList.push({
                            fieldId: res.fieldId,
                            fieldCode: res.fieldCode,
                            fieldName: res.fieldName,
                            option_code: item.code,
                            option_name: item.name,
                        })
                    }
                })
            }
        })
        currentChoosedItem.nonGlassList = nonStandardList
        //设置显示非标参数 (展示非标text)
        if (nonGlassList && nonGlassList.length > 0) {
            let nonGlassListShowTemp = []
            nonGlassList.forEach(it => {
                if (it.option_name && it.option_name != undefined) {
                    nonGlassListShowTemp.push(it.fieldName + '：' + it.option_name)
                }

            })
            //非标参数显示/匹配命中标价的数据
            currentChoosedItem.nonGlassListShow = nonGlassListShowTemp.join('，')

        }
        //设置当前sku的所有非标标价数组
        skuData.forEach(res => {

            if (res.skuId == currentChoosedItem.skuId) {

                //赋值
                currentChoosedItem.nonGlassPriceItems = res.nonGlassPriceItems
            }
        })
        //非标商品匹配标价
        matchingListPrice(currentChoosedItem, currentChoosedItem.nonGlassPriceItems, currentChoosedItem.nonGlassListShow, _this)

  

        _this.setData({
            currentChoosedItem: currentChoosedItem
        })
    }else{
        console.log('没有匹配SKU')
    }
}

/**
* @desc : 非标商品匹配标价
* @author : 于继渤
* @date : 2022/8/24 13:16
*/
function matchingListPrice(currentChoosedItem, nonGlassPriceItems, nonGlassListShow, _this) {
    console.log('currentChoosedItem', currentChoosedItem)
    //铝材颜色：亮光-不锈钢，工艺：蒙砂-正，定制柜系列：B系列
    if (nonGlassPriceItems && nonGlassPriceItems.length > 0) {
        let flgItemList = []  //匹配到的标价数据
        // console.log('已选参非标商参数',nonGlassListShow)
        nonGlassPriceItems.forEach(res => {

            let flag = true
            let nonGlassListShowArr = res.nonGlassListShowArr
            if (nonGlassListShowArr && nonGlassListShowArr.length > 0) {
                console.log('-----------------------')
                res.nonGlassListShowArr.forEach(item => {
                    let nonGlassListShowTwo = nonGlassListShow.split('，')

                    console.log('已选参数=> ', nonGlassListShow)
                    console.log('可用参数=> ', res.nonGlassListShow)
                    console.log('是否包含=> ', (!nonGlassListShowTwo.includes(item) || res.nonGlassListShowArr.length != nonGlassListShowTwo.length) ? '不包含->' : '包含->', item)
                    console.log('-----------------------')
                    if (!nonGlassListShowTwo.includes(item) || res.nonGlassListShowArr.length != nonGlassListShowTwo.length) {
                        flag = false
                    }
                })
            } else {
                flag = false
            }
            if (flag) {
                flgItemList.push(res)
            }
        })
        //如果匹配到数据 设置默认标价
        if (flgItemList.length > 0) {
            console.log('匹配到数据', flgItemList)
            currentChoosedItem.priceStandard = flgItemList[0].itemPrice //标价
            currentChoosedItem.priceSale = currentChoosedItem.priceStandard
            currentChoosedItem.itemAmount = currentChoosedItem.orderItemQuantity * currentChoosedItem.priceSale
            getCombinedDiscount(currentChoosedItem)
            //面积计算
            if (currentChoosedItem.nonStandardArea) {
                calculateFormulaItemAmount(currentChoosedItem, currentChoosedItem.nonStandardArea)
                _this.setData({
                    currentChoosedItem: currentChoosedItem
                })
            }
        } else {
            if (currentChoosedItem.flgNonStandardType == 1) {
                currentChoosedItem.priceStandard = currentChoosedItem.nonGlassPriceDef ? currentChoosedItem.nonGlassPriceDef : Number(0)
            }
            //匹配到sku  但是没有匹配到标价 但是有价格
            if (currentChoosedItem.nonGlassPriceItems && currentChoosedItem.nonGlassPriceItems.length > 0) {
                let objItems = currentChoosedItem.nonGlassPriceItems.filter(res => {
                    if (res.flgDefItem && (!res.nonGlassListShowArr || res.nonGlassListShowArr.length == 0)) {
                        return res;
                    }
                })
                if (objItems && objItems.length > 0) {
                    console.log('匹配到sku  但是没有匹配到标价 但是有价格')
                    currentChoosedItem.priceStandard = objItems[0].itemPrice
                } else {
                    //sku标价改为spu标价
                    console.log('sku标价改为spu标价')
                    currentChoosedItem.priceStandard = currentChoosedItem.skuData.length > 0 ? currentChoosedItem.skuData[0].priceStandard : currentChoosedItem.priceStandardtemp
                }


            }
            currentChoosedItem.priceSale = currentChoosedItem.priceStandard
            currentChoosedItem.itemAmount = currentChoosedItem.orderItemQuantity * currentChoosedItem.priceSale
            getCombinedDiscount(currentChoosedItem)
        }
    } else {
        console.log('没有设置非标参数')
        if (currentChoosedItem.flgNonStandardType == 1 || currentChoosedItem.flgNonStandardType == 2) {
            currentChoosedItem.priceStandard = Number(currentChoosedItem.priceStandardTemp ? currentChoosedItem.priceStandardTemp : 0)
        }

        currentChoosedItem.priceSale = currentChoosedItem.priceStandard
        currentChoosedItem.itemAmount = currentChoosedItem.orderItemQuantity * currentChoosedItem.priceSale
        getCombinedDiscount(currentChoosedItem)
    }
}

/**
* @desc : 非标商品选择事件
* @author : 于继渤
* @date : 2022/8/24 13:16
*/
function nonGlassSpuItemsTap(e, _this) {


    let currentChoosedItem = _this.data.currentChoosedItem
    let skuData = currentChoosedItem.skuData
    let indext = e.currentTarget.dataset.indext //子索引
    let itemt = e.currentTarget.dataset.itemt //子对象

    //初始化按钮状态
    skuData.forEach(res => {
        res.infotype = 'default'
    })
    let nonStandardList = []
    if (itemt.infotype == 'info') {
        return
    }
    //设置按钮状态
    skuData[indext].infotype = itemt.infotype == 'info' ? 'default' : 'info'
    // 设置默认skuId
    currentChoosedItem.defSkuId = itemt.skuId
    let nonStandardListFlag = true
    if (skuData[indext].infotype == 'info') {
        nonStandardList = skuData[indext].nonGlassSpuItems
        if (!skuData[indext].nonGlassSpuItems || skuData[indext].nonGlassSpuItems.length == 0) {
            if (currentChoosedItem.nonGlassList) {
                let nonStandardListCopy = util.copyObj(currentChoosedItem.nonGlassList)
                //没设置标价显示spu默认
                nonStandardList = nonStandardListCopy
                nonStandardListFlag = false
            }

        } else {
            console.log('没有匹配上SKU')
            currentChoosedItem.priceSale = itemt.nonGlassPriceDef
            currentChoosedItem.itemAmount = itemt.nonGlassPriceDef
        }
    }

    _this.setData({
        nonStandardListFlag: nonStandardListFlag
    })
    currentChoosedItem.nonStandardList = nonStandardList

    _this.setData({
        skuData: skuData,
        defSkuId: skuData[indext].skuId,
        currentChoosedItem: currentChoosedItem
    })

    //数据格式化
    chooseSkuItem(_this);
    //设置默认参数
    if (skuData && skuData.length > 0 && itemt.skuId) {
        setDefault(skuData, itemt.skuId, _this)
    }
    //没匹配到标价为0
    // if (!nonStandardListFlag   ) {
    //     currentChoosedItem.priceStandard = 0
    //     _this.setData({
    //         currentChoosedItem: currentChoosedItem
    //     })
    // }

}
/**
* @desc : 非标商品非标参数输入事件
* @author : 于继渤
* @date : 2022/8/24 13:16
*/
function bindInputIsScopeOne(e, _this) {

}

function bindBlurIsScopeOne(e, _this) {

    let currentChoosedItem = _this.data.currentChoosedItem
    let specialFormulaName = currentChoosedItem.specialFormulaName //面积公式
    let nonStandardList = currentChoosedItem.nonStandardList
    let index = e.currentTarget.dataset.pindex  //大数组所在索引
    let item = e.currentTarget.dataset.item
    let value = e.detail.value
    if (value) {
        value = value.replace(/\s/g, '')
    }

    // if (nonStandardList[index].fieldType == 3) {
    //   //数字类型判断最大值最小值
    //   let maxValue = Number(nonStandardList[index].maxValue)
    //   let minValue = Number(nonStandardList[index].minValue)
    //    if(Number(value) > maxValue){
    //     //大于最大值 为最大值
    //     nonStandardList[index].optionName = maxValue
    //     nonStandardList[index].optionCode = maxValue
    //   } else if(Number(value) < minValue){
    //     //小于最小值 为最小值 
    //     nonStandardList[index].optionName = minValue
    //     nonStandardList[index].optionCode = minValue
    //   } 
    //   else if (maxValue >= Number(value) >= minValue) {
    //     //最大值最小值之间
    //     nonStandardList[index].optionName = Number(value)
    //     nonStandardList[index].optionCode = Number(value)
    //   }
    // }else{
    //TODO  非标参数 ios复制问题  2023/03/13 于继渤
    if (nonStandardList[index].fieldType == 3) {
        console.log('面积公式输入参数', value)
        //数字类型
        if (!checkNumber(value)) {
            console.log('请输入正确数字->', value)
            nonStandardList[index].optionName = ""
            nonStandardList[index].optionCode = ""
        } else {
            nonStandardList[index].optionName = value
            nonStandardList[index].optionCode = value
        }

    } else {
        nonStandardList[index].optionName = value
        nonStandardList[index].optionCode = value
    }

    // }

    calculateFormula(_this, currentChoosedItem)
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
}

function checkNumber(theObj) {
    var reg = /^[0-9]+.?[0-9]*$/;
    if (reg.test(theObj)) {
        return true;
    }
    return false;
}
function bindBlurIsScopeOne_t(e, _this) {

    let currentChoosedItem = _this.data.currentChoosedItem
    let specialFormulaName = currentChoosedItem.specialFormulaName //面积公式
    let nonStandardList = currentChoosedItem.nonStandardList
    let index = e.currentTarget.dataset.pindex  //大数组所在索引
    let item = e.currentTarget.dataset.item
    let value = e.detail.value
    nonStandardList[index].optionName = value
    nonStandardList[index].optionCode = value
    calculateFormula_t(_this, currentChoosedItem)
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
}

/**
* @desc : 计算面积
* @author : 于继渤
* @date : 2022/9/21 13:16
*/
function calculateFormula(_this, currentChoosedItem) {

    let nonStandardList = currentChoosedItem.nonStandardList
    let itemAmount = currentChoosedItem.itemAmount
    currentChoosedItem.itemAmount_Temp = itemAmount
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
    let formula = currentChoosedItem.specialFormulaName.toUpperCase()//公式
    let param = {

    }
    let API_FLAG = true
    if (nonStandardList && nonStandardList.length > 0) {
        let list = nonStandardList.filter(item => {
            return item.isScope == 1
        })
        if (list && list.length > 0) {
            list.forEach(res => {
                // if (res.optionCode && res.optionCode != '' && res.optionCode != 0) {
                // if (res.fieldName.indexOf('W1') != -1) {
                //   param.W1 = Number(res.optionCode)
                // }
                // if (res.fieldName.indexOf('W2') != -1) {
                //   param.W2 = Number(res.optionCode)
                // }
                // if (res.fieldName.indexOf('H') != -1) {
                //   param.H = Number(res.optionCode)
                // }
                param[res.fieldName] = Number(res.optionCode) ? Number(res.optionCode) : null
                // } else {
                //     //其中输入没参数 不计算
                //     API_FLAG = false
                // }
                if (res.optionCode == "" || res.optionCode == null || res.optionCode == undefined) {
                    API_FLAG = false
                }
            })
        }
    }
    let paramData = {
        formula: formula,//公式
        param: param
    }
    if (API_FLAG) {
        api.request(Constants.BPM_FORMULA_APPRAVAL + 'calculate_formula', 'POST', paramData).then(res => {
            console.log(res)
            if (res.data.code == 200) {
                if (typeof res.data.data === 'number') {
                    //面积公式中包含【/1000】字符时，输入值不用除以1000，如不包括就要除以1000再计算
                    //现在面积公式【(W1/1000+2*W2/1000)*H1/1000】、【(W1+2*W2)*H1】混合存在
                    if (formula.indexOf('/1000') != -1) {
                        //包含
                        currentChoosedItem.nonStandardArea = res.data.data ? Number(res.data.data).toFixed(2) : 0
                    } else {
                        //不包含
                        currentChoosedItem.nonStandardArea = res.data.data ? Number(res.data.data / 1000).toFixed(2) : 0
                    }

                }
                if (currentChoosedItem.nonStandardArea) {
                    //计算金额
                    calculateFormulaItemAmount(currentChoosedItem, currentChoosedItem.nonStandardArea)
                }
                _this.setData({
                    currentChoosedItem: currentChoosedItem
                })
            }
        })
    }

}

function calculateFormula_t(_this, currentChoosedItem) {

    let nonStandardList = currentChoosedItem.nonStandardList
    let itemAmount = currentChoosedItem.itemAmount
    currentChoosedItem.itemAmount_Temp = itemAmount
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
    let formula = currentChoosedItem.specialFormulaName.toUpperCase()//公式
    let param = {

    }
    let API_FLAG = true
    if (nonStandardList && nonStandardList.length > 0) {
        let list = nonStandardList.filter(item => {
            return item.isScope == 1
        })
        if (list && list.length > 0) {
            console.log('面积接口', list)
            list.forEach(res => {
                // if (res.optionCode && res.optionCode != '' && res.optionCode != 0) {
                // if (res.fieldName.indexOf('W1') != -1) {
                //   param.W1 = Number(res.optionCode)
                // }
                // if (res.fieldName.indexOf('W2') != -1) {
                //   param.W2 = Number(res.optionCode)
                // }
                // if (res.fieldName.indexOf('H') != -1) {
                //   param.H = Number(res.optionCode)
                // }
                param[res.fieldName] = Number(res.optionCode) ? Number(res.optionCode) : null
                // } else {
                //     //其中输入没参数 不计算
                //     API_FLAG = false
                // }
                if (res.optionCode == "" || res.optionCode == null || res.optionCode == undefined) {
                    API_FLAG = false
                }
            })
        }
    }
    let paramData = {
        formula: formula,//公式
        param: param
    }
    if (API_FLAG) {
        api.request(Constants.BPM_FORMULA_APPRAVAL + 'calculate_formula', 'POST', paramData).then(res => {

            if (res.data.code == 200) {
                if (typeof res.data.data === 'number') {
                    currentChoosedItem.nonStandardArea = res.data.data ? Number(res.data.data / 1000).toFixed(2) : 0
                }
                if (currentChoosedItem.nonStandardArea) {
                    //计算金额
                    calculateFormulaItemAmount(currentChoosedItem, currentChoosedItem.nonStandardArea, true)
                }
                _this.setData({
                    currentChoosedItem: currentChoosedItem
                })
            }
        })
    }

}
//面积离焦事件
function bindBlurNonStantardArea(e, _this, flag) {
    let value = e.detail.value
    if (value) {
        value = value.replace(/\s/g, '')
    }
    let currentChoosedItem = _this.data.currentChoosedItem
    if (value) {
        currentChoosedItem.nonStandardArea = Number(value)

        //调用 面积计算合计金额     面积*数量*售价
        calculateFormulaItemAmount(currentChoosedItem, currentChoosedItem.nonStandardArea, flag)
    } else {
        currentChoosedItem.nonStandardArea = 0
    }
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
}
// 面积计算合计金额     面积*数量*售价
function calculateFormulaItemAmount(currentChoosedItem, value, flag) {
    if (flag) {
        if (currentChoosedItem.nonStandardArea) {
            currentChoosedItem.tItemAmount = Number(value * currentChoosedItem.itemQuantity * currentChoosedItem.priceSale).toFixed(2)
        } else {
            currentChoosedItem.tItemAmount = Number(currentChoosedItem.tItemQuantity * currentChoosedItem.priceSale)
        }
    } else {
        if (currentChoosedItem.nonStandardArea) {
            currentChoosedItem.itemAmount = Number(value * currentChoosedItem.itemQuantity * currentChoosedItem.priceSale).toFixed(2)
        } else {
            currentChoosedItem.itemAmount = Number(currentChoosedItem.itemQuantity * currentChoosedItem.priceSale).toFixed(2)
        }
    }

}




function bindInputNonStantardArea(e, _this) {

}
/**
  * @desc : 根据spuid查询非标参数
  * @author : 于继渤
  * @date : 2022/8/24 13:16
  */
function getFlgNonStandardList(item, _this) {
    let param = {
        spuId: item.spuId,
        cpId: app.globalData.user.cpId
    }
    api.request(Constants.GOODS_SPU_API + 'get_flg_non_standard_list', 'POST', param).then(res => {

        if (res.data.code == 200) {
            let data = res.data.data
            if (data && data.length > 0) {
                data.forEach(item => {
                    if (item.options && item.options.value && item.options.value != '[]') {
                        item.options = JSON.parse(item.options.value)
                        item.options.forEach(it => {
                            it.infotype = 'default'
                        })
                    }
                })
            }
            let dataTemp = util.copyObj(data)  //拷贝数据

            _this.setData({
                nonStandardList: dataTemp
            })

            item.nonGlassList = []
            item.nonGlassListShow = ''
            let data_nonStandardList = [...data]
            item.nonStandardList = data_nonStandardList
        }
    })
}

/**
* @desc :  根据spuId获取sku数据
* @date : 2022/9/1 13:49
* @author : 于继渤
*/
function getSkuBySpuId(e, _this) {
    _this.setData({
        skuloading: true
    })
    let item = e.currentTarget.dataset.item
    let param = {}

    // 要记录原始的标价、促销，活动Id
    item.standardIdSocial = item.standardIdSocial ? item.standardIdSocial : item.standardId
    item.promotionIdSocial = item.promotionIdSocial ? item.promotionIdSocial : item.promotionId
    item.activityIdSocial = item.activityIdSocial ? item.activityIdSocial : item.activityId
    param.spuId = item.spuId
    param.standardId = item.standardIdSocial
    param.promotionId = item.promotionIdSocial
    param.activityId = item.activityIdSocial

    param.cpId = app.globalData.user.cpId;
    param.orgId = app.globalData.user.orgId;

    param.customerId = app.globalData.user.sale2CustomerId ? app.globalData.user.sale2CustomerId : null
    api.request(Constants.GOODS_SPU_SPECS + 'select_sku_by_spu_id', 'POST', param).then(res => {
        if (res.data.code === 200) {
            let currentChoosedItem = _this.data.currentChoosedItem
            currentChoosedItem.nonGlassList = []
            currentChoosedItem.skuData = []
            let list = res.data
            // 获取默认的skuId
            let defSkuId = e.currentTarget.dataset.item.defSkuId;

            // 判断如果默认的skuId没有设置标价，那么要取价格最低的作为默认sku
            let filters = list.data.skuData.filter(it => it.skuId == defSkuId)
            if (filters && filters.length > 0) {
                if (filters[0].disabled) {
                    defSkuId = null;
                    item.pricePromotion = undefined;
                    item.priceStandard = undefined;
                    _this.setData({
                        defSkuId: null
                    })
                }
            }

            //判断规格是否有数据
            if (list.data.specsData && list.data.specsData.length == 0) {
                let flgNonStandardTypeFlag = false;
                list.data.skuData.forEach(item => {

                    //设置默认商品按钮
                    if (defSkuId == item.skuId) {

                        //设置默认显示商品按钮
                        item.infotype = 'info'
                        if (item.flgNonStandardType && item.flgNonStandardType != 0) {
                            flgNonStandardTypeFlag = true;

                            if (item.flgNonStandardType == 2) {

                            } else if (item.flgNonStandardType == 1) {
                                item.priceStandard = item.nonGlassPriceDef
                            }
                        }
                        // 有问题，直接跳过了促销价
                        // item.priceSale =  item.priceStandard ? item.priceStandard : 0
                        item.priceSale = item.priceSale ? item.priceSale : (item.priceStandard ? item.priceStandard : 0)
                        item.itemAmount = item.orderItemQuantity * item.priceSale

                        getCombinedDiscount(item)
                    }
                })
                if (flgNonStandardTypeFlag) {
                    _this.setData({
                        show6: true
                    })
                }
            } else {
                _this.setData({
                    show6: false
                })
            }
            if (list.data.specsData.length == 0 && list.data.skuData && list.data.skuData.length > 1) {
                _this.setData({
                    show6: true
                })
            }
            currentChoosedItem.skuData = list.data.skuData
            currentChoosedItem.specsData = list.data.specsData
            currentChoosedItem.skuSpecsList = list.data.specsData
            _this.setData({
                skuData: list.data.skuData,
                currentChoosedItem: currentChoosedItem,
                skuSpecsList: list.data.specsData,
                defSkuId: defSkuId,
            })

            setSkuData(_this);    // 把sku处理为相应的格式
            getSkuSpecs(defSkuId, _this); //判断规格是否可用
            if (e.currentTarget.dataset.item.flgNonStandardType == 1 || e.currentTarget.dataset.item.flgNonStandardType == 2) {
                //非标标价设置默认值
                setDefault(list.data.skuData, defSkuId, _this)
            }

        }
        // 设置赠品
        setFlgGift(_this);

        setTimeout(() => {
            _this.setData({
                skuloading: false
            })
        }, 100)
    })
}
/**
* @desc :  根据spuId获取sku数据
* @date : 2023年9月8日
* @author : 王英杰
*/
function getSkuBySpuIdProduct(e, _this) {
    _this.setData({
        skuloading: true
    })
    let item = e.currentTarget.dataset.item
    let param = {}

    param.spuId = item.spuId
    param.cpId = app.globalData.user.cpId;

    api.request(Constants.OTHER_IN + 'get_sku_for_other_entry', 'POST', param).then(res => {
        if (res.data.code === 200) {
            let currentChoosedItem = _this.data.currentChoosedItem
            currentChoosedItem.nonGlassList = []
            currentChoosedItem.skuData = []
            let list = res.data
            // 获取默认的skuId
            let defSkuId = e.currentTarget.dataset.item.defSkuId;

            // 判断如果默认的skuId没有设置标价，那么要取价格最低的作为默认sku
            let filters = list.data.skuData.filter(it => it.skuId == defSkuId)
            if (filters && filters.length > 0) {
                if (filters[0].disabled) {
                    defSkuId = null;
                    item.pricePromotion = undefined;
                    item.priceStandard = undefined;
                    _this.setData({
                        defSkuId: null
                    })
                }
            }

            //判断规格是否有数据
            if (list.data.specsData && list.data.specsData.length == 0) {
                let flgNonStandardTypeFlag = false;
                list.data.skuData.forEach(item => {

                    //设置默认商品按钮
                    if (defSkuId == item.skuId) {

                        //设置默认显示商品按钮
                        item.infotype = 'info'
                        if (item.flgNonStandardType && item.flgNonStandardType != 0) {
                            flgNonStandardTypeFlag = true;

                            if (item.flgNonStandardType == 2) {

                            } else if (item.flgNonStandardType == 1) {
                                item.priceStandard = item.nonGlassPriceDef
                            }
                        }
                        // 有问题，直接跳过了促销价
                        // item.priceSale =  item.priceStandard ? item.priceStandard : 0
                        item.priceSale = item.priceSale ? item.priceSale : (item.priceStandard ? item.priceStandard : 0)
                        item.itemAmount = item.orderItemQuantity * item.priceSale

                        getCombinedDiscount(item)
                    }
                })
                if (flgNonStandardTypeFlag) {
                    _this.setData({
                        show6: true
                    })
                }
            } else {
                _this.setData({
                    show6: false
                })
            }
            if (list.data.specsData.length == 0 && list.data.skuData && list.data.skuData.length > 1) {

                _this.setData({
                    show6: true
                })
            }

            currentChoosedItem.skuData = list.data.skuData
            currentChoosedItem.specsData = list.data.specsData
            currentChoosedItem.skuSpecsList = list.data.specsData
            _this.setData({
                skuData: list.data.skuData,
                currentChoosedItem: currentChoosedItem,
                skuSpecsList: list.data.specsData,
                defSkuId: defSkuId,
            })

            setSkuData(_this);    // 把sku处理为相应的格式
            getSkuSpecs(defSkuId, _this); //判断规格是否可用
            if (e.currentTarget.dataset.item.flgNonStandardType == 1 || e.currentTarget.dataset.item.flgNonStandardType == 2) {
                //非标标价设置默认值
                setDefault(list.data.skuData, defSkuId, _this)
            }

        }
        // 设置赠品
        setFlgGift(_this);

        setTimeout(() => {
            _this.setData({
                skuloading: false
            })
        }, 100)
    })
}


/**
* @desc :非标标价设置默认值
* @date : 2022/8/25 9:56
* @author : 于继渤
*/
function setDefault(skuData, defSkuId, _this) {

    //当前选中的商品非标参数数组
    let currentChoosedItem = _this.data.currentChoosedItem
    //校验数据不为空 否则报异常
    if (currentChoosedItem.nonStandardList && currentChoosedItem.nonStandardList.length > 0 && _this.data.nonStandardListFlag) {
        let list = []
        let obj = []
        //视图初始化非标标价组
        let nonStandardList = currentChoosedItem.nonStandardList
        //过滤出默认数据
        if (skuData && skuData.length > 0) {
            list = skuData.filter((item) => {
                return item.skuId == defSkuId;
            })
        }
        if (list.length > 0) {
            if (list[0].nonGlassPriceItems) {
                obj = list[0].nonGlassPriceItems.filter((item) => {
                    return item.flgDefItem == true && list[0].skuId == item.skuId;
                })
            }

        }
        //默认标价组

        if (obj.length > 0) {
            let nonGlassList = obj[0].nonGlassList ? obj[0].nonGlassList : {}

            nonStandardList.forEach(res => {
                let temp = nonGlassList.filter((item) => {
                    return item.fieldCode == res.fieldCode;
                })
                if (temp.length > 0) {

                    //过滤出 spu显示的非标参数与默认的非标参数并改变按钮状态
                    let options = res.options.filter((item) => {
                        return item.infotype = ((item.code == temp[0].optionCode) || (item.code == temp[0].option_code)) ? 'default' : '';
                    })
                    if (options && options.length > 0) {
                        options[0].infotype = 'info'
                    }
                }
            })

            //设置显示非标参数 (展示非标text)
            if (nonGlassList && nonGlassList.length > 0) {
                let nonGlassListShowTemp = []
                nonGlassList.forEach(it => {
                    if (it.optionName && it.optionName != undefined) {
                        nonGlassListShowTemp.push(it.fieldName + '：' + it.optionName)
                    }

                })
                //非标参数显示/匹配命中标价的数据
                currentChoosedItem.nonGlassListShow = nonGlassListShowTemp.join('，')
            }
            currentChoosedItem.pricePromotion = obj[0].itemPrice
        }


        currentChoosedItem.priceStandard = currentChoosedItem.priceStandard ? currentChoosedItem.priceStandard : 0
        currentChoosedItem.priceSale = currentChoosedItem.priceSale ? currentChoosedItem.priceSale : 0
        currentChoosedItem.itemAmount = currentChoosedItem.itemAmount ? currentChoosedItem.itemAmount : 0
        currentChoosedItem.nonStandardList = nonStandardList
        _this.setData({
            currentChoosedItem: currentChoosedItem
        })

    } else {

        _this.setData({
            currentChoosedItem: currentChoosedItem
        })
    }

}
/**
  * @desc : 根据组合促销Id获取促销信息
  * @date : 2022/7/2 16:49
  * @author : 周兴
  */
function getCombinedByPriceId(e, _this, type) {
    _this.setData({
        skuloading: true
    })
    let param = {}
    param.cpId = app.globalData.user.cpId;
    param.priceId = e.currentTarget.dataset.item.promotionId;
    param.orgId = app.globalData.user.orgId;
    api.request(Constants.PRICE_PROMOTION_ITEM + 'select_combined_by_price_id', 'POST', param).then(res => {
        _this.setData({
            loading: false
        })
        if (res.data.code === 200) {
            let currentChoosedItem = util.copyObj(_this.data.currentChoosedItem)
            let combinedData = res.data.data
            currentChoosedItem.combinedData = combinedData;
            currentChoosedItem.itemQuantity = 1;  // 数量默认为1
            currentChoosedItem.itemAmount = currentChoosedItem.priceSale // 金额默认等于销售价格
            currentChoosedItem.discountPromotion = 100; // 默认为100
            if (combinedData.combinedItem != null && combinedData.combinedItem.length > 0) {
                currentChoosedItem.combinedData.combinedItem.forEach(item => {
                    if (item.cpFlgGift) {
                        item.flgGift = true
                        item.priceSale_Temp = item.priceSale
                        item.itemAmount_Temp = item.itemAmount
                        item.discountStandard_Temp = item.discountStandard
                        item.discountPromotion_Temp = item.discountPromotion
                        item.priceSale = 0
                        item.discountStandard = 0
                        item.discountPromotion = 0
                        item.itemAmount = 0
                        if (item.skuType == 2) {
                            if (item.bomItems && item.bomItems.length > 0) {
                                item.bomItems.forEach(res => {
                                    res.priceSale = 0
                                    res.discountStandard = 0
                                    res.discountPromotion = 0
                                    res.itemAmount = 0
                                })
                            }

                        }
                    }
                })
            }

            let filterRows = currentChoosedItem.combinedData.combinedItem.filter(it => it.infotype == 'info');
            if (filterRows && filterRows.length > 0) {
                currentChoosedItem.combinedList = filterRows;// 把组合赋值给选择数据

                let itemAmountTemp = 0
                currentChoosedItem.combinedList.forEach(it => {
                    // if (!it.cpFlgGift) {
                    //     itemAmountTemp += it.itemAmount
                    // }
                    if (!it.cpFlgGift) {
                        itemAmountTemp += it.itemAmount * it.orderItemQuantity
                    }
                })
                currentChoosedItem.priceSale = itemAmountTemp
                currentChoosedItem.itemAmount = Number(currentChoosedItem.priceSale * currentChoosedItem.itemQuantity).toFixed(2)

                // 判断是否有可换件
                getReplaceFlag(filterRows, _this);
                if (type == 'sale') {
                    changeCombinedItemCommonSale(filterRows[0], currentChoosedItem, _this)
                }

            }

            _this.setData({

                skuData: [],
                skuSpecsList: [],
                defSkuId: null,

                currentChoosedItem: currentChoosedItem
            })
            // 计算折扣
            getDiscount(_this);
        }
        setTimeout(() => {
            _this.setData({
                skuloading: false
            })
        }, 100)
    })
}
/**
 * @desc : 改变组合促销明细后，重新计算价格
 * @date : 2022/7/5 16:49
 * @author : 周兴
 */
function getCombinedPriceByChange(_this) {
    if (_this.data.currentChoosedItem) {
        let currentChoosedItem = _this.data.currentChoosedItem;
        if (currentChoosedItem.combinedData && currentChoosedItem.combinedData.combinedItem && currentChoosedItem.combinedData.combinedItem.length > 0) {
            let combinedData = currentChoosedItem.combinedData;
            let filterRows = combinedData.combinedItem.filter(it => it.infotype == 'info');
            if (filterRows && filterRows.length > 0) {
                let pricePromotion = 0;
                filterRows.forEach(row => {
                    pricePromotion += Number(row.pricePromotion) * Number(row.orderItemQuantity)
                })
                currentChoosedItem.pricePromotion = pricePromotion;
                currentChoosedItem.combinedList = filterRows;// 把组合赋值给选择数据
                _this.changeSalesPrice(pricePromotion, _this); // 修改价格后修改相关金额
                _this.setData({
                    currentChoosedItem: currentChoosedItem
                })
            }
        }
    }
}

/**
* @desc : 把sku信息处理成对应的格式
* @date : 2022/7/2 16:49
* @author : 周兴
*/
function setSkuData(_this) {
    let skuSpecsData = []
    let currentChoosedItem = _this.data.currentChoosedItem;
    let skuData = currentChoosedItem.skuData;
    if (skuData.length > 0 && skuData) {
        skuData.forEach(it => {
            let specs = {}
            specs.skuId = it.skuId;
            let specsValuesList = it.specsValues
            if (specsValuesList && specsValuesList.length > 0) {
                let title = ''
                it.specsValues.forEach(row => {
                    specs['specsId_' + row.specsId] = row.specsValues
                    title = title + (row.specsId + ':' + row.specsValues[0]) + ","
                })
                // 在前面拼上, 方便进行匹配，要带逗号进行匹配这样才是准确的
                if (title && title != ',') {
                    title = ',' + title
                }
                specs.title = title;
            }
            skuSpecsData.push(specs);
            currentChoosedItem.skuSpecsData = skuSpecsData
        })
        _this.setData({
            skuSpecsData: skuSpecsData,
            currentChoosedItem: currentChoosedItem
        })
    }
}
/**
* @desc : 判断规格是否可用
* @date : 2022/7/2 16:49
* @author : 周兴
*/
function getSkuSpecs(defSkuId, _this) {
    let currentChoosedItem = _this.data.currentChoosedItem
    let skuSpecsList = currentChoosedItem.skuSpecsList
    let skuData = currentChoosedItem.skuData
    let defSku = []
    if (defSkuId) {
        defSku = skuData.filter(it => it.skuId == defSkuId)[0];
    }
    // 查询是否有未设置标价的商品
    // let noPriceStandardFlag = false;
    // let noPriceStandardList =  skuData.filter(it => it.disabled);
    // if(noPriceStandardList && noPriceStandardList.length > 0){
    //   noPriceStandardFlag = true;
    // }
    let defSkuValues = [] // 默认sku的规格
    //处理规格
    if (skuSpecsList && skuSpecsList.length > 0) {
        skuSpecsList.forEach(it => {
            it.specsList = [];
            let defaultFlag = false;
            it.specsValues.forEach(item => {
                let defSkuValue = {}
                let specs = {};
                specs.specsId = it.specsId;
                specs.key = item;
                specs.specsValue = item;
                specs.default = false;
                // 如果没有商品，那么规格都设置为不可用
                if (!skuData || skuData.length == 0) {
                    specs.disabled = true;
                }


                // 设置默认值
                if (defSku && defSku.specsValues && defSku.specsValues.length > 0) {

                    let filterRows = defSku.specsValues.filter(row => row.specsId == it.specsId
                        && row.specsValues && row.specsValues.indexOf(item) >= 0);
                    if (filterRows && filterRows.length > 0) {
                        if (!defaultFlag) {
                            specs.default = true;
                            defaultFlag = true;
                            //记录默认sku的规格信息
                            defSkuValue.specsId = it.specsId;
                            defSkuValue.specsValue = item;
                            defSkuValues.push(defSkuValue);
                        }
                    }
                }
                it.specsList.push(specs);
            })
        })
        _this.setData({
            skuSpecsList: skuSpecsList
        })
        if (defSkuValues && defSkuValues.length > 0) {
            // defSkuValues.forEach(it => {
            //     // 判断规格是否可用
            //     checkSpecsIsEnable(it.specsId, it.specsValue, false, _this);
            // })
            checkSpecsIsEnableNew(_this, defSkuValues, false);
        }
    }
    // 设置选中的sku信息
    chooseSkuItem(_this)
}
/**
 * @desc : 判断规格是否可用
 * @date : 2022/9/25 16:49
 * @author : 周兴
 */
function checkSpecsIsEnableNew(_this, defSkuValues, changeDefSkuId) {
    let currentChoosedItem = _this.data.currentChoosedItem
    let skuSpecsList = currentChoosedItem.skuSpecsList;
    let skuSpecsData = currentChoosedItem.skuSpecsData;
    // 获取当前已经选中规格
    if (!defSkuValues || defSkuValues.length == 0) {
        defSkuValues = []
        skuSpecsList.forEach(it => {
            defSkuValues = defSkuValues.concat(it.specsList.filter(it2 => it2.default))
        })
    }
    defSkuValues = defSkuValues ? defSkuValues : []
    console.log('defSkuValues',defSkuValues)
    // let filterSkus = []
    if (defSkuValues && defSkuValues.length > 0) {
        let title = ''
        defSkuValues.forEach(it => {
            // filterSkus = skuSpecsData.filter(row => row['specsId_' + it.specsId] 
            //       && row['specsId_' + it.specsId].indexOf(it.specsValue) >= 0);
            if (it.specsValue) {
                // 设置当前sku
                title = title + it.specsId + ":" + it.specsValue + ","
            }
        })
        // 在前面拼上, 方便进行匹配，要带逗号进行匹配这样才是准确的
        if (title && title != ',') {
            title = ',' + title
        }

        // 根据选择sku信息
        if (changeDefSkuId && title) {

            let filterRows = skuSpecsData.filter(it => it.title == title);
            if (filterRows && filterRows.length > 0) {
                _this.setData({
                    defSkuId: filterRows[0].skuId
                })
                console.log('filterRows ')
                // 设置选中的sku信息
                chooseSkuItem(_this)
            } else {
                console.log('filterRows 空   根据选择sku信息')
                let currentChoosedItem = _this.data.currentChoosedItem;
                currentChoosedItem.skuId = null;
                currentChoosedItem.skuCode = null;
                currentChoosedItem.skuName = null;
                currentChoosedItem.skuModel = null;
                currentChoosedItem.priceStandard = Number(0);
                currentChoosedItem.itemAmount = Number(0);
                currentChoosedItem.priceSale = Number(0);
                currentChoosedItem.discountStandard = Number(0);
                //HC0129DT
                _this.setData({
                    defSkuId: null,
                    currentChoosedItem: currentChoosedItem
                })
            }
        }
    }
    // 如果都没有选中的sku则全部清空
    if (defSkuValues.length == 0) {
        clearDefaultCommon(_this);
    } else {
        skuSpecsList.forEach(it => {
            // 判断时要去掉当前规格进行判断
            let filterRows = defSkuValues.filter(d => d.specsId != it.specsId);
            if (!filterRows || filterRows.length == 0) {
                // 说明其他规格没有选中的了，那么这个规格下都可用
                it.specsList.forEach(item => {
                    // 未设置标价的不允许可用
                    if (!item.noPriceStandardFlag) {
                        item.disabled = false;
                    }
                })
            } else {
                let title = ''
                let skuSpecsDataTmp = JSON.parse(JSON.stringify(skuSpecsData))
                filterRows.forEach(d => {
                    if (d.specsValue) {
                        // 设置当前sku
                        title = ',' + d.specsId + ":" + d.specsValue + ","
                        skuSpecsDataTmp = filterDataByTitle(skuSpecsDataTmp, title);
                    }
                })

                it.specsList.forEach(item => {
                    let itemFilters = skuSpecsDataTmp.filter(row => row['title'] && !row.noPriceStandardFlag
                        && row['title'].indexOf("," + item.specsId + ":" + item.specsValue + ",") >= 0);

                    if (itemFilters && itemFilters.length > 0) {
                        item.disabled = false;
                    } else {
                        item.disabled = true;
                    }
                })
            }

        })
        let currentChoosedItem = _this.data.currentChoosedItem;
        currentChoosedItem.skuSpecsList = skuSpecsList;
        _this.setData({
            currentChoosedItem: currentChoosedItem
        })
    }
}
/**
 * @desc : 根据内容进行匹配数据 
 * @date : 2022/7/2 16:49
 * @author : 周兴
 */
function filterDataByTitle(data, title) {
    if (data && data.length > 0) {
        let filters = data.filter(row => row['title']
            && row['title'].indexOf(title) >= 0);
        return filters;
    }
    return data;
}
/**
 * @desc : 判断规格是否可用
 * @date : 2022/7/2 16:49
 * @author : 周兴
 */
function checkSpecsIsEnable(specsId, specsValue, changeDefSkuId, _this) {
    let currentChoosedItem = _this.data.currentChoosedItem
    let skuSpecsList = currentChoosedItem.skuSpecsList;
    let skuSpecsData = currentChoosedItem.skuSpecsData;
    // 先获取符合当前规格的商品
    let filterSkus = skuSpecsData.filter(row => row['specsId_' + specsId] && row['specsId_' + specsId].indexOf(specsValue) >= 0);
    //判断其他规格分类是否可用
    let otherSpecsList = skuSpecsList.filter(it => it.specsId != specsId)
    if (otherSpecsList && otherSpecsList.length > 0) {
        otherSpecsList.forEach(it => {
            it.specsList.forEach(item => {
                // 设置是否可用
                if (filterSkus && filterSkus.length > 0) {
                    let filterRows = filterSkus.filter(row => row['specsId_' + it.specsId] && row['specsId_' + it.specsId].indexOf(item.key) >= 0);
                    if (filterRows && filterRows.length > 0) {
                        // 设置默认的skuId
                        if (changeDefSkuId && item.default) {
                            _this.setData({
                                defSkuId: filterRows[0].skuId
                            })
                            // 设置选中的sku信息
                            chooseSkuItem(_this)
                        }
                        //说明存在
                        item.disabled = false;
                    } else {
                        item.disabled = true;
                    }
                }
            })
        });
    } else {
        if (changeDefSkuId && filterSkus && filterSkus.length > 0) {
            _this.setData({
                defSkuId: filterSkus[0].skuId
            })
            // 设置选中的sku信息
            chooseSkuItem(_this)
        }
    }
    currentChoosedItem.skuSpecsList = skuSpecsList;
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
}
/**
* @desc :  选择sku，记录sku相关信息
* @date : 2022/9/1 13:49
* @author : 于继渤
*/
function chooseSkuItem(_this) {
    let currentChoosedItem = _this.data.currentChoosedItem;
    let defSkuId = _this.data.defSkuId;
    let skuData = currentChoosedItem.skuData;
    let filterRows = skuData.filter(it => it.skuId == defSkuId);
    console.log('chooseSkuItem')

    if (filterRows && filterRows.length > 0) {
        console.log('filterRows 111',filterRows)
        let row = filterRows[0];
        currentChoosedItem.titleTag = row.titleTag
        currentChoosedItem.orderItemQuantity = row.orderItemQuantity
        currentChoosedItem.activityId = row.activityId ? row.activityId : null
        currentChoosedItem.promotionItemId = row.promotionItemId
        currentChoosedItem.promotionPriceType = row.promotionPriceType
        currentChoosedItem.skuId = row.skuId
        currentChoosedItem.promotionId = row.promotionId
        currentChoosedItem.promotionType = row.promotionType
        currentChoosedItem.promotionTypeName = row.promotionTypeName
        currentChoosedItem.hasChild = row.hasChild
        currentChoosedItem.id = row.id
        currentChoosedItem.cpFlgGift = row.cpFlgGift ? row.cpFlgGift : null
        currentChoosedItem.parentId = row.parentId
        currentChoosedItem.skuType = row.skuType
        currentChoosedItem.skuCode = row.skuCode
        currentChoosedItem.skuName = row.skuName
        currentChoosedItem.spuCode = row.spuCode
        currentChoosedItem.priceCost = (row.priceCost == 0 || row.priceCost) ? row.priceCost : null
        currentChoosedItem.spuId = row.spuId
        currentChoosedItem.pricePromotion = row.pricePromotion
        currentChoosedItem.priceStandard = row.priceStandard
        currentChoosedItem.priceSale = row.priceSale ? row.priceSale : 0
        currentChoosedItem.itemAmount = currentChoosedItem.priceSale
        currentChoosedItem.discountPromotion = 100;
        currentChoosedItem.discountStandard = 100;
        if (row.priceStandard == 0) {
            currentChoosedItem.discountStandard = 0;
        }
        //面积公式
        if (row.specialFormulaName) {
            currentChoosedItem.specialFormulaName = row.specialFormulaName
            currentChoosedItem.specialFormula = row.specialFormulaName
        }
        currentChoosedItem.titleTag = row.titleTag
        currentChoosedItem.standardId = row.standardId
        currentChoosedItem.standardItemId = row.standardItemId
        currentChoosedItem.skuVolume = row.skuVolume
        currentChoosedItem.skuWeight = row.skuWeight
        currentChoosedItem.bomItems = row.bomItems
        currentChoosedItem.glassThickness = row.glassThickness
        currentChoosedItem.glassCraft = row.glassCraft
        currentChoosedItem.substrateColor = row.substrateColor
        currentChoosedItem.standardId = row.standardId
        currentChoosedItem.skuModel = row.skuModel
        currentChoosedItem.flgAllowSpecsUndefine = row.flgAllowSpecsUndefine
        // currentChoosedItem.skuSpecsList = _this.data.skuSpecsList
        // currentChoosedItem.skuSpecsData = _this.data.skuSpecsData
        //非标
        if (row.flgNonStandardType == 1 || row.flgNonStandardType == 2) {

            if (row.flgNonStandardType == 1) {
                currentChoosedItem.nonGlassPriceDef = row.nonGlassPriceDef ? row.nonGlassPriceDef : null
                currentChoosedItem.priceStandard = currentChoosedItem.nonGlassPriceDef ? currentChoosedItem.nonGlassPriceDef : 0
                currentChoosedItem.priceSale = currentChoosedItem.nonGlassPriceDef ? currentChoosedItem.nonGlassPriceDef : 0
                currentChoosedItem.itemAmount = currentChoosedItem.nonGlassPriceDef ? currentChoosedItem.nonGlassPriceDef : 0

            }
            if (row.flgNonStandardType == 2) {
                currentChoosedItem.priceStandard = currentChoosedItem.priceStandard
                currentChoosedItem.priceSale = currentChoosedItem.priceStandard
                currentChoosedItem.itemAmount = currentChoosedItem.priceStandard
            }
            currentChoosedItem.nonGlassPriceItems = row.nonGlassPriceItems ? row.nonGlassPriceItems : []
            if (row.nonGlassPriceItems && row.nonGlassPriceItems.length > 0) {
                let flgDefItemList = row.nonGlassPriceItems.filter(res => {
                    return res.flgDefItem
                })
                currentChoosedItem.priceStandard = flgDefItemList[0].itemPrice
                currentChoosedItem.priceSale = currentChoosedItem.nonGlassPriceDef ? currentChoosedItem.nonGlassPriceDef : flgDefItemList[0].itemPrice
                currentChoosedItem.itemAmount = currentChoosedItem.nonGlassPriceDef ? currentChoosedItem.nonGlassPriceDef : flgDefItemList[0].itemPrice
            }else{
                console.log('规格没有匹配上sku')
                currentChoosedItem.priceStandard = row.priceStandard
                currentChoosedItem.priceSale = row.priceStandard
                currentChoosedItem.itemAmount = row.priceStandard
            }

        }

        // 设置赠品
        setFlgGift(_this);
        // 判断是否有可换件
        getReplaceFlag([row], _this);
    } else {
        console.log('没有匹配到sku')
        currentChoosedItem.skuId = null
        currentChoosedItem.skuName = null
        currentChoosedItem.skuCode = null
        currentChoosedItem.priceStandard = 0
        currentChoosedItem.itemAmount = Number((currentChoosedItem.itemQuantity ? currentChoosedItem.itemQuantity : 0) * (currentChoosedItem.priceSale ? currentChoosedItem.priceSale : 0));
    }
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
}
/**
* @desc : 切换sku商品规格
* @author : 周兴
* @date : 2022/6/1 10:16
*/
function changeSkuSpecsCommon(e, _this) {

    let disabled = e.target.dataset.itemt.disabled;
    // 说明是不能用的，就不需要切换
    if (disabled) {
        return;
    }
    let currentChoosedItem = _this.data.currentChoosedItem;
    let specsValue = e.target.dataset.itemt.key;
    let specsId = e.target.dataset.itemt.specsId;
    let skuSpecsList = currentChoosedItem.skuSpecsList;

    let defaultFlag = !e.target.dataset.itemt.default;
    // 默认值取反向
    // 更改默认值
    let filterRows = skuSpecsList.filter(it => it.specsId == specsId);
    if (filterRows && filterRows.length > 0) {
        filterRows[0].specsList.forEach(row => {
            if (row.key == specsValue) {
                row.default = defaultFlag;
            } else {
                row.default = false;
            }
        })
    }
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
    // 判断规格是否可用
    // checkSpecsIsEnable(specsId, specsValue, true, _this);
    checkSpecsIsEnableNew(_this, null, true);
}
/**
* @desc : 判断是否可以进行替换
* @date : 2022/9/1 13:49
* @author : 于继渤
*/
function getReplaceFlag(rows, _this) {
    let currentChoosedItem = _this.data.currentChoosedItem;
    currentChoosedItem.replaceFlag = false;
    if (rows && rows.length > 0) {
        for (let j = 0; j < rows.length; j++) {
            if (rows[j].bomItems != null && rows[j].bomItems.length > 0) {
                for (let i = 0; i < rows[j].bomItems.length; i++) {
                    if (rows[j].bomItems[i].replaceableSku != null
                        && rows[j].bomItems[i].replaceableSku.length > 0) {
                        currentChoosedItem.replaceFlag = true;
                        return;
                    }
                }
            }
        }
    }
}

/**
* @desc : 改变标识
* @author : 周兴
* @date : 2022/8/6 17:16
*/
function changeAllReadyCheckBoxCommon(e, _this) {
    let key = e.currentTarget.dataset.key
    let currentChoosedItem = _this.data.currentChoosedItem

    //根据商品的cpFlgGift赠品标识，如果没有设置，不允许订单明细的售价为0，赠品标识也不修改（bom子级按照总单来，组合是可以按照明细控制）

    //bom
    if (currentChoosedItem.skuType == 2) {
        if (currentChoosedItem.cpFlgGift) {
            currentChoosedItem.flgGift = e.detail
            if (e.detail) {
                currentChoosedItem.priceSale_Temp = currentChoosedItem.priceSale ? currentChoosedItem.priceSale : currentChoosedItem.priceSale_Temp
                currentChoosedItem.itemAmount_Temp = currentChoosedItem.itemAmount ? currentChoosedItem.itemAmount : currentChoosedItem.itemAmount_Temp
                currentChoosedItem.discountStandard_Temp = currentChoosedItem.discountStandard
                currentChoosedItem.discountPromotion_Temp = currentChoosedItem.discountPromotion
                currentChoosedItem.priceSale = 0
                currentChoosedItem.discountStandard = 0
                currentChoosedItem.discountPromotion = 0
                currentChoosedItem.itemAmount = 0
            } else {
                //     取消赠品标识时，价格是0时价格恢复成标价，如价格手动改过，则不变
                if (currentChoosedItem.priceSale == 0) {
                    // 恢复到以前的金额
                    currentChoosedItem.priceSale = Number(currentChoosedItem.priceSale_Temp)
                    currentChoosedItem.itemAmount = Number(currentChoosedItem.itemAmount_Temp)
                    currentChoosedItem.discountStandard = Number(currentChoosedItem.discountStandard_Temp)
                    currentChoosedItem.discountPromotion = Number(currentChoosedItem.discountPromotion_Temp)
                }

            }
        }
    } else {

        currentChoosedItem.flgGift = e.detail


        if (e.detail) {
            currentChoosedItem.priceSale_Temp = currentChoosedItem.priceSale
            currentChoosedItem.itemAmount_Temp = currentChoosedItem.itemAmount
            currentChoosedItem.discountStandard_Temp = currentChoosedItem.discountStandard
            currentChoosedItem.discountPromotion_Temp = currentChoosedItem.discountPromotion
            currentChoosedItem.priceSale = 0
            currentChoosedItem.discountStandard = 0
            currentChoosedItem.discountPromotion = 0
            currentChoosedItem.itemAmount = 0
        } else {
            //     取消赠品标识时，价格是0时价格恢复成标价，如价格手动改过，则不变
            if (currentChoosedItem.priceSale == 0) {
                // 恢复到以前的金额
                currentChoosedItem.priceSale = currentChoosedItem.priceSale_Temp
                currentChoosedItem.itemAmount = currentChoosedItem.itemAmount_Temp
                currentChoosedItem.discountStandard = currentChoosedItem.discountStandard_Temp
                currentChoosedItem.discountPromotion = currentChoosedItem.discountPromotion_Temp
            }

        }

    }

    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
}

/**
* @desc : 清空选中数据(确定)
* @date : 2022/7/4 16:49
* @author : 周兴
*/
function clearDefaultCommon(_this) {
    let currentChoosedItem = _this.data.currentChoosedItem
    // 清空sku数据
    currentChoosedItem.skuId = null;
    currentChoosedItem.skuName = null;
    currentChoosedItem.skuCode = null;
    currentChoosedItem.skuModel = null;
    currentChoosedItem.pricePromotion = null;
    currentChoosedItem.priceStandard = null;
    let skuSpecsList = currentChoosedItem.skuSpecsList;
    if (skuSpecsList && skuSpecsList.length > 0) {
        skuSpecsList.forEach(it => {
            if (it.specsList && it.specsList.length > 0) {
                it.specsList.forEach(s => {
                    s.default = false;
                    if (!s.noPriceStandardFlag) {
                        s.disabled = false;
                    }
                })
                _this.setData({
                    currentChoosedItem: currentChoosedItem
                })
            }
        })
    }
}

/**
* @desc : 选择数据(确定)
* @date : 2022/7/4 16:49
* @author : 周兴
*/
function chooseSkuInfoCommon(_this) {
    let currentChoosedItem = _this.data.currentChoosedItem
    currentChoosedItem.goodsSkuList = _this.data.goodsSkuList
    //校验选择商品数量不能为0
    if (currentChoosedItem.itemQuantity === 0 || !currentChoosedItem.itemQuantity) {
        wx.showToast({
            title: '数量不能为0',
            image: '/static/image/warning.png',
            duration: 1000
        })
        return
    }
    // 如果没有sku 那么不允许
    if (currentChoosedItem.promotionType != Constants.PROMOTION_TYPE.COMBINED && (!currentChoosedItem.skuId || currentChoosedItem.skuId == 0)) {
        wx.showToast({
            title: '未选择商品规格',
            image: '/static/image/warning.png',
            duration: 1000
        })
        return
    }

    //非标定制品标价参数赋值
    if (currentChoosedItem.nonStandardList && currentChoosedItem.nonStandardList.length > 0) {
        let nonStandardList = util.copyObj(currentChoosedItem.nonStandardList)
        let nonStandardObjList = []
        nonStandardList.forEach(res => {
            let nonStandardObj = {
                fieldId: null,
                fieldCode: null,
                fieldName: null,
                optionCode: null,
                optionName: null,
            }
            nonStandardObj.fieldId = res.fieldId
            nonStandardObj.fieldCode = res.fieldCode
            nonStandardObj.fieldName = res.fieldName
            nonStandardObj.isScope = res.isScope
            nonStandardObj.fieldType = res.fieldType
            //判断是否是范围值
            if (res.isScope == 1) {
                if (res.optionCode && res.optionCode) {
                    nonStandardObj.optionCode = res.optionCode
                    nonStandardObj.optionName = res.optionName
                }
            }

            if (res.options && res.options.length > 0) {
                res.options.forEach(it => {
                    if (it.infotype == 'info') {
                        nonStandardObj.optionCode = it.code
                        nonStandardObj.optionName = it.name
                    }
                })
            }
            //没有选择的参数不会设置
            if (nonStandardObj.optionCode) {
                nonStandardObjList.push(nonStandardObj)
            }
        })
        currentChoosedItem.nonGlassList = nonStandardObjList   //订单明细字段
        //设置显示非标参数 (展示非标text)
        if (nonStandardObjList && nonStandardObjList.length > 0) {
            let nonGlassListShowTemp = []
            nonStandardObjList.forEach(it => {

                if (it.optionName && it.optionName != undefined) {
                    nonGlassListShowTemp.push(it.fieldName + '：' + it.optionName)
                }

            })
            //非标参数显示/匹配命中标价的数据
            currentChoosedItem.nonGlassListShow = nonGlassListShowTemp.join('，')
        }

    }

    // 已选商品列表
    let choosedGoodsList = util.copyObj(_this.data.choosedGoodsList)
    let currentChoosedItemCopy = util.copyObj(currentChoosedItem)

    //判断已选是不是修改
    if (_this.data.active == 1 || _this.data.active == '1') {

        if (typeof _this.data.activeOneIndex == 'number') {
            choosedGoodsList[_this.data.activeOneIndex] = {}
            choosedGoodsList[_this.data.activeOneIndex] = currentChoosedItemCopy
        }


        _this.setData({
            choosedGoodsList: choosedGoodsList
        })
    }

    // 判断商品是否在已选列表中存在
    let filterRows = choosedGoodsList.filter(it => it.id == currentChoosedItem.id);
    let currentChoosedItemTemp = { ...currentChoosedItem }
    if (!filterRows || filterRows.length == 0) {
        if (_this.data.active != 1 || _this.data.active != '1') {
            choosedGoodsList.push(currentChoosedItemTemp)
        }
    }
    choosedGoodsList.forEach(it => {
        it.flgInstalled = true
    })

    console.log('choosedGoodsList', choosedGoodsList)
    _this.setData({
        choosedGoodsList: choosedGoodsList,
        choosedGoodsListLength: choosedGoodsList.length,
        showAdd: false,
        show6: false
    })

    // 更新购物车已选数量
    handleChoosedNum(_this);

    //计算合计金额
    getSumAmount(_this);
}

/**
* @desc : 计算合计金额
* @date : 2022/7/4 16:49
* @author : 于继渤
*/
function getSumAmount(_this) {
    // 逸轩商品的数组
    let choosedGoodsList = _this.data.choosedGoodsList
    let sumQuantity = 0             // 合计数量
    let sumGoodsAmount = 0          // 货物金额
    let categoryQuantityList = []     // 品类数

    choosedGoodsList.forEach(item => {

        sumGoodsAmount += Number(item.itemAmount)
        // 如果有子级，计算子级的数量
        if (item.bomItems && item.bomItems.length > 0) {
            item.bomItems.forEach(it => {
                sumQuantity += Number(item.itemQuantity * it.orderItemQuantity);
                if (it.skuSpecs == null) {
                    it.skuSpecs = " "
                }
                if (it.skuSpecs && !categoryQuantityList.includes(it.skuSpecs)) {
                    categoryQuantityList.push(it.skuSpecs)
                }
            })
        } else if (item.combinedList && item.combinedList.length > 0) {
            item.combinedList.forEach(it => {

                if (it.bomItems && it.bomItems.length > 0) {
                    it.bomItems.forEach(itemt => {

                        sumQuantity += Number(it.itemQuantity * itemt.orderItemQuantity);

                        if (itemt.skuSpecs == null) {
                            itemt.skuSpecs = " "
                        }
                        if (itemt.skuSpecs && !categoryQuantityList.includes(itemt.skuSpecs)) {
                            categoryQuantityList.push(itemt.skuSpecs)
                        }
                    })
                } else {
                    sumQuantity += Number(item.itemQuantity * it.orderItemQuantity);

                }

                if (it.skuSpecs == null) {
                    it.skuSpecs = " "
                }
                if (it.skuSpecs && !categoryQuantityList.includes(it.skuSpecs)) {
                    categoryQuantityList.push(it.skuSpecs)
                }

            })
        } else {
            // 如果没有子级，就加上即可
            sumQuantity += Number(item.itemQuantity);
            if (item.skuSpecs == null) {
                item.skuSpecs = " "
            }
            if (item.skuSpecs && !categoryQuantityList.includes(item.skuSpecs)) {
                categoryQuantityList.push(item.skuSpecs)
            }
        }
        // 如果有组合

    })
    _this.setData({
        sumGoodsAmount: sumGoodsAmount,
        sumQuantity: sumQuantity,
        categoryQuantity: categoryQuantityList.length,
    })
}

/**
* @desc : 确定时处理数据
* @date : 2022/7/5 16:49
* @author : 周兴
*/
function handleData(goodsList, _this) {

    let followFlag = _this.data.followFlag;
    let choosedGoodsList = util.copyObj(_this.data.choosedGoodsList)

    let title = ''

    if (choosedGoodsList != null && choosedGoodsList.length > 0) {
        let skuTypeFlag = false
        choosedGoodsList.forEach(it => {

            if (!it.id) {
                it.id = util.getGuid()
            }
            //判断bom是否有子级
            if (it.skuType == 2 && (it.bomItems == null || it.bomItems.length == 0)) {
                title = it.title
                skuTypeFlag = true
            } else {
                skuTypeFlag = false
            }

            // 父级id
            it.parentId = null;
            it.hasChild = 0; // 默认设置为无子级
            // 处理金额
            it.itemAmount = Number(it.itemAmount).toFixed(2)

            // 跟单处理为实际量
            if (followFlag) {
                it.tItemQuantity = it.itemQuantity
                it.tItemAmount = it.itemAmount
            }
            console.log('it.bomItems前', it.bomItems)
            // ***如果有子级，需要处理子级的数据  TODO
            if (it.bomItems != null && it.bomItems.length > 0) {
                it.hasChild = 1; // 说明有子级
                it.flgInstalled = true

                it.bomItems.forEach(row => {
                    row['flgBomCombined'] = false;
                    // 父级id
                    row.parentId = it.id;
                    // 数量的处理
                    if (followFlag) {
                        row.tItemQuantity = it.itemQuantity * row.orderItemQuantity
                    } else {
                        row.itemQuantity = it.itemQuantity * row.orderItemQuantity
                    }
                    console.log('bomItem数量的处理', row.itemQuantity)
                    // 如果单品促销
                    if (it.promotionType == Constants.PROMOTION_TYPE.PRMOTION ||
                        it.promotionType == Constants.PROMOTION_TYPE.COMBINED) {

                        // 把促销的信息替换上去
                        //设置明细活动ID
                        row.activityId = it.activityId

                        row.promotionType = it.promotionType;
                        row.promotionId = it.promotionId;
                        if (it.pricePromotion == 0) {
                            row.priceSale = 0
                        } else {
                            // 销售价（子级）= 销售价（父级）/ 标售售价（父级）* 标售售价（子级）
                            row.priceSale = Number((it.priceSale / it.pricePromotion * row.pricePromotion).toFixed(2))
                        }
                    } else {
                        // 正常品
                        if (it.priceStandard == 0 || !it.priceStandard) {
                            row.priceSale = 0
                        } else {
                            // 销售价（子级）= 销售价（父级）/ 标售售价（父级）* 标售售价（子级）
                            row.priceSale = Number((it.priceSale / it.priceStandard * row.priceStandard).toFixed(2))
                        }
                    }
                    if (followFlag) {
                        row.tItemAmount = Number((row.priceSale * row.tItemQuantity).toFixed(2))
                        row.itemAmount = row.tItemAmount
                    } else {
                        row.itemAmount = Number((row.priceSale * row.itemQuantity).toFixed(2))
                    }
                    // 设置标题
                    getTitle(it, row);
                    // 处理图标
                    common.setRowImagePath(row);
                })
                it.goodsSkuList = it.bomItems
            }
            // **** 如果有组合促销，需要进行数据的处理
            if (it.combinedList != null && it.combinedList.length > 0) {
                it.flgInstalled = true
                it.hasChild = 1; // 说明有子级
                it.goodsSkuList = []
                let priceStandard = 0;    // 重新计算标价
                it.combinedList.forEach(row => {
                    row.activityId = it.activityId
                    row.parentId = it.id;
                    row.hasChild = 0;
                    if (row.bomItems == null || row.bomItems.length == 0) {
                        if (row.cpFlgGift) {
                            row.priceSale = 0
                            row.discountStandard = 0
                            row.discountPromotion = 0
                            row.itemAmount = 0
                        } else {
                            // 重新计算价格
                            row.priceSale = getCombinedSalePrice(it, row);
                        }
                        // 重新计算折扣
                        getCombinedDiscount(row);
                        // 计算数量
                        row.itemQuantity = Number(it.itemQuantity) * Number(row.orderItemQuantity)
                        row.itemAmount = Number(row.itemQuantity) * Number(row.priceSale)
                        // 跟单处理为实际量
                        if (followFlag) {
                            row.tItemQuantity = row.itemQuantity
                            row.tItemAmount = row.itemAmount
                        }
                        // 把促销的信息替换上去
                        row.promotionType = it.promotionType;
                        row.promotionId = it.promotionId;
                        // 设置标题
                        getTitle(it, row);
                        // 处理图标
                        common.setRowImagePath(row);
                        row.flgBomCombined = false;
                        it.goodsSkuList.push(row);
                        // 计算标价
                        priceStandard += Number(row.orderItemQuantity) * Number(row.priceStandard)
                    } else {
                        let bomAmount = 0;
                        // bom也需要保存进去，方便报表统计
                        row.itemQuantity = it.itemQuantity * row.orderItemQuantity;
                        row.itemAmount = bomAmount;
                        row.bomItems.forEach(bRow => {
                            // 父级id
                            bRow.parentId = it.id;
                            bRow.hasChild = 0;
                            if (row.cpFlgGift) {
                                bRow.priceSale = 0
                                bRow.discountStandard = 0
                                bRow.discountPromotion = 0
                                bRow.itemAmount = 0
                            } else {
                                // 重新计算价格
                                bRow.priceSale = getCombinedSalePrice(it, bRow);
                            }

                            // 重新计算折扣
                            getCombinedDiscount(bRow);

                            // 数量的处理
                            bRow.itemQuantity = Number(row.itemQuantity) * Number(bRow.orderItemQuantity)
                            bRow.itemAmount = Number(bRow.priceSale) * Number(bRow.itemQuantity).toFixed(2)
                            bomAmount += bRow.itemAmount;
                            if (followFlag) {
                                bRow.tItemQuantity = bRow.itemQuantity
                                bRow.tItemAmount = bRow.itemAmount
                            }
                            bRow.activityId = it.activityId
                            // 把促销的信息替换上去
                            bRow.promotionType = it.promotionType;
                            bRow.promotionId = it.promotionId;
                            // 设置标题
                            getTitle(it, bRow);
                            // 处理图标
                            common.setRowImagePath(bRow);
                            bRow.flgBomCombined = false;
                            it.goodsSkuList.push(bRow);
                            // 计算标价
                            priceStandard += Number(bRow.orderItemQuantity) * Number(bRow.priceStandard)
                        })
                        row.priceSale = row.itemQuantity == 0 ? 0 : (Number(row.itemAmount) / Number(row.itemQuantity)).toFixed(2)
                        if (followFlag) {
                            row.tItemQuantity = row.itemQuantity
                            row.tItemAmount = row.itemAmount
                        }
                        row.hasChild = 1;
                        row.flgBomCombined = true;
                        it.goodsSkuList.push(row);

                    }
                })
                // 给组合的标价总额赋值
                it.priceStandard = priceStandard

            }
            // 计算品类数
            it.categoryQuantity = common.getCategoryQuantity(it.goodsSkuList);
            getCombinedPriceByChange(_this)
            it['flgBomCombined'] = false;
        })

        // 重新计算最后一条数据的金额，用父级金额-其他条金额合计
        common.countLastAmount(choosedGoodsList, followFlag);
        // 把选择的数据加到明细中

        for (let i = 0; i < choosedGoodsList.length; i++) {
            let newGood = choosedGoodsList[i];
            let alreadyExists = false;

            for (let j = 0; j < goodsList.length; j++) {
                if (goodsList[j].id === newGood.id) {
                    alreadyExists = true;
                    break;
                }
            }

            if (!alreadyExists) {
                goodsList.push(newGood);
            }
        }


        // goodsList = goodsList.concat(choosedGoodsList);


        if (skuTypeFlag) {
            wx.showToast({
                title: title + '该BOM没有子件',
                icon: 'none'
            })
        }
    }


    goodsList.forEach(res => {
        res.skuVolume = 0
        res.skuWeight = 0
        if (res.goodsSkuList && res.goodsSkuList.length > 0) {
            res.goodsSkuList.forEach(item => {
                res.skuVolume += item.skuVolume
                res.skuWeight += item.skuWeight
            })
        }
    })
    console.log('goodsList==>', goodsList)
    return goodsList;
}

/**
* @desc : 重新计算组合子件的价格
* @date : 2022/7/6 16:49
* @author : 周兴
*/
function getCombinedSalePrice(pRow, row) {
    let priceSale = 0;
    // 销售价（子级）= 销售价（父级）/ 标售售价（父级）* 标售售价（子级）
    if (pRow.pricePromotion == 0) {
        priceSale = 0
    } else {
        // 销售价（子级）= 销售价（父级）/ 标售售价（父级）* 标售售价（子级）
        priceSale = Number(pRow.priceSale / pRow.pricePromotion * row.pricePromotion).toFixed(2)
    }
    return priceSale;
}
/**
* @desc : 计算折扣
* @date : 2022/7/6 16:49
* @author : 周兴
*/
function getCombinedDiscount(row) {
    let discountStandard = 0;
    let discountPromotion = 0;
    // 标价折扣= 售价/标价
    if (!row.priceStandard || row.priceStandard == 0) {
        discountStandard = 0;
    } else {
        discountStandard = Number(row.priceSale * 100 / row.priceStandard).toFixed(2)
    }
    // 促销折扣= 售价/促销价
    if (!row.pricePromotion || row.pricePromotion == 0) {
        discountPromotion = 100;
    } else {
        discountPromotion = Number(row.priceSale * 100 / row.pricePromotion).toFixed(2)
    }
    row.discountPromotion = discountPromotion;
    row.discountStandard = discountStandard;
}
/**
 * @desc : 设置子级设置标题等信息
 * @date : 2022/7/6 16:49
 * @author : 周兴
 */
function getTitle(pRow, row) {
    // titleTag 标题标签
    if (pRow.promotionType == Constants.PROMOTION_TYPE.COMBINED) {
        row.titleTag = '组合促销';
    } else if (pRow.promotionType == Constants.PROMOTION_TYPE.PRMOTION) {
        row.titleTag = '单品促销';
    } else {
        // 显示品牌
        row.titleTag = row.brandName;
    }
    // title
    row.title = row.skuName;
    row.desc = row.skuCode;
}
/**
* @desc : 切换组合促销明细
* @date : 2022/7/5 16:49
* @author : 周兴
*/
function changeCombinedItemCommon(e, _this) {
    let itemt = e.currentTarget.dataset.itemt;
    let currentChoosedItem = _this.data.currentChoosedItem;
    let combinedData = currentChoosedItem.combinedData
    let combinedItem = combinedData.combinedItem
    // 切换默认项
    combinedItem.forEach(it => {
        //相同类别进行处理

        if (it.combinedTypeId === itemt.combinedTypeId) {
            it.infotype = 'default';
            if (it.id == itemt.id) {
                it.infotype = 'info'
            }
        }
    })
    currentChoosedItem.combinedData = combinedData;
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
    // 修改相关价格
    getCombinedPriceByChange(_this);
 
}

function changeCombinedItemCommonSale(itemt, currentChoosedItem, _this) {
    let combinedData = currentChoosedItem.combinedData
    let combinedItem = combinedData.combinedItem
    // 切换默认项
    combinedItem.forEach(it => {
        //相同类别进行处理

        if (it.combinedTypeId === itemt.combinedTypeId) {
            it.infotype = 'default';
            if (it.id == itemt.id) {
                it.infotype = 'info'
            }
        }
    })
    currentChoosedItem.combinedData = combinedData;
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
    // 修改相关价格
    getCombinedPriceByChange(_this);
  
}
/**
 * @desc : 设置查询参数
 * @date : 2022/7/5 16:49
 * @author : 周兴
 */
function setParam(_this) {
    let param = {}
    param.specsId = undefined;
    if (_this.data.specsId) {
        param.specsId = Number(_this.data.specsId);
    } else if (_this.data.specsId == 0) {
        param.specsId = 0;
    }
    param.pageSize = _this.data.pageInfo.pageSize;
    param.currentPage = _this.data.pageInfo.currentPage;
    param.orgId = app.globalData.user.orgId
    param.searchText = _this.data.searchText
    param.cpId = app.globalData.user.cpId

    let searchText = _this.data.searchText;
    if (searchText) {
        param.spuQuerys = JSON.stringify({
            queryText: searchText,
        })
    }
    return param;
}

/**
  * @desc : 删除选中的数据
  * @author : 周兴
  * @date : 2022/7/4 20:16
  */
function delChoosedGoodsItemCommon(e, _this, flag) {
    let focusIndex = e.target.dataset.index;
    let choosedGoodsList = _this.data.choosedGoodsList;
    // 根据行号进行删除
    choosedGoodsList = choosedGoodsList.filter((it, index) => index != focusIndex);
    _this.setData({
        choosedGoodsList: choosedGoodsList,
        choosedGoodsListLength: choosedGoodsList.length
    })

    if (flag) {
        // 统计已选数量
        handleChoosedNum(_this);
    }

    // 计算合计金额
    getSumAmount(_this);
}

/**
* @desc : 设置已选数据
* @author : 周兴
* @date : 2022/11/10 20:16
*/
function handleChoosedNum(_this) {
    let choosedGoodsList = _this.data.choosedGoodsList;
    choosedGoodsList = choosedGoodsList ? choosedGoodsList : []
    wx.setStorageSync('choosedGoodsList', choosedGoodsList)
    let cartNum = choosedGoodsList.length  //默认数据
    if (cartNum != 0) {
        //设置角标
        wx.setTabBarBadge({
            index: 2, //第几个tab
            text: cartNum.toString()
        })
    } else {
        //移除角标
        wx.removeTabBarBadge({
            index: 2,
        })
    }
}

/**
* @desc : 打开选择明细
* @author : 周兴
* @date : 2022/7/4 20:16
*/
function openChoosedItemInfoCommon(e, _this) {
    let currentChoosedItem = e.currentTarget.dataset.item;
    if (_this.data.active && (_this.data.active == 1 || _this.data.active == '1')) {
        _this.setData({
            activeOneIndex: e.currentTarget.dataset.index  //已选触发位置索引
        })
    }
    // 根据类型显示对应的数据
    if (currentChoosedItem.promotionType == Constants.PROMOTION_TYPE.COMBINED) {
        _this.setData({
            show4: true,
            show1: false,
            show6: false,
            show7: false,
        })
    } else {
        _this.setData({
            show1: true,
            show4: false,
            show6: false,
            show7: false,
        })
    }
    if (currentChoosedItem.flgNonStandardType == 1 || currentChoosedItem.flgNonStandardType == 2) {
        _this.setData({
            show6: false,
            show7: true,
        })
    }
    if (currentChoosedItem.specsData && currentChoosedItem.specsData.length == 0 && currentChoosedItem.skuData && currentChoosedItem.skuData.length > 1) {
        _this.setData({
            show6: true
        })
    }
    _this.setData({
        showAdd: true,
        currentChoosedItem: currentChoosedItem
    })
}
//关闭poup商品
function closeAddItemInfoCommon(_this) {
    _this.setData({
        showAdd: false,
        currentChoosedItem: {},
    })
}
/**
   * @desc : 修改销售价格
  * @author : 周兴
  * @date : 2022/7/6 14:45
  */
function changeSalesPriceCommon(priceSale, _this, flag) {
    let currentChoosedItem = _this.data.currentChoosedItem
    currentChoosedItem.priceSale = priceSale // 销售价格赋值


    let itemAmountTemp = null
    if (flag && currentChoosedItem.combinedList && currentChoosedItem.combinedList.length > 0) {
        currentChoosedItem.combinedList.forEach(it => {
            if (!it.cpFlgGift) {
                itemAmountTemp += it.itemAmount * it.orderItemQuantity
            }
        })
    }

    currentChoosedItem.priceSale = itemAmountTemp ? itemAmountTemp : priceSale
    if (currentChoosedItem.nonStandardArea) {
        currentChoosedItem.itemAmount = Number(currentChoosedItem.priceSale * currentChoosedItem.itemQuantity * currentChoosedItem.nonStandardArea).toFixed(2)
    } else {
        currentChoosedItem.itemAmount = Number(currentChoosedItem.priceSale * currentChoosedItem.itemQuantity).toFixed(2)
    }

    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
    // 计算折扣
    getDiscount(_this);
}

/**
  * @desc : 修改销售金额
  * @author : 周兴
  * @date : 2022/7/4 14:45
  */
function changeItemAmountFieldCommon(e, _this) {
    let currentChoosedItem = _this.data.currentChoosedItem
    let key = e.currentTarget.dataset.key


    // currentChoosedItem.itemAmount = e.detail.value // 销售金额赋值
    if (currentChoosedItem.priceStandard < 0 || currentChoosedItem.pricePromotion < 0) {
        currentChoosedItem.itemAmount = Number(0 - e.detail.value)
    } else {
        currentChoosedItem.itemAmount = e.detail.value // 销售金额赋值
    }
    if (currentChoosedItem.flgNonStandardType == 1 || currentChoosedItem.flgNonStandardType == 2) {

        let nonStandardArea = Number(currentChoosedItem.nonStandardArea)
        //非标

        if (currentChoosedItem.itemQuantity && (currentChoosedItem.itemQuantity > 0) && nonStandardArea && (nonStandardArea > 0)) {
            let tempNumber = Number(currentChoosedItem.itemAmount / currentChoosedItem.itemQuantity).toFixed(2)
            currentChoosedItem.priceSale = Number(tempNumber / currentChoosedItem.nonStandardArea).toFixed(2)

        }
    } else {
        if (currentChoosedItem.itemQuantity && currentChoosedItem.itemQuantity > 0) {
            currentChoosedItem.priceSale = Number(currentChoosedItem.itemAmount / currentChoosedItem.itemQuantity).toFixed(2)
        }
    }


    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
    // 计算折扣
    getDiscount(_this);
}

/**
  * @desc : 编辑商品算折扣
  * @author : 于继渤
  * @date : 2022/5/31 18:16
  */
function getDiscount(_this) {
    let currentChoosedItem = _this.data.currentChoosedItem
    //标价计算折扣
    if (currentChoosedItem.priceStandard && currentChoosedItem.priceStandard != 0) {
        // 售价/标价
        if (currentChoosedItem.priceSale < 0) {
            //负数
            currentChoosedItem.discountStandard = Number((currentChoosedItem.priceSale / currentChoosedItem.priceStandard)).toFixed(2)
        } else {
            currentChoosedItem.discountStandard = Number((currentChoosedItem.priceSale / currentChoosedItem.priceStandard) * 100).toFixed(2)
        }

    } else {
        currentChoosedItem.discountStandard = 0
    }
    //促销价计算折扣
    if (currentChoosedItem.discountPromotion && currentChoosedItem.pricePromotion != 0) {
        // 售价/促销价
        currentChoosedItem.discountPromotion = Number((currentChoosedItem.priceSale / currentChoosedItem.pricePromotion) * 100).toFixed(2)
    } else {
        currentChoosedItem.discountPromotion = 100
    }
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
}

/**
  * @desc : 单品促销单选
  * @author : 于继渤
  * @date : 2022/5/31 18:16
  */
function clickDPCommon(e, _this) {

    let currentChoosedItem = _this.data.currentChoosedItem
    let itemt = e.currentTarget.dataset.itemt;
    let PricePromotionData = _this.data.PricePromotionData
    let infotype = itemt.infotype
    currentChoosedItem.pricePromotion = itemt.priceStandard
    PricePromotionData.forEach(it => {
        if (it.typeId === itemt.typeId) {
            it.infotype = 'default'
        }
    })

    if (infotype == 'default') {
        infotype = 'info'
    } else {
        infotype = 'default'
    }
    PricePromotionData.forEach(it => {
        if (it.skuId === itemt.skuId && it.typeId === itemt.typeId) {
            it.infotype = infotype
        }
    })
    _this.setData({
        PricePromotionData: PricePromotionData,
    })

    let PricePromotionList = []
    PricePromotionData.forEach(it => {
        if (it.infotype === 'info') {
            PricePromotionList.push(it)
        }
    })
    currentChoosedItem.PricePromotionList = PricePromotionList
    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
}


//定制品单选
function clickDZPCommon(e, _this) {
    let itemt = e.target.dataset.itemt;
    let pindex = e.target.dataset.pindex;
    let indext = e.target.dataset.indext;
    let formDZP = _this.data.formDZP
    let infotype = formDZP[pindex].tags[indext].infotype

    infotype = 'default'
    formDZP[pindex].tags.forEach(res => {
        res.infotype = 'default'
    })

    if (infotype == 'default') {
        infotype = 'info'
    } else {
        infotype = 'default'
    }

    formDZP[pindex].tags[indext].infotype = infotype
    _this.setData({
        formDZP
    })
}


//主柜规格单选
function clickZGGGCommon(e, _this) {
    let itemt = e.target.dataset.itemt;
    let pindex = e.target.dataset.pindex;
    let indext = e.target.dataset.indext;
    let formZGGG = _this.data.formZGGG
    let infotype = formZGGG[pindex].tags[indext].infotype

    infotype = 'default'
    formZGGG[pindex].tags.forEach(res => {
        res.infotype = 'default'
    })

    if (infotype == 'default') {
        infotype = 'info'
    } else {
        infotype = 'default'
    }
    formZGGG[pindex].tags[indext].infotype = infotype
    _this.setData({
        formZGGG
    })
}

/**
*@desc:设置默认的标价
*@date:2022/9/149:56
*@author:周兴
*/
function setFlgGift(_this) {
    let currentChoosedItem = _this.data.currentChoosedItem

    if (currentChoosedItem.cpFlgGift) {
        currentChoosedItem.flgGift = true
        currentChoosedItem.priceSale_Temp = currentChoosedItem.priceSale
        currentChoosedItem.itemAmount_Temp = currentChoosedItem.itemAmount
        currentChoosedItem.discountStandard_Temp = currentChoosedItem.discountStandard
        currentChoosedItem.discountPromotion_Temp = currentChoosedItem.discountPromotion
        currentChoosedItem.priceSale = 0
        currentChoosedItem.discountStandard = 0
        currentChoosedItem.discountPromotion = 0
        currentChoosedItem.itemAmount = 0
        if (currentChoosedItem.skuType == 2) {
            if (currentChoosedItem.bomItems && currentChoosedItem.bomItems.length > 0) {
                currentChoosedItem.bomItems.forEach(res => {
                    res.priceSale_Temp = res.priceSale
                    res.discountStandard_Temp = res.discountStandard
                    res.discountPromotion_Temp = res.discountPromotion
                    res.itemAmount_Temp = res.itemAmount
                    res.priceSale = 0
                    res.discountStandard = 0
                    res.discountPromotion = 0
                    res.itemAmount = 0
                })
            }
        }
        _this.setData({
            currentChoosedItem: currentChoosedItem,
        })
    }
}

/**
 * @desc : 计算折扣反算销售价格
 * @author : 于继渤
 * @date : 2022/7/4 14:45
 */
function changeDiscountFieldCommon(e, _this, returnFlag) {
    let key = e.currentTarget.dataset.key
    let currentChoosedItem = _this.data.currentChoosedItem
    let priceSale = 0
    if (key == 'discountStandard') {
        let discountStandard = e.detail.value ? Number(e.detail.value) : 0
        if (!currentChoosedItem.priceStandard || currentChoosedItem.priceStandard == 0) {
            priceStandard = 0;
        } else {
            //  售价  = 标价折扣*  标价
            priceSale = Number((discountStandard / 100) * currentChoosedItem.priceStandard).toFixed(2)
        }
        currentChoosedItem.discountStandard = Number(e.detail.value)
    }
    if (key == 'discountPromotion') {
        let discountPromotion = e.detail.value ? Number(e.detail.value) : 0
        if (!currentChoosedItem.pricePromotion || currentChoosedItem.pricePromotion == 0) {
            pricePromotion = 0;
        } else {
            // 售价=  促销折扣 * 促销价
            priceSale = Number((discountPromotion / 100) * currentChoosedItem.pricePromotion).toFixed(2)
        }
        currentChoosedItem.discountPromotion = Number(e.detail.value)
    }
    currentChoosedItem.priceSale = priceSale
    //退还补
    if (returnFlag) {
        currentChoosedItem.tItemAmount = Number(priceSale) * Number(currentChoosedItem.tItemQuantity)
    } else {
        currentChoosedItem.itemAmount = Number(priceSale) * Number(currentChoosedItem.itemQuantity)
    }

    _this.setData({
        currentChoosedItem: currentChoosedItem
    })
}

//chooseGoodsCommon
module.exports = {
    openAddItemInfoCommon: openAddItemInfoCommon,
    chooseSkuItem: chooseSkuItem,
    getReplaceFlag: getReplaceFlag,
    getSkuBySpuId: getSkuBySpuId,
    checkSpecsIsEnable: checkSpecsIsEnable,
    getCombinedByPriceId: getCombinedByPriceId,
    getSumAmount: getSumAmount,
    getCombinedPriceByChange: getCombinedPriceByChange,
    chooseSkuInfoCommon: chooseSkuInfoCommon,
    changeDiscountFieldCommon: changeDiscountFieldCommon,
    handleData: handleData,
    handleChoosedNum: handleChoosedNum,
    changeCombinedItemCommon: changeCombinedItemCommon,
    changeSkuSpecsCommon: changeSkuSpecsCommon,
    setParam: setParam,
    changeAllReadyCheckBoxCommon: changeAllReadyCheckBoxCommon,
    delChoosedGoodsItemCommon: delChoosedGoodsItemCommon,
    openChoosedItemInfoCommon: openChoosedItemInfoCommon,
    changeSalesPriceCommon: changeSalesPriceCommon,
    changeItemAmountFieldCommon: changeItemAmountFieldCommon,
    getDiscount: getDiscount,
    clickDPCommon: clickDPCommon,
    clickDZPCommon: clickDZPCommon,
    clickZGGGCommon: clickZGGGCommon,
    closeAddItemInfoCommon: closeAddItemInfoCommon,
    clearDefaultCommon: clearDefaultCommon,

    //非标
    nonStandardTap: nonStandardTap,
    nonGlassSpuItemsTap: nonGlassSpuItemsTap,
    bindInputIsScopeOne: bindInputIsScopeOne,
    bindBlurIsScopeOne: bindBlurIsScopeOne,
    bindBlurNonStantardArea: bindBlurNonStantardArea,
    bindInputNonStantardArea: bindInputNonStantardArea,
    calculateFormulaItemAmount: calculateFormulaItemAmount,
    bindBlurIsScopeOne_t: bindBlurIsScopeOne_t
}