Waste.php 9.8 KB

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