WasteRecord.php 31 KB

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