| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- /*
- * @Descripttion:
- * @copyright: Copyright (c) 2021 广东七件事集团
- * @Author: lun
- * @Date: 2022-01-01 15:55:25
- */
- namespace addons\Rebate\common\models;
- use addons\Rebate\common\enums\RebateEnum;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "qimall_addons_rebate_share".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $r_id 对应qimall_addons_rebate.id
- * @property int $pid 分享者id
- * @property int $user_id 用户id
- * @property int $item_id 商品id
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- */
- class RebateShare extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_rebate_share';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id'], 'required'],
- [['mall_id', 'r_id', 'pid', 'user_id', 'item_id', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'r_id' => '对应qimall_addons_rebate.id',
- 'pid' => '分享者id',
- 'user_id' => '用户id',
- 'item_id' => '商品id',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * 关联用户表
- * @Author: lun
- * @DateTime: 2022/1/15 0015 9:36
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2022/1/15 0015 9:36
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param array $params
- * @return bool
- */
- public static function setData($mall_id, array $params){
- $trans = Yii::$app->db->beginTransaction();
- try{
- $model = self::findOne(['mall_id' => $mall_id, 'r_id' => $params['r_id'], 'pid' => $params['pid'], 'user_id' => $params['user_id'], 'status' => 1]);
- if ($model) throw new Exception('分享已存在');
-
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- if(!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
- // 增加推荐者分享数量
- $params['user_id'] = $params['pid'];
- $params['share_num'] = 1;
- $res = RebateRecommendNum::setData($mall_id, $params);
- if($res == false) throw new Exception('增加分享数量失败:' . RebateRecommendNum::getStaticError());
- $trans->commit();
- return true;
- }catch(\Exception $e){
- $trans->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 判断用户是否达到分享条件,并保存分享数据
- * @Author: lun
- * @DateTime: 2022/1/18 0018 19:32
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $goods_id
- * @param int $user_id
- * @param int $parent_id
- * @return bool
- */
- public static function checkShare($mall_id, $goods_id, $user_id = 0, $parent_id = 0)
- {
- // 根据商品id获取规则id
- $select = "id,initiate_type,initiate_level,consume_type,consume_level";
- $rebate = Rebate::find()->select($select)->where(['mall_id' => $mall_id,'item_id' => $goods_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
- if (empty($rebate)) return true;
- // 判断上级是否有权限
- if($rebate['initiate_type'] == RebateEnum::INITIATE_TYPE_NEW && Rebate::checkUserHasOrder($mall_id, $parent_id) !== false) return true;
- if($rebate['initiate_type'] == RebateEnum::INITIATE_TYPE_LEVEL && Rebate::checkUserLevel($parent_id, $rebate['initiate_level']) == false) return true;
- // 判断消费者是否符合权限
- if($rebate['consume_type'] == RebateEnum::CONSUME_TYPE_NEW) {
- $order_num = Rebate::checkUserHasOrder($mall_id, $user_id);
- if (!empty($order_num) && $order_num > 1) return true;
- }
- if($rebate['consume_type'] == RebateEnum::CONSUME_TYPE_LEVEL && Rebate::checkUserLevel($user_id, $rebate['consume_level']) == false) return true;
- // 插入数据
- $params['r_id'] = $rebate['id'];
- $params['pid'] = $parent_id;
- $params['user_id'] = $user_id;
- $params['item_id'] = $goods_id;
- self::setData($mall_id, $params);
- }
- }
|