Waste.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace app\api\controller\screen;
  3. use app\hander\HelpHander;
  4. use app\api\controller\screen\Index;
  5. use think\Db;
  6. class Waste extends Index
  7. {
  8. public function cateList(){
  9. $type = input('type',1);
  10. if($type == 1){
  11. $map[] = ['create_yyyy','=',date('Y')];
  12. }else{
  13. $map[] = ['create_yyyymm','=',date('Ym')];
  14. }
  15. $list = Db::name('waste_type')
  16. ->field('id,title')
  17. //->where('org_id',$this->orgId)
  18. ->where('del',0)
  19. ->where('enable',1)
  20. ->limit(6)
  21. ->select();
  22. foreach ($list as $k=>$v){
  23. $record = Db::name('waste_record')
  24. ->where('cateid',$v['id'])
  25. ->where('org_id',$this->orgId)
  26. ->where($map)
  27. ->where('del',0)
  28. ->sum('weight');
  29. $list[$k]['kg'] = round($record/1000,1);
  30. }
  31. HelpHander::success($list);
  32. }
  33. public function deviceList(){
  34. $device = Db::name('dep')
  35. ->alias('d')
  36. ->field('d.title,a.id')
  37. ->join('address a','a.dep_id=d.id')
  38. ->where('d.org_id',$this->orgId)
  39. ->where('d.del',0)
  40. ->where('d.enable',1)
  41. ->where('','exp',Db::raw("FIND_IN_SET(7,a.types)"))
  42. ->where('a.del',0)
  43. ->where('a.enable',1)
  44. ->group('d.title')
  45. ->select();
  46. $cate = Db::name('waste_type')
  47. ->field('id,title')
  48. // ->where('org_id',$this->orgId)
  49. ->where('del',0)
  50. ->where('enable',1)
  51. ->select();
  52. foreach ($device as $k=>$v){
  53. $device[$k]['kg'] = Db::name('waste_record')
  54. ->where('org_id',$this->orgId)
  55. ->where('waste_device_id',$v['id'])
  56. ->where('del',0)
  57. ->where('create_yyyymmdd',date('Ymd'))
  58. ->sum('weight');
  59. }
  60. $newList = array_slice(list_sort_by($device,'kg','desc'),0,5);
  61. $lists = $cateTitle = [];
  62. foreach ($cate as $k=>$v){
  63. $cateTitle[] = $v['title'];
  64. $aa = [];
  65. $lists[$k]['title'] = $v['title'];
  66. foreach ($newList as $kk=>$vv){
  67. $record = Db::name('waste_record')
  68. ->where('org_id',$this->orgId)
  69. ->where('waste_device_id',$vv['id'])
  70. ->where('cateid',$v['id'])
  71. ->where('del',0)
  72. ->where('create_yyyymmdd',date('Ymd'))
  73. ->sum('weight');
  74. $aa[] = !empty($record)? round($record/1000,1):0;
  75. }
  76. $lists[$k]['list'] = $aa;
  77. }
  78. $title = array_column($newList,'title');
  79. // $cc = [];
  80. // foreach ($cateTitle as $k=>$v){
  81. // $cc[$k]['title'] = $v;
  82. // foreach ($lists as $kk=>$vv){
  83. // $cc[$k]['list'] = $vv;
  84. // }
  85. // }
  86. $data = [
  87. 'title'=>$title,
  88. 'title1'=>$cateTitle,
  89. 'list'=>$lists,
  90. ];
  91. HelpHander::success($data);
  92. }
  93. public function userWorkWaste(){
  94. $record = Db::name('waste_record')
  95. ->where('org_id',$this->orgId)
  96. ->where('del',0)
  97. ->where('weight','>',0)
  98. ->where('create_yyyymmdd',date('Ymd'))
  99. ->group('user_id')
  100. ->column('user_id');
  101. $lists = [];
  102. foreach ($record as $k=>$v){
  103. $list1 = Db::name('waste_record')
  104. ->where('org_id',$this->orgId)
  105. ->where('user_id',$v)
  106. ->where('del',0)
  107. ->where('create_yyyymmdd',date('Ymd'))
  108. ->sum('weight');
  109. $lists[$k]['title'] = Db::name('user')->where('id',$v)->value('real_name');
  110. $lists[$k]['kg'] = round($list1/1000,1);
  111. }
  112. $endData = array_slice(list_sort_by($lists,'kg','desc'),0,5);
  113. $data = [
  114. 'title'=>array_column($endData,'title'),
  115. 'lists'=>array_column($endData,'kg'),
  116. ];
  117. HelpHander::success($data);
  118. }
  119. public function wasteRecord(){
  120. $list = Db::name('waste_record')
  121. ->where('del',0)
  122. ->where('org_id',$this->orgId)
  123. ->where('create_yyyymmdd',date('Ymd'))
  124. ->select();
  125. foreach ($list as $k=>$v){
  126. $list[$k]['device_name'] = Db::name('address')
  127. ->where('id',$v['waste_device_id'])
  128. ->value('title');
  129. $list[$k]['user_name'] = Db::name('user')
  130. ->where('id',$v['user_id'])
  131. ->value('real_name');
  132. if($v['status'] == 0){
  133. $list[$k]['status'] = '已收取';
  134. }elseif ($v['status'] == 1){
  135. $list[$k]['status'] = '已交接';
  136. }elseif ($v['status'] == 2){
  137. $list[$k]['status'] = '已转运';
  138. }else{
  139. $list[$k]['status'] = '医废异常';
  140. }
  141. $list[$k]['create_time'] = date('m-d H:i',strtotime($v['create_time']));
  142. }
  143. $data = [];
  144. foreach ($list as $k=>$v){
  145. $data[$k][] = $v['device_name'];
  146. $data[$k][] = $v['user_name'];
  147. $data[$k][] = $v['create_time'];
  148. $data[$k][] = $v['status'];
  149. }
  150. HelpHander::success($data);
  151. }
  152. public function monthCateRecord(){
  153. $month = input('month','');
  154. if($month !=''){
  155. if($month > 0 && $month < 10){
  156. $date = date('Y').'0'.$month;
  157. }elseif ($month >10){
  158. $date = date('Y').$month;
  159. }
  160. }else{
  161. $date = date('Ym');
  162. }
  163. // $em = cal_days_in_month(CAL_GREGORIAN,$month,date('Y'));
  164. $em = date('t',strtotime($date));
  165. $sDay = strtotime($date.'01');
  166. $arr = [];
  167. for ($i = 0;$i<$em;$i++){
  168. $arr[$i]['date'] = date('m-d',$sDay+$i*86400);
  169. $arr[$i]['ymd'] = date('Ymd',$sDay+$i*86400);
  170. }
  171. $cate = Db::name('waste_type')
  172. ->field('id,title')
  173. // ->where('org_id',$this->orgId)
  174. ->where('del',0)
  175. ->where('enable',1)
  176. ->select();
  177. $lists = $cateTitle = [];
  178. foreach ($cate as $k=>$v){
  179. $cateTitle[] = $v['title'];
  180. $lists[$k]['title'] = $v['title'];
  181. $aa = [];
  182. foreach ($arr as $kk=>$vv){
  183. $record = Db::name('waste_record')
  184. ->where('org_id',$this->orgId)
  185. ->where('cateid',$v['id'])
  186. ->where('del',0)
  187. ->where('create_yyyymmdd',$vv['ymd'])
  188. ->sum('weight');
  189. $aa[$kk] = $record>0? round($record/1000,1):0;
  190. }
  191. $lists[$k]['list'] = $aa;
  192. }
  193. // $cc = [];
  194. // foreach ($cateTitle as $k=>$v){
  195. // $cc[$k]['title'] = $v;
  196. // foreach ($lists as $kk=>$vv){
  197. // $cc[$k]['list'] = $vv['weight'];
  198. // }
  199. // }
  200. $data = [
  201. 'date' =>array_column($arr,'date'),
  202. 'cate' =>$cateTitle,
  203. 'list'=>$lists
  204. ];
  205. HelpHander::success($data);
  206. }
  207. // 医废收取量统计 当天及30天内平均收取重量
  208. public function depRecord(){
  209. //先取30内的科室
  210. $curDay = date('Ymd');
  211. $start = date('Ymd',strtotime(date('Y-m-d')) - 29*24*60*60);
  212. $lists = Db::name('waste_record')
  213. ->alias('wr')
  214. ->join('address a','a.id = wr.waste_device_id')
  215. ->join('dep d','d.id = a.dep_id')
  216. ->where('wr.org_id',$this->orgId)
  217. ->where('wr.del',0)
  218. ->where('wr.create_yyyymmdd','<=',$curDay)
  219. ->where('wr.create_yyyymmdd','>=',$start)
  220. ->field('d.id,d.title')
  221. ->group('d.id')
  222. ->distinct(true)
  223. ->select();
  224. $titles = [];
  225. $y1 = [];
  226. $y2 = [];
  227. foreach ($lists as $k=>$v){
  228. $titles[] = $v['title'];
  229. $count1 = Db::name('waste_record')
  230. ->where('org_id',$this->orgId)
  231. ->where('del',0)
  232. ->where('create_yyyymmdd','=',$curDay)
  233. ->sum('weight');
  234. $y1[] = round($count1/1000,1);
  235. $count2 = Db::name('waste_record')
  236. ->where('org_id',$this->orgId)
  237. ->where('del',0)
  238. ->where('create_yyyymmdd','<=',$curDay)
  239. ->where('create_yyyymmdd','>=',$start)
  240. ->sum('weight');
  241. $y2[] = round(($count2/1000)/30,1);
  242. }
  243. HelpHander::success(['titles' => $titles,'y1' => $y1,'y2' => $y2]);
  244. }
  245. public function depRecordList(){
  246. $dep = Db::name('dep')
  247. ->alias('d')
  248. ->field('d.title,a.id')
  249. ->join('address a','a.dep_id=d.id')
  250. ->where('d.org_id',$this->orgId)
  251. ->where('d.del',0)
  252. ->where('d.enable',1)
  253. ->where('','exp',Db::raw("FIND_IN_SET(7,a.types)"))
  254. ->where('a.del',0)
  255. ->where('a.enable',1)
  256. ->group('d.title')
  257. ->limit(5)
  258. ->select();
  259. $x = $y1 = $y2 = [];
  260. foreach ($dep as $k=>$v){
  261. $x[] = $v['title'];
  262. $record = Db::name('waste_record')
  263. ->where('waste_device_id',$v['id'])
  264. ->where('create_yyyymmdd',date('Ymd'))
  265. ->where('del',0)
  266. ->sum('weight');
  267. $y1[] = round($record/1000,1);
  268. $record2 = Db::name('waste_record')
  269. ->where('waste_device_id',$v['id'])
  270. ->where('create_yyyymmdd','>',date("Ymd",mktime(0,0,0,date("m")-1,date("d"),date("Y"))))
  271. ->where('create_yyyymmdd','<=',date('Ymd'))
  272. ->where('del',0)
  273. ->sum('weight');
  274. $y2[] = round($record2/1000/30,1);
  275. }
  276. HelpHander::success(['x'=>$x,'y1'=>$y1,'y2'=>$y2]);
  277. }
  278. }