WasteStatistics.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class WasteStatistics extends Auth {
  6. public function work() {
  7. $cur = date('Y-m-d');
  8. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  9. $end = input('end', date('Y-m-d'));
  10. $start1 = $start . ' 00:00:00';
  11. $end1 = $end . ' 23:59:59';
  12. $type = input('type');
  13. $typeList = Db::name('waste_type')
  14. ->where('enable', 1)
  15. ->where('del', 0)
  16. //->where('org_id', $this->orgId)
  17. ->select();
  18. $list = $this->workData($start1, $end1, $type);
  19. $this->assign('list', $list);
  20. $this->assign('type', $type);
  21. $this->assign('typeList', $typeList);
  22. $this->assign('start', $start);
  23. $this->assign('end', $end);
  24. return $this->fetch();
  25. }
  26. public function workData($start1, $end1, $type) {
  27. $map1[] = ['create_time', '>=', $start1];
  28. $map1[] = ['create_time', '<=', $end1];
  29. $map1[] = ['org_id', '=', $this->orgId];
  30. if (!empty($type)) {
  31. $map1[] = ['cateid', '=', $type];
  32. }
  33. $list = Db::name('waste_record')
  34. ->where($map1)
  35. ->where('del', 0)
  36. ->group('user_id')
  37. ->select();
  38. foreach ($list as $k => $v) {
  39. $list[$k]['title'] = Db::name('user')
  40. ->where('id', $v['user_id'])
  41. ->value('real_name');
  42. $c = Db::name('waste_record')
  43. ->where($map1)
  44. ->where('del', 0)
  45. ->where('user_id', $v['user_id'])
  46. ->count();
  47. $list[$k]['count'] = $c ? $c : 0;
  48. $weight = Db::name('waste_record')
  49. ->where($map1)
  50. ->where('del', 0)
  51. ->where('user_id', $v['user_id'])
  52. ->sum('weight');
  53. $list[$k]['weight'] = $weight ? round($weight / 1000, 2) : 0;
  54. }
  55. return $list;
  56. }
  57. public function workExport() {
  58. $cur = date('Y-m-d');
  59. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  60. $end = input('end', date('Y-m-d'));
  61. $start1 = $start . ' 00:00:00';
  62. $end1 = $end . ' 23:59:59';
  63. $type = input('type');
  64. $ret = $this->workData($start1, $end1, $type);
  65. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  66. //实例化PHPExcel类
  67. $objPHPExcel = new \PHPExcel();
  68. //激活当前的sheet表
  69. $objPHPExcel->setActiveSheetIndex(0);
  70. //设置表格头(即excel表格的第一行)
  71. $objPHPExcel->setActiveSheetIndex(0)
  72. ->setCellValue('A1', '编号')
  73. ->setCellValue('B1', '姓名')
  74. ->setCellValue('C1', '单数')
  75. ->setCellValue('D1', '重量(kg)');
  76. // 设置表格头水平居中
  77. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  78. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  79. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  80. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  81. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  82. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  83. //设置列水平居中
  84. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  85. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  86. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  87. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  88. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  89. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  90. //设置单元格宽度
  91. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  92. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  93. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  94. //循环刚取出来的数组,将数据逐一添加到excel表格。
  95. for ($i = 0; $i < count($ret); $i++) {
  96. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['user_id']);
  97. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['title']);
  98. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['count']);
  99. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['weight']);
  100. }
  101. //设置保存的Excel表格名称
  102. $filename = '医废工作量统计_' . date('YmdHis', time()) . '.xls';
  103. //设置当前激活的sheet表格名称
  104. $objPHPExcel->getActiveSheet()->setTitle('医废工作量统计');
  105. //设置浏览器窗口下载表格
  106. ob_end_clean();
  107. header("Content-Type: application/force-download");
  108. header("Content-Type: application/octet-stream");
  109. header("Content-Type: application/download");
  110. header('Content-Disposition:inline;filename="' . $filename);
  111. //生成excel文件
  112. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  113. //下载文件在浏览器窗口
  114. return $objWriter->save('php://output');
  115. }
  116. // 近两年医废统计
  117. public function waste_year() {
  118. $org_id = $this->orgId;
  119. $year = date('Y');
  120. $qyear = $year - 1;
  121. $arr1 = [$year . '01', $year . '02', $year . '03', $year . '04', $year . '05', $year . '06', $year . '07', $year . '08', $year . '09', $year . '10', $year . '11', $year . '12',];
  122. $arr2 = [$qyear . '01', $qyear . '02', $qyear . '03', $qyear . '04', $qyear . '05', $qyear . '06', $qyear . '07', $qyear . '08', $qyear . '09', $qyear . '10', $qyear . '11', $qyear . '12',];
  123. $arrs = array_merge($arr2, $arr1);
  124. $lists = [];
  125. foreach ($arrs as $k => $v) {
  126. $weight = Db::name('waste_record')
  127. ->where('org_id', $org_id)
  128. ->where('del', 0)
  129. ->where('create_yyyymm', $v)
  130. ->sum('weight');
  131. $lists[] = [
  132. 'month' => $v,
  133. 'month_text' => intval(date('m', strtotime($v . '01'))) . '月',
  134. 'weight' => $weight && !empty($weight) ? round($weight / 1000, 2) : 0
  135. ];
  136. }
  137. $warr = $warr1 = $warr2 = [];
  138. $tmonths = [];
  139. $hb = [];
  140. // 计算环比
  141. foreach ($lists as $k => $v) {
  142. if ($k == 0) {
  143. $lists[$k]['hb'] = 0;
  144. }
  145. else {
  146. if ($lists[$k - 1]['weight'] > 0) {
  147. $lists[$k]['hb'] = round(($v['weight'] - $lists[$k - 1]['weight']) / $lists[$k - 1]['weight'], 3) * 100;
  148. }
  149. else {
  150. $lists[$k]['hb'] = 0;
  151. }
  152. }
  153. if ($k >= 12) {
  154. $warr1[] = $v['weight'];
  155. }
  156. else {
  157. $warr2[] = $v['weight'];
  158. }
  159. $warr[] = $v['weight'];
  160. $tmonths[] = date('Y-m', strtotime($v['month'] . '01'));
  161. $hb[] = $lists[$k]['hb'];
  162. }
  163. //计算同比
  164. $tbarr = [];
  165. foreach ($arr1 as $k => $v) {
  166. $v1 = $lists[$k]['weight'];
  167. $v2 = $lists[$k + 12]['weight'];
  168. if ($v1 > 0) {
  169. $tbarr[] = round(($v2 - $v1) / $v1, 3) * 100;
  170. }
  171. else {
  172. $tbarr[] = 0;
  173. }
  174. }
  175. $data = [
  176. 'lists' => $lists,
  177. 'tb' => $tbarr,
  178. 'months' => ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
  179. 'year' => $year,
  180. 'qyear' => $qyear,
  181. 'warr' => $warr,
  182. 'warr1' => $warr1,
  183. 'warr2' => $warr2,
  184. 'tmonths' => $tmonths,
  185. 'hb' => $hb
  186. ];
  187. $this->assign('data', $data);
  188. return $this->fetch();
  189. }
  190. //医疗报表统计
  191. public function view(){
  192. $month = input('month',date('Y-m'));
  193. $dep_id = input('dep_id','');
  194. $addr_id = input('addr_id','');
  195. $type_id = input('type_id','');
  196. $address =(new \app\common\model\Address())->getListByType(7,$this->orgId);
  197. $dep = (new \app\common\model\Dep())->getList($this->orgId);
  198. $typeList = Db::name('waste_type')
  199. ->where('enable', 1)
  200. ->where('del', 0)
  201. // ->where('org_id', $this->orgId)
  202. ->select();
  203. $return = $this->viewData($month,$dep_id,$addr_id,$type_id);
  204. $this->assign('data',$return);
  205. $this->assign('address',$address);
  206. $this->assign('dep',$dep);
  207. $this->assign('typeList',$typeList);
  208. $this->assign('month',$month);
  209. $this->assign('dep_id',$dep_id);
  210. $this->assign('addr_id',$addr_id);
  211. $this->assign('type_id',$type_id);
  212. return $this->fetch();
  213. }
  214. public function viewData($month,$dep_id,$addr_id,$type_id){
  215. $map[] = ['org_id','=',$this->orgId];
  216. $typeMap = [];
  217. if($type_id !=''){
  218. $typeMap[] = ['id','=',$type_id];
  219. }
  220. $typeList = Db::name('waste_type')
  221. ->where('enable', 1)
  222. ->where('del', 0)
  223. ->where($typeMap)
  224. // ->where('org_id', $this->orgId)
  225. ->select();
  226. $recordMap = [];
  227. $depWhere = [];
  228. if($dep_id !=''){
  229. $depWhere[] = ['id','=',$dep_id];
  230. $map[] = ['del','=',0];
  231. $map[] = ['enable','=',1];
  232. $map[] = ['dep_id','=',$dep_id];
  233. $map[]=['','exp',Db::raw("FIND_IN_SET(7,types)")];
  234. $address = Db::name('address')
  235. ->where($map)
  236. ->order('id desc')
  237. ->field('id,title,remark,sn,x,y')
  238. ->select();
  239. if($address){
  240. $recordMap[] = ['waste_device_id','in',array_column($address,'id')];
  241. }else{
  242. $recordMap[] = ['waste_device_id','=',0];
  243. }
  244. }
  245. if($addr_id!=''){
  246. $recordMap[] = ['waste_device_id','=',$addr_id];
  247. }
  248. $name1 = [];
  249. $name2 = [];
  250. $name3 = [];
  251. $data1 = [];
  252. $data2 = [];
  253. $data3 = [];
  254. $data4 = [];
  255. $day = getDay(strtotime($month));
  256. foreach ($day as $k1=>$v){
  257. $name2[] = date('d',strtotime($v));
  258. }
  259. $dep = Db::name('dep')
  260. ->where('del',0)
  261. ->where('org_id',$this->orgId)
  262. ->where('enable',1)
  263. ->where($depWhere)
  264. ->select();
  265. $depList = [];
  266. foreach ($dep as $k=>$v){
  267. $dep_map[] = ['del','=',0];
  268. $dep_map[] = ['enable','=',1];
  269. $dep_map[] = ['dep_id','=',$v['id']];
  270. $dep_map[]=['','exp',Db::raw("FIND_IN_SET(7,types)")];
  271. $address = Db::name('address')
  272. ->where($dep_map)
  273. ->order('id desc')
  274. ->field('id,title,remark,sn,x,y')
  275. ->select();
  276. if($address){
  277. $depList[] = [
  278. 'title'=>$v['title'],
  279. 'ids'=>array_column($address,'id')
  280. ];
  281. }
  282. }
  283. foreach ($depList as $k2=>$v2){
  284. $map = [];
  285. if($month){
  286. $map[] =['create_yyyymm','=',date('Ym',strtotime($month))];
  287. }
  288. $nums1 = Db::name('waste_record')
  289. ->where('org_id',$this->orgId)
  290. ->where('del',0)
  291. ->where($recordMap)
  292. ->where($map)
  293. ->where('waste_device_id','in',$v2['ids'])
  294. ->sum('weight');
  295. $depList[$k2]['total'] = $nums1?round($nums1/1000,2):0;
  296. }
  297. $depList = $depList?list_sort_by($depList,'total','desc'):[];
  298. $name3[] = $depList?array_column($depList,'title'):[];
  299. foreach ($typeList as $k=>$v){
  300. $map = [];
  301. if($month){
  302. $map[] =['create_yyyymm','=',date('Ym',strtotime($month))];
  303. }
  304. $num = Db::name('waste_record')
  305. ->where('org_id',$this->orgId)
  306. ->where('del',0)
  307. ->where('cateid',$v['id'])
  308. ->where($recordMap)
  309. ->where($map)
  310. ->sum('weight');
  311. $total = $num?round($num/1000,2):0;
  312. $name1[] = $v['title'];
  313. $data1[] = $total;
  314. $data2[] =['value'=>$total,'name'=>$v['title']];
  315. $numList = [];
  316. $numList1 = [];
  317. foreach ($day as $k1=>$v1){
  318. $nums = Db::name('waste_record')
  319. ->where('org_id',$this->orgId)
  320. ->where('del',0)
  321. ->where('cateid',$v['id'])
  322. ->where($recordMap)
  323. ->where($map)
  324. ->where('create_yyyymmdd',$v1)
  325. ->sum('weight');
  326. $totals = $nums?round($nums/1000,2):0;
  327. $numList[] = $totals;
  328. }
  329. foreach ($depList as $k2=>$v2){
  330. if($k2<=9){
  331. $nums1 = Db::name('waste_record')
  332. ->where('org_id',$this->orgId)
  333. ->where('del',0)
  334. ->where('cateid',$v['id'])
  335. ->where($map)
  336. ->where('waste_device_id','in',$v2['ids'])
  337. ->sum('weight');
  338. $totals1 = $nums1?round($nums1/1000,2):0;
  339. $numList1[] = $totals1;
  340. }
  341. }
  342. $data3[] = [
  343. 'name'=>$v['title'],
  344. 'type'=>'line',
  345. 'stack'=>'stack',
  346. 'data'=>$numList
  347. ];
  348. $data4[] = [
  349. 'name'=>$v['title'],
  350. 'type'=>'line',
  351. 'stack'=>'stack',
  352. 'data'=>$numList1
  353. ];
  354. }
  355. $return = [
  356. 'name1'=>$name1,
  357. 'name2'=>$name2,
  358. 'name3'=>$name3,
  359. 'data1'=>$data1,
  360. 'data2'=>$data2,
  361. 'data3'=>$data3,
  362. 'data4'=>$data4,
  363. ];
  364. return $return;
  365. }
  366. public function viewExportData($month,$dep_id,$addr_id,$type_id){
  367. $tMap = [];
  368. $tMap[] = ['enable','=',1];
  369. $tMap[] = ['del','=',0];
  370. //$tMap[] = ['org_id','=',$this->orgId];
  371. if($type_id!=''){
  372. $tMap[] = ['id','=',$type_id];
  373. }
  374. $lists = [];
  375. $type = Db::name('waste_type')->where($tMap)->select();
  376. $type = $type?$type:[];
  377. $first = [
  378. 'name' => '类型',
  379. ];
  380. $second = [
  381. 'name' => '重量',
  382. ];
  383. $third = [
  384. 'name' => '比例',
  385. ];
  386. $fourth = [
  387. 'name' => '',
  388. ];
  389. $five = [
  390. 'name' => '按天统计',
  391. ];
  392. $total = 0;
  393. $count = 1;
  394. foreach ($type as $k=>$v){
  395. $count++;
  396. $map = [];
  397. $map[] = ['del','=',0];
  398. $map[] = ['org_id','=',$this->orgId];
  399. $map[] = ['status','in',[0,1,2]];
  400. if($type_id){
  401. $map[] = ['cateid','=',$type_id];
  402. }
  403. if($addr_id){
  404. $map[] = ['waste_device_id','=',$addr_id];
  405. }
  406. if($dep_id){
  407. $addrs = Db::name('address')->where('dep_id',$dep_id)->where('enable',1)->where('del',0)->column('id');
  408. if($addrs){
  409. $map[] = ['waste_device_id','in',$addr_id];
  410. }else{
  411. $map[] = ['waste_device_id','=',-1];
  412. }
  413. }
  414. if($month){
  415. $map[] = ['create_yyyymm','=',str_ireplace('-','',$month)];
  416. }
  417. $weight = Db::name('waste_record')->where($map)->sum('weight');
  418. $total += $weight;
  419. $first['type'.$v['id']] = $v['title'];
  420. $second['type'.$v['id']] = round($weight/1000,2);
  421. $third['type'.$v['id']] = $weight;
  422. $fourth['type'.$v['id']] = '';
  423. $five['type'.$v['id']] = '';
  424. }
  425. foreach ($type as $k=>$v){
  426. $third['type'.$v['id']] = $total>0?round($third['type'.$v['id']]/$total,2):0;
  427. }
  428. $lists[] = $first;
  429. $lists[] = $second;
  430. $lists[] = $third;
  431. $lists[] = $fourth;
  432. $lists[] = $five;
  433. $days = getDay(strtotime($month.'-01'));
  434. foreach ($days as $kk=>$vv){
  435. $arr = [
  436. 'name' => date('Y-m-d',strtotime($vv))
  437. ];
  438. foreach ($type as $k=>$v){
  439. $map = [];
  440. $map[] = ['del','=',0];
  441. $map[] = ['org_id','=',$this->orgId];
  442. $map[] = ['status','in',[0,1,2]];
  443. if($type_id){
  444. $map[] = ['cateid','=',$type_id];
  445. }
  446. if($addr_id){
  447. $map[] = ['waste_device_id','=',$addr_id];
  448. }
  449. if($dep_id){
  450. $addrs = Db::name('address')->where('dep_id',$dep_id)->where('enable',1)->where('del',0)->column('id');
  451. if($addrs){
  452. $map[] = ['waste_device_id','in',$addr_id];
  453. }else{
  454. $map[] = ['waste_device_id','=',-1];
  455. }
  456. }
  457. $map[] = ['create_yyyymmdd','=',$vv];
  458. $weight = Db::name('waste_record')->where($map)->sum('weight');
  459. $arr['type'.$v['id']] = round($weight/1000,2);
  460. }
  461. $lists[] = $arr;
  462. }
  463. $arr2 = [
  464. 'name' => '',
  465. ];
  466. $arr3 = [
  467. 'name' => '按科室统计',
  468. ];
  469. foreach ($type as $k=>$v){
  470. $arr2['type'.$v['id']] = '';
  471. $arr3['type'.$v['id']] = '';
  472. }
  473. $lists[] = $arr2;
  474. $lists[] = $arr3;
  475. $m[] = ['org_id','=',$this->orgId];
  476. $m[] = ['enable','=',1];
  477. $m[] = ['del','=',0];
  478. if($dep_id > 0){
  479. $m[] = ['id','=',$dep_id];
  480. }
  481. $deps = Db::name('dep')->where($m)->select();
  482. $deps = $deps?$deps:[];
  483. foreach ($deps as $kk=>$vv){
  484. $arr = [
  485. 'name' => $vv['title'],
  486. ];
  487. foreach ($type as $k=>$v){
  488. $map = [];
  489. $map[] = ['del','=',0];
  490. $map[] = ['org_id','=',$this->orgId];
  491. $map[] = ['status','in',[0,1,2]];
  492. if($type_id){
  493. $map[] = ['cateid','=',$type_id];
  494. }
  495. if($addr_id){
  496. $map[] = ['waste_device_id','=',$addr_id];
  497. }
  498. if($vv['id']){
  499. $addrs = Db::name('address')->where('dep_id',$dep_id)->where('enable',1)->where('del',0)->column('id');
  500. if($addrs){
  501. $map[] = ['waste_device_id','in',$addr_id];
  502. }else{
  503. $map[] = ['waste_device_id','=',-1];
  504. }
  505. }
  506. $map[] = ['create_yyyymm','=',$month];
  507. $weight = Db::name('waste_record')->where($map)->sum('weight');
  508. $arr['type'.$v['id']] = round($weight/1000,2);
  509. }
  510. $lists[] = $arr;
  511. }
  512. $address = '地点:全部';
  513. if($addr_id){
  514. $address = '地点:'.Db::name('address')->where('id',$addr_id)->value('title');
  515. }
  516. $dep = '科室:全部';
  517. if($dep_id){
  518. $dep = '科室:'.Db::name('dep')->where('id',$dep_id)->value('title');
  519. }
  520. $typestr = '医废类型:全部';
  521. if($type_id){
  522. $typestr = '医废类型:'.Db::name('waste_type')->where('id',$type_id)->value('title');
  523. }
  524. $monthstr = "月份:".$month;
  525. $data = [
  526. 'count' => $count,
  527. 'address' => $address,
  528. 'dep' => $dep,
  529. 'type' => $typestr,
  530. 'types' => $type,
  531. 'monthstr' => $monthstr,
  532. 'lists' => $lists
  533. ];
  534. return $data;
  535. }
  536. public function viewExport(){
  537. $month = input('month',date('Y-m'));
  538. $dep_id = input('dep_id','');
  539. $addr_id = input('addr_id','');
  540. $type_id = input('type_id','');
  541. $data = $this->viewExportData($month,$dep_id,$addr_id,$type_id);
  542. $lists = $data['lists'];
  543. $types = $data['types'];
  544. $char = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
  545. $lins = [];
  546. // 暂时支持2层
  547. for( $i=0;$i<$data['count'];$i++ ){
  548. $r1 = $i%26;
  549. $r2 = floor($i/26);
  550. if($r2 == 0){
  551. $lins[] = $char[$r1];
  552. }else if($r2 == 1){
  553. $lins[] = $char[$r2-1].$char[$r1];
  554. }else if($r2 == 2){
  555. $lins[] = $char[$r2-1].$char[$r1];
  556. }else if($r2 == 3){
  557. $lins[] = $char[$r2-1].$char[$r1];
  558. }
  559. }
  560. $spreadsheet = new Spreadsheet();
  561. $worksheet = $spreadsheet->getActiveSheet();
  562. //设置工作表标题名称
  563. $worksheet->setTitle('医疗废弃物报表');
  564. foreach ($lins as $k=>$v){
  565. $spreadsheet->setActiveSheetIndex(0)->getStyle($v)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  566. $spreadsheet->setActiveSheetIndex(0)->getColumnDimension($v)->setAutoSize(true);
  567. }
  568. //表头
  569. //设置单元格内容
  570. $worksheet->setCellValueByColumnAndRow(1, 1, $data['address']);
  571. $worksheet->setCellValueByColumnAndRow(2, 1, $data['dep']);
  572. $worksheet->setCellValueByColumnAndRow(3, 1, $data['type']);
  573. $worksheet->setCellValueByColumnAndRow(4, 1, $data['monthstr']);
  574. foreach ($lists as $k=>$v){
  575. $worksheet->setCellValueByColumnAndRow(1, $k+2, $v['name']);
  576. foreach ($types as $kk=>$vv){
  577. $worksheet->setCellValueByColumnAndRow($kk+2, $k+2, $v['type'.$vv['id']]);
  578. }
  579. }
  580. $filename = '医疗废弃物报表'.$month.'.xlsx';
  581. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  582. header('Content-Disposition: attachment;filename="'.$filename.'"');
  583. header('Cache-Control: max-age=0');
  584. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  585. $writer->save('php://output');
  586. }
  587. // 医废日统计
  588. public function wasteDay() {
  589. $month = input('start',date('Y-m-d'));
  590. $addr_id = input('addr_id','');
  591. $type_id = input('type_id','');
  592. $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
  593. $tMap[] = ['del','=',0];
  594. $tMap[] = ['enable','=',1];
  595. //$tMap[] = ['org_id','=',$this->orgId];
  596. $typeList1 = Db::name('waste_type')
  597. ->where($tMap)
  598. ->field('id,title')
  599. ->select();
  600. if($type_id!=''){
  601. $tMap[] = ['id','in',explode(',',$type_id)];
  602. $typeList = Db::name('waste_type')
  603. ->where($tMap)
  604. ->field('id,title')
  605. ->select();
  606. }else{
  607. $typeList = Db::name('waste_type')
  608. ->where($tMap)
  609. ->field('id,title')
  610. ->select();
  611. }
  612. $data = $this->wasteDataDay($month,$addr_id,$type_id);
  613. $this->assign('data',$data);
  614. $this->assign('typeList',$typeList);
  615. $this->assign('typeList1',$typeList1);
  616. $this->assign('addressList',$addressList);
  617. $this->assign('start',$month);
  618. $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
  619. $this->assign('type_id',$type_id?explode(',',$type_id):[]);
  620. return $this->fetch();
  621. }
  622. public function wasteDataDay($month,$addr_id,$type_id){
  623. $org_id = $this->orgId;
  624. $aMap = [];
  625. if($addr_id!=''){
  626. $aMap[] = ['id','in',explode(',',$addr_id)];
  627. }
  628. $aMap[]=['','exp',Db::raw("FIND_IN_SET(7,types)")];
  629. $dep = Db::name('address')
  630. ->where('org_id',$org_id)
  631. ->where('enable',1)
  632. ->where('del',0)
  633. ->where($aMap)
  634. ->select();
  635. $tMap = [];
  636. if($type_id!=''){
  637. $tMap[] = ['id','in',explode(',',$type_id)];
  638. }
  639. $type = Db::name('waste_type')
  640. ->where('enable',1)
  641. ->where('del',0)
  642. //->where('org_id',$this->orgId)
  643. ->where($tMap)
  644. ->select();
  645. $date = date('Ymd',strtotime($month));
  646. foreach ($dep as $k=>$v){
  647. foreach ($type as $k1=>$v1){
  648. $map = [];
  649. $map[] = ['CREATE_YYYYMMDD','=',$date];
  650. $row = Db::name('waste_record')
  651. ->where('waste_device_id',$v['id'])
  652. ->where('cateid',$v1['id'])
  653. ->where('org_id',$org_id)
  654. ->where('del',0)
  655. ->where($map)
  656. ->count();
  657. $dep[$k]['data'][] = $row;
  658. $map1 = [];
  659. if($addr_id!=''){
  660. $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
  661. }
  662. $dep[$k]['total'][] = Db::name('waste_record')
  663. ->where('cateid',$v1['id'])
  664. ->where('org_id',$org_id)
  665. ->where('del',0)
  666. ->where($map)
  667. ->where($map1)
  668. ->count();
  669. $kg = Db::name('waste_record')
  670. ->where('waste_device_id',$v['id'])
  671. ->where('cateid',$v1['id'])
  672. ->where('org_id',$org_id)
  673. ->where('del',0)
  674. ->where($map)
  675. ->sum('weight');
  676. $we = $kg?round($kg/1000,2):0;
  677. $dep[$k]['data'][] = $we;
  678. $kg = Db::name('waste_record')
  679. ->where('cateid',$v1['id'])
  680. ->where('org_id',$org_id)
  681. ->where('del',0)
  682. ->where($map)
  683. ->where($map1)
  684. ->sum('weight');
  685. $we = $kg?round($kg/1000,2):0;
  686. $dep[$k]['total'][] =$we;
  687. }
  688. }
  689. return $dep;
  690. }
  691. //医废日统计导出
  692. public function wasteDayExport()
  693. {
  694. set_time_limit(0);
  695. ini_set("memory_limit", "1024M");
  696. $month = input('start',date('Y-m-d'));
  697. $addr_id = input('addr_id','');
  698. $type_id = input('type_id','');
  699. $data = $this->wasteDataDay($month,$addr_id,$type_id);
  700. $tMap[] = ['del','=',0];
  701. $tMap[] = ['enable','=',1];
  702. //$tMap[] = ['org_id','=',$this->orgId];
  703. if($type_id!=''){
  704. $tMap[] = ['id','in',explode(',',$type_id)];
  705. $type = Db::name('waste_type')
  706. ->where($tMap)
  707. ->field('id,title')
  708. ->select();
  709. }else{
  710. $type = Db::name('waste_type')
  711. ->where($tMap)
  712. ->field('id,title')
  713. ->select();
  714. }
  715. $title = '医废日统计';
  716. if(!empty($month)){
  717. $title = $month.'医废日统计';
  718. }
  719. header("Content-type: application/vnd.ms-excel");
  720. header("Content-Type: application/force-download");
  721. header("Content-Disposition: attachment; filename=".$title.".xls");
  722. header('Expires:0');
  723. header('Pragma:public');
  724. $res = '';
  725. $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
  726. $res.='<tr style="background: #ffffff;">
  727. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">地点</th>';
  728. foreach ($type as $k=>$v){
  729. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
  730. }
  731. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
  732. $res.='<tr style="background: #ffffff;">';
  733. foreach ($type as $v){
  734. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
  735. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量/kg</th>';
  736. }
  737. foreach ($data as $k=>$v){
  738. $res.='<tr style="background: #ffffff;">';
  739. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['title'].'</td>';
  740. foreach ($v['data'] as $k1=>$v1){
  741. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  742. }
  743. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
  744. $res.='</tr>';
  745. }
  746. $res.='<tr style="background: #ffffff;">
  747. <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">共计:</td>';
  748. if(!empty($data)){
  749. foreach ($data[0]['total'] as $k1=>$v1){
  750. $res.=' <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  751. }
  752. }
  753. $res.='<td style="border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>
  754. </tr>';
  755. echo $res;
  756. }
  757. // 医废月统计
  758. public function wasteMonth() {
  759. $month = input('start',date('Y-m'));
  760. $addr_id = input('addr_id','');
  761. $type_id = input('type_id','');
  762. $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
  763. $tMap[] = ['del','=',0];
  764. $tMap[] = ['enable','=',1];
  765. // $tMap[] = ['org_id','=',$this->orgId];
  766. $typeList1 = Db::name('waste_type')
  767. ->where($tMap)
  768. ->field('id,title')
  769. ->select();
  770. if($type_id!=''){
  771. $tMap[] = ['id','in',explode(',',$type_id)];
  772. $typeList = Db::name('waste_type')
  773. ->where($tMap)
  774. ->field('id,title')
  775. ->select();
  776. }else{
  777. $typeList = Db::name('waste_type')
  778. ->where($tMap)
  779. ->field('id,title')
  780. ->select();
  781. }
  782. $data = $this->wasteMonthData($month,$addr_id,$type_id);
  783. $this->assign('data',$data['res']);
  784. $this->assign('totalData',$data['total']);
  785. $this->assign('typeList',$typeList);
  786. $this->assign('typeList1',$typeList1);
  787. $this->assign('addressList',$addressList);
  788. $this->assign('start',$month);
  789. $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
  790. $this->assign('type_id',$type_id?explode(',',$type_id):[]);
  791. return $this->fetch();
  792. }
  793. public function wasteMonthData($month,$addr_id,$type_id){
  794. $org_id = $this->orgId;
  795. $dep = getDay(strtotime($month));
  796. $tMap = [];
  797. if($type_id!=''){
  798. $tMap[] = ['id','in',explode(',',$type_id)];
  799. }
  800. $type = Db::name('waste_type')
  801. ->where('enable',1)
  802. ->where('del',0)
  803. // ->where('org_id',$this->orgId)
  804. ->where($tMap)
  805. ->select();
  806. $res = [];
  807. $total = [];
  808. foreach ($dep as $k=>$v){
  809. foreach ($type as $k1=>$v1){
  810. $map = [];
  811. $map[] = ['CREATE_YYYYMMDD','=',$v];
  812. $map1 = [];
  813. if($addr_id!=''){
  814. $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
  815. }
  816. $row = Db::name('waste_record')
  817. ->where('cateid',$v1['id'])
  818. ->where('org_id',$org_id)
  819. ->where('del',0)
  820. ->where($map)
  821. ->where($map1)
  822. ->count();
  823. $res[$k][] = $row;
  824. $map2 = [];
  825. $map2[] = ['CREATE_YYYYMM','=',date('Ym',strtotime($v))];
  826. $row = Db::name('waste_record')
  827. ->where('cateid',$v1['id'])
  828. ->where('org_id',$org_id)
  829. ->where('del',0)
  830. ->where($map1)
  831. ->where($map2)
  832. ->count();
  833. $total[$k][] = $row;
  834. $kg = Db::name('waste_record')
  835. ->where('cateid',$v1['id'])
  836. ->where('org_id',$org_id)
  837. ->where('del',0)
  838. ->where($map)
  839. ->where($map1)
  840. ->sum('weight');
  841. $we = $kg?round($kg/1000,2):0;
  842. $res[$k][] = $we;
  843. $kg = Db::name('waste_record')
  844. ->where('cateid',$v1['id'])
  845. ->where('org_id',$org_id)
  846. ->where('del',0)
  847. ->where($map1)
  848. ->where($map2)
  849. ->sum('weight');
  850. $we = $kg?round($kg/1000,2):0;
  851. $total[$k][] =$we;
  852. }
  853. }
  854. return ['res'=>$res,'total'=>$total];
  855. }
  856. public function wasteMonthExport(){
  857. $month = input('start',date('Y-m'));
  858. $addr_id = input('addr_id','');
  859. $type_id = input('type_id','');
  860. set_time_limit(0);
  861. ini_set("memory_limit", "1024M");
  862. $tMap[] = ['del','=',0];
  863. $tMap[] = ['enable','=',1];
  864. // $tMap[] = ['org_id','=',$this->orgId];
  865. if($type_id!=''){
  866. $tMap[] = ['id','in',explode(',',$type_id)];
  867. $typeList = Db::name('waste_type')
  868. ->where($tMap)
  869. ->field('id,title')
  870. ->select();
  871. }else{
  872. $typeList = Db::name('waste_type')
  873. ->where($tMap)
  874. ->field('id,title')
  875. ->select();
  876. }
  877. $data = $this->wasteMonthData($month,$addr_id,$type_id);
  878. $title = '医废月统计';
  879. if(!empty($month)){
  880. $title = $month.'医废月统计';
  881. }
  882. header("Content-type: application/vnd.ms-excel");
  883. header("Content-Type: application/force-download");
  884. header("Content-Disposition: attachment; filename=".$title.".xls");
  885. header('Expires:0');
  886. header('Pragma:public');
  887. $res = '';
  888. $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
  889. $res.='<tr style="background: #ffffff;">
  890. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">日期</th>';
  891. foreach ($typeList as $k=>$v){
  892. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
  893. }
  894. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
  895. $res.='<tr style="background: #ffffff;">';
  896. foreach ($typeList as $v){
  897. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
  898. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量/kg</th>';
  899. }
  900. foreach ($data['res'] as $k=>$v){
  901. $res.='<tr style="background: #ffffff;">';
  902. $kk = $k+1;
  903. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$kk.'</td>';
  904. foreach ($v as $k1=>$v1){
  905. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  906. }
  907. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
  908. $res.='</tr>';
  909. }
  910. $res.='<tr style="background: #ffffff;">
  911. <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">共计:</td>';
  912. if(!empty($data['total'])){
  913. foreach ($data['total'][0] as $k1=>$v1){
  914. $res.=' <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  915. }
  916. }
  917. $res.='<td style="border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>
  918. </tr>';
  919. echo $res;
  920. }
  921. // 医废年统计
  922. public function wasteYears() {
  923. $month = input('start',date('Y'));
  924. $addr_id = input('addr_id','');
  925. $type_id = input('type_id','');
  926. $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
  927. $tMap[] = ['del','=',0];
  928. $tMap[] = ['enable','=',1];
  929. //$tMap[] = ['org_id','=',$this->orgId];
  930. $typeList1 = Db::name('waste_type')
  931. ->where($tMap)
  932. ->field('id,title')
  933. ->select();
  934. if($type_id!=''){
  935. $tMap[] = ['id','in',explode(',',$type_id)];
  936. $typeList = Db::name('waste_type')
  937. ->where($tMap)
  938. ->field('id,title')
  939. ->select();
  940. }else{
  941. $typeList = Db::name('waste_type')
  942. ->where($tMap)
  943. ->field('id,title')
  944. ->select();
  945. }
  946. $data = $this->wasteYearsData($month,$addr_id,$type_id);
  947. $this->assign('data',$data);
  948. $this->assign('typeList',$typeList);
  949. $this->assign('typeList1',$typeList1);
  950. $this->assign('addressList',$addressList);
  951. $this->assign('start',$month);
  952. $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
  953. $this->assign('type_id',$type_id?explode(',',$type_id):[]);
  954. return $this->fetch();
  955. }
  956. public function wasteYearsData($month,$addr_id,$type_id){
  957. $org_id = $this->orgId;
  958. $dep = get_month($month);
  959. $tMap = [];
  960. if($type_id!=''){
  961. $tMap[] = ['id','in',explode(',',$type_id)];
  962. }
  963. $type = Db::name('waste_type')
  964. ->where('enable',1)
  965. ->where('del',0)
  966. //->where('org_id',$this->orgId)
  967. ->where($tMap)
  968. ->select();
  969. foreach ($dep as $k=>$v){
  970. foreach ($type as $k1=>$v1){
  971. $map = [];
  972. $map[] = ['create_yyyymm','=',$v['date']];
  973. $map1 = [];
  974. if($addr_id!=''){
  975. $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
  976. }
  977. $row = Db::name('waste_record')
  978. ->where('cateid',$v1['id'])
  979. ->where('org_id',$org_id)
  980. ->where('del',0)
  981. ->where($map)
  982. ->where($map1)
  983. ->count();
  984. $dep[$k]['list'][] = $row;
  985. $map2 = [];
  986. $map2[] = ['create_yyyy','=',$month];
  987. $row = Db::name('waste_record')
  988. ->where('cateid',$v1['id'])
  989. ->where('org_id',$org_id)
  990. ->where('del',0)
  991. ->where($map1)
  992. ->where($map2)
  993. ->count();
  994. $dep[$k]['total'][] = $row;
  995. $kg = Db::name('waste_record')
  996. ->where('cateid',$v1['id'])
  997. ->where('org_id',$org_id)
  998. ->where('del',0)
  999. ->where($map)
  1000. ->where($map1)
  1001. ->sum('weight');
  1002. $we = $kg?round($kg/1000,2):0;
  1003. $dep[$k]['list'][] = $we;
  1004. $kg = Db::name('waste_record')
  1005. ->where('cateid',$v1['id'])
  1006. ->where('org_id',$org_id)
  1007. ->where('del',0)
  1008. ->where($map1)
  1009. ->where($map2)
  1010. ->sum('weight');
  1011. $we = $kg?round($kg/1000,2):0;
  1012. $dep[$k]['total'][] = $we;
  1013. }
  1014. }
  1015. return $dep;
  1016. }
  1017. public function wasteYearsExport() {
  1018. $month = input('start',date('Y'));
  1019. $addr_id = input('addr_id','');
  1020. $type_id = input('type_id','');
  1021. set_time_limit(0);
  1022. ini_set("memory_limit", "1024M");
  1023. $tMap[] = ['del','=',0];
  1024. $tMap[] = ['enable','=',1];
  1025. //$tMap[] = ['org_id','=',$this->orgId];
  1026. if($type_id!=''){
  1027. $tMap[] = ['id','in',explode(',',$type_id)];
  1028. $typeList = Db::name('waste_type')
  1029. ->where($tMap)
  1030. ->field('id,title')
  1031. ->select();
  1032. }else{
  1033. $typeList = Db::name('waste_type')
  1034. ->where($tMap)
  1035. ->field('id,title')
  1036. ->select();
  1037. }
  1038. $data = $this->wasteYearsData($month,$addr_id,$type_id);
  1039. $title = '医废年统计';
  1040. if(!empty($month)){
  1041. $title = $month.'医废年统计';
  1042. }
  1043. header("Content-type: application/vnd.ms-excel");
  1044. header("Content-Type: application/force-download");
  1045. header("Content-Disposition: attachment; filename=".$title.".xls");
  1046. header('Expires:0');
  1047. header('Pragma:public');
  1048. $res = '';
  1049. $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
  1050. $res.='<tr style="background: #ffffff;">
  1051. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">日期</th>';
  1052. foreach ($typeList as $k=>$v){
  1053. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
  1054. }
  1055. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
  1056. $res.='</tr>';
  1057. $res.='<tr style="background: #ffffff;">';
  1058. foreach ($typeList as $v){
  1059. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
  1060. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量/kg</th>';
  1061. }
  1062. $res.='</tr>';
  1063. foreach ($data as $k=>$v){
  1064. $res.='<tr style="background: #ffffff;">';
  1065. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['key'].'</td>';
  1066. foreach ($v['list'] as $k1=>$v1){
  1067. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  1068. }
  1069. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
  1070. $res.='</tr>';
  1071. }
  1072. $res.='<tr style="background: #ffffff;">
  1073. <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">共计:</td>';
  1074. if(!empty($data)){
  1075. foreach ($data[1]['total'] as $k1=>$v1){
  1076. $res.=' <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  1077. }
  1078. }
  1079. $res.='<td style="border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>
  1080. </tr>';
  1081. echo $res;
  1082. }
  1083. // 医废暂存统计
  1084. public function wasteZc() {
  1085. $addr_id = input('addr_id','');
  1086. $type_id = input('type_id','');
  1087. $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
  1088. $tMap[] = ['del','=',0];
  1089. $tMap[] = ['enable','=',1];
  1090. // $tMap[] = ['org_id','=',$this->orgId];
  1091. $typeList1 = Db::name('waste_type')
  1092. ->where($tMap)
  1093. ->field('id,title')
  1094. ->select();
  1095. if($type_id!=''){
  1096. $tMap[] = ['id','in',explode(',',$type_id)];
  1097. $typeList = Db::name('waste_type')
  1098. ->where($tMap)
  1099. ->field('id,title')
  1100. ->select();
  1101. }else{
  1102. $typeList = Db::name('waste_type')
  1103. ->where($tMap)
  1104. ->field('id,title')
  1105. ->select();
  1106. }
  1107. $data = $this->wasteZcData($addr_id,$type_id);
  1108. $this->assign('data',$data);
  1109. $this->assign('typeList',$typeList);
  1110. $this->assign('typeList1',$typeList1);
  1111. $this->assign('addressList',$addressList);
  1112. $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
  1113. $this->assign('type_id',$type_id?explode(',',$type_id):[]);
  1114. return $this->fetch();
  1115. }
  1116. public function wasteZcData($addr_id,$type_id){
  1117. $org_id = $this->orgId;
  1118. $tMap = [];
  1119. if($type_id!=''){
  1120. $tMap[] = ['id','in',explode(',',$type_id)];
  1121. }
  1122. $type = Db::name('waste_type')
  1123. ->where('enable',1)
  1124. ->where('del',0)
  1125. //->where('org_id',$this->orgId)
  1126. ->where($tMap)
  1127. ->select();
  1128. $res = [];
  1129. foreach ($type as $k1=>$v1){
  1130. $map1 = [];
  1131. if($addr_id!=''){
  1132. $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
  1133. }
  1134. $row = Db::name('waste_record')
  1135. ->where('cateid',$v1['id'])
  1136. ->where('org_id',$org_id)
  1137. ->where('del',0)
  1138. ->where($map1)
  1139. ->where(function ($query)
  1140. {
  1141. $query->where('status', 1)->whereor('status', 2);
  1142. })
  1143. ->count();
  1144. $res[]= $row;
  1145. $kg = Db::name('waste_record')
  1146. ->where('cateid',$v1['id'])
  1147. ->where('org_id',$org_id)
  1148. ->where('del',0)
  1149. ->where($map1)
  1150. ->where(function ($query)
  1151. {
  1152. $query->where('status', 1)->whereor('status', 2);
  1153. })
  1154. ->sum('weight');
  1155. $we = $kg?round($kg/1000,2):0;
  1156. $res[]= $we;
  1157. }
  1158. return $res;
  1159. }
  1160. // 医废暂存统计导出
  1161. public function wasteZcExport() {
  1162. $addr_id = input('addr_id','');
  1163. $type_id = input('type_id','');
  1164. set_time_limit(0);
  1165. ini_set("memory_limit", "1024M");
  1166. $tMap[] = ['del','=',0];
  1167. $tMap[] = ['enable','=',1];
  1168. //$tMap[] = ['org_id','=',$this->orgId];
  1169. if($type_id!=''){
  1170. $tMap[] = ['id','in',explode(',',$type_id)];
  1171. $typeList = Db::name('waste_type')
  1172. ->where($tMap)
  1173. ->field('id,title')
  1174. ->select();
  1175. }else{
  1176. $typeList = Db::name('waste_type')
  1177. ->where($tMap)
  1178. ->field('id,title')
  1179. ->select();
  1180. }
  1181. $data = $this->wasteZcData($addr_id,$type_id);
  1182. $title = '医废暂存统计';
  1183. header("Content-type: application/vnd.ms-excel");
  1184. header("Content-Type: application/force-download");
  1185. header("Content-Disposition: attachment; filename=".$title.".xls");
  1186. header('Expires:0');
  1187. header('Pragma:public');
  1188. $res = '';
  1189. $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
  1190. $res.='<tr style="background: #ffffff;">';
  1191. foreach ($typeList as $k=>$v){
  1192. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
  1193. }
  1194. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
  1195. $res.='</tr>';
  1196. $res.='<tr style="background: #ffffff;">';
  1197. foreach ($typeList as $v){
  1198. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
  1199. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量/kg</th>';
  1200. }
  1201. $res.='</tr>';
  1202. $res.='<tr style="background: #ffffff;">';
  1203. foreach ($data as $k=>$v){
  1204. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v.'</td>';
  1205. }
  1206. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
  1207. $res.='</tr>';
  1208. echo $res;
  1209. }
  1210. }