Fan.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Fan as fanModel;
  7. use catchAdmin\worklocation\model\Workplan;
  8. use catcher\base\CatchRequest;
  9. use think\facade\Db;
  10. use Workerman\Worker;
  11. class Fan extends CatchController
  12. {
  13. protected $fanModel;
  14. public function __construct(FanModel $fanModel)
  15. {
  16. $this->fanModel = $fanModel;
  17. }
  18. /**
  19. * 列表
  20. * @time 2022年04月28日 19:53
  21. * @param Request $request
  22. */
  23. public function index(Request $request): \think\Response
  24. {
  25. return CatchResponse::paginate($this->fanModel->getList());
  26. }
  27. /**
  28. *根据风场的id获取风机的列表
  29. *
  30. * @param Request $request
  31. * @return void
  32. */
  33. public function getList(Request $request)
  34. {
  35. return CatchResponse::success($this->fanModel->getFanList());
  36. }
  37. /**
  38. * 保存信息
  39. * @time 2022年04月28日 19:53
  40. * @param Request $request
  41. */
  42. public function save(Request $request): \think\Response
  43. {
  44. $data = $request->post();
  45. $save_data = array(
  46. 'wind_id' => $data['wind_id'],
  47. 'fan_model' => $data['fan_model'],
  48. 'supplier' => $data['supplier'],
  49. 'out_date' => date('Y-m-d', $data['out_date']),
  50. 'address' => $data['address'],
  51. 'info' => $data['info'],
  52. 'creator_id' => $data['creator_id'],
  53. 'created_at' => time(),
  54. );
  55. if (isset($data['out_date']) && is_int($data['out_date'])) {
  56. $save_data['out_date'] = date('Y-m-d', $data['out_date']);
  57. }
  58. $add_fans = array();
  59. if ($data['mul_number']) {
  60. $numArr = explode(',', $data['mul_number']);
  61. foreach ($numArr as $value) {
  62. $save_data['number'] = $value;
  63. array_push($add_fans, $save_data);
  64. }
  65. }
  66. $rule_data = $data['rule_data'];
  67. if ($rule_data['number_length'] && $rule_data['start_number']) {
  68. $start = $rule_data['start_number'];
  69. $length = $rule_data['start_number'] + $rule_data['number_length'];
  70. for ($i = $start; $i < $length; $i++) {
  71. if ($rule_data['zero_fill']) {
  72. $number = str_pad($i, $rule_data['zero_length'], '0', STR_PAD_LEFT);
  73. } else {
  74. $number = $i;
  75. }
  76. $save_data['number'] = $rule_data['number_first'] . $number . $rule_data['number_last'];
  77. array_push($add_fans, $save_data);
  78. }
  79. }
  80. $count1 = $this->fanModel->saveAll($add_fans);
  81. //下发风机模型
  82. $this->fanModel->IssuedFanModel($data['wind_id']);
  83. return CatchResponse::success('添加成功,共' . count($count1) . '条');
  84. // return CatchResponse::success($this->fanModel->storeBy($data));
  85. }
  86. /**
  87. * 读取
  88. * @time 2022年04月28日 19:53
  89. * @param $id
  90. */
  91. public function read($id): \think\Response
  92. {
  93. return CatchResponse::success($this->fanModel->findBy($id));
  94. }
  95. /**
  96. * 更新
  97. * @time 2022年04月28日 19:53
  98. * @param Request $request
  99. * @param $id
  100. */
  101. public function update(Request $request, $id): \think\Response
  102. {
  103. $data = $request->post();
  104. if (isset($data['out_date']) && is_int($data['out_date'])) {
  105. $data['out_date'] = date('Y-m-d', $data['out_date']);
  106. }
  107. $data['id'] = $id;
  108. $this->fanModel->updateBy($id, $data);
  109. //下发风机模型
  110. $this->fanModel->IssuedFanModel($data['wind_id']);
  111. return CatchResponse::success();
  112. }
  113. /**
  114. * 删除
  115. * @time 2022年04月28日 19:53
  116. * @param $id
  117. */
  118. public function delete($id): \think\Response
  119. {
  120. if (!is_array($id)) {
  121. $count = Workplan::where('fan_id', $id)->count();
  122. if ($count > 0) {
  123. return CatchResponse::fail('存在工作计划,无法删除');
  124. }
  125. $data = $this->fanModel->findBy($id);
  126. $this->fanModel->deleteBy($id, true);
  127. $this->fanModel->IssuedFanModel($data['wind_id']);
  128. } else {
  129. $count = Workplan::where('fan_id', 'in', $id)->count();
  130. if ($count > 0) {
  131. return CatchResponse::fail('存在工作计划,无法删除');
  132. }
  133. $where[] = ['id', 'in', $id];
  134. $list = $this->fanModel->where($where)->group('wind_id')->column('wind_id');
  135. $this->fanModel->deleteBy($id, true);
  136. foreach ($list as $item) {
  137. $this->fanModel->IssuedFanModel($item['wind_id']);
  138. }
  139. }
  140. return CatchResponse::success();
  141. }
  142. }