ProtocolController.php 4.1 KB

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