error = '类型错误'; return false; } try{ foreach ($goods as $k=>$v){ if($type == 1||$type == 2){ // 使用小包中的耗材,检查库存的量是否充足,并减去相应库存 $ginfo = Db::name('mate_user_goods') ->where('user_id',$userId) ->where('org_id',$orgId) ->where('goods_id',$v['goodsId']) ->find(); if(!$ginfo){ \exception('物品不存在'); } if($ginfo['nums'] < $v['nums']){ \exception('库存不足'); } $mug = Db::name('mate_user_goods') ->where('id',$ginfo['id']) ->setDec('nums',$v['nums']); if(!$mug){ \exception('操作失败'); } $data = [ 'org_id' => $orgId, 'user_id' => $userId, 'bus_id' => $busId, 'goods_id' => $v['goodsId'], 'type' => $type, 'nums' => $v['nums'], 'price' => $ginfo['price'], 'total_price' => round($ginfo['price']*$v['nums'],2), 'remark' => $remark, 'create_time' => date('Y-m-d H:i:s') ]; $addUse = Db::name('mate_goods_use')->insert($data); if(!$addUse){ \exception('操作失败'); } }else{ // 领取消耗/损坏消耗 $data = [ 'org_id' => $orgId, 'user_id' => $userId, 'bus_id' => $busId, 'type' => $type, 'nums' => $v['nums'], 'price' => $v['price'], 'total_price' => round($v['price']*$v['nums'],2), 'remark' => $remark, 'create_time' => date('Y-m-d H:i:s') ]; $add = Db::name('mate_goods_use')->insert($data); if(!$add){ \exception('操作失败'); } } } }catch (\Exception $e){ $this->error = $e->getMessage(); return false; } return true; } // 根据类型和业务id获取使用耗材记录 public function lists($type,$busId,$orgId){ $lists = Db::name('mate_goods_use') ->field('goods_id,nums as total,price as money') ->where('type',$type) ->where('bus_id',$busId) ->where('org_id',$orgId) ->select(); foreach ($lists as $k=>$v){ $lists[$k]['title'] = Db::name('mate_goods')->where('id',$v['goods_id'])->value('title'); } return $lists; } }