| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace addons\Clockin\common\models;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use services\common\UserWalletService;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_clockin_user}}".
- *
- * @property int $id
- * @property int|null $mall_id 商城ID
- * @property int|null $user_id 会员ID
- * @property float|null $total_reward 总奖励
- * @property int|null $apply_num 申请次数
- * @property int|null $success_num 打卡成功次数
- * @property int $status 状态[0禁用 1启用 -1删除]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class ClockinUser extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_clockin_user}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'apply_num', 'success_num', 'status', 'created_at', 'updated_at'], 'integer'],
- [['total_reward'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'user_id' => '会员ID',
- 'total_reward' => '总奖励',
- 'apply_num' => '申请次数',
- 'success_num' => '打卡成功次数',
- 'status' => '状态[0禁用 1启用 -1删除]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**
- * 关联用户
- * @Author: lun
- * @DateTime: 2023/7/26 0026 9:43
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url');
- }
- /**
- * 赠送奖励
- * @Author: lun
- * @DateTime: 2023/7/26 0026 15:25
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $apply
- * @throws Exception
- */
- public static function giveReward($apply)
- {
- // 获取配置信息
- $config = Yii::$app->services->setting->addons->get($apply['mall_id'], 'Clockin', 'basic');
- // 获取当前用户信息
- $model = self::findOne(['user_id' => $apply['user_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $apply['mall_id'];
- $model->user_id = $apply['user_id'];
- }
- // 计算可得积分
- $reward_score = 0;
- $reward_limit = $config['reward_limit']??0;
- $reward_score = $apply['reward'];
- if (!empty($config['reward_limit']) && $model->total_reward + $apply['reward'] > $config['reward_limit']) {
- $reward_score = $config['reward_limit'] - $model->total_reward;
- $model->total_reward = $config['reward_limit'];
- } else {
- $model->total_reward += $reward_score;
- }
- if($reward_score>0&&empty($config['is_reward'])) throw new \Exception('奖励未开启');
- if ($apply['is_pass'] == 1) $model->success_num += 1;
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- // 增加积分
- if (!empty($reward_score)) {
- // 增加奖励记录
- ClockinRewardLog::addLog($apply, $reward_score);
- $insert_wallet[] = [
- 'mall_id' => $apply['mall_id'],
- 'user_id' => $apply['user_id'],
- 'is_frozen' => false,
- 'wallet_type' => 'score',
- 'money' => $reward_score,
- 'desc' => '打卡奖励',
- 'source_table' => '',
- 'source_table_id' => 0,
- 'from_type' => 'Clockin',
- 'capital_type' => 'give',
- ];
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) throw new \Exception('打卡插入钱包队列:' . UserWalletService::getStaticError());
- }
- return ['reward_score'=>$reward_score,'reward_limit'=>$reward_limit];
- }
- }
|