Wind.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 getWindList(Request $request) : \think\Response
  29. {
  30. $list=$this->windModel->getWindList();
  31. foreach($list as $val){
  32. if($val['wind_shape']=='circle'){
  33. $val['center_lng']=$val['wind_info']['center']['lng'];
  34. $val['center_lat']=$val['wind_info']['center']['lat'];
  35. }
  36. if($val['wind_shape']=='polygon'){
  37. $val['center_lng']=$val['wind_info'][0]['lng'];
  38. $val['center_lat']=$val['wind_info'][0]['lat'];
  39. }
  40. }
  41. return CatchResponse::success($list);
  42. }
  43. /**
  44. * 保存信息
  45. * @time 2022年04月27日 10:49
  46. * @param Request $request
  47. */
  48. public function save(Request $request) : \think\Response
  49. {
  50. $post = $request->post();
  51. if (!isset($post['name']) || !$post['name']) {
  52. return CatchResponse::fail('风场名称不能为空');
  53. }
  54. if (!isset($post['wind_shape']) || !$post['wind_shape']) {
  55. return CatchResponse::fail('获取风场类型失败');
  56. }
  57. if (!isset($post['wind_info']) || !is_array($post['wind_info'])) {
  58. return CatchResponse::fail('获取风场坐标数据失败');
  59. }
  60. if (!isset($post['department_id']) || !is_array($post['department_id'])) {
  61. return CatchResponse::fail('获取风场所属部门失败');
  62. }
  63. // 检测名称重复
  64. if($this->windModel->where('name',$post['name'])->count()){
  65. return CatchResponse::fail('风场名称已存在');
  66. }
  67. $post['wind_info'] = json_encode($post['wind_info']);
  68. $post['department_id'] = array_pop($post['department_id']);
  69. return CatchResponse::success($this->windModel->storeBy($post));
  70. }
  71. /**
  72. * 读取
  73. * @time 2022年04月27日 10:49
  74. * @param $id
  75. */
  76. public function read($id) : \think\Response
  77. {
  78. return CatchResponse::success($this->windModel->findBy($id));
  79. }
  80. /**
  81. * 更新
  82. * @time 2022年04月27日 10:49
  83. * @param Request $request
  84. * @param $id
  85. */
  86. public function update(Request $request, $id) : \think\Response
  87. {
  88. $post = $request->post();
  89. if(isset($post['name'])){
  90. // 检测名称重复
  91. $n_id = $this->windModel->where('name',$post['name'])->value('id');
  92. if($n_id && ($n_id != $id)){
  93. return CatchResponse::fail('风场名称已存在');
  94. }
  95. }
  96. if(isset($post['department_id']) && is_array($post['department_id'])){
  97. $post['department_id'] = array_pop($post['department_id']);
  98. }
  99. $post['wind_info'] = json_encode($post['wind_info']);
  100. return CatchResponse::success($this->windModel->updateBy($id, $post));
  101. }
  102. /**
  103. * 删除
  104. * @time 2022年04月27日 10:49
  105. * @param $id
  106. */
  107. public function delete($id) : \think\Response
  108. {
  109. return CatchResponse::success($this->windModel->deleteBy($id,true));
  110. }
  111. /**
  112. * 获取风场下拉菜单
  113. * @time 2022年04月28日 19:53
  114. */
  115. public function getWindOptions(Request $request){
  116. $list=$this->windModel->field('id as value,name as text')->select();
  117. return CatchResponse::success($list);
  118. }
  119. }