OpcDataController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Modules\OpcData\Http\Controllers\Api;
  3. use Illuminate\Contracts\Support\Renderable;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Routing\Controller;
  6. use Illuminate\Support\Facades\DB;
  7. class OpcDataController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. * @return Renderable
  12. */
  13. public function index()
  14. {
  15. return view('opcdata::index');
  16. }
  17. /**
  18. * Show the form for creating a new resource.
  19. * @return Renderable
  20. */
  21. public function create()
  22. {
  23. return view('opcdata::create');
  24. }
  25. /**
  26. * Store a newly created resource in storage.
  27. * @param Request $request
  28. * @return Renderable
  29. */
  30. public function store(Request $request)
  31. {
  32. //
  33. }
  34. /**
  35. * Show the specified resource.
  36. * @param int $id
  37. * @return Renderable
  38. */
  39. public function show($id)
  40. {
  41. return view('opcdata::show');
  42. }
  43. /**
  44. * Show the form for editing the specified resource.
  45. * @param int $id
  46. * @return Renderable
  47. */
  48. public function edit($id)
  49. {
  50. return view('opcdata::edit');
  51. }
  52. /**
  53. * Update the specified resource in storage.
  54. * @param Request $request
  55. * @param int $id
  56. * @return Renderable
  57. */
  58. public function update(Request $request, $id)
  59. {
  60. //
  61. }
  62. /**
  63. * Remove the specified resource from storage.
  64. * @param int $id
  65. * @return Renderable
  66. */
  67. public function destroy($id)
  68. {
  69. //
  70. }
  71. public function getData()
  72. {
  73. $db = DB::connection('mysql_opc_jinjiaqu');
  74. $sqlStr = "
  75. select t1.fan_type_id,
  76. t1.fan_type_name,
  77. t1.device_num,
  78. t1.device_name,
  79. case t.val
  80. when 'True' then 1
  81. when 'False' then 2
  82. else t.val
  83. end val,
  84. t1.json_key,
  85. t1.row_number,
  86. t1.unit
  87. from `equipment` t
  88. join tb_fan_dict_list t1 on t.selItem = t1.selItem
  89. where t1.group_id = 1
  90. order by t1.fan_type_id, t1.row_number, t1.device_num
  91. ";
  92. $dbResult = $db->select($sqlStr);
  93. $dataArr = array();
  94. foreach ($dbResult as $key => $val) {
  95. $fanNum = $val->fan_type_id - 1; // 风机号
  96. $label = $val->device_name; // 设备名
  97. $devVal = $val->val; // 数值
  98. $dataType = $val->json_key; // json键值
  99. $unit = $val->unit;
  100. $dataArr[$fanNum]['title'] = $val->fan_type_name;
  101. $valArr = [
  102. 'label' => $label,
  103. 'value' => $devVal,
  104. 'unit' => $unit
  105. ];
  106. if($label == '电机') {
  107. $hisVal = 0;
  108. if(array_key_exists('state', $dataArr)){
  109. $hisVal = $dataArr[$fanNum]['state'];
  110. } else {
  111. $hisVal = 0;
  112. };
  113. $valArr = [
  114. 'label' => $label,
  115. 'state' => max($devVal, $hisVal)
  116. ];
  117. $dataArr[$fanNum]['state'] = $devVal;
  118. } else {
  119. $dataArr[$fanNum][$dataType][] = $valArr;
  120. }
  121. }
  122. return response()->json($dataArr);
  123. }
  124. }