RebateShare.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * @Descripttion:
  4. * @copyright: Copyright (c) 2021 广东七件事集团
  5. * @Author: lun
  6. * @Date: 2022-01-01 15:55:25
  7. */
  8. namespace addons\Rebate\common\models;
  9. use addons\Rebate\common\enums\RebateEnum;
  10. use common\enums\StatusEnum;
  11. use common\models\user\User;
  12. use Yii;
  13. use yii\db\Exception;
  14. /**
  15. * This is the model class for table "qimall_addons_rebate_share".
  16. *
  17. * @property int $id
  18. * @property int $mall_id 商城id
  19. * @property int $r_id 对应qimall_addons_rebate.id
  20. * @property int $pid 分享者id
  21. * @property int $user_id 用户id
  22. * @property int $item_id 商品id
  23. * @property int $status 状态[-1:删除;0:禁用;1启用]
  24. * @property int $created_at 创建时间
  25. * @property int $updated_at 更新时间
  26. */
  27. class RebateShare extends \common\models\base\BaseActiveRecord
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function tableName()
  33. {
  34. return 'qimall_addons_rebate_share';
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['mall_id'], 'required'],
  43. [['mall_id', 'r_id', 'pid', 'user_id', 'item_id', 'status', 'created_at', 'updated_at'], 'integer'],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'mall_id' => '商城id',
  54. 'r_id' => '对应qimall_addons_rebate.id',
  55. 'pid' => '分享者id',
  56. 'user_id' => '用户id',
  57. 'item_id' => '商品id',
  58. 'status' => '状态[-1:删除;0:禁用;1启用]',
  59. 'created_at' => '创建时间',
  60. 'updated_at' => '更新时间',
  61. ];
  62. }
  63. /**
  64. * 关联用户表
  65. * @Author: lun
  66. * @DateTime: 2022/1/15 0015 9:36
  67. * @Copyright: copyright (c) 2021 广东七件事集团
  68. * @return \yii\db\ActiveQuery
  69. */
  70. public function getUser()
  71. {
  72. return $this->hasOne(User::class, ['id' => 'user_id']);
  73. }
  74. /**
  75. * 保存数据
  76. * @Author: lun
  77. * @DateTime: 2022/1/15 0015 9:36
  78. * @Copyright: copyright (c) 2021 广东七件事集团
  79. * @param $mall_id
  80. * @param array $params
  81. * @return bool
  82. */
  83. public static function setData($mall_id, array $params){
  84. $trans = Yii::$app->db->beginTransaction();
  85. try{
  86. $model = self::findOne(['mall_id' => $mall_id, 'r_id' => $params['r_id'], 'pid' => $params['pid'], 'user_id' => $params['user_id'], 'status' => 1]);
  87. if ($model) throw new Exception('分享已存在');
  88. $model = new self();
  89. $model->loadDefaultValues();
  90. $model->mall_id = $mall_id;
  91. if(!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  92. // 增加推荐者分享数量
  93. $params['user_id'] = $params['pid'];
  94. $params['share_num'] = 1;
  95. $res = RebateRecommendNum::setData($mall_id, $params);
  96. if($res == false) throw new Exception('增加分享数量失败:' . RebateRecommendNum::getStaticError());
  97. $trans->commit();
  98. return true;
  99. }catch(\Exception $e){
  100. $trans->rollBack();
  101. self::setStaticError($e->getMessage());
  102. return false;
  103. }
  104. }
  105. /**
  106. * 判断用户是否达到分享条件,并保存分享数据
  107. * @Author: lun
  108. * @DateTime: 2022/1/18 0018 19:32
  109. * @Copyright: copyright (c) 2021 广东七件事集团
  110. * @param $mall_id
  111. * @param $goods_id
  112. * @param int $user_id
  113. * @param int $parent_id
  114. * @return bool
  115. */
  116. public static function checkShare($mall_id, $goods_id, $user_id = 0, $parent_id = 0)
  117. {
  118. // 根据商品id获取规则id
  119. $select = "id,initiate_type,initiate_level,consume_type,consume_level";
  120. $rebate = Rebate::find()->select($select)->where(['mall_id' => $mall_id,'item_id' => $goods_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
  121. if (empty($rebate)) return true;
  122. // 判断上级是否有权限
  123. if($rebate['initiate_type'] == RebateEnum::INITIATE_TYPE_NEW && Rebate::checkUserHasOrder($mall_id, $parent_id) !== false) return true;
  124. if($rebate['initiate_type'] == RebateEnum::INITIATE_TYPE_LEVEL && Rebate::checkUserLevel($parent_id, $rebate['initiate_level']) == false) return true;
  125. // 判断消费者是否符合权限
  126. if($rebate['consume_type'] == RebateEnum::CONSUME_TYPE_NEW) {
  127. $order_num = Rebate::checkUserHasOrder($mall_id, $user_id);
  128. if (!empty($order_num) && $order_num > 1) return true;
  129. }
  130. if($rebate['consume_type'] == RebateEnum::CONSUME_TYPE_LEVEL && Rebate::checkUserLevel($user_id, $rebate['consume_level']) == false) return true;
  131. // 插入数据
  132. $params['r_id'] = $rebate['id'];
  133. $params['pid'] = $parent_id;
  134. $params['user_id'] = $user_id;
  135. $params['item_id'] = $goods_id;
  136. self::setData($mall_id, $params);
  137. }
  138. }