Logs.php 2.4 KB

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