| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use yii\behaviors\TimestampBehavior;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%order_rules_buyer_location}}".
- *
- * @property integer $id
- * @property integer $store_id
- * @property string $name
- * @property string $detail
- * @property integer $created_at
- * @property integer $is_enable
- * @property integer $is_delete
- * @property string $updated_at
- */
- class OrderRulesBuyerLocation extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%order_rules_buyer_location}}';
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class,
- 'value' => time()
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['store_id', 'name', 'detail'], 'required'],
- [['store_id', 'created_at', 'is_enable', 'is_delete', 'updated_at'], 'integer'],
- [['detail'], 'string'],
- [['name'], 'string', 'max' => 255],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'name' => '名称',
- 'detail' => '规则详细',
- 'created_at' => '创建时间',
- 'is_enable' => '是否启用:0=否,1=是',
- 'is_delete' => 'Is Delete',
- 'updated_at' => '更新时间'
- ];
- }
-
- public static function checkLocationGoods($goods_id, $address) {
- $goods = Goods::findOne($goods_id);
- $rule_id = $goods->order_rules_buyer_location_id;
- return self::checkLocation($rule_id, $address);
- }
- public static function checkLocationGoodsList($goods_ids, $address, $user_id = 0) {
- $res = null;
- foreach ($goods_ids as $gid) {
- if ($address) {
- $address_status = self::checkLocationGoods($gid, $address);
- //检测收货地址是否被限制
- $arr = null;
- if (!$address_status) {
- $arr = [
- 'status' => $address_status,
- 'msg' => '不支持销售到当前收货地址'
- ];
- }
- }
- //检测商品是否有会员限制
- //判断会员是否可以购买
- $goods = Goods::findOne($gid);
- // $saas_user = SaasUser::findOne($address->user_id);
- // $user = User::findOne(['binding' => $saas_user->mobile, 'store_id' => $goods->store_id]);
- if (!empty($goods->buy_type_info)) {
- $buy_type_info = json_decode($goods->buy_type_info, true);
- if ((int)$buy_type_info['type'] === 1) { //会员购买
- if (!empty($buy_type_info['member_level'])) {
- $user = User::findOne($user_id);
- if (!in_array(0, $buy_type_info['member_level']) && !in_array($user->level, $buy_type_info['member_level'])) {
- $level = Level::find()->where(['level' => $buy_type_info['member_level'], 'store_id' => $goods->store_id])
- ->select('name')->column();
- $arr = [
- 'status' => false,
- 'msg' => '仅支持' . implode(',', $level) . '购买',
- ];
- }
- }
- }
- if ((int)$buy_type_info['type'] === 2) { //新用户购买
- $order = Order::findOne(['user_id' => $user_id, 'is_pay' => 1]);
- if (!empty($order)) {
- $arr = [
- 'status' => false,
- 'msg' => '当前商品仅支持新用户购买',
- ];
- }
- }
- }
- if (!empty($arr)) {
- $res[$gid] = $arr;
- }
- }
- return $res;
- }
- public static function checkLocation($rule_id, $address) {
- if(!$rule_id) {
- return true;
- }
- $rule = self::findOne(['id' => $rule_id, 'is_delete' => 0]);
- if(!$rule) {
- return true;
- }
- if (!$rule->is_enable) {
- return true;
- }
- $list = json_decode($rule['detail']);
- if (empty($list)) {
- return true;
- }
- foreach ($list as $item) {
- foreach ($item->province_list as $province) {
- if ($province->id == $address->province_id || $province->id==$address->city_id || $province->id==$address->district_id) {
- return true;
- }
- }
- }
- return false;
- }
- }
|