ProtocolController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\validate\sys_manage\AdvValidate;
  4. use app\admin\validate\sys_manage\ArticleValidate;
  5. use app\admin\validate\sys_manage\ContentValidate;
  6. use app\controller\Curd;
  7. use app\model\Adv;
  8. use app\model\Article;
  9. use app\model\Content;
  10. use app\model\SysCategory;
  11. use support\exception\BusinessException;
  12. use support\Request;
  13. use support\Response;
  14. class ProtocolController extends Curd
  15. {
  16. public function __construct()
  17. {
  18. $this->model = new Content();
  19. $this->validate = true;
  20. $this->validateClass = new ContentValidate();
  21. }
  22. /** 列表
  23. * @Desc
  24. * @Author Gorden
  25. * @Date 2024/3/5 10:00
  26. *
  27. * @param Request $request
  28. * @return Response
  29. * @throws \support\exception\BusinessException
  30. */
  31. public function select(Request $request): Response
  32. {
  33. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  34. $where['content_category'] = 'PROTOCOL';
  35. $order = $request->get('order', 'desc');
  36. $field = $field ?? 'content_addtimes';
  37. $query = $this->doSelect($where, $field, $order);
  38. return $this->doFormat($query, $format, $limit);
  39. }
  40. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  41. {
  42. $model = $this->model->with('category');
  43. foreach ($where as $column => $value) {
  44. if (is_array($value)) {
  45. if ($value[0] === 'like' || $value[0] === 'not like') {
  46. $model = $model->where($column, $value[0], "%$value[1]%");
  47. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  48. $model = $model->where($column, $value[0], $value[1]);
  49. } elseif ($value[0] == 'in' && !empty($value[1])) {
  50. $valArr = $value[1];
  51. if (is_string($value[1])) {
  52. $valArr = explode(",", trim($value[1]));
  53. }
  54. $model = $model->whereIn($column, $valArr);
  55. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  56. $valArr = $value[1];
  57. if (is_string($value[1])) {
  58. $valArr = explode(",", trim($value[1]));
  59. }
  60. $model = $model->whereNotIn($column, $valArr);
  61. } elseif ($value[0] == 'null') {
  62. $model = $model->whereNull($column);
  63. } elseif ($value[0] == 'not null') {
  64. $model = $model->whereNotNull($column);
  65. } elseif ($value[0] !== '' || $value[1] !== '') {
  66. $model = $model->whereBetween($column, $value);
  67. }
  68. } else {
  69. $model = $model->where($column, $value);
  70. }
  71. }
  72. if ($field) {
  73. $model = $model->orderBy($field, $order);
  74. }
  75. return $model;
  76. }
  77. /**
  78. * @Desc 查询服务协议列表
  79. * @Author Gorden
  80. * @Date 2024/12/2 13:51
  81. *
  82. * @param Request $request
  83. * @return Response
  84. */
  85. public function selectList(Request $request)
  86. {
  87. $category_type = $request->get('category_type');
  88. $categoryIds = [];
  89. if (!empty($category_type)) {
  90. $categoryIds = SysCategory::where('category_type', $category_type)->pluck('category_id')->toArray();
  91. }
  92. $protocol = Content::whereIn('join_content_category_id',$categoryIds)
  93. // ->where('content_status','ACTIVED')
  94. ->select('content_id','content_title')
  95. ->get()
  96. ->toArray();
  97. return json_success('success',$protocol);
  98. }
  99. public function insert(Request $request): Response
  100. {
  101. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  102. return json_fail($this->validateClass->getError());
  103. }
  104. if (Content::where('join_content_category_id', $request->post('join_content_category_id'))->exists()) {
  105. return json_fail("当前分类已存在,请不要重复添加");
  106. }
  107. try {
  108. $data = $this->insertInput($request);
  109. $this->doInsert($data);
  110. } catch (BusinessException $customException) {
  111. return json_fail($customException->getMessage());
  112. } catch (\Exception $e) {
  113. dump($e->getMessage());
  114. return json_fail('数据写入失败11');
  115. }
  116. return json_success('success');
  117. }
  118. /**
  119. * @Desc
  120. * @Author Gorden
  121. * @Date 2024/3/27 10:24
  122. *
  123. * @param Request $request
  124. * @return array
  125. * @throws \support\exception\BusinessException
  126. */
  127. protected function insertInput(Request $request): array
  128. {
  129. $data = $this->inputFilter($request->post());
  130. $data['content_category'] = 'PROTOCOL';
  131. $data['content_status'] = 'NOSEARCH';
  132. return $data;
  133. }
  134. }