1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace app\common\model;
- use think\Db;
- class MateGoodsUse extends Base
- {
- // 添加消耗使用记录,不添加事务,使用逻辑中的事务
- public function save1($type,$busId,$userId,$orgId,$goods,$remark=''){ //[{"goodsId":1,"nums","price":0}]
- if(!in_array($type,[1,2,3,4])){
- $this->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;
- }
- }
|