Logs.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace backend\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "crm_logs".
  6. *
  7. * @property integer $id
  8. * @property integer $admin_id
  9. * @property integer member_id
  10. * @property string $memo
  11. * @property string $memo_obj
  12. * @property string $date
  13. */
  14. class Logs extends \yii\db\ActiveRecord
  15. {
  16. /**
  17. * @inheritdoc
  18. */
  19. public static function tableName()
  20. {
  21. return 'crm_logs';
  22. }
  23. /**
  24. * @return \yii\db\Connection the database connection used by this AR class.
  25. */
  26. public static function getDb()
  27. {
  28. return Yii::$app->get('dbXcrm');
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['admin_id', 'member_id'], 'number'],
  37. [['memo', 'memo_obj','date'], 'string'],
  38. ];
  39. }
  40. /**
  41. * 日志列表数据
  42. * @param array $post
  43. * @return array
  44. */
  45. public function getList($post)
  46. {
  47. $result = ['code' => 0, 'data' => [], 'message' => ''];
  48. $order = isset($post['order']) ? strtolower($post['order']) : '';
  49. $orderBy = isset($post['orderBy']) ? strtolower($post['orderBy']) : 'desc';
  50. $search = isset($post['search']) ? $post['search'] : '';
  51. $start = isset($post['start']) ? (int) $post['start'] : 0;
  52. $length = isset($post['length']) ? (int) $post['length'] : 20;
  53. $draw = isset($post['draw']) ? $post['draw'] : 1;
  54. $where = ['and'];
  55. // 搜索
  56. if ($search) {
  57. $where[] = ['like', 'memo', $search];
  58. }
  59. // 排序
  60. $allowOrderColumn = ['id', 'admin_id', 'member_id', 'memo', 'memo_obj'];
  61. if (in_array($order, $allowOrderColumn) && in_array($orderBy, ['asc', 'desc'])) {
  62. if ($orderBy == 'asc') {
  63. $orderCondition = [$order => SORT_ASC];
  64. } else {
  65. $orderCondition = [$order => SORT_DESC];
  66. }
  67. } else {
  68. $orderCondition = ['id' => SORT_DESC];
  69. }
  70. $query = static::find();
  71. $query->where($where)
  72. ->orderBy($orderCondition);
  73. $count = $query->count();
  74. $query->offset($start)->limit($length);
  75. $list = $query->asArray()->all();
  76. $data['data'] = $list;
  77. $data['draw'] = $draw;
  78. $data['recordsFiltered'] = $count;
  79. $data['recordsTotal'] = $count;
  80. $result['data'] = $data;
  81. $result['code'] = 1;
  82. return $result;
  83. }
  84. }