where(['user_id' => $user_id,'company_id' => $company_id])->lock(true)->find(); if(!$wallet) { return 0; //abort(500,'钱包余额获取失败'); } if($wallet_name) { //返回指定钱包 return isset($wallet[$wallet_name]) ? $wallet[$wallet_name] : 0; } else { //返回所有钱包 return $wallet; } } /** * * @param floatval $number 金额(正数进账,负数出账) * @param $accountType 货币类型,money,score * @param $logtype 日志的类型 * @param $remark 备注 * @param $user_id 用户id * @param $table 来源表 * @param $data_id 表id * @param $isAdmin 是否是管理员处理 * @return array * @return array[status] * @return array[msg] * @return array[log_table] * @return array[log_id] */ public function lockChangeAccountRemain($company_id,$user_id,$accountType='money',$number,$logtype='',$remark='',$table='',$table_id=0,$isAdmin=false) { //初始化 $result = array( 'status'=>false, 'code' => 0, //报错时候采用 'msg'=>'', 'log_table' => '', 'log_id' => '', ); //获取小数点 $point = $accountType == 'money' ? 2 : 0; bcscale($point); //钱包名称 $wallet_name = $this->getwalletname($accountType); //检测 $number = floatval( $number ); if( $number == 0 ) { $result['msg'] = '交易金额:0'; return $result; } if(0 === bccomp($number, 0)){ $result['msg'] = '交易金额:0'; return $result; } //检测 $wallet = Db::name('user_wallet')->where(['user_id' => $user_id,'company_id' => $company_id])->lock(true)->find(); if(!$wallet) { $result['msg'] = '不存在的用户'; return $result; } if(bccomp(bcadd($wallet[$accountType], $number), 0) === -1) { $result['code'] = 10; $result['msg'] = $wallet_name.'余额不足!'; return $result; } if(0 !== bccomp($number, 0)) { //钱币记录 $data = array(); $data['company_id'] = $company_id; $data['user_id'] = $user_id; $data['log_type'] = $logtype; $data['before'] = $wallet[$accountType]; $data['change_value'] = $number; $data['remain'] = bcadd($wallet[$accountType], $number); $data['table'] = $table; $data['table_id'] = $table_id; $data['remark'] = $remark; $data['createtime'] = time(); $data['updatetime'] = time(); //新的方式 $rs1 = Db::name('user_wallet')->where(['user_id' => $user_id,'company_id' => $company_id])->update([$accountType => $data['remain']]); ///////////// $log_table = 'user_'.$accountType.'_log'; $rs2_id = Db::name($log_table)->insertGetId($data); if($rs1 === false || $rs2_id === false){ $result['msg'] = '更新财务记录失败!'; return $result; } if( $rs1 !== false && $rs2_id !== false ) { $result['status'] = true; $result['msg'] = '账户余额已更新!'; $result['log_table'] = $accountType.'_log'; $result['log_id'] = $rs2_id; return $result; } else { $result['msg'] = '更新财务记录失败!'; return $result; } } else { $result['msg'] = '金额不足0.01'; return $result; } } }