post(); $data['org_id'] = cur_org_id(); $result = validate('AssetWithdraw')->check($data,[],''); if(true !== $result){ $this->error = validate('AssetWithdraw')->getError(); return false; } $data['agent_dep_id'] = Db::name('user_dep')->where('user_id',$data['agent_user_id'])->value('dep_id'); $id = $data['id']; unset($data['id']); if($id > 0){ $data['update_time'] = date('Y-m-d H:i:s'); unset($data['items']); $ret = $this->allowField(true)->save($data,['id'=>$id]); if(!$ret){ $this->error = '操作失败'; return false; } }else{ $data['create_time'] = date('Y-m-d H:i:s'); Db::startTrans(); try{ $data['sn'] = $this->getSn($data['org_id']); $items = $data['items']; unset($data['items']); $receiveId = Db::name('asset_withdraw')->insertGetId($data); if(!$receiveId){ \exception('操作失败'); } $d = []; $itemids = []; foreach ($items as $k=>$v){ $iteminfo = Db::name('asset_items') ->where('id',$v['id']) ->field('id,user_id,dep_id') ->find(); $d[] = [ 'withdraw_id' => $receiveId, 'items_id' => $v['id'], 'user_id' => $iteminfo['user_id'], 'dep_id' => $iteminfo['dep_id'], ]; $itemids[] = $v['id']; } $res = Db::name('asset_withdraw_items')->insertAll($d); if($res != count($d)){ \exception('操作失败'); } $sd = [ 'address' => $data['address'], 'enable' => 1, 'user_id' => 0, 'dep_id' => 0, 'used'=>'', 'update_time' => date('Y-m-d H:i:s') ]; $res = Db::name('asset_items')->where('id','in',$itemids)->update($sd); if($res != count($itemids)){ \exception('操作失败'); } Db::commit(); }catch (Exception $e){ Db::rollback(); $this->error = $e->getMessage(); return false; } } return true; } public function getSn($orgId){ $max = Db::name('asset_withdraw') ->where('org_id',$orgId) ->where('create_time','>=',date('Y-m-d').' 00:00:00') ->order('id desc') ->value('sn'); $maxinit = $max?$max:date('Ymd').'0000'; return $maxinit + 1; } public function info($id){ $info = $this->where('id',$id)->find(); if(!$info){ $this->error = '数据不存在'; return false; } $data = $info->toArray(); $data['agentDepName'] = Db::name('dep')->where('id',$info['agent_dep_id'])->value('title'); $data['withDepName'] = Db::name('dep')->where('id',$info['with_dep_id'])->value('title'); $data['agentUserName'] = Db::name('user')->where('id',$info['agent_user_id'])->value('real_name'); $items = Db::name('asset_withdraw_items') ->alias('ari') ->join('asset_items ai','ai.id = ari.items_id') ->join('asset_unit au','au.id = ai.unit_id') ->where('ari.withdraw_id',$id) ->field('ai.*,au.title as unitName,ari.user_id as old_user_id,ari.dep_id as old_dep_id') ->select(); $total = 0; foreach ($items as $k=>$v){ $items[$k]['userName'] = Db::name('user')->where('id',$v['old_user_id'])->value('real_name'); $items[$k]['depName'] = Db::name('dep')->where('id',$v['old_dep_id'])->value('title'); $total += $v['price']; } $data['items'] = $items?$items:[]; $data['total'] = $total; return $data; } }