| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace common\models\goods;
- use common\enums\StatusEnum;
- use common\helpers\ArrayHelper;
- use common\models\common\Region;
- use Exception;
- use yii\helpers\Json;
- /**
- * 预约商品模型
- * This is the model class for table "{{%goods_reserve}}"
- * @property int int $id
- * @property int $mall_id 商城id
- * @property int $mch_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 $reserve_date 可预约日期
- * @property int $reserve_time_range_type 预约模式(1按天预约 2按时间段预约)
- * @property int $time_slot_type 时间段划分方式(1自定义划分 2自动划分)
- * @property string $reserve_time_slot 时间段划分
- * @property int $reserve_price_type 价格设置方式(0统一售价 1按日期时间段设置)
- * @property int $reserve_time_slot_price 时间段价格设置
- * @property int $reserve_max_buy 每天最大预约数量
- * @property int $reserve_advance_hourse 提前预约时间(小时)
- * @property int $reserve_days_range 限制选择并购买的时间在设置内天数的商品
- * @property int $reserve_date_range 限制选择并购买N天以内的时间段
- * @property string $reserve_week 可预约周(逗号分隔)
- * @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 int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $service_type 服务方式(1上门 2到店,3上门/到店)
- * @property int $reserve_type 0商品预约,1项目服务预约
- * @property int $service_min 0商品预约,1项目服务预约
- * @property string $ordering_information
- * @property string $information_detail
- * @property string $region_name
- * @property float $distance
- */
- class GoodsReserve extends \common\models\base\BaseActiveRecord
- {
- const RESERVE_TYPE_HOME_MARKING = 0; //项目预约
- const RESERVE_TYPE_SERVICE = 1; //技师预约
- const RESERVE_SERVICE_TO = 1;
- const RESERVE_SERVICE_FROM = 2;
- const RESERVE_SERVICE_ALL = 3;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%goods_reserve}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'mch_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'],'string'],
- [[ 'lat', 'lng','ordering_information'],'safe'],
- [['service_type'],'integer']
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '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;
- }
- }
- }
|