sku算法分享

894 阅读2分钟

本文纯属个人实战收获和分享

// 点击属性项
attrClick (it, index, idx) {  
    let isSot = false // 记录一下是否点击-取消  
    if (this.selectArr[index] === it.value) {    // 如果是取消选中    
        isSot = true    
        this.$set(this.selectArr, index, '')    
        it.active = false    
        this.data.salePrice = null    //清空准备提交的一些属性值
        this.data.startSaleNum = null    
        this.data.quantity = null    
        this.data.skuId = null    
        this.data.skuName = ''     
    } else { // 一个属性行只能选中一个属性值
        isSot = false    
        this.$set(this.selectArr, index, it.value)    
        this.data.attributeLists[index].values.forEach(e => {      
            e.active = false    
        })    
        it.active = true  
    }  
    if (this.selectArr.filter(d=>d).length === this.data.attributeLists.length) {    
        // 选取了所有的规格时 拿去skuId    
        let str = ''    
        this.selectArr.forEach(item => {      
            str += '_' + item
        })
        str = str.slice(1)
        // 所有规格都选了时,进行匹配skuId,此处数据用于传给后台
        this.data.mallProductSpecsVOList.forEach(item => { // 匹配skuId
            if (item.attrValue && item.attrValue === str) {
                this.data.salePrice = item.salePrice
                this.data.startSaleNum = item.startSaleNum
                this.data.quantity = item.startSaleNum
                this.data.skuId = item.id
                this.data.skuName = item.attrValue
            } //this.formData.skuId = item.id
        })
    }  
    // 做完点击操作,准备去计算sku
    this.getData(
        index, 
        it.value, 
        this.selectArr, 
        this.data.attributeLists, 
        this.data.mallProductSpecsVOList, 
        isSot) // 校验库存
}

// 进行循环判断哪些sku组合库存为空
// 点击属性行, 点击的属性值, 点击后存起来的属性值组, 属性组, sku组, 是否是点击取消
getData(itindex, itvalue, selectArr, skuSpec, skuList, isSot) {
  //let checkLen = skuSpec.length - 1  // 3行规格 选2行时校验,2行规格,选1行时校验
  let selectArrC = JSON.parse(JSON.stringify(selectArr)).filter(d=>d);
  if (selectArrC.length === 1) {
    // 如果只有一个属性, 就先重置一下
    skuSpec.forEach((item) => {
      item.values.forEach((it) => {
        it.hasskuSn = true
      })
    })
  }
  if (selectArrC.length === 0) { // 如果没有属性, 就重置一下
    skuSpec.forEach((item) => {
      item.values.forEach((it) => {
        it.hasskuSn = true
      })
    })
  } else {
    skuSpec.forEach((item, index) => { // 先把选中的属性之外的属性行筛选一下
      if (selectArr[index]) return
      item.values.forEach((it, idx) => {
        let c = JSON.parse(JSON.stringify(selectArr)) // 深度拷贝
        c[index] = it.value
        let hasType = false // 是否存在
        let state1 = false // 状态1
        let state2 = true // 状态2
        // 组合后去sku组里面查询是否存在
        skuList.forEach((e, i) => {
          if (hasType === false) {
            state2 = true
            c.forEach(f =>{
              if (f === null) return
              if(e.attrValue && e.attrValue.indexOf(f) !== -1) {
                state1 = true
              } else {
                state1 = false
                state2 = false
              }
            })
            if (state1 === true && state2 === true) {
              it.hasskuSn = true
              hasType = true
            } else {
              it.hasskuSn = false
            }
          }
        })
      })
    })
    // 单个属性在遍历一遍
    if (isSot === false) { // 如果不是点击-取消 这个属性 就把这个属性之外的属性行 再筛选一遍
      skuSpec.forEach((item, index) => {
        let selectArrOne = []
        selectArrOne[itindex] = itvalue
        if (selectArrOne[index]) return
        item.values.forEach((it, idx) => {
          selectArrOne[index] = it.value
          let hasType = false // 是否存在
          let state1 = false // 状态1
          let state2 = true // 状态2
          // 组合后去sku组里面查询是否存在
          skuList.forEach((e, i) => {
            if (hasType === false) {
              state2 = true
              selectArrOne.forEach(f =>{
                //console.log(f)
                if (f === null) return
                if(e.attrValue && e.attrValue.indexOf(f) !== -1) {
                  state1 = true
                } else {
                  state1 = false
                  state2 = false
                }
              })
              if (state1 === true && state2 === true) {
                it.hasskuSn = true
                hasType = true
              } else {
                it.hasskuSn = false
              }
            }
          })
        })
      })
      if(this.data.skuId === null){
        this.data.skuId = 0
        this.data.skuId = null
      }
    }
  }}