ClockinUser.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace addons\Clockin\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\user\User;
  5. use services\common\UserWalletService;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "{{%addons_clockin_user}}".
  10. *
  11. * @property int $id
  12. * @property int|null $mall_id 商城ID
  13. * @property int|null $user_id 会员ID
  14. * @property float|null $total_reward 总奖励
  15. * @property int|null $apply_num 申请次数
  16. * @property int|null $success_num 打卡成功次数
  17. * @property int $status 状态[0禁用 1启用 -1删除]
  18. * @property int $created_at 创建时间
  19. * @property int $updated_at 修改时间
  20. */
  21. class ClockinUser extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_clockin_user}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'user_id', 'apply_num', 'success_num', 'status', 'created_at', 'updated_at'], 'integer'],
  37. [['total_reward'], 'number'],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城ID',
  48. 'user_id' => '会员ID',
  49. 'total_reward' => '总奖励',
  50. 'apply_num' => '申请次数',
  51. 'success_num' => '打卡成功次数',
  52. 'status' => '状态[0禁用 1启用 -1删除]',
  53. 'created_at' => '创建时间',
  54. 'updated_at' => '修改时间',
  55. ];
  56. }
  57. /**
  58. * 关联用户
  59. * @Author: lun
  60. * @DateTime: 2023/7/26 0026 9:43
  61. * @Copyright: copyright (c) 2021 广东七件事集团
  62. * @return \yii\db\ActiveQuery
  63. */
  64. public function getUser()
  65. {
  66. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url');
  67. }
  68. /**
  69. * 赠送奖励
  70. * @Author: lun
  71. * @DateTime: 2023/7/26 0026 15:25
  72. * @Copyright: copyright (c) 2021 广东七件事集团
  73. * @param $apply
  74. * @throws Exception
  75. */
  76. public static function giveReward($apply)
  77. {
  78. // 获取配置信息
  79. $config = Yii::$app->services->setting->addons->get($apply['mall_id'], 'Clockin', 'basic');
  80. // 获取当前用户信息
  81. $model = self::findOne(['user_id' => $apply['user_id'], 'status' => StatusEnum::ENABLED]);
  82. if (empty($model)) {
  83. $model = new self();
  84. $model->loadDefaultValues();
  85. $model->mall_id = $apply['mall_id'];
  86. $model->user_id = $apply['user_id'];
  87. }
  88. // 计算可得积分
  89. $reward_score = 0;
  90. $reward_limit = $config['reward_limit']??0;
  91. $reward_score = $apply['reward'];
  92. if (!empty($config['reward_limit']) && $model->total_reward + $apply['reward'] > $config['reward_limit']) {
  93. $reward_score = $config['reward_limit'] - $model->total_reward;
  94. $model->total_reward = $config['reward_limit'];
  95. } else {
  96. $model->total_reward += $reward_score;
  97. }
  98. if($reward_score>0&&empty($config['is_reward'])) throw new \Exception('奖励未开启');
  99. if ($apply['is_pass'] == 1) $model->success_num += 1;
  100. if (!$model->save()) throw new Exception($model->getErrorMessage());
  101. // 增加积分
  102. if (!empty($reward_score)) {
  103. // 增加奖励记录
  104. ClockinRewardLog::addLog($apply, $reward_score);
  105. $insert_wallet[] = [
  106. 'mall_id' => $apply['mall_id'],
  107. 'user_id' => $apply['user_id'],
  108. 'is_frozen' => false,
  109. 'wallet_type' => 'score',
  110. 'money' => $reward_score,
  111. 'desc' => '打卡奖励',
  112. 'source_table' => '',
  113. 'source_table_id' => 0,
  114. 'from_type' => 'Clockin',
  115. 'capital_type' => 'give',
  116. ];
  117. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  118. if (empty($res)) throw new \Exception('打卡插入钱包队列:' . UserWalletService::getStaticError());
  119. }
  120. return ['reward_score'=>$reward_score,'reward_limit'=>$reward_limit];
  121. }
  122. }