AlarmRecords.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace catchAdmin\alarm\controller;
  3. use catcher\base\CatchRequest as Request;
  4. use catcher\CatchResponse;
  5. use catcher\base\CatchController;
  6. use catchAdmin\alarm\model\AlarmRecords as alarmRecordsModel;
  7. use think\facade\Db;
  8. class AlarmRecords extends CatchController
  9. {
  10. protected $alarmRecordsModel;
  11. public function __construct(AlarmRecordsModel $alarmRecordsModel)
  12. {
  13. $this->alarmRecordsModel = $alarmRecordsModel;
  14. }
  15. /**
  16. * 列表
  17. * @time 2021年05月27日 11:20
  18. * @param Request $request
  19. */
  20. public function index(Request $request) : \think\Response
  21. {
  22. return CatchResponse::paginate($this->alarmRecordsModel->getList());
  23. }
  24. /**
  25. * 保存信息
  26. * @time 2021年05月27日 11:20
  27. * @param Request $request
  28. */
  29. public function save(Request $request) : \think\Response
  30. {
  31. return CatchResponse::success($this->alarmRecordsModel->storeBy($request->post()));
  32. }
  33. /**
  34. * 保存处理记录
  35. * @time 2021年05月27日 11:20
  36. * @param Request $request
  37. */
  38. public function handleAlarm(Request $request) : \think\Response
  39. {
  40. $params=$request->post();
  41. $params['end_time']=time();
  42. $params['state']='end';
  43. $params['handler_id']=$request->user()->id;
  44. $res=$this->alarmRecordsModel
  45. ->where('device_number',$params['device_number'])
  46. ->where('state','start')
  47. ->whereIn('alarm_reason',['press','fence_in','fence_out'])
  48. ->update($params);
  49. if($res){
  50. //2表示已出警'
  51. $count=$this->alarmRecordsModel
  52. ->where('device_number',$params['device_number'])
  53. ->where('state','start')
  54. ->where('alarm_reason','low_bat')
  55. ->count();
  56. if(!$count){
  57. Db::table('devices')->where('imei', $params['device_number'])->update(['alarm_state' => 0]);
  58. }
  59. }
  60. return CatchResponse::success($res);
  61. }
  62. /**
  63. * 读取
  64. * @time 2021年05月27日 11:20
  65. * @param $id
  66. */
  67. public function read($id) : \think\Response
  68. {
  69. return CatchResponse::success($this->alarmRecordsModel->findBy($id));
  70. }
  71. /**
  72. * 更新
  73. * @time 2021年05月27日 11:20
  74. * @param Request $request
  75. * @param $id
  76. */
  77. public function update(Request $request, $id) : \think\Response
  78. {
  79. $params=$request->post();
  80. $params['end_time']=time();
  81. $params['state']='end';
  82. $params['handler_id']=$request->user()->id;
  83. $res=$this->alarmRecordsModel->updateBy($id, $params);
  84. if($res){
  85. $device_number=$this->alarmRecordsModel->where('id', $id)->value('device_number');
  86. $where = ['device_number'=>$device_number,'state'=>'start'];
  87. $count=$this->alarmRecordsModel->where($where)->count();
  88. if(!$count){
  89. Db::table('devices')->where('imei', $device_number)->update(['alarm_state' => 0]);
  90. }
  91. }
  92. return CatchResponse::success($res);
  93. }
  94. /**
  95. * 删除
  96. * @time 2021年05月27日 11:20
  97. * @param $id
  98. */
  99. public function delete($id) : \think\Response
  100. {
  101. return CatchResponse::success($this->alarmRecordsModel->deleteBy($id));
  102. }
  103. }