Question.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace app\admin\controller\base\question;
  3. use app\admin\controller\Base;
  4. use app\common\model\base\question\Question as QuestionModel;
  5. class Question extends Base
  6. {
  7. public function initialize()
  8. {
  9. parent::initialize();
  10. $this->model = new QuestionModel;
  11. }
  12. public function createWhere()
  13. {
  14. $data = $this->request->param();
  15. $where = [];
  16. $whereOr = [];
  17. if(!empty($data['type'])){
  18. $where[] = ['type_id','=',$data['type']];
  19. }
  20. if(!empty($data['keyword'])){
  21. $keyword = $data['keyword'];
  22. $where[] = ['content','LIKE',"%$keyword%"];
  23. }
  24. return ['where'=>$where,'whereOr'=>$whereOr];
  25. }
  26. public function getList(int $pageNo=0,int $pageSize=10)
  27. {
  28. $whereAry = $this->createWhere();
  29. if(!empty($pageNo)){
  30. $res = $this->model->where($whereAry['where'])->where(function($query)use($whereAry){
  31. $query->where($whereAry['whereOr']);
  32. })->with(['user','answers'=>function($query){$query->with(['user']);}])->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]);
  33. return pageRes(1,'获取成功',$res->total(),$res->items());
  34. }else{
  35. $list = $this->model->where($whereAry['where'])->where(function($query)use($whereAry){
  36. $query->where($whereAry['whereOr']);
  37. })->with(['user','answers'=>function($query){$query->with(['user']);}])->select();
  38. return res(1,'获取成功',$list);
  39. }
  40. }
  41. /**
  42. * @title:选择器数据
  43. * @desc: 描述
  44. * @return {*}
  45. * @author: 系统开发
  46. * @method: POST
  47. * @Date: 2024-06-12 16:37:22
  48. */
  49. public function getTree($labelField='content')
  50. {
  51. $fields = ['id',$labelField];
  52. $where = [];
  53. $list = $this->model->where($where)->field($fields)->select();
  54. return res(1,"获取成功",$list);
  55. }
  56. /**
  57. * @title:查询信息
  58. * @desc: 描述
  59. * @param {int} {id} {} {信息ID}
  60. * @return {*}
  61. * @author: 系统开发
  62. * @method: POST
  63. * @Date: 2024-06-12 16:37:22
  64. */
  65. public function getInfo(int $id=0)
  66. {
  67. $info = $this->model->with(['answers'])->find($id);
  68. return res(1,'获取成功',$info);
  69. }
  70. /**
  71. * @title:新增/编辑信息
  72. * @desc: 描述
  73. * @param {int} {id} {} {主键ID,不传或传0表示新增,大于0表示修改}
  74. * @param {varchar} {content} {} {问题内容}
  75. * @param {int} {type_id} {} {问题类型}
  76. * @return {*}
  77. * @author: 系统开发
  78. * @method: POST
  79. * @Date: 2024-06-12 16:37:22
  80. */
  81. public function doEdit()
  82. {
  83. $data = $this->request->param();
  84. $data['uid'] = $this->userinfo->user_id;
  85. $data['status'] = 1;//默认待回复
  86. $res = $this->model->replace()->save($data);
  87. return res(1,'编辑成功');
  88. }
  89. /**
  90. * @title:删除信息
  91. * @desc: 描述
  92. * @param {int} {id} {} {信息ID}
  93. * @return {*}
  94. * @author: 系统开发
  95. * @method: POST
  96. * @Date: 2024-06-12 16:37:22
  97. */
  98. public function doDelete(int $id=0)
  99. {
  100. $info = $this->model->find($id);
  101. $info->delete();
  102. return res(1,'删除成功');
  103. }
  104. /**
  105. * @title:获取可选项
  106. * @desc: 描述
  107. * @return {*}
  108. * @author: 系统开发
  109. * @method: POST
  110. * @Date: 2023-07-06 16:37:22
  111. */
  112. public function getOptions()
  113. {
  114. $data = [
  115. 'statusList' => QuestionModel::statusList(),
  116. 'typeList' => QuestionModel::typeList(),
  117. ];
  118. return res(1,'获取成功',$data);
  119. }
  120. public function __call($name,$arguments)
  121. {
  122. return res(2,"方法{$name}不存在");
  123. }
  124. }