RedpackUserCountDay.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace addons\Redpack\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. /**
  7. * This is the model class for table "{{%addons_redpack_user_count_day}}".
  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_num_total 已抢红包总次数
  15. * @property int $inviter_num 已邀请人数
  16. * @property int $date 时间,如220316
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class RedpackUserCountDay extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_redpack_user_count_day}}';
  29. }
  30. public static function briefTableName()
  31. {
  32. return 'addons_redpack_user_count_day';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id'], 'required'],
  41. [['mall_id', 'rid', 'user_id', 'grab_num', 'inviter_num', 'date', 'status', 'created_at', 'updated_at'], 'integer'],
  42. [['grab_num_total'], 'number'],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => 'ID',
  52. 'mall_id' => '商城ID',
  53. 'rid' => 'qimall_addons_redpack.id',
  54. 'user_id' => '用户ID',
  55. 'grab_num' => '可抢红包次数',
  56. 'grab_num_total' => '已抢红包总次数',
  57. 'inviter_num' => '已邀请人数',
  58. 'date' => '时间,如220316',
  59. 'status' => '状态[-1:删除;0:禁用;1启用]',
  60. 'created_at' => 'Created At',
  61. 'updated_at' => 'Updated At',
  62. ];
  63. }
  64. /**
  65. * 获取每日抢红包总计列表
  66. * @Author: lun
  67. * @DateTime: 2021/10/25 0025 10:05
  68. * @Copyright: copyright (c) 2021 广东七件事集团
  69. * @param array $cond
  70. * @param array $with
  71. * @param bool $is_array
  72. * @param string $select
  73. * @return array|\yii\db\ActiveRecord[]
  74. */
  75. public static function getRedpackUserCountDay(array $cond=[], array $with=[],bool $is_array=true,string $select='*'){
  76. $default_cond = [['<>','status',StatusEnum::DELETE]];
  77. $cond = array_merge($default_cond,$cond);
  78. $query = self::find()->select($select);
  79. self::paramPack(['where'=>$cond, 'with'=>$with],$query);
  80. $data = $query->asArray($is_array)->all();
  81. return $data;
  82. }
  83. /**
  84. * 保存数据
  85. * @Author: lun
  86. * @DateTime: 2022/3/22 0022 16:53
  87. * @Copyright: copyright (c) 2021 广东七件事集团
  88. * @param $params
  89. * @return bool
  90. */
  91. public static function setData($params)
  92. {
  93. try {
  94. $date = date('ymd', time());
  95. $model = self::findOne(['rid' => $params['rid'],'user_id' => $params['user_id'], 'date' => $date, 'status' => StatusEnum::ENABLED]);
  96. if (empty($model)) {
  97. $model = new self();
  98. $model->date = $date;
  99. $model->loadDefaultValues();
  100. }
  101. !empty($params['is_inviter']) && $model->inviter_num = $model->inviter_num + 1;
  102. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  103. return true;
  104. } catch (\Exception $e) {
  105. self::setStaticError($e->getMessage());
  106. return false;
  107. }
  108. }
  109. }