WasteStatistics.php 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013
  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,type')
  605. ->select();
  606. }else{
  607. $typeList = Db::name('waste_type')
  608. ->where($tMap)
  609. ->field('id,title,type')
  610. ->select();
  611. }
  612. $data = $this->wasteDataDay($month,$addr_id,$type_id);
  613. $this->assign('data',$data['list']);
  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($tMap)
  641. ->where('enable',1)
  642. ->where('del',0)
  643. ->select();
  644. $date = date('Ymd',strtotime($month));
  645. $tnum = $tkg = 0;
  646. $records = Db::name('waste_record')
  647. ->where('org_id',$org_id)
  648. ->where('del',0)
  649. ->where('create_yyyymmdd',$date)
  650. ->field('waste_device_id,cateid,weight,num')
  651. ->select();
  652. foreach ($dep as $k=>$v){
  653. $ttkg = $ttnums = 0;
  654. foreach ($type as $k1=>$v1){
  655. $datas = 0;
  656. $totals = 0;
  657. $weight = 0;
  658. $weight2 = 0;
  659. $ttnums = 0;
  660. $totalkg = 0;
  661. $num = '';
  662. $totalNum = 0;
  663. $num2 = '';
  664. $totalNum2 = 0;
  665. foreach ($records as $rk=>$rv){
  666. if($rv['waste_device_id'] == $v['id'] && $rv['cateid'] == $v1['id']){
  667. $datas++;
  668. $weight += $rv['weight'];
  669. // $weight2 += $rv['weight'];
  670. if($v1['type'] == 1){
  671. $totalNum += $rv['num'];
  672. }
  673. }
  674. if($rv['cateid'] == $v1['id']){
  675. if($addr_id!=''){
  676. $addr_ids = explode(',',$addr_id);
  677. if(in_array($rv['waste_device_id'],$addr_ids)){
  678. $totals++;
  679. $weight2 += $rv['weight'];
  680. if($v1['type'] == 1){
  681. $totalNum2 += $rv['num'];
  682. }
  683. }
  684. }else{
  685. $totals++;
  686. $weight2 += $rv['weight'];
  687. if($v1['type'] == 1){
  688. $totalNum2 += $rv['num'];
  689. }
  690. }
  691. }
  692. if($rv['waste_device_id'] == $v['id']){
  693. if($addr_id!=''){
  694. $addr_ids = explode(',',$addr_id);
  695. if(in_array($rv['waste_device_id'],$addr_ids)){
  696. $ttnums++;
  697. $totalkg += $rv['weight'];
  698. if($v1['type'] == 1){
  699. $totalNum2 += $rv['num'];
  700. }
  701. }
  702. }else{
  703. $ttnums++;
  704. $totalkg += $rv['weight'];
  705. if($v1['type'] == 1){
  706. $totalNum2 += $rv['num'];
  707. }
  708. }
  709. }
  710. }
  711. $dep[$k]['data'][] = $datas;
  712. $dep[$k]['total'][] = $totals;
  713. // $dep[$k]['data'][] = $weight?round($weight/1000,2):0;
  714. // $dep[$k]['total'][] = $weight2?round($weight2/1000,2):0;
  715. $dep[$k]['nums'] = $ttnums;
  716. $ttkg = $totalkg?round($totalkg/1000,3):0;
  717. $dep[$k]['totalkg'] = $ttkg;
  718. $we = $weight?round($weight/1000,3):0;
  719. if($totalNum > 0){
  720. $num = '('.$totalNum.$v1['unit'].')';
  721. }
  722. $dep[$k]['data'][] = $we.$num;
  723. $we2 = $weight2?round($weight2/1000,3):0;
  724. if($totalNum2 > 0){
  725. $num2 = '('.$totalNum2.$v1['unit'].')';
  726. }
  727. $dep[$k]['total'][] =$we2.$num2;
  728. // $dep[$k]['total'][] = $we2 > 0 ? $we2 : $totalNum2.$v1['unit'];
  729. }
  730. $tnum += $ttnums;
  731. $tkg += $ttkg;
  732. }
  733. $data = [
  734. 'list'=>$dep,
  735. 'tnum'=>$tnum,
  736. 'tkg'=>$tkg,
  737. ];
  738. return $data;
  739. }
  740. public function wasteDataDayOld($month,$addr_id,$type_id){
  741. $org_id = $this->orgId;
  742. $aMap = [];
  743. if($addr_id!=''){
  744. $aMap[] = ['id','in',explode(',',$addr_id)];
  745. }
  746. $aMap[]=['','exp',Db::raw("FIND_IN_SET(7,types)")];
  747. $dep = Db::name('address')
  748. ->where('org_id',$org_id)
  749. ->where('enable',1)
  750. ->where('del',0)
  751. ->where($aMap)
  752. ->select();
  753. $tMap = [];
  754. if($type_id!=''){
  755. $tMap[] = ['id','in',explode(',',$type_id)];
  756. }
  757. $type = Db::name('waste_type')
  758. ->where('enable',1)
  759. ->where('del',0)
  760. //->where('org_id',$this->orgId)
  761. ->where($tMap)
  762. ->select();
  763. $date = date('Ymd',strtotime($month));
  764. foreach ($dep as $k=>$v){
  765. foreach ($type as $k1=>$v1){
  766. $map = [];
  767. $map[] = ['CREATE_YYYYMMDD','=',$date];
  768. $row = Db::name('waste_record')
  769. ->where('waste_device_id',$v['id'])
  770. ->where('cateid',$v1['id'])
  771. ->where('org_id',$org_id)
  772. ->where('del',0)
  773. ->where($map)
  774. ->count();
  775. $dep[$k]['data'][] = $row;
  776. $map1 = [];
  777. if($addr_id!=''){
  778. $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
  779. }
  780. $dep[$k]['total'][] = Db::name('waste_record')
  781. ->where('cateid',$v1['id'])
  782. ->where('org_id',$org_id)
  783. ->where('del',0)
  784. ->where($map)
  785. ->where($map1)
  786. ->count();
  787. $kg = Db::name('waste_record')
  788. ->where('waste_device_id',$v['id'])
  789. ->where('cateid',$v1['id'])
  790. ->where('org_id',$org_id)
  791. ->where('del',0)
  792. ->where($map)
  793. ->sum('weight');
  794. $we = $kg?round($kg/1000,2):0;
  795. $dep[$k]['data'][] = $we;
  796. $kg = Db::name('waste_record')
  797. ->where('cateid',$v1['id'])
  798. ->where('org_id',$org_id)
  799. ->where('del',0)
  800. ->where($map)
  801. ->where($map1)
  802. ->sum('weight');
  803. $we = $kg?round($kg/1000,2):0;
  804. $dep[$k]['total'][] =$we;
  805. }
  806. }
  807. return $dep;
  808. }
  809. //医废日统计导出
  810. public function wasteDayExport()
  811. {
  812. set_time_limit(0);
  813. ini_set("memory_limit", "1024M");
  814. $month = input('start',date('Y-m-d'));
  815. $addr_id = input('addr_id','');
  816. $type_id = input('type_id','');
  817. $data = $this->wasteDataDay($month,$addr_id,$type_id);
  818. $data = $data['list'];
  819. $tMap[] = ['del','=',0];
  820. $tMap[] = ['enable','=',1];
  821. //$tMap[] = ['org_id','=',$this->orgId];
  822. if($type_id!=''){
  823. $tMap[] = ['id','in',explode(',',$type_id)];
  824. $type = Db::name('waste_type')
  825. ->where($tMap)
  826. ->field('id,title,type')
  827. ->select();
  828. }else{
  829. $type = Db::name('waste_type')
  830. ->where($tMap)
  831. ->field('id,title,type')
  832. ->select();
  833. }
  834. $title = '医废日统计';
  835. if(!empty($month)){
  836. $title = $month.'医废日统计';
  837. }
  838. header("Content-type: application/vnd.ms-excel");
  839. header("Content-Type: application/force-download");
  840. header("Content-Disposition: attachment; filename=".$title.".xls");
  841. header('Expires:0');
  842. header('Pragma:public');
  843. $res = '';
  844. $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
  845. $res.='<tr style="background: #ffffff;">
  846. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">地点</th>';
  847. foreach ($type as $k=>$v){
  848. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
  849. }
  850. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
  851. $res.='<tr style="background: #ffffff;">';
  852. foreach ($type as $v){
  853. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
  854. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量kg</th>';
  855. }
  856. foreach ($data as $k=>$v){
  857. $res.='<tr style="background: #ffffff;">';
  858. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['title'].'</td>';
  859. foreach ($v['data'] as $k1=>$v1){
  860. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  861. }
  862. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
  863. $res.='</tr>';
  864. }
  865. $res.='<tr style="background: #ffffff;">
  866. <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">共计:</td>';
  867. if(!empty($data)){
  868. foreach ($data[0]['total'] as $k1=>$v1){
  869. $res.=' <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  870. }
  871. }
  872. $res.='<td style="border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>
  873. </tr>';
  874. echo $res;
  875. }
  876. // 医废月统计
  877. public function wasteMonth() {
  878. $month = input('start',date('Y-m'));
  879. $addr_id = input('addr_id','');
  880. $type_id = input('type_id','');
  881. $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
  882. $tMap[] = ['del','=',0];
  883. $tMap[] = ['enable','=',1];
  884. // $tMap[] = ['org_id','=',$this->orgId];
  885. $typeList1 = Db::name('waste_type')
  886. ->where($tMap)
  887. ->field('id,title')
  888. ->select();
  889. if($type_id!=''){
  890. $tMap[] = ['id','in',explode(',',$type_id)];
  891. $typeList = Db::name('waste_type')
  892. ->where($tMap)
  893. ->field('id,title,type')
  894. ->select();
  895. }else{
  896. $typeList = Db::name('waste_type')
  897. ->where($tMap)
  898. ->field('id,title,type')
  899. ->select();
  900. }
  901. $data = $this->wasteMonthData($month,$addr_id,$type_id);
  902. $this->assign('data',$data['res']);
  903. $this->assign('totalData',$data['total']);
  904. $this->assign('typeList',$typeList);
  905. $this->assign('typeList1',$typeList1);
  906. $this->assign('addressList',$addressList);
  907. $this->assign('start',$month);
  908. $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
  909. $this->assign('type_id',$type_id?explode(',',$type_id):[]);
  910. return $this->fetch();
  911. }
  912. public function wasteMonthData($month,$addr_id,$type_id){
  913. $org_id = $this->orgId;
  914. $dep = getDay(strtotime($month));
  915. $tMap = [];
  916. if($type_id!=''){
  917. $tMap[] = ['id','in',explode(',',$type_id)];
  918. }
  919. // $type = Db::name('waste_type')
  920. // ->where('enable',1)
  921. // ->where('del',0)
  922. // ->where($tMap)
  923. // ->select();
  924. $type = Db::name('waste_type')
  925. ->where($tMap)
  926. ->where('enable',1)
  927. ->where('del',0)
  928. ->select();
  929. $newTypeIds = array_column($type,'id');
  930. $res = [];
  931. $total = [];
  932. $records = Db::name('waste_record')
  933. ->alias('wr')
  934. ->join('address a','a.id=wr.waste_device_id')
  935. ->join('waste_type wt','wt.id=wr.cateid')
  936. ->where('wr.org_id',$org_id)
  937. ->where('wr.del',0)
  938. ->where('wr.cateid','in',$newTypeIds)
  939. ->where('wr.create_yyyymm',date('Ym',strtotime($month)))
  940. ->field('wr.waste_device_id,wr.cateid,wr.weight,wr.create_yyyymmdd,wr.num')
  941. ->select();
  942. $tnum = $tkg = 0;
  943. foreach ($dep as $k=>$v){
  944. $ttkg = $ttnums = 0;
  945. foreach ($type as $k1=>$v1){
  946. $row = 0;
  947. $row2 = 0;
  948. $kg = 0;
  949. $kg2 = 0;
  950. $num = '';
  951. $totalNum = 0;
  952. $num2 = '';
  953. $totalNum2 = 0;
  954. $ttnums = 0;
  955. $totalkg = 0;
  956. foreach ($records as $rk=>$rv){
  957. if($rv['cateid'] == $v1['id'] && $rv['create_yyyymmdd'] == $v){
  958. if($addr_id!=''){
  959. $addr_ids = explode(',',$addr_id);
  960. if(in_array($rv['waste_device_id'],$addr_ids)){
  961. $row++;
  962. $kg += $rv['weight'];
  963. if($v1['type'] == 1){
  964. $totalNum += $rv['num'];
  965. }
  966. }
  967. }else{
  968. $row++;
  969. $kg += $rv['weight'];
  970. if($v1['type'] == 1){
  971. $totalNum += $rv['num'];
  972. }
  973. }
  974. }
  975. if($rv['create_yyyymmdd'] == $v){
  976. if($addr_id!=''){
  977. $addr_ids = explode(',',$addr_id);
  978. if(in_array($rv['waste_device_id'],$addr_ids)){
  979. $ttnums +=1;
  980. $totalkg += $rv['weight'];
  981. }
  982. }else{
  983. $ttnums +=1;
  984. $totalkg += $rv['weight'];
  985. }
  986. }
  987. if($rv['cateid'] == $v1['id']){
  988. if($addr_id!=''){
  989. $addr_ids = explode(',',$addr_id);
  990. if(in_array($rv['waste_device_id'],$addr_ids)){
  991. $row2++;
  992. $kg2 += $rv['weight'];
  993. if($v1['type'] == 1){
  994. $totalNum2 += $rv['num'];
  995. }
  996. }
  997. }else{
  998. $row2++;
  999. $kg2 += $rv['weight'];
  1000. if($v1['type'] == 1){
  1001. $totalNum2 += $rv['num'];
  1002. }
  1003. }
  1004. }
  1005. }
  1006. $res[$k]['list'][] = $row;
  1007. $total[$k][] = $row2;
  1008. $res[$k]['nums'] = $ttnums;
  1009. $ttkg = $totalkg?round($totalkg/1000,3):0;
  1010. $res[$k]['totalkg'] = $ttkg;
  1011. $we = $kg?round($kg/1000,2):0;
  1012. // $res[$k]['list'][] = $we > 0 ? $we : $totalNum.$v1['unit'];
  1013. $we2 = $kg2?round($kg2/1000,2):0;
  1014. if($totalNum > 0){
  1015. $num = '('.$totalNum.$v1['unit'].')';
  1016. }
  1017. $res[$k]['list'][] = $we.$num;
  1018. if($totalNum2 > 0){
  1019. $num2 = '('.$totalNum2.$v1['unit'].')';
  1020. }
  1021. $total[$k][] =$we2.$num2;
  1022. // $total[$k][] = $we2 > 0 ? $we2 : $totalNum2.$v1['unit'];
  1023. // $we = $kg?round($kg/1000,2):0;
  1024. // $res[$k][] = $we;
  1025. //
  1026. // $we = $kg2?round($kg2/1000,2):0;
  1027. // $total[$k][] =$we;
  1028. }
  1029. $tnum += $ttnums;
  1030. $tkg += $ttkg;
  1031. }
  1032. return ['res'=>$res,'total'=>$total,'tnum'=>$tnum,'tkg'=>$tkg];
  1033. }
  1034. public function wasteMonthDataOld($month,$addr_id,$type_id){
  1035. $org_id = $this->orgId;
  1036. $dep = getDay(strtotime($month));
  1037. $tMap = [];
  1038. if($type_id!=''){
  1039. $tMap[] = ['id','in',explode(',',$type_id)];
  1040. }
  1041. $type = Db::name('waste_type')
  1042. ->where('enable',1)
  1043. ->where('del',0)
  1044. // ->where('org_id',$this->orgId)
  1045. ->where($tMap)
  1046. ->select();
  1047. $res = [];
  1048. $total = [];
  1049. foreach ($dep as $k=>$v){
  1050. foreach ($type as $k1=>$v1){
  1051. $map = [];
  1052. $map[] = ['CREATE_YYYYMMDD','=',$v];
  1053. $map1 = [];
  1054. if($addr_id!=''){
  1055. $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
  1056. }
  1057. $row = Db::name('waste_record')
  1058. ->where('cateid',$v1['id'])
  1059. ->where('org_id',$org_id)
  1060. ->where('del',0)
  1061. ->where($map)
  1062. ->where($map1)
  1063. ->count();
  1064. $res[$k][] = $row;
  1065. $map2 = [];
  1066. $map2[] = ['CREATE_YYYYMM','=',date('Ym',strtotime($v))];
  1067. $row = Db::name('waste_record')
  1068. ->where('cateid',$v1['id'])
  1069. ->where('org_id',$org_id)
  1070. ->where('del',0)
  1071. ->where($map1)
  1072. ->where($map2)
  1073. ->count();
  1074. $total[$k][] = $row;
  1075. $kg = Db::name('waste_record')
  1076. ->where('cateid',$v1['id'])
  1077. ->where('org_id',$org_id)
  1078. ->where('del',0)
  1079. ->where($map)
  1080. ->where($map1)
  1081. ->sum('weight');
  1082. $we = $kg?round($kg/1000,2):0;
  1083. $res[$k][] = $we;
  1084. $kg = Db::name('waste_record')
  1085. ->where('cateid',$v1['id'])
  1086. ->where('org_id',$org_id)
  1087. ->where('del',0)
  1088. ->where($map1)
  1089. ->where($map2)
  1090. ->sum('weight');
  1091. $we = $kg?round($kg/1000,2):0;
  1092. $total[$k][] =$we;
  1093. }
  1094. }
  1095. return ['res'=>$res,'total'=>$total];
  1096. }
  1097. public function wasteMonthExport(){
  1098. $month = input('start',date('Y-m'));
  1099. $addr_id = input('addr_id','');
  1100. $type_id = input('type_id','');
  1101. set_time_limit(0);
  1102. ini_set("memory_limit", "1024M");
  1103. $tMap[] = ['del','=',0];
  1104. $tMap[] = ['enable','=',1];
  1105. // $tMap[] = ['org_id','=',$this->orgId];
  1106. if($type_id!=''){
  1107. $tMap[] = ['id','in',explode(',',$type_id)];
  1108. $typeList = Db::name('waste_type')
  1109. ->where($tMap)
  1110. ->field('id,title,type')
  1111. ->select();
  1112. }else{
  1113. $typeList = Db::name('waste_type')
  1114. ->where($tMap)
  1115. ->field('id,title,type')
  1116. ->select();
  1117. }
  1118. $data = $this->wasteMonthData($month,$addr_id,$type_id);
  1119. $lists = $data['res'];
  1120. $title = '医废月统计';
  1121. if(!empty($month)){
  1122. $title = $month.'医废月统计';
  1123. }
  1124. header("Content-type: application/vnd.ms-excel");
  1125. header("Content-Type: application/force-download");
  1126. header("Content-Disposition: attachment; filename=".$title.".xls");
  1127. header('Expires:0');
  1128. header('Pragma:public');
  1129. $res = '';
  1130. $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
  1131. $res.='<tr style="background: #ffffff;">
  1132. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">日期</th>';
  1133. foreach ($typeList as $k=>$v){
  1134. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
  1135. }
  1136. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
  1137. $res.='<tr style="background: #ffffff;">';
  1138. foreach ($typeList as $v){
  1139. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
  1140. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量kg</th>';
  1141. }
  1142. if(isset($lists[0]['list'])){
  1143. foreach ($lists as $k=>$v){
  1144. $res.='<tr style="background: #ffffff;">';
  1145. $kk = $k+1;
  1146. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$kk.'</td>';
  1147. if(isset($v['list'])){
  1148. foreach ($v['list'] as $k1=>$v1){
  1149. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  1150. }
  1151. }
  1152. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
  1153. $res.='</tr>';
  1154. }
  1155. }
  1156. $res.='<tr style="background: #ffffff;">
  1157. <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">共计:</td>';
  1158. if(!empty($data['total'])){
  1159. foreach ($data['total'][0] as $k1=>$v1){
  1160. $res.=' <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  1161. }
  1162. }
  1163. $res.='<td style="border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>
  1164. </tr>';
  1165. echo $res;
  1166. }
  1167. //科室月统计
  1168. public function wasteDeviceMonth() {
  1169. $month = input('start',date('Y-m'));
  1170. $addr_id = input('addr_id','');
  1171. $type_id = input('type_id','');
  1172. $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
  1173. $tMap[] = ['del','=',0];
  1174. $tMap[] = ['enable','=',1];
  1175. //$tMap[] = ['org_id','=',$this->orgId];
  1176. $typeList1 = Db::name('waste_type')
  1177. ->where($tMap)
  1178. ->field('id,title')
  1179. ->select();
  1180. if($type_id!=''){
  1181. $tMap[] = ['id','in',explode(',',$type_id)];
  1182. $typeList = Db::name('waste_type')
  1183. ->where($tMap)
  1184. ->field('id,title,type')
  1185. ->select();
  1186. }else{
  1187. $typeList = Db::name('waste_type')
  1188. ->where($tMap)
  1189. ->field('id,title,type')
  1190. ->select();
  1191. }
  1192. $data = $this->wasteDeviceMonthData($month,$addr_id,$type_id);
  1193. $this->assign('data',$data['list']);
  1194. $this->assign('typeList',$typeList);
  1195. $this->assign('typeList1',$typeList1);
  1196. $this->assign('addressList',$addressList);
  1197. $this->assign('start',$month);
  1198. $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
  1199. $this->assign('type_id',$type_id?explode(',',$type_id):[]);
  1200. return $this->fetch();
  1201. }
  1202. public function wasteDeviceMonthData($month,$addr_id,$type_id){
  1203. $org_id = $this->orgId;
  1204. $aMap = [];
  1205. if($addr_id!=''){
  1206. $aMap[] = ['id','in',explode(',',$addr_id)];
  1207. }
  1208. $aMap[]=['','exp',Db::raw("FIND_IN_SET(7,types)")];
  1209. $dep = Db::name('address')
  1210. ->where('org_id',$org_id)
  1211. ->where('enable',1)
  1212. ->where('del',0)
  1213. ->where($aMap)
  1214. ->select();
  1215. $tMap = [];
  1216. if($type_id!=''){
  1217. $tMap[] = ['id','in',explode(',',$type_id)];
  1218. }
  1219. $type = Db::name('waste_type')
  1220. ->where($tMap)
  1221. ->where('enable',1)
  1222. ->where('del',0)
  1223. ->select();
  1224. $date = date('Ym',strtotime($month));
  1225. $tnum = $tkg = 0;
  1226. $records = Db::name('waste_record')
  1227. ->where('org_id',$org_id)
  1228. ->where('del',0)
  1229. ->where('create_yyyymm',$date)
  1230. ->field('waste_device_id,cateid,weight,num')
  1231. ->select();
  1232. foreach ($dep as $k=>$v){
  1233. $ttkg = $ttnums = 0;
  1234. foreach ($type as $k1=>$v1){
  1235. $datas = 0;
  1236. $totals = 0;
  1237. $weight = 0;
  1238. $weight2 = 0;
  1239. $ttnums = 0;
  1240. $totalkg = 0;
  1241. $num = '';
  1242. $totalNum = 0;
  1243. $num2 = '';
  1244. $totalNum2 = 0;
  1245. foreach ($records as $rk=>$rv){
  1246. if($rv['waste_device_id'] == $v['id'] && $rv['cateid'] == $v1['id']){
  1247. $datas++;
  1248. $weight += $rv['weight'];
  1249. // $weight2 += $rv['weight'];
  1250. if($v1['type'] == 1){
  1251. $totalNum += $rv['num'];
  1252. }
  1253. }
  1254. if($rv['cateid'] == $v1['id']){
  1255. if($addr_id!=''){
  1256. $addr_ids = explode(',',$addr_id);
  1257. if(in_array($rv['waste_device_id'],$addr_ids)){
  1258. $totals++;
  1259. $weight2 += $rv['weight'];
  1260. if($v1['type'] == 1){
  1261. $totalNum2 += $rv['num'];
  1262. }
  1263. }
  1264. }else{
  1265. $totals++;
  1266. $weight2 += $rv['weight'];
  1267. if($v1['type'] == 1){
  1268. $totalNum2 += $rv['num'];
  1269. }
  1270. }
  1271. }
  1272. if($rv['waste_device_id'] == $v['id']){
  1273. if($addr_id!=''){
  1274. $addr_ids = explode(',',$addr_id);
  1275. if(in_array($rv['waste_device_id'],$addr_ids)){
  1276. $ttnums++;
  1277. $totalkg += $rv['weight'];
  1278. if($v1['type'] == 1){
  1279. $totalNum2 += $rv['num'];
  1280. }
  1281. }
  1282. }else{
  1283. $ttnums++;
  1284. $totalkg += $rv['weight'];
  1285. if($v1['type'] == 1){
  1286. $totalNum2 += $rv['num'];
  1287. }
  1288. }
  1289. }
  1290. }
  1291. $dep[$k]['data'][] = $datas;
  1292. $dep[$k]['total'][] = $totals;
  1293. // $dep[$k]['data'][] = $weight?round($weight/1000,2):0;
  1294. // $dep[$k]['total'][] = $weight2?round($weight2/1000,2):0;
  1295. $dep[$k]['nums'] = $ttnums;
  1296. $ttkg = $totalkg?round($totalkg/1000,3):0;
  1297. $dep[$k]['totalkg'] = $ttkg;
  1298. $we = $weight?round($weight/1000,3):0;
  1299. if($totalNum > 0){
  1300. $num = '('.$totalNum.$v1['unit'].')';
  1301. }
  1302. $dep[$k]['data'][] = $we.$num;
  1303. $we2 = $weight2?round($weight2/1000,3):0;
  1304. if($totalNum2 > 0){
  1305. $num2 = '('.$totalNum2.$v1['unit'].')';
  1306. }
  1307. $dep[$k]['total'][] =$we2.$num2;
  1308. // $dep[$k]['total'][] = $we2 > 0 ? $we2 : $totalNum2.$v1['unit'];
  1309. }
  1310. $tnum += $ttnums;
  1311. $tkg += $ttkg;
  1312. }
  1313. $data = [
  1314. 'list'=>$dep,
  1315. 'tnum'=>$tnum,
  1316. 'tkg'=>$tkg,
  1317. ];
  1318. return $data;
  1319. }
  1320. public function wasteDeviceMonthExport()
  1321. {
  1322. set_time_limit(0);
  1323. ini_set("memory_limit", "1024M");
  1324. $month = input('start',date('Y-m-d'));
  1325. $addr_id = input('addr_id','');
  1326. $type_id = input('type_id','');
  1327. $data = $this->wasteDeviceMonthData($month,$addr_id,$type_id);
  1328. $data = $data['list'];
  1329. $tMap[] = ['del','=',0];
  1330. $tMap[] = ['enable','=',1];
  1331. if($type_id!=''){
  1332. $tMap[] = ['id','in',explode(',',$type_id)];
  1333. $type = Db::name('waste_type')
  1334. ->where($tMap)
  1335. ->field('id,title,type')
  1336. ->select();
  1337. }else{
  1338. $type = Db::name('waste_type')
  1339. ->where($tMap)
  1340. ->field('id,title,type')
  1341. ->select();
  1342. }
  1343. $title = '科室月统计';
  1344. if(!empty($month)){
  1345. $title = $month.'科室月统计';
  1346. }
  1347. header("Content-type: application/vnd.ms-excel");
  1348. header("Content-Type: application/force-download");
  1349. header("Content-Disposition: attachment; filename=".$title.".xls");
  1350. header('Expires:0');
  1351. header('Pragma:public');
  1352. $res = '';
  1353. $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
  1354. $res.='<tr style="background: #ffffff;">
  1355. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">地点</th>';
  1356. foreach ($type as $k=>$v){
  1357. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
  1358. }
  1359. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
  1360. $res.='<tr style="background: #ffffff;">';
  1361. foreach ($type as $v){
  1362. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
  1363. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量kg</th>';
  1364. }
  1365. foreach ($data as $k=>$v){
  1366. $res.='<tr style="background: #ffffff;">';
  1367. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['title'].'</td>';
  1368. foreach ($v['data'] as $k1=>$v1){
  1369. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  1370. }
  1371. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
  1372. $res.='</tr>';
  1373. }
  1374. $res.='<tr style="background: #ffffff;">
  1375. <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">共计:</td>';
  1376. if(!empty($data)){
  1377. foreach ($data[0]['total'] as $k1=>$v1){
  1378. $res.=' <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  1379. }
  1380. }
  1381. $res.='<td style="border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>
  1382. </tr>';
  1383. echo $res;
  1384. }
  1385. // 医废年统计
  1386. public function wasteYears() {
  1387. $month = input('start',date('Y'));
  1388. $addr_id = input('addr_id','');
  1389. $type_id = input('type_id','');
  1390. $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
  1391. $tMap[] = ['del','=',0];
  1392. $tMap[] = ['enable','=',1];
  1393. //$tMap[] = ['org_id','=',$this->orgId];
  1394. $typeList1 = Db::name('waste_type')
  1395. ->where($tMap)
  1396. ->field('id,title')
  1397. ->select();
  1398. if($type_id!=''){
  1399. $tMap[] = ['id','in',explode(',',$type_id)];
  1400. $typeList = Db::name('waste_type')
  1401. ->where($tMap)
  1402. ->field('id,title,type')
  1403. ->select();
  1404. }else{
  1405. $typeList = Db::name('waste_type')
  1406. ->where($tMap)
  1407. ->field('id,title,type')
  1408. ->select();
  1409. }
  1410. $data = $this->wasteYearsData($month,$addr_id,$type_id);
  1411. $this->assign('data',$data['list']);
  1412. $this->assign('typeList',$typeList);
  1413. $this->assign('typeList1',$typeList1);
  1414. $this->assign('addressList',$addressList);
  1415. $this->assign('start',$month);
  1416. $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
  1417. $this->assign('type_id',$type_id?explode(',',$type_id):[]);
  1418. return $this->fetch();
  1419. }
  1420. public function wasteYearsData($month,$addr_id,$type_id){
  1421. $org_id = $this->orgId;
  1422. $dep = get_month1($month);
  1423. $tMap = [];
  1424. if($type_id!=''){
  1425. $tMap[] = ['id','in',explode(',',$type_id)];
  1426. }
  1427. $type = Db::name('waste_type')
  1428. ->where($tMap)
  1429. ->where('enable',1)
  1430. ->where('del',0)
  1431. ->select();
  1432. $records = Db::name('waste_record')
  1433. ->where('org_id',$org_id)
  1434. ->where('del',0)
  1435. ->where('create_yyyy',$month)
  1436. ->field('waste_device_id,cateid,weight,create_yyyymm,num')
  1437. ->select();
  1438. $tnum = $tkg = 0;
  1439. foreach ($dep as $k=>$v){
  1440. $ttkg = $ttnums = 0;
  1441. foreach ($type as $k1=>$v1){
  1442. $row = 0;
  1443. $row2 = 0;
  1444. $kg = 0;
  1445. $kg2 = 0;
  1446. $num = '';
  1447. $totalNum = 0;
  1448. $num2 = '';
  1449. $totalNum2 = 0;
  1450. $ttnums = 0;
  1451. $totalkg = 0;
  1452. foreach ($records as $rk=>$rv){
  1453. if($rv['cateid'] == $v1['id'] && $rv['create_yyyymm'] == $v['date']){
  1454. if($addr_id!=''){
  1455. $addr_ids = explode(',',$addr_id);
  1456. if(in_array($rv['waste_device_id'],$addr_ids)){
  1457. $row++;
  1458. $kg += $rv['weight'];
  1459. if($v1['type'] == 1){
  1460. $totalNum += $rv['num'];
  1461. }
  1462. }
  1463. }else{
  1464. $row++;
  1465. $kg += $rv['weight'];
  1466. if($v1['type'] == 1){
  1467. $totalNum += $rv['num'];
  1468. }
  1469. }
  1470. }
  1471. if($rv['create_yyyymm'] == $v['date']){
  1472. if($addr_id!=''){
  1473. $addr_ids = explode(',',$addr_id);
  1474. if(in_array($rv['waste_device_id'],$addr_ids)){
  1475. $ttnums++;
  1476. $totalkg += $rv['weight'];
  1477. }
  1478. }else{
  1479. $ttnums++;
  1480. $totalkg += $rv['weight'];
  1481. }
  1482. }
  1483. if($rv['cateid'] == $v1['id']){
  1484. if($addr_id!=''){
  1485. $addr_ids = explode(',',$addr_id);
  1486. if(in_array($rv['waste_device_id'],$addr_ids)){
  1487. $row2++;
  1488. $kg2 += $rv['weight'];
  1489. if($v1['type'] == 1){
  1490. $totalNum2 += $rv['num'];
  1491. }
  1492. }
  1493. }else{
  1494. $row2++;
  1495. $kg2 += $rv['weight'];
  1496. if($v1['type'] == 1){
  1497. $totalNum2 += $rv['num'];
  1498. }
  1499. }
  1500. }
  1501. }
  1502. $dep[$k]['list'][] = $row;
  1503. $dep[$k]['total'][] = $row2;
  1504. $dep[$k]['nums'] = $ttnums;
  1505. $ttkg = $totalkg?round($totalkg/1000,3):0;
  1506. $dep[$k]['totalkg'] = $ttkg;
  1507. $we = $kg?round($kg/1000,2):0;
  1508. $we2 = $kg2?round($kg2/1000,2):0;
  1509. if($totalNum > 0){
  1510. $num = '('.$totalNum.$v1['unit'].')';
  1511. }
  1512. $dep[$k]['list'][] = $we.$num;
  1513. if($totalNum2 > 0){
  1514. $num2 = '('.$totalNum2.$v1['unit'].')';
  1515. }
  1516. $dep[$k]['total'][] =$we2.$num2;
  1517. }
  1518. $tnum += $ttnums;
  1519. $tkg += $ttkg;
  1520. }
  1521. $data = [
  1522. 'list'=>$dep,
  1523. 'tnum'=>$tnum,
  1524. 'tkg'=>$tkg,
  1525. ];
  1526. return $data;
  1527. }
  1528. public function wasteYearsDataOld($month,$addr_id,$type_id){
  1529. $org_id = $this->orgId;
  1530. $dep = get_month($month);
  1531. $tMap = [];
  1532. if($type_id!=''){
  1533. $tMap[] = ['id','in',explode(',',$type_id)];
  1534. }
  1535. $type = Db::name('waste_type')
  1536. ->where('enable',1)
  1537. ->where('del',0)
  1538. //->where('org_id',$this->orgId)
  1539. ->where($tMap)
  1540. ->select();
  1541. foreach ($dep as $k=>$v){
  1542. foreach ($type as $k1=>$v1){
  1543. $map = [];
  1544. $map[] = ['create_yyyymm','=',$v['date']];
  1545. $map1 = [];
  1546. if($addr_id!=''){
  1547. $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
  1548. }
  1549. $row = Db::name('waste_record')
  1550. ->where('cateid',$v1['id'])
  1551. ->where('org_id',$org_id)
  1552. ->where('del',0)
  1553. ->where($map)
  1554. ->where($map1)
  1555. ->count();
  1556. $dep[$k]['list'][] = $row;
  1557. $map2 = [];
  1558. $map2[] = ['create_yyyy','=',$month];
  1559. $row = Db::name('waste_record')
  1560. ->where('cateid',$v1['id'])
  1561. ->where('org_id',$org_id)
  1562. ->where('del',0)
  1563. ->where($map1)
  1564. ->where($map2)
  1565. ->count();
  1566. $dep[$k]['total'][] = $row;
  1567. $kg = Db::name('waste_record')
  1568. ->where('cateid',$v1['id'])
  1569. ->where('org_id',$org_id)
  1570. ->where('del',0)
  1571. ->where($map)
  1572. ->where($map1)
  1573. ->sum('weight');
  1574. $we = $kg?round($kg/1000,2):0;
  1575. $dep[$k]['list'][] = $we;
  1576. $kg = Db::name('waste_record')
  1577. ->where('cateid',$v1['id'])
  1578. ->where('org_id',$org_id)
  1579. ->where('del',0)
  1580. ->where($map1)
  1581. ->where($map2)
  1582. ->sum('weight');
  1583. $we = $kg?round($kg/1000,2):0;
  1584. $dep[$k]['total'][] = $we;
  1585. }
  1586. }
  1587. return $dep;
  1588. }
  1589. public function wasteYearsExport() {
  1590. $month = input('start',date('Y'));
  1591. $addr_id = input('addr_id','');
  1592. $type_id = input('type_id','');
  1593. set_time_limit(0);
  1594. ini_set("memory_limit", "1024M");
  1595. $tMap[] = ['del','=',0];
  1596. $tMap[] = ['enable','=',1];
  1597. //$tMap[] = ['org_id','=',$this->orgId];
  1598. if($type_id!=''){
  1599. $tMap[] = ['id','in',explode(',',$type_id)];
  1600. $typeList = Db::name('waste_type')
  1601. ->where($tMap)
  1602. ->field('id,title,type')
  1603. ->select();
  1604. }else{
  1605. $typeList = Db::name('waste_type')
  1606. ->where($tMap)
  1607. ->field('id,title,type')
  1608. ->select();
  1609. }
  1610. $data = $this->wasteYearsData($month,$addr_id,$type_id);
  1611. $list = $data['list'];
  1612. $title = '医废年统计';
  1613. if(!empty($month)){
  1614. $title = $month.'医废年统计';
  1615. }
  1616. header("Content-type: application/vnd.ms-excel");
  1617. header("Content-Type: application/force-download");
  1618. header("Content-Disposition: attachment; filename=".$title.".xls");
  1619. header('Expires:0');
  1620. header('Pragma:public');
  1621. $res = '';
  1622. $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
  1623. $res.='<tr style="background: #ffffff;">
  1624. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">日期</th>';
  1625. foreach ($typeList as $k=>$v){
  1626. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
  1627. }
  1628. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
  1629. $res.='</tr>';
  1630. $res.='<tr style="background: #ffffff;">';
  1631. foreach ($typeList as $v){
  1632. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
  1633. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量kg</th>';
  1634. }
  1635. $res.='</tr>';
  1636. if(isset($list[0]['list'])){
  1637. foreach ($list as $k=>$v){
  1638. $res.='<tr style="background: #ffffff;">';
  1639. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['key'].'</td>';
  1640. if(isset($v['list'])){
  1641. foreach ($v['list'] as $k1=>$v1){
  1642. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  1643. }
  1644. }
  1645. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
  1646. $res.='</tr>';
  1647. }
  1648. }
  1649. $res.='<tr style="background: #ffffff;">
  1650. <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">共计:</td>';
  1651. if(isset($list[1]['total'])){
  1652. foreach ($list[1]['total'] as $k1=>$v1){
  1653. $res.=' <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
  1654. }
  1655. }
  1656. $res.='<td style="border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>
  1657. </tr>';
  1658. echo $res;
  1659. }
  1660. // 医废暂存统计
  1661. public function wasteZc() {
  1662. $addr_id = input('addr_id','');
  1663. $type_id = input('type_id','');
  1664. $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
  1665. $tMap[] = ['del','=',0];
  1666. $tMap[] = ['enable','=',1];
  1667. // $tMap[] = ['org_id','=',$this->orgId];
  1668. $typeList1 = Db::name('waste_type')
  1669. ->where($tMap)
  1670. ->field('id,title')
  1671. ->select();
  1672. if($type_id!=''){
  1673. $tMap[] = ['id','in',explode(',',$type_id)];
  1674. $typeList = Db::name('waste_type')
  1675. ->where($tMap)
  1676. ->field('id,title,type')
  1677. ->select();
  1678. }else{
  1679. $typeList = Db::name('waste_type')
  1680. ->where($tMap)
  1681. ->field('id,title,type')
  1682. ->select();
  1683. }
  1684. $data = $this->wasteZcData($addr_id,$type_id);
  1685. $this->assign('data',$data);
  1686. $this->assign('typeList',$typeList);
  1687. $this->assign('typeList1',$typeList1);
  1688. $this->assign('addressList',$addressList);
  1689. $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
  1690. $this->assign('type_id',$type_id?explode(',',$type_id):[]);
  1691. return $this->fetch();
  1692. }
  1693. public function wasteZcData($addr_id,$type_id){
  1694. $org_id = $this->orgId;
  1695. $tMap = [];
  1696. if($type_id!=''){
  1697. $tMap[] = ['id','in',explode(',',$type_id)];
  1698. }
  1699. $type = Db::name('waste_type')
  1700. ->where('enable',1)
  1701. ->where('del',0)
  1702. //->where('org_id',$this->orgId)
  1703. ->where($tMap)
  1704. ->select();
  1705. $res = [];
  1706. foreach ($type as $k1=>$v1){
  1707. $map1 = [];
  1708. if($addr_id!=''){
  1709. $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
  1710. }
  1711. $row = Db::name('waste_record')
  1712. ->where('cateid',$v1['id'])
  1713. ->where('org_id',$org_id)
  1714. ->where('del',0)
  1715. ->where($map1)
  1716. ->where(function ($query)
  1717. {
  1718. $query->where('status', 1)->whereor('status', 2);
  1719. })
  1720. ->count();
  1721. $res[]= $row;
  1722. $kg = Db::name('waste_record')
  1723. ->where('cateid',$v1['id'])
  1724. ->where('org_id',$org_id)
  1725. ->where('del',0)
  1726. ->where($map1)
  1727. ->where(function ($query)
  1728. {
  1729. $query->where('status', 1)->whereor('status', 2);
  1730. })
  1731. ->sum('weight');
  1732. $we = $kg?round($kg/1000,2):0;
  1733. $num = Db::name('waste_record')
  1734. ->where('cateid',$v1['id'])
  1735. ->where('org_id',$org_id)
  1736. ->where('del',0)
  1737. ->where($map1)
  1738. ->where(function ($query)
  1739. {
  1740. $query->where('status', 1)->whereor('status', 2);
  1741. })
  1742. ->sum('num');
  1743. if($num > 0){
  1744. $we = $we.'kg'.'('.$num.$v1['unit'].')';
  1745. }
  1746. $res[]= $we;
  1747. }
  1748. return $res;
  1749. }
  1750. // 医废暂存统计导出
  1751. public function wasteZcExport() {
  1752. $addr_id = input('addr_id','');
  1753. $type_id = input('type_id','');
  1754. set_time_limit(0);
  1755. ini_set("memory_limit", "1024M");
  1756. $tMap[] = ['del','=',0];
  1757. $tMap[] = ['enable','=',1];
  1758. //$tMap[] = ['org_id','=',$this->orgId];
  1759. if($type_id!=''){
  1760. $tMap[] = ['id','in',explode(',',$type_id)];
  1761. $typeList = Db::name('waste_type')
  1762. ->where($tMap)
  1763. ->field('id,title,type')
  1764. ->select();
  1765. }else{
  1766. $typeList = Db::name('waste_type')
  1767. ->where($tMap)
  1768. ->field('id,title,type')
  1769. ->select();
  1770. }
  1771. $data = $this->wasteZcData($addr_id,$type_id);
  1772. $title = '医废暂存统计';
  1773. header("Content-type: application/vnd.ms-excel");
  1774. header("Content-Type: application/force-download");
  1775. header("Content-Disposition: attachment; filename=".$title.".xls");
  1776. header('Expires:0');
  1777. header('Pragma:public');
  1778. $res = '';
  1779. $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
  1780. $res.='<tr style="background: #ffffff;">';
  1781. foreach ($typeList as $k=>$v){
  1782. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
  1783. }
  1784. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
  1785. $res.='</tr>';
  1786. $res.='<tr style="background: #ffffff;">';
  1787. foreach ($typeList as $v){
  1788. $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
  1789. <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量kg</th>';
  1790. }
  1791. $res.='</tr>';
  1792. $res.='<tr style="background: #ffffff;">';
  1793. foreach ($data as $k=>$v){
  1794. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v.'</td>';
  1795. }
  1796. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
  1797. $res.='</tr>';
  1798. echo $res;
  1799. }
  1800. }