0
0

TemperatureDevice.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\DeviceLog;
  4. use app\common\model\Group;
  5. use app\temperature\controller\Push;
  6. use think\App;
  7. use think\Db;
  8. require_once $_SERVER['DOCUMENT_ROOT'] . '/../vendor/wenkong/TemperatureDevice.php';
  9. class TemperatureDevice extends Auth
  10. {
  11. protected $abnormal = [
  12. '正常',
  13. '设备离线',
  14. '传感器异常',
  15. '传感器未连接',
  16. ];
  17. public function __construct(App $app) {
  18. parent::__construct($app);
  19. $this->api = new Push();
  20. }
  21. public function index() {
  22. if (request()->isAjax()) {
  23. //分页参数
  24. $length = input('rows', 10, 'intval'); //每页条数
  25. $page = input('page', 1, 'intval'); //第几页
  26. $start = ($page - 1) * $length; //分页开始位置
  27. //排序
  28. $sortRow = input('sidx', 'id', 'trim'); //排序列
  29. $sort = input('sord', 'desc', 'trim'); //排序方式
  30. $order = $sortRow . ' ' . $sort;
  31. $title = input('title', '', 'trim');
  32. if ($title) {
  33. $map[] = ['dev_name|snaddr', 'like', '%' . $title . '%'];
  34. }
  35. $enable = input('enable', '', 'trim');
  36. if ($enable != '') {
  37. $map[] = ['enable', '=', $enable];
  38. }
  39. $group = input('group_id', '', 'trim');
  40. if($group!=''){
  41. $iiDs = Db::name('group_device')
  42. ->where('org_id',$this->orgId)
  43. ->where('group_id',$group)
  44. ->column('device_id');
  45. if(empty($iiDs)){
  46. $map[] = ['id', '=', -1];
  47. }else{
  48. $map[] = ['id', 'in', $iiDs];
  49. }
  50. }else{
  51. $groupList = (new Group())->getList();
  52. if(!empty($groupList)){
  53. $iiDs = Db::name('group_device')
  54. ->where('org_id',$this->orgId)
  55. ->where('group_id',$groupList[0]['id'])
  56. ->column('device_id');
  57. if(empty($iiDs)){
  58. $map[] = ['id', '=', -1];
  59. }else{
  60. $map[] = ['id', 'in', $iiDs];
  61. }
  62. }
  63. }
  64. $map[] = ['org_id', '=', $this->orgId];
  65. $map[] = ['del', '=', 0];
  66. if($this->userId!==1){
  67. $ids = Db::name('temperature_auth')
  68. ->where('user_id',$this->userId)
  69. ->where('org_id',$this->orgId)
  70. ->column('temperature_id');
  71. if(!empty($ids)){
  72. $map[] = ['id', 'in',$ids];
  73. }else{
  74. $map[] = ['id', '=',-1];
  75. }
  76. }
  77. $map = empty($map) ? true : $map;
  78. //数据查询
  79. $this->api->getDeviceData();
  80. $lists = Db::name('temperature_device')
  81. ->where($map)->limit($start, $length)->order($order)->select();
  82. foreach ($lists as $k => &$v) {
  83. $v['tj'] = '';
  84. $v['abnormal_name'] = $this->abnormal[$v['abnormal']];
  85. $gd = Db::name('group_device')
  86. ->where('org_id',$this->orgId)
  87. ->where('device_id',$v['id'])
  88. ->column('group_id');
  89. if(empty($gd)){
  90. $t = '';
  91. }else{
  92. $x = Db::name('group')
  93. ->where('id','in',$gd)
  94. ->column('title');
  95. $t = implode(',',$x);
  96. }
  97. $v['gName'] = $t;
  98. }
  99. //数据返回
  100. $totalCount = Db::name('temperature_device')->where($map)->count();
  101. $totalPage = ceil($totalCount / $length);
  102. $result['page'] = $page;
  103. $result['total'] = $totalPage;
  104. $result['records'] = $totalCount;
  105. $result['rows'] = $lists;
  106. return json($result);
  107. }
  108. else {
  109. $group = (new Group())->getList();
  110. $this->assign('group', $group);
  111. return $this->fetch();
  112. }
  113. }
  114. /**
  115. * 新增/编辑
  116. */
  117. public function add($id = 0) {
  118. if (request()->isPost()) {
  119. $model = new \app\common\model\TemperatureDevice();
  120. $post = request()->post();
  121. $data = [
  122. 'snaddr'=>trim($post['snaddr']),
  123. 'dev_name'=>$post['dev_name'],
  124. 'remark'=>$post['remark'],
  125. 'enable'=>$post['enable'],
  126. 'org_id'=>$this->orgId,
  127. 'id'=>$id,
  128. ];
  129. $validate = new \app\common\validate\TemperatureDevice();
  130. $result = $validate->scene('')->check($data,[]);
  131. if(true !== $result){
  132. $this->error($validate->getError());
  133. }
  134. $log =Db::name('device_log')
  135. ->where('type',DeviceLog::TYPE5)
  136. ->where('sn',$data['snaddr'])
  137. ->where('del',0)
  138. ->find();
  139. if(empty($log)){
  140. $this->error('该设备无法添加');
  141. }
  142. unset($data['id']);
  143. if($id <=0){
  144. $data['create_time'] = date('Y-m-d H:i:s');
  145. $res = Db::name('temperature_device')
  146. ->insertGetId($data);
  147. $id = $res;
  148. }else{
  149. $data['update_time'] = date('Y-m-d H:i:s');
  150. $res = Db::name('temperature_device')
  151. ->where('id',$id)
  152. ->update($data);
  153. }
  154. if ($res) {
  155. Db::name('group_device')
  156. ->where('org_id',$this->orgId)
  157. ->where('device_id',$id)
  158. ->delete();
  159. $data = request()->post();
  160. if(isset($data['group_id']) && !empty($data['group_id'])){
  161. $ids = $data['group_id'];
  162. $a = [
  163. 'org_id'=>$this->orgId,
  164. 'device_id'=>$id,
  165. 'group_id'=>$ids,
  166. ];
  167. Db::name('group_device')
  168. ->insertGetId($a);
  169. }
  170. $this->success('操作成功', url('index'));
  171. }
  172. else {
  173. $this->error($model->getError());
  174. }
  175. }
  176. else {
  177. $meta_title = '新增设备';
  178. if ($id) {
  179. $info = Db::name('temperature_device')->where('id', $id)->find();
  180. $info['gd'] = Db::name('group_device')
  181. ->where('org_id',$this->orgId)
  182. ->where('device_id',$id)
  183. ->value('group_id');
  184. $this->assign('info', $info);
  185. $meta_title = '编辑设备';
  186. }
  187. $group = (new Group())->getList();
  188. $this->assign('meta_title', $meta_title);
  189. $this->assign('group', $group);
  190. return $this->fetch();
  191. }
  192. }
  193. /**
  194. * 删除记录
  195. * @param int $id
  196. */
  197. public function del($id = 0) {
  198. if (!$id) {
  199. $this->error('参数错误');
  200. }
  201. $res = Db::name('temperature_device')->where('id', $id)->update(['del' => 1]);
  202. Db::name('group_device')
  203. ->where('device_id',$id)
  204. ->where('org_id',$this->orgId)
  205. ->delete();
  206. if ($res) {
  207. $this->success('删除成功');
  208. }
  209. else {
  210. $this->error('删除失败');
  211. }
  212. }
  213. /**
  214. * 改变字段值
  215. * @param int $fv
  216. * @param string $fn
  217. * @param int $fv
  218. */
  219. public function changeField($id = 0, $fn = '', $fv = 0) {
  220. if (!$fn || !$id) {
  221. $this->error('参数错误');
  222. }
  223. $res = Db::name('temperature_device')->where('id', $id)->update([$fn => $fv]);
  224. if ($res) {
  225. $this->success('操作成功');
  226. }
  227. else {
  228. $this->error('操作失败');
  229. }
  230. }
  231. /**
  232. * 24小时温度图
  233. *
  234. * @author wst
  235. * @date 2021/6/9 10:59
  236. */
  237. public function tj1($id) {
  238. $time = time();
  239. $timeData = [];
  240. for ($i = 0; $i <= 23; $i++) {
  241. if (strlen($i) <= 1) {
  242. $i = '0' . $i;
  243. }
  244. $timeData[] = date('Ymd', $time) . $i;
  245. }
  246. $data = [];
  247. foreach ($timeData as $k => $v) {
  248. $info = Db::name('temperature_device_data')
  249. ->where('snaddr', $id)
  250. ->where('create_ymdh', $v)
  251. ->where('abnormal', 0)
  252. ->avg('temp');
  253. $a = [
  254. 'name' => substr($v, -2) . ':00',
  255. 'value' => round($info, 2)
  256. ];
  257. $data[] = $a;
  258. }
  259. $this->assign('key', array_column($data, 'name'));
  260. $this->assign('value', array_column($data, 'value'));
  261. $i = array_chunk($data, 16);
  262. $this->assign('data', $i);
  263. return $this->fetch();
  264. }
  265. /**
  266. * 最近七日温度显示
  267. *
  268. * @author wst
  269. * @date 2021/6/9 14:16
  270. */
  271. public function tj2($id) {
  272. $time = time();
  273. $timeData = [];
  274. for ($i = 6; $i >= 0; $i--) {
  275. $timeData[] = date('Ymd', $time - ($i * 86400));
  276. }
  277. $data = [];
  278. foreach ($timeData as $k => $v) {
  279. $info = Db::name('temperature_device_data')
  280. ->where('snaddr', $id)
  281. ->where('create_ymd', $v)
  282. ->where('abnormal', 0)
  283. ->avg('temp');
  284. $a = [
  285. 'name' => substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, -2),
  286. 'value' => round($info, 2)
  287. ];
  288. $data[] = $a;
  289. }
  290. $this->assign('key', array_column($data, 'name'));
  291. $this->assign('value', array_column($data, 'value'));
  292. $this->assign('data', $data);
  293. return $this->fetch();
  294. }
  295. /**
  296. * 最近30日温度显示
  297. *
  298. * @author wst
  299. * @date 2021/6/9 14:16
  300. */
  301. public function tj3($id) {
  302. $time = time();
  303. $timeData = [];
  304. for ($i = 29; $i >= 0; $i--) {
  305. $timeData[] = date('Ymd', $time - ($i * 86400));
  306. }
  307. $data = [];
  308. foreach ($timeData as $k => $v) {
  309. $info = Db::name('temperature_device_data')
  310. ->where('snaddr', $id)
  311. ->where('create_ymd', $v)
  312. ->where('abnormal', 0)
  313. ->avg('temp');
  314. $a = [
  315. 'name' => substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, -2),
  316. 'value' => round($info, 2)
  317. ];
  318. $data[] = $a;
  319. }
  320. $this->assign('key', array_column($data, 'name'));
  321. $this->assign('value', array_column($data, 'value'));
  322. $i = array_chunk($data, 10);
  323. $this->assign('data', $i);
  324. return $this->fetch();
  325. }
  326. public function auth() {
  327. if (request()->isAjax()) {
  328. //分页参数
  329. $length = input('rows', 10, 'intval'); //每页条数
  330. $page = input('page', 1, 'intval'); //第几页
  331. $start = ($page - 1) * $length; //分页开始位置
  332. //排序
  333. $sortRow = input('sidx', 'id', 'trim'); //排序列
  334. $sort = input('sord', 'desc', 'trim'); //排序方式
  335. $order = $sortRow . ' ' . $sort;
  336. $title = input('title', '', 'trim');
  337. if ($title) {
  338. $map1[] = ['dev_name|snaddr', 'like', '%' . $title . '%'];
  339. $map1[] = ['org_id', '=', $this->orgId];
  340. $deviceIds = Db::name('temperature_device')
  341. ->where($map1)
  342. ->column('id');
  343. if (!empty($deviceIds)) {
  344. $map[] = ['temperature_id', 'in', $deviceIds];
  345. }
  346. else {
  347. $map[] = ['temperature_id', '=', -1];
  348. }
  349. }
  350. $title1 = input('name', '', 'trim');
  351. if ($title1) {
  352. $map2[] = ['a.org_id', '=', $this->orgId];
  353. $map2[] = ['b.real_name', 'like', '%' . $title1 . '%'];
  354. $userIds = Db::name('user')
  355. ->alias('b')
  356. ->join('user_org a', 'b.id=a.user_id')
  357. ->where($map2)
  358. ->column('b.id');
  359. if (!empty($deviceIds)) {
  360. $map[] = ['user_id', 'in', $userIds];
  361. }
  362. else {
  363. $map[] = ['user_id', '=', -1];
  364. }
  365. }
  366. $map[] = ['org_id', '=', $this->orgId];
  367. $map = empty($map) ? true : $map;
  368. //数据查询
  369. $lists = Db::name('temperature_auth')
  370. ->where($map)
  371. ->limit($start, $length)
  372. ->field('user_id,count(*) as num')
  373. ->group('user_id')
  374. ->distinct(true)
  375. ->order($order)->select();
  376. foreach ($lists as $k => &$v) {
  377. $v['nickName'] = Db::name('user')
  378. ->where('id', $v['user_id'])
  379. ->value('real_name');
  380. }
  381. //数据返回
  382. $totalCount = Db::name('temperature_auth')
  383. ->group('user_id')
  384. ->where($map)->count();
  385. $totalPage = ceil($totalCount / $length);
  386. $result['page'] = $page;
  387. $result['total'] = $totalPage;
  388. $result['records'] = $totalCount;
  389. $result['rows'] = $lists;
  390. return json($result);
  391. }
  392. else {
  393. return $this->fetch();
  394. }
  395. }
  396. public function add_auth() {
  397. if (request()->isPost()) {
  398. $data = request()->post();
  399. if(!isset($data['user_id']) || empty($data['user_id'])){
  400. $this->error('用户不能为空');
  401. }
  402. if(!isset($data['temperature_id']) || empty($data['temperature_id'])){
  403. $this->error('设备不能为空');
  404. }
  405. $ids = explode(',',$data['temperature_id']);
  406. $ii = [];
  407. foreach ($ids as $k=>$v) {
  408. if (strpos($v, 'p') === false && !empty($v)) {
  409. $aa = explode('-',$v);
  410. $ii[] = $aa[0];
  411. }
  412. }
  413. $ids = $ii;
  414. $a = [];
  415. foreach ($ids as $k=>$v){
  416. $check = Db::name('temperature_auth')
  417. ->where('user_id',$data['user_id'])
  418. ->where('temperature_id',$v)
  419. ->find();
  420. if($check){
  421. $info = Db::name('temperature_device')
  422. ->where('id',$v)
  423. ->find();
  424. $this->error($info['dev_name'].'('.$info['snaddr'].')设备已绑定');
  425. }
  426. $a[] = [
  427. 'user_id'=>$data['user_id'],
  428. 'temperature_id'=>$v,
  429. 'org_id'=>$this->orgId
  430. ];
  431. }
  432. $res = Db::name('temperature_auth')
  433. ->insertAll($a);
  434. if($res){
  435. Db::name('user')
  436. ->where('id',$data['user_id'])
  437. ->update(['group'=>$data['temperature_id']]);
  438. $this->success('添加成功', url('auth'));
  439. }
  440. $this->error('添加失败');
  441. }
  442. else {
  443. $meta_title = '新增';
  444. $map[] = ['uo.org_id', '=', $this->orgId];
  445. $map[] = ['u.del', '=', 0];
  446. //数据查询
  447. $lists = Db::name('user')
  448. ->alias('u')
  449. ->join('user_org uo', 'uo.user_id = u.id')
  450. ->join('user_dep ud', 'ud.user_id = u.id', 'left')
  451. ->join('user_roles ur', 'ur.user_id = u.id', 'left')
  452. ->where($map)
  453. ->field('u.id as id,real_name as title')
  454. ->select();
  455. $group = Db::name('group')
  456. ->where('org_id',$this->orgId)
  457. ->field('id,title as label')
  458. ->where('enable',1)
  459. ->select();
  460. foreach ($group as $k=>$v){
  461. $group[$k]['children'] = [];
  462. $deviceId = Db::name('group_device')
  463. ->where('org_id',$this->orgId)
  464. ->where('group_id',$v['id'])
  465. ->column('device_id');
  466. if($deviceId){
  467. $device = Db::name('temperature_device')
  468. ->where('id','in',$deviceId)
  469. ->where('del', 0)
  470. ->field('id,dev_name as label,snaddr')
  471. ->select();
  472. foreach ($device as $k1 => $v1) {
  473. $device[$k1]['label'] = $v1['label'] . '(' . $v1['snaddr'] . ')';
  474. }
  475. $group[$k]['children'] =$device;
  476. }
  477. }
  478. foreach ($group as $k=>$v){
  479. $group[$k]['id'] = $v['id'].'-p';
  480. foreach ($v['children'] as $k1=>$v1){
  481. $group[$k]['children'][$k1]['id'] = $v1['id'].'-c';
  482. }
  483. }
  484. $this->assign('group', $group);
  485. $this->assign('user', $lists);
  486. $this->assign('meta_title', $meta_title);
  487. return $this->fetch();
  488. }
  489. }
  490. /**
  491. * 新增/编辑
  492. */
  493. public function edit_auth($id = 0) {
  494. if (request()->isPost()) {
  495. $data = request()->post();
  496. if(!isset($data['user_id']) || empty($data['user_id'])){
  497. $this->error('用户不能为空');
  498. }
  499. if(!isset($data['temperature_id']) || empty($data['temperature_id'])){
  500. $this->error('设备不能为空');
  501. }
  502. $ids = explode(',',$data['temperature_id']);
  503. $ii = [];
  504. foreach ($ids as $k=>$v) {
  505. if (strpos($v, 'p') === false && !empty($v)) {
  506. $aa = explode('-',$v);
  507. $ii[] = $aa[0];
  508. }
  509. }
  510. $ids = $ii;
  511. $a = [];
  512. Db::name('temperature_auth')
  513. ->where('user_id',$data['user_id'])
  514. ->delete();
  515. foreach ($ids as $k=>$v){
  516. $a[] = [
  517. 'user_id'=>$data['user_id'],
  518. 'temperature_id'=>$v,
  519. 'org_id'=>$this->orgId
  520. ];
  521. }
  522. $res = Db::name('temperature_auth')
  523. ->insertAll($a);
  524. if ($res) { Db::name('user')
  525. ->where('id',$data['user_id'])
  526. ->update(['group'=>$data['temperature_id']]);
  527. $this->success('操作成功', url('auth'));
  528. }
  529. else {
  530. $this->error('修改失败');
  531. }
  532. }
  533. else {
  534. $meta_title = '新增';
  535. if ($id) {
  536. // $info = Db::name('temperature_auth')
  537. // ->where('user_id', $id)->column('temperature_id');
  538. $iii = Db::name('user')
  539. ->where('id',$id)
  540. ->find();
  541. $info = $iii?explode(',',$iii['group']):[];
  542. $this->assign('info', $info);
  543. $meta_title = '编辑';
  544. }
  545. $map[] = ['uo.org_id', '=', $this->orgId];
  546. $map[] = ['u.del', '=', 0];
  547. //数据查询
  548. $lists = Db::name('user')
  549. ->alias('u')
  550. ->join('user_org uo', 'uo.user_id = u.id')
  551. ->join('user_dep ud', 'ud.user_id = u.id', 'left')
  552. ->join('user_roles ur', 'ur.user_id = u.id', 'left')
  553. ->where($map)
  554. ->field('u.id as id,real_name as title')
  555. ->select();
  556. $group = Db::name('group')
  557. ->where('org_id',$this->orgId)
  558. ->field('id,title as label')
  559. ->where('enable',1)
  560. ->select();
  561. foreach ($group as $k=>$v){
  562. $group[$k]['children'] = [];
  563. $deviceId = Db::name('group_device')
  564. ->where('org_id',$this->orgId)
  565. ->where('group_id',$v['id'])
  566. ->column('device_id');
  567. if($deviceId){
  568. $device = Db::name('temperature_device')
  569. ->where('id','in',$deviceId)
  570. ->where('del', 0)
  571. ->field('id,dev_name as label,snaddr')
  572. ->select();
  573. foreach ($device as $k1 => $v1) {
  574. $device[$k1]['label'] = $v1['label'] . '(' . $v1['snaddr'] . ')';
  575. }
  576. $group[$k]['children'] =$device;
  577. }
  578. }
  579. foreach ($group as $k=>$v){
  580. $group[$k]['id'] = $v['id'].'-p';
  581. foreach ($v['children'] as $k1=>$v1){
  582. $group[$k]['children'][$k1]['id'] = $v1['id'].'-c';
  583. }
  584. }
  585. $this->assign('group', $group);
  586. $this->assign('user', $lists);
  587. $this->assign('meta_title', $meta_title);
  588. $this->assign('user_id',$id);
  589. return $this->fetch();
  590. }
  591. }
  592. /**
  593. * 删除记录
  594. * @param int $id
  595. */
  596. public function del_auth($id = 0) {
  597. if (!$id) {
  598. $this->error('参数错误');
  599. }
  600. $res = Db::name('temperature_auth')->where('user_id', $id)->delete();
  601. if ($res) {
  602. $this->success('删除成功');
  603. }
  604. else {
  605. $this->error('删除失败');
  606. }
  607. }
  608. public function detail_auth($id){
  609. $info = Db::name('temperature_auth')
  610. ->where('user_id', $id)->column('temperature_id');
  611. $d = Db::name('temperature_device')
  612. ->where('id','in',$info)
  613. ->select();
  614. $this->assign('list',$d);
  615. return $this->fetch();
  616. }
  617. }