Fan.php 4.3 KB

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