/**
 * @params sumary  Map() 所有数据的集合
 * @params bundling  Map() 套装数据的集合
 * @params goods_list  需要遍历的数据 array, 每个item是object, goods_id, goods_num, bl_id // bl_id > 0,查bundling 反之goods_id查sumary
 */
// bl_id: 12, bl_name: "222", bl_price: 122, goods: Array(4), goods_num: 3 有套餐返回的数据样式， 渲染时判断bl_id 再循环goods即可。。。 goods_num通用
// goods_id: 4807, goods_num: 1, bl_id: 0, goods_summary{} 无套餐返回的数据样式， 渲染时取goods_summary数据即可

 

export default class Bundle {
  constructor(goods_list,sumary,bundling) {
    this.goods_list = goods_list
    this.sumary = sumary
    this.bundling = bundling
    // this.createOrders()
  }

  createOrders() {
    let arr = []
    this.goods_list.forEach(item => {
      if(item['bl_id'] > 0) {
        // 有套餐， 生成套餐数据
        let data = this.createBuildings(item)
        data = Object.assign({}, item, data)
        // data['goods_num'] = item['goods_num']
        let totalPrice = 0;
        data['goods'].forEach(value => {
          value['goods_summary'] = this.sumary.get(value['goods_id'])
          value['goods_summary']['goods_price'] = parseFloat(value['goods_summary']['goods_price'])
          totalPrice += value['goods_summary']['goods_price']
        })
        data['totalPrice'] = totalPrice
        arr.push(data)
      }
      else {
        let defaultData = this.createDefault(item)
        item['goods_summary'] = defaultData
        item['goods_summary']['bonus_price'] = parseFloat(item['goods_summary']['bonus_price'])
        item['goods_summary']['goods_promotion_price'] = parseFloat(item['goods_summary']['goods_promotion_price'])
        item['goods_summary']['goods_price'] = parseFloat(item['goods_summary']['goods_price'])
        arr.push(item)
      }
    })
    return arr
  }
  createBuildings(item) {
    return this.bundling.get(item['bl_id'])
  }
  createDefault(item) {
    return this.sumary.get(item['goods_id'])
  }
}