| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace addons\ScoreExpansion\common\models;
- use common\models\user\User;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_redpack_user_grab}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $rid qimall_addons_redpack.id
- * @property int $user_id 用户ID
- * @property int $grab_num 抢红包次数
- * @property float $grab_money 抢红包总金额
- * @property int $grab_time 最后抢红包时间
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class ScoreExpansionUserGrab extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_score_expansion_user_grab}}';
- }
- public static function briefTableName()
- {
- return 'addons_score_expansion_user_grab';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id'], 'required'],
- [['mall_id', 'rid', 'user_id', 'grab_num', 'grab_time', 'status', 'created_at', 'updated_at'], 'integer'],
- [['grab_money'], 'number'],
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'rid' => 'qimall_addons_score_expansion.id',
- 'user_id' => '用户ID',
- 'grab_num' => '抢红包次数',
- 'grab_money' => '抢红包总金额',
- 'grab_time' => '最后抢红包时间',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * @Author: ltm
- * @DateTime: 2022/3/18 0018 16:21
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function setData(array $params){
- try{
- $model = self::findOne(['mall_id'=>$params['mall_id'], 'rid' => $params['rid'], 'user_id' => $params['user_id']]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->rid = $params['rid'];
- $model->user_id = $params['user_id'];
- $model->loadDefaultValues();
- }
- $model->grab_num = $model->grab_num + 1;
- $model->grab_money = $model->grab_money + $params['grab_money'];
- $model->grab_time = time();
- if(!$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- return true;
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|