Wind.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace catchAdmin\wind\controller;
  3. use catcher\base\CatchRequest as Request;
  4. use catcher\CatchResponse;
  5. use catcher\base\CatchController;
  6. use catchAdmin\wind\model\Wind as windModel;
  7. class Wind extends CatchController
  8. {
  9. protected $windModel;
  10. public function __construct(WindModel $windModel)
  11. {
  12. $this->windModel = $windModel;
  13. }
  14. /**
  15. * 列表
  16. * @time 2022年04月27日 10:49
  17. * @param Request $request
  18. */
  19. public function index(Request $request) : \think\Response
  20. {
  21. return CatchResponse::paginate($this->windModel->getList());
  22. }
  23. /**
  24. * 保存信息
  25. * @time 2022年04月27日 10:49
  26. * @param Request $request
  27. */
  28. public function save(Request $request) : \think\Response
  29. {
  30. $post = $request->post();
  31. if (!isset($post['name']) || !$post['name']) {
  32. return CatchResponse::fail('风场名称不能为空');
  33. }
  34. if (!isset($post['wind_shape']) || !$post['wind_shape']) {
  35. return CatchResponse::fail('获取风场类型失败');
  36. }
  37. if (!isset($post['wind_info']) || !is_array($post['wind_info'])) {
  38. return CatchResponse::fail('获取风场坐标数据失败');
  39. }
  40. if (!isset($post['department_id']) || !is_array($post['department_id'])) {
  41. return CatchResponse::fail('获取风场所属部门失败');
  42. }
  43. // 检测名称重复
  44. if($this->windModel->where('name',$post['name'])->count()){
  45. return CatchResponse::fail('风场名称已存在');
  46. }
  47. $post['wind_info'] = json_encode($post['wind_info']);
  48. $post['department_id'] = array_pop($post['department_id']);
  49. return CatchResponse::success($this->windModel->storeBy($post));
  50. }
  51. /**
  52. * 读取
  53. * @time 2022年04月27日 10:49
  54. * @param $id
  55. */
  56. public function read($id) : \think\Response
  57. {
  58. return CatchResponse::success($this->windModel->findBy($id));
  59. }
  60. /**
  61. * 更新
  62. * @time 2022年04月27日 10:49
  63. * @param Request $request
  64. * @param $id
  65. */
  66. public function update(Request $request, $id) : \think\Response
  67. {
  68. $post = $request->post();
  69. if (!isset($post['name']) || !$post['name']) {
  70. return CatchResponse::fail('风场名称不能为空');
  71. }
  72. if (!isset($post['wind_shape']) || !$post['wind_shape']) {
  73. return CatchResponse::fail('获取风场类型失败');
  74. }
  75. if (!isset($post['wind_info']) || !is_array($post['wind_info'])) {
  76. return CatchResponse::fail('获取风场坐标数据失败');
  77. }
  78. if (!isset($post['department_id']) || !$post['department_id']) {
  79. return CatchResponse::fail('获取风场所属部门失败');
  80. }
  81. // 检测名称重复
  82. $n_id = $this->windModel->where('name',$post['name'])->value('id');
  83. if($n_id && $n_id != $id){
  84. return CatchResponse::fail('风场名称已存在');
  85. }
  86. $post['wind_info'] = json_encode($post['wind_info']);
  87. if(isset($post['department_id']) && is_array($post['department_id'])){
  88. $post['department_id'] = array_pop($post['department_id']);
  89. }
  90. return CatchResponse::success($this->windModel->updateBy($id, $post));
  91. }
  92. /**
  93. * 删除
  94. * @time 2022年04月27日 10:49
  95. * @param $id
  96. */
  97. public function delete($id) : \think\Response
  98. {
  99. return CatchResponse::success($this->windModel->deleteBy($id,true));
  100. }
  101. }