500], [['sign_id', 'pic_url'], 'string', 'max' => 255], [['reserve_week'], 'string', 'max' => 32], [['unit'], 'string', 'max' => 50], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'store_id' => 'Store ID', 'name' => 'Name', 'sign_id' => 'Sign ID', 'pic_url' => 'Pic Url', 'goods_id' => 'Goods ID', 'price' => 'Price', 'original_price' => 'Original Price', 'cost_price' => 'Cost Price', 'reserve_date_type' => 'Reserve Date Type', 'reserve_week' => 'Reserve Week', 'reserve_date' => 'Reserve Date', 'reserve_time_range_type' => 'Reserve Time Range Type', 'time_slot_type' => 'Time Slot Type', 'reserve_time_slot' => 'Reserve Time Slot', 'reserve_price_type' => 'Reserve Price Type', 'reserve_time_slot_price' => 'Reserve Time Slot Price', 'reserve_max_buy' => 'Reserve Max Buy', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'service_min' => 'Service Min', 'unit' => 'Unit', ]; } public function getReserve() { return $this->hasOne(StoreGoodsReserve::class, ['goods_id' => 'goods_id']); } public function getGoods() { return $this->hasOne(StoreGoods::class, ['id' => 'goods_id']); } public function getTimes() { return $this->hasMany(StoreGoodsReserveTime::class, ['reserve_sku_id' => 'id']); } // 无规格sku存储 public static function setSku($reserveGoods) { try { // 清除SKU // self::deleteAll(['goods_id' => $reserveGoods['goods_id']]); $model = self::findOne(['goods_id' => $reserveGoods['goods_id']]); if (empty($model)) { $model = new self(); $model->loadDefaultValues(); $model->goods_id = $reserveGoods['goods_id']; } $trans = Yii::$app->db->beginTransaction(); if (!$model->load($reserveGoods, '') || !$model->save()) { throw new Exception($model->getErrorMessage()); } // 写入预约时间 $reserveGoodsSku = ArrayHelper::toArray($model); $res = StoreGoodsReserveTime::setSkuTime($reserveGoodsSku); if ($res === false) throw new Exception('处理预约商品预约时间异常,msg:' . StoreGoodsReserveTime::getStaticError()); $trans->commit(); return true; } catch (Exception $e) { if (isset($trans)) $trans->rollback(); self::$static_error = $e->getMessage(); return false; } } /** * @param array $goods * @param array $skus * @param StoreGoodsReserve $reserve * @return bool */ public static function setSkus($goods, $skus, $reserve) { try { $attrGroups = json_decode($goods['attr_groups'], true); if (!empty($skus)) { $sign_ids = []; foreach ($skus as $sku) { $sku_ids = array_column($sku['attr_list'], 'attr_id'); asort($sku_ids); $sku['pic_url'] = $skuPicList[$sku_ids[0]] ?? ''; $sign_ids[] = implode(':', $sku_ids); } self::deleteAll([ 'and', ['goods_id' => $goods['id']], ['not in', 'sign_id', $sign_ids] ]); $total_stock = 0 ; $skuPicList = array_column($attrGroups[0]['attr_list'], 'pic_url', 'attr_id'); foreach ($skus as $sku) { //print_r($sku); $sku['mall_id'] = $goods['mall_id']; $sku['store_id'] = $goods['store_id']; $sku['goods_id'] = $goods['id']; $sku['price'] = $sku['price'] ?? $goods['price']; $sku['original_price'] = $sku['original_price'] ?? $goods['original_price']; $sku['cost_price'] = $goods['cost_price']; $sku['service_min'] = !empty($sku['service_min']) ? $sku['service_min'] : $reserve->service_min; $sku['unit'] = !empty($sku['unit']) ? $sku['unit'] : $goods['unit']; $sku_ids = array_column($sku['attr_list'], 'attr_id'); asort($sku_ids); $sku['pic_url'] = $skuPicList[$sku_ids[0]] ?? ''; $sku['sign_id'] = implode(':', $sku_ids); is_array($sku['reserve_week']) && $sku['reserve_week'] = implode(',',$sku['reserve_week']); is_array($sku['reserve_date']) && $sku['reserve_date'] = json_encode($sku['reserve_date'],JSON_UNESCAPED_SLASHES); is_array($sku['reserve_time_slot']) && $sku['reserve_time_slot'] = json_encode($sku['reserve_time_slot'],JSON_UNESCAPED_SLASHES); is_array($sku['reserve_time_slot_price']) && $sku['reserve_time_slot_price'] = json_encode($sku['reserve_time_slot_price'],JSON_UNESCAPED_SLASHES); $sku['reserve_max_buy'] = $sku['reserve_max_buy'] > 0 ? $sku['reserve_max_buy'] : 0; $total_stock = $total_stock + $sku['reserve_max_buy']; $sku_group_name_list = array_column($sku['attr_list'], 'attr_group_name'); $sku_name_list = array_column($sku['attr_list'], 'attr_name'); $sku_name_arr = []; foreach ($sku_group_name_list as $index => $sku_group_name) { $sku_name_arr[] = $sku_group_name . ':' . $sku_name_list[$index]; } $sku['name'] = implode(',', $sku_name_arr); if (!empty($sku['sign_id'])) { $model = self::findOne(['sign_id' => $sku['sign_id'], 'goods_id' => $goods['id']]); if (empty($model)) { $model = new self(); $model->loadDefaultValues(); $model->goods_id = $goods['id']; } } else { $model = new self(); $model->loadDefaultValues(); $model->goods_id = $goods['id']; } if (!$model->load($sku, '') || !$model->save()) { throw new Exception($model->getErrorMessage()); } // 写入预约时间 $reserveGoodsSku = ArrayHelper::toArray($model); $res = StoreGoodsReserveTime::setSkuTime($reserveGoodsSku); if ($res === false) throw new Exception('处理预约商品预约时间异常,msg:' . StoreGoodsReserveTime::getStaticError()); } //修改商品总库存 if($total_stock > 0){ $goods = StoreGoods::findOne($goods['id']); $goods->stock = $goods->stock + $total_stock; if (!$goods->save()) throw new Exception($goods->getErrorMessage()); } } return true; } catch (Exception $e) { if (isset($trans)) $trans->rollback(); self::$static_error = $e->getMessage(); return false; } } }