RedpackUserGrab.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace addons\Redpack\common\models;
  3. use common\models\user\User;
  4. use Yii;
  5. use yii\db\Exception;
  6. /**
  7. * This is the model class for table "{{%addons_redpack_user_grab}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $rid qimall_addons_redpack.id
  12. * @property int $user_id 用户ID
  13. * @property int $grab_num 抢红包次数
  14. * @property float $grab_money 抢红包总金额
  15. * @property int $grab_time 最后抢红包时间
  16. * @property int $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int $created_at
  18. * @property int $updated_at
  19. */
  20. class RedpackUserGrab extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_redpack_user_grab}}';
  28. }
  29. public static function briefTableName()
  30. {
  31. return 'addons_redpack_user_grab';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['mall_id'], 'required'],
  40. [['mall_id', 'rid', 'user_id', 'grab_num', 'grab_time', 'status', 'created_at', 'updated_at'], 'integer'],
  41. [['grab_money'], 'number'],
  42. ];
  43. }
  44. public function getUser()
  45. {
  46. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => 'ID',
  55. 'mall_id' => '商城ID',
  56. 'rid' => 'qimall_addons_redpack.id',
  57. 'user_id' => '用户ID',
  58. 'grab_num' => '抢红包次数',
  59. 'grab_money' => '抢红包总金额',
  60. 'grab_time' => '最后抢红包时间',
  61. 'status' => '状态[-1:删除;0:禁用;1启用]',
  62. 'created_at' => '创建时间',
  63. 'updated_at' => '更新时间',
  64. ];
  65. }
  66. /**
  67. * @Author: ltm
  68. * @DateTime: 2022/3/18 0018 16:21
  69. * @Copyright: copyright (c) 2021 广东七件事集团
  70. * @param $data
  71. * @return string
  72. */
  73. public static function setData(array $params){
  74. try{
  75. $model = self::findOne(['mall_id'=>$params['mall_id'], 'rid' => $params['rid'], 'user_id' => $params['user_id']]);
  76. if (empty($model)) {
  77. $model = new self();
  78. $model->mall_id = $params['mall_id'];
  79. $model->rid = $params['rid'];
  80. $model->user_id = $params['user_id'];
  81. $model->loadDefaultValues();
  82. }
  83. $model->grab_num = $model->grab_num + 1;
  84. $model->grab_money = $model->grab_money + $params['grab_money'];
  85. $model->grab_time = time();
  86. if(!$model->save()){
  87. throw new Exception($model->getErrorMessage());
  88. }
  89. return true;
  90. }catch(\Exception $e){
  91. self::$static_error = $e->getMessage();
  92. return false;
  93. }
  94. }
  95. }