WashingRecord.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class WashingRecord extends Base
  6. {
  7. public function addSave($post){
  8. $data = [
  9. 'create_time'=>date('Y-m-d H:i:s'),
  10. 'create_yyyy'=>date('Y'),
  11. 'create_yyyymm'=>date('Ym'),
  12. 'create_yyyymmdd'=>date('Ymd'),
  13. ];
  14. $items =$post['items'];
  15. unset($post['items']);
  16. $data = array_merge($data,$post);
  17. $this->startTrans();
  18. try{
  19. $recordId = $this->insertGetId($data);
  20. if(intval($recordId) <=0){
  21. exception('添加记录失败');
  22. }
  23. foreach ($items as $k=>$v){
  24. $items[$k]['create_time'] =date('Y-m-d H:i:s');
  25. $items[$k]['washing_record_id'] =$recordId;
  26. }
  27. $res = Db::name('washing_record_item')->insertAll($items);
  28. if (!$res) {
  29. \exception('保存物品失败');
  30. }
  31. $this->commit();
  32. return true;
  33. }catch (Exception $e){
  34. $this->rollback();
  35. $this->error = $e->getMessage();
  36. return false;
  37. }
  38. }
  39. public function returnWash($id,$orgId){
  40. $ret = $this->where('id',$id)
  41. ->where('mode',1)
  42. ->where('org_id',$orgId)
  43. ->find();
  44. if(!$ret){
  45. $this->error='记录不存在,或当前状态已归还';
  46. return false;
  47. }
  48. $res = $this->where('id',$id)->update(
  49. ['mode'=>2,'return_time'=>date('Y-m-d H:i:s')]);
  50. return $res;
  51. }
  52. public function confirmWash($id,$orgId,$sign=''){
  53. $ret = $this->where('id',$id)
  54. ->where('mode',2)
  55. ->where('org_id',$orgId)
  56. ->find();
  57. if(!$ret){
  58. $this->error='记录不存在,或当前状态不可确认';
  59. return false;
  60. }
  61. $res = $this->where('id',$id)->update(
  62. ['mode'=>3,'confirm_time'=>date('Y-m-d H:i:s'),'sign'=>$sign]);
  63. return $res;
  64. }
  65. public function getApiList($page,$size,$type,$userId,$orgId){
  66. $offset =($page-1)*$size;
  67. $data = $this
  68. ->alias('a')
  69. ->join('address b','a.dep_id=b.id')
  70. ->field('a.id,a.create_time,a.return_time,a.confirm_time,b.title,a.mode as mode')
  71. ->where('a.mode',$type)
  72. ->where('a.user_id',$userId)
  73. ->where('a.org_id',$orgId)
  74. ->where('a.del',0)
  75. ->limit($offset,$size)
  76. ->select();
  77. $data = $data?$data->toArray():[];
  78. foreach ($data as $k=>$v){
  79. $data[$k]['return_time'] = $v['return_time']?$v['return_time']:'';
  80. $data[$k]['confirm_time'] = $v['confirm_time']?$v['confirm_time']:'';
  81. }
  82. return $data;
  83. }
  84. }