| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- namespace addons\Store\common\models;
- use common\models\common\Region;
- use Exception;
- use Yii;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Json;
- /**
- * This is the model class for table "qimall_addons_store_goods_reserve".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $store_id 商家id
- * @property int $goods_id 商品id
- * @property float $price 售价
- * @property float $original_price 原价(划线价仅展示)
- * @property float $cost_price 成本价
- * @property int $reserve_date_type 可预约时间类型(1范围 2自选日期)
- * @property string|null $reserve_week 可预约周
- * @property string|null $reserve_date 可预约日期
- * @property int $reserve_time_range_type 预约模式(1按天预约 2按时间段预约)
- * @property int $time_slot_type 时间段划分方式(1自定义划分 2自动划分)
- * @property string|null $reserve_time_slot 时间段划分
- * @property int $reserve_price_type 价格设置方式(0统一售价 1按日期时间段设置)
- * @property string|null $reserve_time_slot_price 时间段价格设置
- * @property int $reserve_max_buy 每天最大预约数量
- * @property int $reserve_advance_hourse 提前预约时间(小时)
- * @property int $reserve_days_range 限制选择并购买的时间在设置内天数的商品
- * @property int $cancel_type 取消类型(1不允许取消 2服务开始前取消 3订单类型取消)
- * @property string $cancel_rule 取消退款规则
- * @property int $expired_handle_type 预约码过期后处理方式(1不退款 2自动退款)
- * @property int $province_id 省ID
- * @property int $city_id 市ID
- * @property int $district_id 区县ID
- * @property string $detail 详细地址
- * @property string $lat 纬度
- * @property string $lng 经度
- * @property string $region_name 省市区字符串
- * @property int $service_type 服务方式(1上门 2到店)
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property string $ordering_information 订购须知
- * @property string $information_detail 须知详情
- * @property int $reserve_type 0商品预约,1项目服务预约
- * @property int $service_min 0商品预约,1项目服务预约
- * @property float $distance 0商品预约,1项目服务预约
- * @property int $appointments_number 同时间点可预约人数
- */
- class StoreGoodsReserve extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_store_goods_reserve';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'goods_id', 'reserve_date_type', 'reserve_time_range_type', 'time_slot_type', 'reserve_price_type',
- 'reserve_max_buy', 'reserve_advance_hourse','reserve_days_range', 'cancel_type', 'expired_handle_type', 'province_id',
- 'city_id', 'district_id', 'created_at', 'updated_at', 'reserve_type', 'service_min'], 'integer'],
- [['price', 'original_price', 'cost_price', 'distance'], 'number'],
- [['reserve_date', 'reserve_time_slot', 'reserve_time_slot_price', 'reserve_week', 'cancel_rule', 'detail','region_name'], 'string'],
- [['information_detail','ordering_information'],'string'],
- [[ 'lat', 'lng'],'safe'],
- [['service_type','appointments_number'],'integer']
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'store_id' => 'Store ID',
- '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',
- 'reserve_advance_hourse' => 'Reserve Advance Hourse',
- 'reserve_days_range' => 'Reserve Days Range',
- 'cancel_type' => 'Cancel Type',
- 'cancel_rule' => 'Cancel Rule',
- 'expired_handle_type' => 'Expired Handle Type',
- 'province_id' => 'Province ID',
- 'city_id' => 'City ID',
- 'district_id' => 'District ID',
- 'detail' => 'Detail',
- 'lat' => 'Lat',
- 'lng' => 'Lng',
- 'region_name' => 'Region Name',
- 'service_type' => 'Service Type',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'ordering_information' => 'Ordering Information',
- 'information_detail' => 'Information Detail',
- 'reserve_type' => 'Reserve Type',
- 'appointments_number' => 'appointments_number',
- ];
- }
- public function getSkus()
- {
- return $this->hasMany(StoreGoodsReserveSku::class, ['goods_id' => 'goods_id']);
- }
- public function getGoods()
- {
- return $this->hasOne(StoreGoods::class, ['id' => 'goods_id']);
- }
- // 预约商品
- /**
- * @param array $goods
- * @param array $params
- * @return StoreGoodsReserve|false|null
- */
- 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'],
- 'store_id' => $goods['store_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['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']);
- $params['appointments_number'] = empty($params['appointments_number']) ? 0 : intval($params['appointments_number']);
- 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'])) { // 是单规格 将商品设置到规格中
- $attributes = array_merge($params, $goodsAttributes);
- } else {
- $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);
- is_array($attributes['cancel_rule']) && $attributes['cancel_rule'] = json_encode($attributes['cancel_rule'],JSON_UNESCAPED_SLASHES);
- //($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 = StoreGoods::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 = StoreGoodsReserveSku::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 = StoreGoodsReserveSku::setSku($reserveGoods);
- }
- if($res === false) throw new Exception('处理预约商品规格出现异常,msg:'.StoreGoodsReserveSku::getStaticError());
- return $model;
- } catch (Exception $e) {
- if (isset($trans)) $trans->rollback();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|