Waste.php 12 KB

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