Wind.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace catchAdmin\wind\controller;
  3. use catchAdmin\wind\model\Fan;
  4. use catcher\base\CatchRequest as Request;
  5. use catcher\CatchResponse;
  6. use catcher\base\CatchController;
  7. use catchAdmin\wind\model\Wind as windModel;
  8. use catchAdmin\worklocation\model\Workplan;
  9. use catcher\Utils;
  10. class Wind extends CatchController
  11. {
  12. protected $windModel;
  13. public function __construct(WindModel $windModel)
  14. {
  15. $this->windModel = $windModel;
  16. }
  17. /**
  18. * 列表
  19. * @time 2022年04月27日 10:49
  20. * @param Request $request
  21. */
  22. public function index(Request $request): \think\Response
  23. {
  24. return CatchResponse::paginate($this->windModel->getList());
  25. }
  26. /**
  27. * 列表
  28. * @time 2022年04月27日 10:49
  29. * @param Request $request
  30. */
  31. public function getWindList(Request $request): \think\Response
  32. {
  33. $list = $this->windModel->getWindList();
  34. foreach ($list as $val) {
  35. if ($val['wind_shape'] == 'circle') {
  36. $val['center_lng'] = $val['wind_info']['center']['lng'];
  37. $val['center_lat'] = $val['wind_info']['center']['lat'];
  38. }
  39. if ($val['wind_shape'] == 'polygon') {
  40. $val['center_lng'] = $val['wind_info'][0]['lng'];
  41. $val['center_lat'] = $val['wind_info'][0]['lat'];
  42. }
  43. }
  44. return CatchResponse::success($list);
  45. }
  46. /**
  47. * 保存信息
  48. * @time 2022年04月27日 10:49
  49. * @param Request $request
  50. */
  51. public function save(Request $request): \think\Response
  52. {
  53. $post = $request->post();
  54. if (!isset($post['name']) || !$post['name']) {
  55. return CatchResponse::fail('风场名称不能为空');
  56. }
  57. if (!isset($post['wind_shape']) || !$post['wind_shape']) {
  58. return CatchResponse::fail('获取风场类型失败');
  59. }
  60. if (!isset($post['wind_info']) || !is_array($post['wind_info'])) {
  61. return CatchResponse::fail('获取风场坐标数据失败');
  62. }
  63. if (!isset($post['department_id']) || !is_array($post['department_id'])) {
  64. return CatchResponse::fail('获取风场所属部门失败');
  65. }
  66. // 检测名称重复
  67. if ($this->windModel->where('name', $post['name'])->count()) {
  68. return CatchResponse::fail('风场名称已存在');
  69. }
  70. if ($this->windModel->where('number', $post['number'])->count()) {
  71. return CatchResponse::fail('风场编号已存在');
  72. }
  73. $post['wind_info'] = json_encode($post['wind_info']);
  74. $post['department_id'] = array_pop($post['department_id']);
  75. return CatchResponse::success($this->windModel->storeBy($post));
  76. }
  77. /**
  78. * 读取
  79. * @time 2022年04月27日 10:49
  80. * @param $id
  81. */
  82. public function read($id): \think\Response
  83. {
  84. return CatchResponse::success($this->windModel->findBy($id));
  85. }
  86. /**
  87. * 更新
  88. * @time 2022年04月27日 10:49
  89. * @param Request $request
  90. * @param $id
  91. */
  92. public function update(Request $request, $id): \think\Response
  93. {
  94. $post = $request->post();
  95. $post['id'] = $id;
  96. if (isset($post['name'])) {
  97. // 检测名称重复
  98. $n_id = $this->windModel->where('name', $post['name'])->value('id');
  99. if ($n_id && ($n_id != $id)) {
  100. return CatchResponse::fail('风场名称已存在');
  101. }
  102. }
  103. if (isset($post['number'])) {
  104. // 检测名称重复
  105. $n_id = $this->windModel->where('number', $post['number'])->value('id');
  106. if ($n_id && ($n_id != $id)) {
  107. return CatchResponse::fail('风场编号已存在');
  108. }
  109. }
  110. if (isset($post['department_id']) && is_array($post['department_id'])) {
  111. $post['department_id'] = array_pop($post['department_id']);
  112. }
  113. $post['wind_info'] = json_encode($post['wind_info']);
  114. return CatchResponse::success($this->windModel->updateBy($id, $post));
  115. }
  116. /**
  117. * 删除
  118. * @time 2022年04月27日 10:49
  119. * @param $id
  120. */
  121. public function delete($id): \think\Response
  122. {
  123. $count = Fan::where('wind_id', $id)->count();
  124. if ($count > 0) {
  125. return CatchResponse::fail('存在所属风场的风机');
  126. }
  127. $plan_count = Workplan::where('wind_id', $id)->count();
  128. if ($plan_count > 0) {
  129. return CatchResponse::fail('存在所属风场的工作计划');
  130. }
  131. return CatchResponse::success($this->windModel->deleteBy($id, true));
  132. }
  133. /**
  134. * 获取风场下拉菜单
  135. * @time 2022年04月28日 19:53
  136. */
  137. public function getWindOptions(Request $request)
  138. {
  139. $list = $this->windModel->field('id as value,name as text')->select();
  140. return CatchResponse::success($list);
  141. }
  142. }