| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace addons\Clockin\common\models;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_clockin_apply}}".
- *
- * @property int $id
- * @property int|null $mall_id 商城ID
- * @property int|null $user_id 会员ID
- * @property string|null $pic 打卡照片
- * @property int $is_pass 审核状态[0审核中 1审核通过 -1拒绝]
- * @property float|null $reward 奖励
- * @property string|null $reason 拒绝原因
- * @property string|null $remark 用户备注
- * @property int $status 状态[0禁用 1启用 -1删除]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class ClockinApply extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_clockin_apply}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'is_pass', 'status', 'created_at', 'updated_at'], 'integer'],
- [['reward'], 'number'],
- [['reason'], 'string'],
- [['pic','remark'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'user_id' => '会员ID',
- 'pic' => '打卡照片',
- 'is_pass' => '审核状态[0审核中 1审核通过 -1拒绝]',
- 'reward' => '奖励',
- 'reason' => '拒绝原因',
- 'remark' => '用户备注',
- '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/27 0027 15:35
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $params
- * @return bool
- */
- public static function setData($mall_id, $params)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
- if (empty($model)) throw new \Exception('查无此信息');
- if($model->is_pass==1) throw new \Exception('已审核通过的不允许修改');
- } else {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- $clockin_user = ClockinUser::findOne(['user_id'=>$params['user_id']]);
- if(empty($clockin_user)){
- $clockin_user = new ClockinUser();
- $clockin_user->loadDefaultValues();
- $clockin_user->mall_id = $params['mall_id'];
- $clockin_user->user_id = $params['user_id'];
- }
- $clockin_user->apply_num++;
- $res = $clockin_user->save();
- if($res==false) throw new Exception('增加申请次数失败');
- }
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 修改状态
- * @Author: lun
- * @DateTime: 2023/7/26 0026 15:26
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $params
- * @return bool
- */
- public static function changeStatus($mall_id, $params)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- // 查询打卡记录
- $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id]);
- if (empty($model)) throw new \Exception('查无此记录');
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- // 赠送用户积分
- if ($model->is_pass == StatusEnum::ENABLED) {
- $res = ClockinUser::giveReward($model);
- $reward_score = $res['reward_score'];
- $reward_limit = $res['reward_limit'];
- if($reward_score!=$params['reward'])throw new \Exception("上限{$reward_limit},该用户剩余最高{$reward_score}积分额度");
- $model->reward = $reward_score;
- $model->save();
- }
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|