TemperatureDevice.php 22 KB

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