UserReport.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace common\models\user;
  3. use common\models\base\BaseActiveRecord;
  4. use Yii;
  5. use yii\data\Pagination;
  6. /**
  7. * This is the model class for table "{{%user_report}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id Mall Id
  11. * @property int $user_id 会员ID
  12. * @property string $title 举报标题
  13. * @property string|null $content 举报内容
  14. * @property int|null $created_at
  15. * @property int|null $updated_at
  16. */
  17. class UserReport extends BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%user_report}}';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id', 'user_id', 'title'], 'required'],
  33. [['mall_id', 'user_id', 'created_at', 'updated_at'], 'integer'],
  34. [['content'], 'string'],
  35. [['title'], 'string', 'max' => 255],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'mall_id' => 'Mall ID',
  46. 'user_id' => 'User ID',
  47. 'title' => 'Title',
  48. 'content' => 'Content',
  49. 'created_at' => 'Created At',
  50. 'updated_at' => 'Updated At',
  51. ];
  52. }
  53. /**
  54. * @return ActiveQueryInterface the relational query object.
  55. */
  56. public function getUser()
  57. {
  58. return $this->hasOne(User::class, ['id' => 'user_id']);
  59. }
  60. /**
  61. * 获取分页数据
  62. *
  63. * @param integer $mall_id
  64. * @param integer $page
  65. * @param integer $limit
  66. * @param callable $callback
  67. * @param string $order_by
  68. * @return array
  69. */
  70. public static function getPageList(
  71. int $mall_id, int $page, int $limit, callable $callback, string $order_by = 'id desc'
  72. ) :array
  73. {
  74. $model = static::find()->where(['mall_id' => $mall_id]);
  75. if (!empty($callback)) call_user_func($callback, $model);
  76. $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
  77. $model->limit($pagination->limit)->offset($pagination->offset);
  78. $list = $model->asArray()->orderBy($order_by)->all();
  79. $data['list'] = $list;
  80. $data['page_count'] = $pagination->pageCount;
  81. $data['current_page'] = $page + 1;
  82. return $data;
  83. }
  84. /**
  85. * 添加举报
  86. *
  87. * @param integer $user_id
  88. * @param integer $mall_id
  89. * @param string $title
  90. * @param string $content
  91. * @return true|static
  92. */
  93. public static function addReport(
  94. int $user_id,
  95. int $mall_id,
  96. string $title,
  97. string $content
  98. )
  99. {
  100. $model = new static();
  101. $model->load(compact('user_id', 'mall_id', 'title', 'content'), '');
  102. return $model->save() ? true : $model;
  103. }
  104. }