0
0

Energy.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <?php
  2. namespace app\api\controller\energy;
  3. use app\hander\HelpHander;
  4. use think\Controller;
  5. use think\Db;
  6. class Energy extends Base
  7. {
  8. public function energyCount(){
  9. $addr = input('addr','');
  10. $ssmap = [];
  11. if($addr !=''){
  12. $ids = $this->addrSubIds($addr);
  13. $map[] = ['ed.room_id','in',$ids];
  14. $ssmap[] = ['room_id','in',$ids];
  15. }
  16. $map[] = ['ed.del','=',1];
  17. $map[] = ['ed.cate','=',1];
  18. $map[] = ['ed.org_id','=',$this->orgId];
  19. $map1[] = ['ep.report_time','>=',date('Y-m-d').' 00:00:00'];
  20. $map1[] = ['ep.report_time','<=',date('Y-m-d').' 23:59:59'];
  21. $map2[] = ['ep.report_time','>=',date('Y-m-d',strtotime('-1 day')).' 00:00:00'];
  22. $map2[] = ['ep.report_time','<=',date('Y-m-d',strtotime('-1 day')).' 23:59:59'];
  23. $lists = Db::name('energy_param')
  24. ->alias('ep')
  25. ->join('energy_device ed','ep.device_sn = ed.sn')
  26. ->where($map)
  27. ->where($map1)
  28. ->field('ep.device_sn,max(ep.degree) as degree')
  29. ->group('ep.device_sn')
  30. ->order('ep.report_time desc')
  31. ->select();
  32. $count = 0;
  33. foreach ($lists as $k=>$v){
  34. $count += $v['degree'];
  35. }
  36. $lists2 = Db::name('energy_param')
  37. ->alias('ep')
  38. ->join('energy_device ed','ep.device_sn = ed.sn')
  39. ->where($map)
  40. ->where($map2)
  41. ->field('ep.device_sn,max(ep.degree) as degree')
  42. ->group('ep.device_sn')
  43. ->order('ep.report_time desc')
  44. ->select();
  45. $count1 = 0;
  46. foreach ($lists2 as $k=>$v){
  47. $count1 += $v['degree'];
  48. }
  49. $power = 0;
  50. $devices = Db::name('energy_device')
  51. ->where('org_id',$this->orgId)
  52. ->where('cate',5)
  53. ->where($ssmap)
  54. ->where('del',0)
  55. ->select();
  56. foreach ($devices as $k=>$v){
  57. $info = Db::name('energy_param')->where('device_sn',$v['sn'])->where('org_id',$this->orgId)->order('report_time desc')->find();
  58. if($info){
  59. $content = json_decode($info['content'],true);
  60. if(isset($content['P'])){
  61. $power += $content['P'];
  62. }
  63. }
  64. }
  65. $zongList = Db::name('energy_param')
  66. ->alias('ep')
  67. ->join('energy_device ed','ep.device_sn = ed.sn')
  68. ->where($map)
  69. ->field('ep.device_sn,max(ep.degree) as degree')
  70. ->group('ep.device_sn')
  71. ->order('ep.report_time desc')
  72. ->select();
  73. $zongNum = 0;
  74. foreach ($zongList as $k=>$v){
  75. $zongNum += $v['degree'];
  76. }
  77. $pfm = 85000; //面积(m2)
  78. $bzm = round($zongNum/8137*1000/$pfm,2);
  79. //用电量折算标准煤:1.229(t/万KWh) 1万千瓦时=1.229吨标煤;8137千瓦时=1吨标煤
  80. //1千瓦时的二氧化碳排放量约为960克。
  81. $co2 = round($zongNum*0.96/85000,2);
  82. $data = [
  83. 'count'=>round($count,2),
  84. 'count1'=>round($count1,2),
  85. 'num'=>round($power,2),
  86. 'num1'=>$bzm,
  87. 'num2'=>$co2,
  88. ];
  89. HelpHander::success($data);
  90. }
  91. public function addrList(){
  92. $lists = Db::name('energy_address')
  93. ->where('pid',0)
  94. ->where('org_id',$this->orgId)
  95. ->where('del',0)
  96. ->where('enable',1)
  97. ->select();
  98. HelpHander::success($lists);
  99. }
  100. public function addrSubIds($id){
  101. $lists = Db::name('energy_address')
  102. ->where('pid',$id)
  103. ->where('org_id',$this->orgId)
  104. ->where('del',0)
  105. ->where('enable',1)
  106. ->select();
  107. $ids = [];
  108. foreach ($lists as $k=>$v){
  109. $ids[] = Db::name('energy_address')
  110. ->where('pid',$v['id'])
  111. ->where('org_id',$this->orgId)
  112. ->where('del',0)
  113. ->where('enable',1)
  114. ->column('id');
  115. }
  116. $aids = [];
  117. foreach ($ids as $k=>$v){
  118. foreach ($v as $kk=>$vv){
  119. $aids[] = $vv;
  120. }
  121. }
  122. return $aids;
  123. }
  124. public function addrElectricityList(){
  125. $lists = Db::name('energy_address')
  126. ->where('pid',0)
  127. ->where('org_id',$this->orgId)
  128. ->where('del',0)
  129. ->where('enable',1)
  130. ->order('id desc')
  131. ->limit(8)
  132. ->select();
  133. $map[] = ['ed.del','=',1];
  134. $map[] = ['ed.org_id','=',$this->orgId];
  135. $map1[] = ['ep.report_time','>=',date('Y-m-d').' 00:00:00'];
  136. $map1[] = ['ep.report_time','<=',date('Y-m-d').' 23:59:59'];
  137. $data = [];
  138. foreach ($lists as $k=>$v){
  139. $ids = $this->addrSubIds($v['id']);
  140. $value = Db::name('energy_device')
  141. ->alias('ed')
  142. ->join('energy_param ep','ep.device_sn=ed.sn')
  143. ->where('ed.room_id','in',$ids)
  144. ->where($map)
  145. ->where($map1)
  146. ->sum('degree');
  147. $data[$k]['name'] = $v['title'];
  148. $data[$k]['value'] = $value;
  149. }
  150. HelpHander::success($data);
  151. }
  152. public function timesList(){
  153. $x = $y = $y1 = [];
  154. $list = [];
  155. for ($i=1;$i<=24;$i++){
  156. $x[] = $i;
  157. $t= $i-1;
  158. if($i < 10){
  159. $i = '0'.$i;
  160. }
  161. if($t < 10){
  162. $t = '0'.$i;
  163. }
  164. $list[$i-1]['st'] = date('Y-m-d').' '.$t.'00:00';
  165. $list[$i-1]['et'] = date('Y-m-d').' '.$i.'00:00';
  166. }
  167. foreach ($list as $k=>$v){
  168. $jin = Db::name('energy_param')
  169. ->where('org_id',$this->orgId)
  170. ->where('report_time','>=',$v['st'])
  171. ->where('report_time','<=',$v['et'])
  172. ->sum('degree');
  173. $y[] = round($jin,1);
  174. $zuo = Db::name('energy_param')
  175. ->where('org_id',$this->orgId)
  176. ->where('report_time','>=',date('Y-m-d H:i:s',strtotime($v['st'])-86400))
  177. ->where('report_time','<=',date('Y-m-d H:i:s',strtotime($v['et'])-86400))
  178. ->sum('degree');
  179. $y1[] = round($zuo,1);
  180. }
  181. $data =[
  182. 'x'=>$x,
  183. 'y'=>$y,
  184. 'y1'=>$y1
  185. ];
  186. HelpHander::success($data);
  187. }
  188. public function deviceList(){
  189. $lists = Db::name('energy_device')
  190. ->where('org_id',$this->orgId)
  191. ->where('del',0)
  192. ->limit(6)
  193. ->select();
  194. $x = $y = [];
  195. foreach ($lists as $k=>$v){
  196. $x[] = $v['title'];
  197. $degree = Db::name('energy_param')
  198. ->where('device_sn',$v['sn'])
  199. ->where('org_id',$this->orgId)
  200. ->where('report_time','>=',date('Y-m-d').' 00:00:00')
  201. ->where('report_time','<=',date('Y-m-d').' 23:59:59')
  202. ->sum('degree');
  203. $y[] = round($degree,1);
  204. }
  205. $data = [
  206. 'x'=>$x,
  207. 'y'=>$y
  208. ];
  209. HelpHander::success($data);
  210. }
  211. //电耗报表
  212. public function meterList(){
  213. $date = input('date','');
  214. $deviceName = input('deviceName');
  215. if($deviceName !=''){
  216. $map[] = ['ed.title','like','%'.$deviceName.'%'];
  217. }
  218. $where = [];
  219. if($date !=''){
  220. $st = $date[0];
  221. $et = $date[1];
  222. $where[] = ['report_time','>=',date('Y-m-d H:i:s',strtotime($st))];
  223. $where[] = ['report_time','<=',date('Y-m-d H:i:s',strtotime($et))];
  224. }
  225. $map[] = ['ed.org_id','=',$this->orgId];
  226. $map[] = ['ed.del','=',0];
  227. $map[] = ['ed.protocol','=',1];
  228. $map[] = ['ed.cate','in',[1,2,5]];
  229. // $list = Db::name('energy_device')
  230. // ->alias('ed')
  231. // ->field('ed.*,er.title as room')
  232. // ->join('energy_room er','er.id=ed.room_id')
  233. // ->where($map)
  234. // ->order('ed.id desc')
  235. // ->limit(50)
  236. // ->select();
  237. $list = Db::name('energy_device')
  238. ->alias('ed')
  239. ->field('ed.*,ea.title as address')
  240. ->join('energy_address ea','ea.id=ed.address_id')
  241. ->where($map)
  242. ->order('ed.id desc')
  243. ->limit(30)
  244. ->select();
  245. $data = [];
  246. foreach ($list as $k=>$v){
  247. // $address = Db::name('energy_address')->where('id',$v['address_id'])->value('title');
  248. $paramList = Db::name('energy_param')
  249. ->field('content')
  250. ->where('device_sn',$v['sn'])
  251. ->where('org_id',$this->orgId)
  252. ->where($where)
  253. ->select();
  254. $EPIF = $EPIJ = $EPIP = $EPIG = 0;
  255. foreach ($paramList as $kk=>$vv){
  256. $content = json_decode($vv['content'],true);
  257. if(isset($content['EPIF'])){
  258. $EPIF += round($content['EPIF'],1);
  259. $EPIJ += round($content['EPIJ'],1);
  260. $EPIP += round($content['EPIP'],1);
  261. $EPIG += round($content['EPIG'],1);
  262. }
  263. }
  264. $zong = $EPIF+$EPIJ+$EPIP+$EPIG;
  265. $data[] =[
  266. // 'room'=>$v['room'],
  267. // 'address'=>$address,
  268. 'address'=>$v['address'],
  269. 'device'=>$v['title'],
  270. 'jian'=>round($EPIF,1),
  271. 'feng'=> round($EPIJ,1),
  272. 'ping'=>round($EPIP,1),
  273. 'gu'=>round($EPIG,1),
  274. 'count'=>round($zong,1),
  275. 'total_price'=>round($zong*0.5,1),
  276. ];
  277. }
  278. $data = list_sort_by($data,'ping','desc');
  279. $columns = [
  280. // ["title" => "房间","key" => "room"],
  281. ["title" => "地点","key" => "address"],
  282. ["title" => "设备","key" => "device"],
  283. ["title" => "尖用量(kWh)","key" => "jian"],
  284. ["title" => "峰用量(kWh)","key" => "feng"],
  285. ["title" => "平用量(kWh)","key" => "ping"],
  286. ["title" => "谷用量(kWh)","key" => "gu"],
  287. ["title" => "总用量(kWh)","key" => "count"],
  288. ["title" => "总费用(元)","key" => "totalPrice"],
  289. ];
  290. $dt = [
  291. 'columns'=>$columns,
  292. 'list'=>$data
  293. ];
  294. HelpHander::success($dt);
  295. }
  296. //抄表明细表
  297. public function meterReadingList(){
  298. $page = input('page',1);
  299. $size = input('size',6);
  300. $date = input('date','');
  301. $deviceName = input('deviceName');
  302. if($deviceName !=''){
  303. $map[] = ['title','like','%'.$deviceName.'%'];
  304. }
  305. $where = [];
  306. if($date !=''){
  307. $st = $date[0];
  308. $et = $date[1];
  309. $where[] = ['report_time','>=',date('Y-m-d H:i:s',strtotime($st))];
  310. $where[] = ['report_time','<=',date('Y-m-d H:i:s',strtotime($et))];
  311. }
  312. $map[] = ['org_id','=',$this->orgId];
  313. $map[] = ['del','=',0];
  314. $map[] = ['cate','=',1];
  315. $list = Db::name('energy_device')
  316. ->field('id,title,sn,address_id')
  317. ->where($map)
  318. ->order('id desc')
  319. ->page($page,$size)
  320. ->select();
  321. foreach ($list as $k=>$v){
  322. $list[$k]['address'] = Db::name('energy_address')->where('id',$v['address_id'])->value('title');
  323. $sinfo = Db::name('energy_param')
  324. ->where('device_sn',$v['sn'])
  325. ->where($where)
  326. ->where('org_id',$this->orgId)
  327. ->where('report_time','<>','')
  328. ->where('report_time','<>',null)
  329. ->order('report_time asc')
  330. ->find();
  331. $einfo = Db::name('energy_param')
  332. ->where('device_sn',$v['sn'])
  333. ->where($where)
  334. ->where('org_id',$this->orgId)
  335. ->where('report_time','<>','')
  336. ->where('report_time','<>',null)
  337. ->order('report_time desc')
  338. ->find();
  339. $list[$k]['st'] = '';
  340. $list[$k]['et'] = '';
  341. $list[$k]['snum'] = 0;
  342. $list[$k]['enum'] = 0;
  343. $list[$k]['yl'] = 0;
  344. $snum = $enum = 0;
  345. if($sinfo){
  346. $list[$k]['st'] = $sinfo['report_time']?date('Y-m-d H:i',strtotime($sinfo['report_time'])):'';
  347. $list[$k]['snum'] = $snum = round($sinfo['degree'],1);
  348. }
  349. if($einfo){
  350. $list[$k]['et'] = $einfo['report_time']?date('Y-m-d H:i',strtotime($einfo['report_time'])):'';
  351. $list[$k]['enum'] = $enum = round($einfo['degree'],1);
  352. }
  353. $list[$k]['yl'] = round($enum-$snum,1);
  354. }
  355. $total = Db::name('energy_device')
  356. ->where($map)
  357. ->count();
  358. $data = [
  359. 'total'=>$total,
  360. 'list'=>$list
  361. ];
  362. HelpHander::success($data);
  363. }
  364. public function meterReadingListExport(){
  365. $ids = input('ids','');
  366. $date = input('date','');
  367. $deviceName = input('deviceName');
  368. if($deviceName !=''){
  369. $map[] = ['title','like','%'.$deviceName.'%'];
  370. }
  371. $where = [];
  372. if($date !=''){
  373. $st = $date[0];
  374. $et = $date[1];
  375. $where[] = ['report_time','>=',date('Y-m-d H:i:s',strtotime($st))];
  376. $where[] = ['report_time','<=',date('Y-m-d H:i:s',strtotime($et))];
  377. }
  378. if($ids){
  379. $map[] = ['id','in',explode(',',$ids)];
  380. }
  381. $map[] = ['org_id','=',$this->orgId];
  382. $map[] = ['del','=',0];
  383. $map[] = ['cate','=',1];
  384. $list = Db::name('energy_device')
  385. ->field('id,title,sn,address_id')
  386. ->where($map)
  387. ->order('id desc')
  388. ->select();
  389. foreach ($list as $k=>$v){
  390. $list[$k]['address'] = Db::name('energy_address')->where('id',$v['address_id'])->value('title');
  391. $sinfo = Db::name('energy_param')
  392. ->where('device_sn',$v['sn'])
  393. ->where($where)
  394. ->where('org_id',$this->orgId)
  395. ->where('report_time','<>','')
  396. ->where('report_time','<>',null)
  397. ->order('report_time asc')
  398. ->find();
  399. $einfo = Db::name('energy_param')
  400. ->where('device_sn',$v['sn'])
  401. ->where($where)
  402. ->where('org_id',$this->orgId)
  403. ->where('report_time','<>','')
  404. ->where('report_time','<>',null)
  405. ->order('report_time desc')
  406. ->find();
  407. $list[$k]['st'] = '';
  408. $list[$k]['et'] = '';
  409. $list[$k]['snum'] = 0;
  410. $list[$k]['enum'] = 0;
  411. $list[$k]['yl'] = 0;
  412. $snum = $enum = 0;
  413. if($sinfo){
  414. $list[$k]['st'] = $sinfo['report_time']?date('Y-m-d H:i',strtotime($sinfo['report_time'])):'';
  415. $list[$k]['snum'] = $snum = round($sinfo['degree'],1);
  416. }
  417. if($einfo){
  418. $list[$k]['et'] = $einfo['report_time']? date('Y-m-d H:i',strtotime($einfo['report_time'])):'';
  419. $list[$k]['enum'] = $enum = round($einfo['degree'],1);
  420. }
  421. $list[$k]['yl'] = round($enum-$snum,1);
  422. }
  423. $columns = [
  424. ["title" => "设备id","key" => "id"],
  425. ["title" => "地点","key" => "address"],
  426. ["title" => "设备名称","key" => "title"],
  427. ["title" => "起始时间","key" => "st"],
  428. ["title" => "终止时间","key" => "snum"],
  429. ["title" => "起始读数","key" => "et"],
  430. ["title" => "终止读数","key" => "enum"],
  431. ["title" => "用量","key" => "yl"],
  432. ];
  433. $dt = [
  434. 'columns'=>$columns,
  435. 'list'=>$list
  436. ];
  437. HelpHander::success($dt);
  438. }
  439. }