ClockinApply.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace addons\Clockin\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\user\User;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "{{%addons_clockin_apply}}".
  9. *
  10. * @property int $id
  11. * @property int|null $mall_id 商城ID
  12. * @property int|null $user_id 会员ID
  13. * @property string|null $pic 打卡照片
  14. * @property int $is_pass 审核状态[0审核中 1审核通过 -1拒绝]
  15. * @property float|null $reward 奖励
  16. * @property string|null $reason 拒绝原因
  17. * @property string|null $remark 用户备注
  18. * @property int $status 状态[0禁用 1启用 -1删除]
  19. * @property int $created_at 创建时间
  20. * @property int $updated_at 修改时间
  21. */
  22. class ClockinApply extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_clockin_apply}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'user_id', 'is_pass', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['reward'], 'number'],
  39. [['reason'], 'string'],
  40. [['pic','remark'], 'string'],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => '商城ID',
  51. 'user_id' => '会员ID',
  52. 'pic' => '打卡照片',
  53. 'is_pass' => '审核状态[0审核中 1审核通过 -1拒绝]',
  54. 'reward' => '奖励',
  55. 'reason' => '拒绝原因',
  56. 'remark' => '用户备注',
  57. 'status' => '状态[0禁用 1启用 -1删除]',
  58. 'created_at' => '创建时间',
  59. 'updated_at' => '修改时间',
  60. ];
  61. }
  62. /**
  63. * 关联用户
  64. * @Author: lun
  65. * @DateTime: 2023/7/26 0026 9:43
  66. * @Copyright: copyright (c) 2021 广东七件事集团
  67. * @return \yii\db\ActiveQuery
  68. */
  69. public function getUser()
  70. {
  71. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url');
  72. }
  73. /**
  74. * 保存数据
  75. * @Author: lun
  76. * @DateTime: 2023/7/27 0027 15:35
  77. * @Copyright: copyright (c) 2021 广东七件事集团
  78. * @param $mall_id
  79. * @param $params
  80. * @return bool
  81. */
  82. public static function setData($mall_id, $params)
  83. {
  84. $trans = Yii::$app->db->beginTransaction();
  85. try {
  86. if (!empty($params['id'])) {
  87. $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  88. if (empty($model)) throw new \Exception('查无此信息');
  89. if($model->is_pass==1) throw new \Exception('已审核通过的不允许修改');
  90. } else {
  91. $model = new self();
  92. $model->loadDefaultValues();
  93. $model->mall_id = $mall_id;
  94. $clockin_user = ClockinUser::findOne(['user_id'=>$params['user_id']]);
  95. if(empty($clockin_user)){
  96. $clockin_user = new ClockinUser();
  97. $clockin_user->loadDefaultValues();
  98. $clockin_user->mall_id = $params['mall_id'];
  99. $clockin_user->user_id = $params['user_id'];
  100. }
  101. $clockin_user->apply_num++;
  102. $res = $clockin_user->save();
  103. if($res==false) throw new Exception('增加申请次数失败');
  104. }
  105. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  106. $trans->commit();
  107. return true;
  108. } catch (\Exception $e) {
  109. $trans->rollBack();
  110. self::setStaticError($e->getMessage());
  111. return false;
  112. }
  113. }
  114. /**
  115. * 修改状态
  116. * @Author: lun
  117. * @DateTime: 2023/7/26 0026 15:26
  118. * @Copyright: copyright (c) 2021 广东七件事集团
  119. * @param $mall_id
  120. * @param $params
  121. * @return bool
  122. */
  123. public static function changeStatus($mall_id, $params)
  124. {
  125. $trans = Yii::$app->db->beginTransaction();
  126. try {
  127. // 查询打卡记录
  128. $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id]);
  129. if (empty($model)) throw new \Exception('查无此记录');
  130. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  131. // 赠送用户积分
  132. if ($model->is_pass == StatusEnum::ENABLED) {
  133. $res = ClockinUser::giveReward($model);
  134. $reward_score = $res['reward_score'];
  135. $reward_limit = $res['reward_limit'];
  136. if($reward_score!=$params['reward'])throw new \Exception("上限{$reward_limit},该用户剩余最高{$reward_score}积分额度");
  137. $model->reward = $reward_score;
  138. $model->save();
  139. }
  140. $trans->commit();
  141. return true;
  142. } catch (\Exception $e) {
  143. $trans->rollBack();
  144. self::setStaticError($e->getMessage());
  145. return false;
  146. }
  147. }
  148. }