OrderRulesBuyerLocation.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use yii\behaviors\TimestampBehavior;
  9. use yii\helpers\Json;
  10. /**
  11. * This is the model class for table "{{%order_rules_buyer_location}}".
  12. *
  13. * @property integer $id
  14. * @property integer $store_id
  15. * @property string $name
  16. * @property string $detail
  17. * @property integer $created_at
  18. * @property integer $is_enable
  19. * @property integer $is_delete
  20. * @property string $updated_at
  21. */
  22. class OrderRulesBuyerLocation extends \yii\db\ActiveRecord
  23. {
  24. /**
  25. * @inheritdoc
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%order_rules_buyer_location}}';
  30. }
  31. public function behaviors()
  32. {
  33. return [
  34. [
  35. // 自动更新创建和更新时间
  36. 'class' => TimestampBehavior::class,
  37. 'value' => time()
  38. ]
  39. ];
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [['store_id', 'name', 'detail'], 'required'],
  48. [['store_id', 'created_at', 'is_enable', 'is_delete', 'updated_at'], 'integer'],
  49. [['detail'], 'string'],
  50. [['name'], 'string', 'max' => 255],
  51. ];
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. public function attributeLabels()
  57. {
  58. return [
  59. 'id' => 'ID',
  60. 'store_id' => 'Store ID',
  61. 'name' => '名称',
  62. 'detail' => '规则详细',
  63. 'created_at' => '创建时间',
  64. 'is_enable' => '是否启用:0=否,1=是',
  65. 'is_delete' => 'Is Delete',
  66. 'updated_at' => '更新时间'
  67. ];
  68. }
  69. public static function checkLocationGoods($goods_id, $address) {
  70. $goods = Goods::findOne($goods_id);
  71. $rule_id = $goods->order_rules_buyer_location_id;
  72. return self::checkLocation($rule_id, $address);
  73. }
  74. public static function checkLocationGoodsList($goods_ids, $address, $user_id = 0) {
  75. $res = null;
  76. foreach ($goods_ids as $gid) {
  77. if ($address) {
  78. $address_status = self::checkLocationGoods($gid, $address);
  79. //检测收货地址是否被限制
  80. $arr = null;
  81. if (!$address_status) {
  82. $arr = [
  83. 'status' => $address_status,
  84. 'msg' => '不支持销售到当前收货地址'
  85. ];
  86. }
  87. }
  88. //检测商品是否有会员限制
  89. //判断会员是否可以购买
  90. $goods = Goods::findOne($gid);
  91. // $saas_user = SaasUser::findOne($address->user_id);
  92. // $user = User::findOne(['binding' => $saas_user->mobile, 'store_id' => $goods->store_id]);
  93. if (!empty($goods->buy_type_info)) {
  94. $buy_type_info = json_decode($goods->buy_type_info, true);
  95. if ((int)$buy_type_info['type'] === 1) { //会员购买
  96. if (!empty($buy_type_info['member_level'])) {
  97. $user = User::findOne($user_id);
  98. if (!in_array(0, $buy_type_info['member_level']) && !in_array($user->level, $buy_type_info['member_level'])) {
  99. $level = Level::find()->where(['level' => $buy_type_info['member_level'], 'store_id' => $goods->store_id])
  100. ->select('name')->column();
  101. $arr = [
  102. 'status' => false,
  103. 'msg' => '仅支持' . implode(',', $level) . '购买',
  104. ];
  105. }
  106. }
  107. }
  108. if ((int)$buy_type_info['type'] === 2) { //新用户购买
  109. $order = Order::findOne(['user_id' => $user_id, 'is_pay' => 1]);
  110. if (!empty($order)) {
  111. $arr = [
  112. 'status' => false,
  113. 'msg' => '当前商品仅支持新用户购买',
  114. ];
  115. }
  116. }
  117. }
  118. if (!empty($arr)) {
  119. $res[$gid] = $arr;
  120. }
  121. }
  122. return $res;
  123. }
  124. public static function checkLocation($rule_id, $address) {
  125. if(!$rule_id) {
  126. return true;
  127. }
  128. $rule = self::findOne(['id' => $rule_id, 'is_delete' => 0]);
  129. if(!$rule) {
  130. return true;
  131. }
  132. if (!$rule->is_enable) {
  133. return true;
  134. }
  135. $list = json_decode($rule['detail']);
  136. if (empty($list)) {
  137. return true;
  138. }
  139. foreach ($list as $item) {
  140. foreach ($item->province_list as $province) {
  141. if ($province->id == $address->province_id || $province->id==$address->city_id || $province->id==$address->district_id) {
  142. return true;
  143. }
  144. }
  145. }
  146. return false;
  147. }
  148. }