WasteRecord.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. <?php
  2. namespace app\common\model;
  3. use function PHPSTORM_META\map;
  4. use think\Db;
  5. use think\Exception;
  6. class WasteRecord extends Base {
  7. public $table = 'waste_record';
  8. protected $validateName = 'WasteRecord';
  9. public $status = [
  10. '已收取',
  11. '已交接',
  12. '已转运',
  13. '医废异常',
  14. '未转运',
  15. ];
  16. /**
  17. * 医废记录详情
  18. *
  19. * @author wst
  20. * @date 2021/9/8 17:32
  21. * @return array
  22. */
  23. public function info($id,$orgId){
  24. $info = $this->getOne($id,$orgId);
  25. if($info){
  26. $info['real_name'] = Db::name('user')
  27. ->where('id',$info['user_id'])
  28. ->value('real_name');
  29. $info['waste_device'] = Db::name('address')
  30. ->where('id',$info['waste_device_id'])
  31. ->value('title');
  32. $info['cate'] = Db::name('waste_type')
  33. ->where('id',$info['cateid'])
  34. ->value('title');
  35. $info['weight'] =round($info['weight']/1000,2);
  36. $info['status'] =$this->status[$info['status']];
  37. $rlist = Db::name('waste_record_user')
  38. ->alias('waste_record_user')
  39. ->join('user user','user.id = waste_record_user.user_id')
  40. ->where('waste_record_user.waste_record_id',$info['id'])
  41. ->field('waste_record_user.*,user.real_name')
  42. ->order('status', 'asc')
  43. ->select();
  44. $info['rlist'] = $rlist?$rlist:array();
  45. $info = json_decode(json_encode($info),true);
  46. }
  47. return $info;
  48. }
  49. //转运
  50. public function finish($id,$user_id){
  51. $this->startTrans();
  52. try{
  53. $ret = $this
  54. ->where('id', $id)
  55. ->update(['status'=>2]);
  56. if(!$ret){
  57. throw new Exception('更新状态失败');
  58. }
  59. $ret2 = Db::name('waste_record_user')->insert(array(
  60. 'waste_record_id' => $id,
  61. 'user_id' => $user_id,
  62. 'status' => 2,
  63. 'create_time' => getTime()
  64. ));
  65. if(!$ret2){
  66. throw new exception('保存操作记录失败');
  67. }
  68. $this->commit();
  69. return true;
  70. }catch (exception $e){
  71. $this->rollback();
  72. $this->error=$e->getMessage();
  73. return false;
  74. }
  75. }
  76. public function getOne($id,$orgId=0){
  77. $info = $this
  78. ->where('id',$id)
  79. ->where('org_id',$orgId)
  80. ->whereOr('serial_number',$id)
  81. ->find()
  82. ->toArray();
  83. return $info;
  84. }
  85. /*----------------api----------------*/
  86. public function recordInfo($serialNum,$orgId){
  87. $map[] = ['serial_number','=',$serialNum];
  88. $map[] = ['org_id','=',$orgId];
  89. $map[] = ['del','=',0];
  90. $info = $this->field('id,serial_number,org_id,waste_device_id,user_id,cateid,weight,status,create_time,reason,is_print,weight2')
  91. ->where($map)->find();
  92. $info = $info?$info->toArray():[];
  93. if(!$info){
  94. $this->error = '记录不存在';
  95. return false;
  96. }
  97. $org = Db::name('org')
  98. ->where('id',$info['org_id'])
  99. ->field('name')->find();
  100. $info['org_name'] = $org?$org['name']:'';
  101. $user = Db::name('user')
  102. ->where('id',$info['user_id'])
  103. ->field('real_name')->find();
  104. $info['real_name'] = $user?$user['real_name']:'';
  105. $wasteDevice = Db::name('address')
  106. ->where('id',$info['waste_device_id'])
  107. ->field('title')
  108. ->find();
  109. $info['title'] = $wasteDevice?$wasteDevice['title']:'';
  110. $info['cate_id'] = $info['cateid'];
  111. $info['cate_name'] =Db::name('waste_type')
  112. ->where('id',$info['cateid'])->value('title');
  113. unset($info['cateid']);
  114. $lists = Db::name('waste_record_user')
  115. ->alias('wru')
  116. ->where('wru.waste_record_id',$info['id'])
  117. ->join('user u','u.id = wru.user_id')
  118. ->field('u.real_name,wru.status,wru.create_time')
  119. ->order('wru.id asc')
  120. ->select();
  121. $data = [
  122. 'type' => 1, // 1=医废记录详情 2=医废包详情
  123. 'info' => $info,
  124. 'status_list' => $lists?$lists:[]
  125. ];
  126. return $data;
  127. }
  128. public function queryIsPrint($id){
  129. $ret=$this
  130. ->where('id',$id)
  131. ->field('is_print')
  132. ->find()->toArray();
  133. return $ret;
  134. }
  135. public function wasteRecordList($orgId,$status,$createTime,$page,$size){
  136. $offset= ($page - 1) * $size;
  137. $starttime=$createTime.' 00:00:00';
  138. $endtime=$createTime.' 23:59:59';
  139. $where[] = ['create_time','between',[$starttime,$endtime]];
  140. $list=$this
  141. ->field('waste_device_id,cateid,create_time,is_print,serial_number,id,weight')
  142. ->where([
  143. 'org_id'=>$orgId,
  144. 'status'=>$status,
  145. 'del'=>0,
  146. ])
  147. ->where($where)
  148. ->order('id desc')
  149. ->limit($offset,$size)
  150. ->select()->toArray();
  151. $ret['list']=$list;
  152. foreach ($list as $k=>$v){
  153. $title=Db::name('address')
  154. ->where([
  155. 'id'=>$v['waste_device_id'],
  156. 'del'=>0,
  157. 'enable'=>1,
  158. ])->value('title');
  159. $ret['list'][$k]['title']= $title;
  160. $cate = Db::name('waste_type')
  161. ->where('id',$v['cateid'])
  162. ->value('title');
  163. $ret['list'][$k]['cate_title']= $cate;
  164. }
  165. return $ret;
  166. }
  167. public function unTransportList($orgId,$createTime){
  168. $where = [];
  169. if($createTime){
  170. $starttime=$createTime.' 00:00:00';
  171. $endtime=$createTime.' 23:59:59';
  172. $where[] = ['create_time','between',[$starttime,$endtime]];
  173. }
  174. $list=$this
  175. ->field('waste_device_id,cateid,create_time,is_print,serial_number,id,weight')
  176. ->where('org_id',$orgId)
  177. ->where('del',0)
  178. ->where($where)
  179. ->where('status','in',[0,1])
  180. ->order('id','desc')
  181. ->select()
  182. ->toArray();
  183. $ret['list']=$list;
  184. foreach ($list as $k=>$v){
  185. $title=Db::name('address')
  186. ->where([
  187. 'id'=>$v['waste_device_id'],
  188. 'del'=>0,
  189. 'enable'=>1,
  190. ])->value('title');
  191. $ret['list'][$k]['title']= $title;
  192. $cate =Db::name('waste_type')
  193. ->where('id',$v['cateid'])
  194. ->value('title');;
  195. $ret['list'][$k]['cate_title']= $cate;
  196. }
  197. return $ret;
  198. }
  199. public function saveRecord($data){
  200. $curtime = getTime();
  201. $data['serial_number'] = get_unique_id();
  202. $data['create_time'] = $curtime;
  203. $data['create_yyyy'] = date('Y');
  204. $data['create_yyyymm'] = date('Ym');
  205. $data['create_yyyymmdd'] = date('Ymd');
  206. $this->startTrans();
  207. try{
  208. $waste_record_id = $this->insertGetId($data);
  209. if (!$waste_record_id) {
  210. throw new Exception('保存记录失败');
  211. }
  212. $sdata = [
  213. 'status' => 0,
  214. 'create_time' => $curtime,
  215. 'user_id' => $data['user_id'],
  216. 'waste_record_id' => $waste_record_id,
  217. ];
  218. $res = Db::name('waste_record_user')->insert( $sdata);
  219. if (!$res) {
  220. throw new Exception('保存操作记录失败');
  221. }
  222. $this->commit();
  223. return $waste_record_id;
  224. }catch (Exception $e){
  225. $this->error = $e->getMessage();
  226. $this->rollback();
  227. return false;
  228. }
  229. }
  230. public function saveStatus($data){
  231. $info = $this->
  232. where('serial_number',$data['serialNumber'])
  233. ->find();
  234. if(!$info){
  235. $this->error = '记录不存在';
  236. return false;
  237. }
  238. $curTime = date('Y-m-d H:i:s');
  239. $this->startTrans();
  240. try{
  241. $rdata = [
  242. 'weight2' => isset($data['weight2'])?$data['weight2']:0,
  243. 'status' => $data['status'],
  244. 'reason' => isset($data['reason'])?$data['reason']:'',
  245. 'package_id' => 0 // 有任何变化,都删除与包的关系
  246. ];
  247. $ret = $this
  248. ->where('id',$info['id'])
  249. ->update( $rdata);
  250. if (!$ret) {
  251. \exception('操作失败1');
  252. }
  253. $sdata = [
  254. 'status' => $data['status'],
  255. 'create_time' => $curTime,
  256. 'user_id' => $data['userId'],
  257. 'waste_record_id' => $info['id'],
  258. ];
  259. $res = Db::name('waste_record_user')
  260. ->insert($sdata);
  261. if (!$res) {
  262. \exception('操作失败');
  263. }
  264. // 如果在保内,检查包内是否还有可以记录,没有则删除包记录
  265. if($info['package_id'] > 0){
  266. $count = $this
  267. ->where('package_id',$info['package_id'])
  268. ->where('id','<>',$info['id'])
  269. ->count();
  270. if($count == 0){
  271. $res = Db::name('waste_package')->
  272. where('id',$info['package_id'])
  273. ->update(['del' => 1]);
  274. if (!$res) {
  275. \exception('操作失败');
  276. }
  277. }
  278. }
  279. $this->commit();
  280. }catch (Exception $e){
  281. $this->error = '操作失败'.$e->getMessage();
  282. $this->rollback();
  283. return false;
  284. }
  285. return true;
  286. }
  287. public function lists($orgId,$depId,$date,$page,$size,$userId){
  288. $offset= ($page - 1) * $size;
  289. $map[]= ['wr.org_id','=',$orgId];
  290. $map[]= ['wr.sign','=',0];
  291. $map[]= ['wr.del','=',0];
  292. $map1[]= ['wr.user_id','=',$userId];
  293. if($depId){
  294. $map1[]= ['wd.dep_id','=',$depId];
  295. }
  296. if($date){
  297. $date = date('Ymd',strtotime($date));
  298. $map[]= ['wr.create_yyyymmdd','=',$date];
  299. }
  300. $list = $this
  301. ->alias('wr')
  302. ->join('address wd','wd.id = wr.waste_device_id')
  303. ->where(function ($query) use ($map1) {
  304. $query->whereOr($map1);
  305. })->where($map)->field('wr.waste_device_id,wr.cateid,wr.create_time,wr.is_print,wr.serial_number,wr.id,wr.weight,wr.sign')
  306. ->order('wr.id','desc')
  307. ->limit($offset,$size)->select();
  308. $list = $list?$list->toArray():[];
  309. foreach ($list as $k=>$v){
  310. $title=Db::name('address')
  311. ->where('id',$v['waste_device_id'])
  312. ->value('title');
  313. $list[$k]['waste_device_name'] = $title;
  314. $cate = Db::name('waste_type')->
  315. where('id',$v['cateid'])->value('title');
  316. $list[$k]['cate_title']= $cate?$cate:'';
  317. }
  318. $ret['list']=$list?$list:[];
  319. return $ret;
  320. }
  321. public function savePackage($post){
  322. $curTime =getTime();
  323. $data = [
  324. 'org_id' => $post['org_id'],
  325. 'user_id' => $post['user_id'],
  326. 'ids' => $post['ids'],
  327. 'create_time' => $curTime,
  328. ];
  329. // 检查是否符合要求
  330. $count = Db::name('waste_record')
  331. ->where('del',0)
  332. ->where('status',0)
  333. ->where('package_id',0)
  334. ->where('user_id',$data['user_id'])
  335. ->where('id','in',$data['ids'])
  336. ->count();
  337. if($count != count($data['ids'])){
  338. $this->error = '有不符合要求的医废记录';
  339. return false;
  340. }
  341. $this->startTrans();
  342. try{
  343. $pData = [
  344. 'org_id' => $post['org_id'],
  345. 'user_id' => $post['user_id'],
  346. 'record' => implode(',',$post['ids']),
  347. 'create_time' => $curTime,
  348. 'sn' => get_unique_id2()
  349. ];
  350. $id= Db::name('waste_package')
  351. ->insertGetId($pData);
  352. if (!$id) {
  353. exception('操作失败');
  354. }
  355. $sData = [
  356. 'package_id' => $id,
  357. ];
  358. $res = $this->where('id','in',$data['ids'])
  359. ->update( $sData);
  360. if (!$res) {
  361. exception('操作失败');
  362. }
  363. $this->commit();
  364. }catch (Exception $e){
  365. $this->error = $e->getMessage();
  366. $this->rollback();
  367. return false;
  368. }
  369. return $id;
  370. }
  371. public function packInfo($serialNum,$orgId){
  372. $map[] = ['sn','=',$serialNum];
  373. $map[] = ['org_id','=',$orgId];
  374. $map[] = ['del','=',0];
  375. $info = Db::name('waste_package')
  376. ->where($map)
  377. ->field('id,sn,org_id,user_id,status,create_time,jj_time,zy_time,jj_user_id,zy_user_id')
  378. ->find();
  379. if(!$info){
  380. $this->error = '记录不存在';
  381. return false;
  382. }
  383. $org = Db::name('org')
  384. ->where('id',$info['org_id'])
  385. ->field('name')->find();
  386. $info['org_name'] = $org?$org['name']:'';
  387. $user = Db::name('user')
  388. ->where('id',$info['user_id'])
  389. ->field('real_name')->find();
  390. $info['real_name'] = $user?$user['real_name']:'';
  391. $info['jj_time'] = $info['jj_time']?$info['jj_time']:'';
  392. $info['zy_time'] = $info['zy_time']?$info['zy_time']:'';
  393. if($info['jj_user_id'] > 0){
  394. $user = Db::name('user')
  395. ->where('id',$info['jj_user_id'])
  396. ->field('real_name')->find();
  397. $info['jj_real_name'] = $user?$user['real_name']:'';
  398. }
  399. if($info['zy_user_id'] > 0){
  400. $user = Db::name('user')
  401. ->where('id',$info['zy_user_id'])
  402. ->field('real_name')->find();
  403. $info['zy_real_name'] = $user?$user['real_name']:'';
  404. }
  405. $weight =$this
  406. ->where('package_id',$info['id'])
  407. ->sum('weight');
  408. // $info['weight'] = $weight?round($weight/1000,2):0;
  409. $info['weight'] = $weight;
  410. $count = $this->where('package_id',$info['id'])
  411. ->count();
  412. $info['count'] = $count?$count:0;
  413. $record = Db::name('waste_record')
  414. ->alias('wr')
  415. ->where('wr.del',0)
  416. ->where('wr.package_id',$info['id'])
  417. ->field('wr.waste_device_id,wr.cateid,wr.create_time,wr.is_print,wr.serial_number,wr.id,wr.weight,wr.sign')
  418. ->order('wr.id desc')
  419. ->select();
  420. foreach ($record as $k=>$v){
  421. $title=Db::name('address')
  422. ->field('title')
  423. ->where('id',$v['waste_device_id'])
  424. ->find();
  425. $record[$k]['title'] = $title?$title['title']:'';
  426. $record[$k]['cate_name'] =Db::name('waste_type')
  427. ->where('id',$v['cateid'])->value('title');
  428. // $record[$k]['weight'] = $v['weight']?round($v['weight']/1000,2):0;
  429. $record[$k]['weight'] = $v['weight']?$v['weight']:0;
  430. }
  431. $data = [
  432. 'type' => 2,
  433. 'pinfo' => $info,
  434. 'record' => $record?$record:[]
  435. ];
  436. return $data;
  437. }
  438. public function packagesLists($orgId,$page,$size,$userId){
  439. $offset= ($page - 1) * $size;
  440. $map[] = ['org_id','=',$orgId];
  441. $map[] = ['user_id','=',$userId];
  442. $map[] = ['del','=',0];
  443. $list = Db::name('waste_package')
  444. ->field('id,sn,org_id,user_id,status,create_time')
  445. ->order('id desc')
  446. ->where($map)
  447. ->limit($offset,$size)
  448. ->select();
  449. foreach ($list as $k=>$v){
  450. $user = Db::name('user')
  451. ->where('id',$v['user_id'])
  452. ->field('real_name')
  453. ->find();
  454. $list[$k]['real_name'] = $user?$user['real_name']:'';
  455. $weight = $this->where('package_id',$v['id'])
  456. ->sum('weight');
  457. $list[$k]['weight'] = $weight?$weight:0;
  458. $count = $this->where('package_id',$v['id'])
  459. ->count();
  460. $list[$k]['count'] = $count?$count:0;
  461. }
  462. return $list?$list:[];
  463. }
  464. public function delpackage($orgId,$userId,$id,$recordId){
  465. $info = $this
  466. ->where('del',0)
  467. ->where('org_id',$orgId)
  468. ->where('package_id',$id)
  469. ->where('id',$recordId)
  470. ->find();
  471. if(!$info){
  472. $this->error = '记录不存在';
  473. return false;
  474. }
  475. $this->startTrans();
  476. try{
  477. $res = $this->where('id',$recordId)
  478. ->update(['package_id' => 0]);
  479. if (!$res) {
  480. exception('操作失败');
  481. }
  482. // 如果在保内,检查包内是否还有可以记录,没有则删除包记录
  483. if($info['package_id'] > 0){
  484. $count = $this
  485. ->where('package_id',$id)
  486. ->where('id','<>',$recordId)
  487. ->count();
  488. if($count == 0){
  489. $res = Db::name('waste_package')
  490. ->where('id',$id)
  491. ->update(['del' => 1]);
  492. if (!$res) {
  493. exception('操作失败');
  494. }
  495. }
  496. }
  497. $this->commit();
  498. return true;
  499. }catch (Exception $e){
  500. $this->error = '操作失败'.$e->getmessage();
  501. $this->rollback();
  502. return false;
  503. }
  504. }
  505. // 交接
  506. public function jjpackage($orgId,$userId,$id){
  507. $info = Db::name('waste_package')
  508. ->where('del',0)
  509. ->where('org_id',$orgId)
  510. ->where('id',$id)
  511. ->find();
  512. if(!$info){
  513. $this->error = '记录不存在';
  514. return false;
  515. }
  516. if($info['status'] != 0){
  517. $this->error = '无权限操作';
  518. return false;
  519. }
  520. $lists = $this
  521. ->where('package_id',$id)
  522. ->where('del',0)
  523. ->select();
  524. $lists = $lists?$lists->toArray():[];
  525. if(count($lists) == 0){
  526. $this->error = '无可操作的医废记录';
  527. return false;
  528. }
  529. $ids = [];
  530. foreach ($lists as $k=>$v){
  531. $ids[] = $v['id'];
  532. }
  533. $this->startTrans();
  534. try{
  535. $rData = [
  536. 'jj_time' => getTime(),
  537. 'status' => 1,
  538. 'jj_user_id' => $userId,
  539. 'jj_record' => implode(',',$ids)
  540. ];
  541. $ret = Db::name('waste_package')
  542. ->where('id',$id)
  543. ->update($rData);
  544. if (!$ret) {
  545. exception('操作失败1');
  546. }
  547. foreach ($lists as $k=>$v){
  548. $rrData = [
  549. 'weight2' => 0,
  550. 'status' => 1,
  551. 'reason' => '',
  552. ];
  553. $ret = $this
  554. ->where('id',$v['id'])
  555. ->update($rrData);
  556. if (!$ret) {
  557. exception('操作失败');
  558. }
  559. $sData = [
  560. 'status' => 1,
  561. 'create_time' => getTime(),
  562. 'user_id' => $userId,
  563. 'waste_record_id' => $v['id'],
  564. ];
  565. $res = Db::name('waste_record_user')
  566. ->insert($sData);
  567. if (!$res) {
  568. exception('操作失败');
  569. }
  570. }
  571. $this->commit();
  572. return true;
  573. }catch (Exception $e){
  574. $this->error = '操作失败'.$e->getmessage();
  575. $this->rollback();
  576. return false;
  577. }
  578. }
  579. // 转运
  580. public function zypackage($orgId,$userId,$id){
  581. $info = Db::name('waste_package')
  582. ->where('del',0)
  583. ->where('org_id',$orgId)
  584. ->where('id',$id)
  585. ->find();
  586. if(!$info){
  587. $this->error = '记录不存在';
  588. return false;
  589. }
  590. if($info['status'] != 1){
  591. $this->error = '无权限操作';
  592. return false;
  593. }
  594. $lists = $this
  595. ->where('package_id',$id)
  596. ->where('del',0)
  597. ->select();
  598. $lists = $lists?$lists->toArray():[];
  599. if(count($lists) == 0){
  600. $this->error = '无可操作的医废记录';
  601. return false;
  602. }
  603. $ids = [];
  604. foreach ($lists as $k=>$v){
  605. $ids[] = $v['id'];
  606. }
  607. $this->startTrans();
  608. try{
  609. $rData = [
  610. 'zy_time' => date('Y-m-d H:i:s'),
  611. 'status' => 2,
  612. 'zy_user_id' => $userId,
  613. 'zy_record' => implode(',',$ids)
  614. ];
  615. $ret = Db::name('waste_package')
  616. ->where('id',$id)
  617. ->update($rData);
  618. if (!$ret) {
  619. throw new exception('操作失败');
  620. }
  621. foreach ($lists as $k=>$v){
  622. $rrdata = [
  623. 'weight2' => 0,
  624. 'status' => 2,
  625. 'reason' => '',
  626. ];
  627. $ret = $this
  628. ->where('id',$v['id'])
  629. ->update($rrdata);
  630. if (!$ret) {
  631. exception('操作失败');
  632. }
  633. $sdata = [
  634. 'status' => 2,
  635. 'create_time' => getTime(),
  636. 'user_id' => $userId,
  637. 'waste_record_id' => $v['id'],
  638. ];
  639. $res = Db::name('waste_record_user')
  640. ->insert($sdata);
  641. if (!$res) {
  642. exception('操作失败');
  643. }
  644. }
  645. $this->commit();
  646. return true;
  647. }catch (Exception $e){
  648. $this->error = '操作失败'.$e->getmessage();
  649. $this->rollback();
  650. return false;
  651. }
  652. }
  653. public function yflist($orgId,$userId){
  654. $map[] = ['wr.org_id','=',$orgId];
  655. $map[] = ['wr.user_id','=',$userId];
  656. $map[] = ['wr.package_id','=',0];
  657. $map[] = ['wr.del','=',0];
  658. $map[] = ['wr.status','=',0];
  659. $list=$this->alias('wr')
  660. ->where($map)
  661. ->field('wr.waste_device_id,wr.cateid,wr.create_time,wr.is_print,wr.serial_number,wr.id,wr.weight,wr.sign')
  662. ->order('wr.id desc')
  663. ->select();
  664. $list = $list?$list->toArray():[];
  665. foreach ($list as $k=>$v){
  666. $title=Db::name('address')
  667. ->field('title')
  668. ->where('id',$v['waste_device_id'])
  669. ->find();
  670. $list[$k]['title'] = $title?$title['title']:'';
  671. $list[$k]['cate_name'] =Db::name('waste_type')
  672. ->where('id',$v['cateid'])->value('title');
  673. }
  674. return $list?$list:[];
  675. }
  676. public function printList($orgId,$userId){
  677. $title = input('title','');
  678. if($title!=''){
  679. $type = 7;
  680. $map1[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
  681. $address = Db::name('address')
  682. ->where('del',0)
  683. ->where('enable',1)
  684. ->where('org_id',$orgId)
  685. ->where('title','like','%'.$title.'%')
  686. ->where($map1)
  687. ->column('id');
  688. if($address){
  689. $map[] = ['wr.waste_device_id','in',$address];
  690. }else{
  691. $map[] = ['wr.waste_device_id','=',-1];
  692. }
  693. }
  694. $map[] = ['wr.org_id','=',$orgId];
  695. $map[] = ['wr.user_id','=',$userId];
  696. $map[] = ['wr.del','=',0];
  697. $map[] = ['wr.status','=',0];
  698. $map[] = ['wr.create_yyyymmdd','=',date('Ymd')];
  699. $list=$this->alias('wr')
  700. ->where($map)
  701. ->field('wr.waste_device_id,wr.cateid,wr.create_time,wr.is_print,wr.serial_number,wr.id,wr.weight,wr.sign')
  702. ->order('wr.id desc')
  703. ->limit(1000)
  704. ->select();
  705. $list = $list?$list->toArray():[];
  706. foreach ($list as $k=>$v){
  707. $title=Db::name('address')
  708. ->field('title')
  709. ->where('id',$v['waste_device_id'])
  710. ->find();
  711. $list[$k]['title'] = $title?$title['title']:'';
  712. $list[$k]['cate_name'] =Db::name('waste_type')
  713. ->where('id',$v['cateid'])->value('title');
  714. }
  715. return $list?$list:[];
  716. }
  717. public function recordLists($orgId,$wasteDeviceId,$start,$end,$page,$size,$userId){
  718. $offset= ($page - 1) * $size;
  719. $map[] = ['wr.org_id','=',$orgId];
  720. $map[] = ['wr.del','=',0];
  721. if($wasteDeviceId>0){
  722. $map[] = ['wr.waste_device_id','=',$wasteDeviceId];
  723. }
  724. if($start){
  725. $date = date('Ymd',strtotime($start));
  726. $map[] = ['wr.create_yyyymmdd','>=',$date];
  727. }
  728. if($end){
  729. $date = date('Ymd',strtotime($end));
  730. $map[] = ['wr.create_yyyymmdd','<=',$date];
  731. }
  732. $list=db('waste_record')
  733. ->alias('wr')
  734. ->field('wr.waste_device_id,wr.status,wr.cateid,wr.create_time,wr.is_print,wr.serial_number,wr.id,wr.weight,wr.sign')
  735. ->order('wr.id','desc')
  736. ->where($map)
  737. ->limit($offset,$size)
  738. ->select();
  739. foreach ($list as $k=>$v){
  740. $title=db('address')
  741. ->field('title')
  742. ->where('id',$v['waste_device_id'])
  743. ->find();
  744. $list[$k]['title'] = $title?$title['title']:'';
  745. $list[$k]['cate_title'] = $this->getTableField('waste_type',['id'=>$v['cateid']],'title');
  746. }
  747. $ret=$list?$list:[];
  748. return $ret;
  749. }
  750. public function getByDepDayList($wasteDeviceId,$orgId){
  751. $typeList = model('WasteType')->getList();
  752. $str ='';
  753. foreach ($typeList as $k=>$v){
  754. $sum = Db::name('waste_record')
  755. ->where('create_yyyymmdd',date('Ymd'))
  756. ->where('org_id',$orgId)
  757. ->where('waste_device_id',$wasteDeviceId)
  758. ->where('cateid',$v['id'])
  759. ->where('del',0)
  760. ->sum('weight');
  761. if($sum >0){
  762. $count = ($sum/1000).'kg';
  763. $str.=$v['title'].':'.$count.';';
  764. }
  765. }
  766. if($str!=''){
  767. $s = explode(';',$str);
  768. unset($s[count($s)-1]);
  769. $str = implode(';',$s);
  770. }
  771. return $str;
  772. }
  773. public function print($ids,$orgId,$userId){
  774. $waste_device_id = [];
  775. $id = explode(',',$ids);
  776. $total = 0;
  777. $cate = [];
  778. foreach ($id as $k=>$v){
  779. $info = Db::name('waste_record')
  780. ->where('id',$v)
  781. ->find();
  782. if(empty($info)){
  783. $this->error='记录id'.$v.'不存在';
  784. return false;
  785. }
  786. if($info['user_id']!=$userId){
  787. $this->error='记录id'.$v.'收取人不是自己';
  788. return false;
  789. }
  790. $cateInfo = Db::name('waste_type')
  791. ->where('id',$info['cateid'])
  792. ->find();
  793. $cate[] = [
  794. 'id'=>$info['cateid'],
  795. 'title'=>$cateInfo['title'],
  796. 'num'=>$info['weight'],
  797. 'count'=>1,
  798. ];
  799. $total+=$info['weight'];
  800. $waste_device_id[] = $info['waste_device_id'];
  801. }
  802. $aa = [];
  803. foreach ($cate as $k=>$v){
  804. if(isset($aa[$v['id']])){
  805. $aa[$v['id']]['num'] = $aa[$v['id']]['num']+$v['num'];
  806. $aa[$v['id']]['count'] = $aa[$v['id']]['count']+1;
  807. }else{
  808. $aa[$v['id']] = $v;
  809. }
  810. }
  811. $ab = [];
  812. foreach ($aa as $k=>$v){
  813. $kg = sprintf("%.2f",$v['num']/1000);
  814. $ab[]=$v['title'].':'.$v['count'].' '.$kg.'kg';
  815. }
  816. if(count(array_unique($waste_device_id)) >1){
  817. $this->error='只能打印同一个科室下的记录';
  818. return false;
  819. }
  820. $address = Db::name('address')
  821. ->where('id',$waste_device_id[0])
  822. ->find();
  823. if(empty($address['user'])){
  824. $this->error='当前科室未绑定负责人';
  825. return false;
  826. }
  827. $amount = $total>0?sprintf("%.2f",$total/1000):"0";
  828. $total_desc = $amount.'kg,'.count($id).'袋';
  829. $a = [
  830. 'org_id'=>$orgId,
  831. 'waste_record_id'=>$ids,
  832. 'address'=>$address['title'],
  833. 'address_user'=>$address['user'],
  834. 'create_time'=>getTime(),
  835. 'total'=>$total_desc,
  836. 'user_id'=>$userId,
  837. 'desc'=>implode(',',$ab),
  838. ];
  839. $r = Db::name('waste_print_log')
  840. ->insertGetId($a);
  841. $a['org_name'] = Db::name('org')
  842. ->where('id',$a['org_id'])
  843. ->value('name');
  844. $a['user_name'] = Db::name('user')
  845. ->where('id',$userId)
  846. ->value('real_name');
  847. unset($a['user_id']);
  848. return $a;
  849. }
  850. }