InstitutionalService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\InstitutionalModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 制度参数管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * @package App\Services\Common
  19. */
  20. class InstitutionalService extends BaseService
  21. {
  22. // 静态对象
  23. protected static $instance = null;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new InstitutionalModel();
  32. }
  33. /**
  34. * 静态入口
  35. * @return static|null
  36. */
  37. public static function make()
  38. {
  39. if (!self::$instance) {
  40. self::$instance = (new static());
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * 获取列表
  46. * @param $params 参数
  47. * @param int $pageSize 分页大小:默认 15
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 10, $field = [])
  51. {
  52. $query = $this->getQuery($params);
  53. $list = $query->select($field ? $field : ['a.*'])
  54. ->orderBy('a.id','desc')
  55. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  56. $list = $list ? $list->toArray() : [];
  57. if ($list) {
  58. foreach ($list['data'] as &$item) {
  59. $item['create_time'] = datetime($item['create_time'],'Y-m-d H:i:s');
  60. }
  61. }
  62. return [
  63. 'pageSize' => $pageSize,
  64. 'total' => isset($list['total']) ? $list['total'] : 0,
  65. 'list' => isset($list['data']) ? $list['data'] : []
  66. ];
  67. }
  68. /**
  69. * 查询构造
  70. * @param $params
  71. * @return \Illuminate\Database\Eloquent\Builder
  72. */
  73. public function getQuery($params)
  74. {
  75. $where = ['a.mark' => 1];
  76. return $this->model->from('institutional as a')
  77. ->where($where)
  78. ->where(function ($query) use ($params) {
  79. $kw = isset($params['keyword']) ? trim($params['keyword']) : '';
  80. if ($kw) {
  81. $query->where('a.name', 'like', "%{$params['keyword']}%");
  82. }
  83. })
  84. ->where(function ($query) use($params){
  85. $type = isset($params['type'])? $params['type'] : 0;
  86. if ($type) {
  87. $query->where('a.type', $type);
  88. }
  89. $status = isset($params['status'])? $params['status'] : 0;
  90. if (is_array($status)) {
  91. $query->whereIn('a.status', $status);
  92. } else if($status){
  93. $query->where('a.status', $status);
  94. }
  95. });
  96. }
  97. }