Logs.php 2.5 KB

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