'ID', 'mall_id' => '商城id', 'mch_id' => '商家id', 'goods_id' => '商品id', 'price' => '售价', 'original_price' => '原价(划线价仅展示)', 'cost_price' => '成本价', 'reserve_date_type' => '可预约时间类型(1范围 2自选日期)', 'reserve_week' => '可预约周', 'reserve_date' => '可预约日期', 'reserve_time_range_type' => '预约模式(1按天预约 2按时间段预约)', 'time_slot_type' => '时间段划分方式(1自定义划分 2自动划分)', 'reserve_time_slot' => '时间段划分', 'reserve_price_type' => '价格设置方式(0统一售价 1按日期时间段设置)', 'reserve_time_slot_price' => '时间段价格设置', 'reserve_max_buy' => '每天最大预约数量', 'reserve_advance_hourse' => '提前预约时间(小时)', 'reserve_days_range' => '限制选择并购买的时间在设置内天数的商品', 'cancel_type' => '取消类型', 'cancel_rule' => '取消规则', 'expired_handle_type' => '预约码过期后处理方式', "province_id" => "省ID", "city_id" => "城市ID", "district_id" => "区县ID", "detail" => "详细地址", "lat" => "纬度", "lng" => "经度", "region_name" => '地区字符串', 'created_at' => '创建时间', 'updated_at' => '修改时间', ]; } public function getSkus() { return $this->hasMany(GoodsReserveSku::class, ['goods_id' => 'goods_id']); } public function getGoods() { return $this->hasOne(Goods::class, ['id' => 'goods_id']); } // 预约商品 public static function setData(&$goods, $params) { try { $model = self::findOne(['goods_id' => $goods['id']]); if (empty($model)) { $model = new self(); $model->loadDefaultValues(); } $goodsAttributes = [ 'mall_id' => $goods['mall_id'], 'mch_id' => $goods['mch_id'], 'goods_id' => $goods['id'], 'price' => $goods['price'] ?? min(array_column($params['skus'], "price")), 'original_price' => $goods['original_price'] ?? min(array_column($params['skus'], "original_price")), 'cost_price' => $goods['cost_price'] ?? min(array_column($params['skus'], !empty($params['skus'][0]["cost_price"]) ? "cost_price" : "price")), ]; $params['information_detail'] = $params['information_detail'] ?? ''; $params['ordering_information'] = $params['ordering_information'] ?? ''; $params['ordering_information'] = (string)$params['ordering_information']; $params['district_id'] = empty($params['district_id']) ? 0 : intval($params['district_id']); $params['province_id'] = empty($params['province_id']) ? 0 : intval($params['province_id']); $params['city_id'] = empty($params['city_id']) ? 0 : intval($params['city_id']); $params['reserve_advance_hourse'] = empty($params['reserve_advance_hourse']) ? 0 : intval($params['reserve_advance_hourse']); $params['distance'] = empty($params['distance']) ? 0 : intval($params['distance']); $params['service_min'] = empty($params['service_min']) ? 0 : intval($params['service_min']); if (!empty($params['province_id'])) { $params['region_name'] = implode(" ", Region::find() ->where(['id' => [$params['province_id'], $params['city_id'], $params['district_id']]]) ->orderBy('id asc')->select('name')->column()); } if (empty($goods['is_attr'])) { // 如果 $params['price'] = $goods['price'] ?? $goodsAttributes['price']; } $attributes = array_merge($goodsAttributes, $params); is_array($attributes['reserve_week']) && $attributes['reserve_week'] = implode(',',$attributes['reserve_week']); is_array($attributes['reserve_date']) && $attributes['reserve_date'] = json_encode($attributes['reserve_date'],JSON_UNESCAPED_SLASHES); is_array($attributes['reserve_time_slot']) && $attributes['reserve_time_slot'] = json_encode($attributes['reserve_time_slot'],JSON_UNESCAPED_SLASHES); is_array($attributes['reserve_time_slot_price']) && $attributes['reserve_time_slot_price'] = json_encode($attributes['reserve_time_slot_price'],JSON_UNESCAPED_SLASHES); if(empty($attributes['cancel_rule']['cancel_hour'])){ $attributes['cancel_rule']['cancel_hour'] = 0; } if(empty($attributes['cancel_rule']['cancel_fee_percent'])){ $attributes['cancel_rule']['cancel_fee_percent'] = 0; } is_array($attributes['cancel_rule']) && $attributes['cancel_rule'] = json_encode($attributes['cancel_rule'],JSON_UNESCAPED_SLASHES); if (!empty($attributes['reserve_type'])) { // 项目服务人预约 $attributes['service_type'] = StatusEnum::ENABLED; // 项目服务人预约只能上门服务 } //($attributes['service_type'] ?? false) && $attributes['service_type'] = json_encode($attributes['service_type'],JSON_UNESCAPED_SLASHES); if (!$model->load($attributes, '') || !$model->save()) { throw new Exception($model->getErrorMessage()); } // 处理预约商品规格 $reserveGoods = ArrayHelper::toArray($model); if($goods['is_attr'] == 1){ // 多规格预约商品 $skus = $params['skus']; // 因为预约商品按天设置时商品售价存储位置不同,所以需要单独处理 $is_day = true; $timeSlotPriceList = []; foreach ($skus as $item) { if ($item['reserve_price_type'] == 2 || $item['reserve_price_type'] == 1) { $timeSlotPriceList = array_merge($timeSlotPriceList, array_values($item['reserve_time_slot_price'])); } else { $is_day = false; } } if (!empty($timeSlotPriceList) && $is_day) { $timeSkusGoods = array_column($timeSlotPriceList, 'price'); $timeMinSkusGoods = array_search(min($timeSkusGoods), $timeSkusGoods); $timeSlotPriceList = $timeSlotPriceList[$timeMinSkusGoods] ?? []; } $skusGoods = array_column($skus, 'price'); $minSkusGoods = array_search(min($skusGoods), $skusGoods); $reserveSkusGoods = $skus[$minSkusGoods] ?? []; $goodsModel = Goods::findOne($goods['id']); if (!empty($goodsModel)) { // 赋值给主商品对应的商品 if ($is_day) { $goodsModel->price = $timeSlotPriceList['price'] ?? 0; $goodsModel->original_price = $timeSlotPriceList['original_price'] ?? 0; } else { $goodsModel->price = $reserveSkusGoods['price'] ?? 0; $goodsModel->original_price = $reserveSkusGoods['original_price'] ?? 0; } $goodsModel->save(); } $res = GoodsReserveSku::setSkus($goods, $skus, $model); }else{ $reserveGoods['service_min'] = $params['service_min']; $reserveGoods['unit'] = $goods['unit'] ?? ''; is_array($reserveGoods['reserve_time_slot_price']) && $reserveGoods['reserve_time_slot_price'] = Json::encode($reserveGoods['reserve_time_slot_price']); $reserveGoods['price'] = $goodsAttributes['price']; $reserveGoods['name'] = ''; $res = GoodsReserveSku::setSku($reserveGoods); } if($res === false) throw new Exception('处理预约商品规格出现异常,msg:'.GoodsReserveSku::getStaticError()); return $model; } catch (Exception $e) { if (isset($trans)) $trans->rollback(); self::$static_error = $e->getMessage(); return false; } } }