WasteRecord.php 31 KB

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