setName('SettleRebate')->setDescription('结算返水'); } protected function execute(Input $input, Output $output) { $this->SettleRebate($input, $output); } // 结算洗码 private function settleRebate($input, $output) { $settleTime = time() - 86400; $where = []; $where['r.is_checkout'] = 0; $where['r.create_time'] = array('<=',$settleTime); $where['u.agent_parent_id'] = array('>',0); $list = Db::name('rebate')->alias('r') ->join('cg_user u','u.id=r.user_id') ->field('r.user_id,sum(r.rebate_amount_actual) as rebate_amount, sum(r.amount) as amount') ->where($where) ->group('r.user_id') ->select(); foreach($list as $v){ $userId = $v['user_id']; $amount = $v['amount']; $rebateAmount = $v['rebate_amount']; try{ Db::startTrans(); // 用户信息 总代不结算 $userInfo = Db::name('user')->where('id',$userId)->find(); if($userInfo['agent_parent_id'] == 0){ throw new Exception("结算返水:{$userInfo['username']} 为总代"); } // 更新返水表状态 $rebateWhere = []; $rebateWhere['is_checkout'] = 0; $rebateWhere['user_id'] = $userId; $rebateWhere['create_time'] = array('<=',$settleTime); $result = Db::name('rebate')->where($rebateWhere)->update(['is_checkout'=>1,'checkout_time'=>time()]); if(!$result){ throw new Exception("结算洗码:{$userInfo['username']} 操作失败"); } // 增加下级余额 $userMoney = $userInfo['money'] + $amount; $result = Db::name('user')->where(['id'=>$userInfo['id']])->update(['money'=>$userMoney,'last_xima_time'=>time()]); if(!$result){ throw new Exception("结算洗码:{$userInfo['username']} 操作失败"); } // 返水记录 $dataXima = array(); $dataXima['user_id'] = $userInfo['id']; $dataXima['username'] = $userInfo['username']; $dataXima['admin_or_agent'] = 5; $dataXima['amount'] = $amount; $dataXima['rebate_amount'] = $rebateAmount; $dataXima['rebate_rate'] = $userInfo['rebate_rate']; $dataXima['create_time'] = time(); $dataXima['old_money'] = $userInfo['money']; $dataXima['new_money'] = $userMoney; $dataXima['type'] = 1; $result = Db::name('rebate_log')->insert($dataXima); if(!$result){ throw new Exception("结算返水:{$userInfo['username']} 操作失败"); } // 处理上分 $dataRecharge = array(); $dataRecharge['type'] = 2; $dataRecharge['money'] = $rebateAmount; $dataRecharge['user_id'] = $userInfo['id']; $dataRecharge['create_time'] = time(); $dataRecharge['old_money'] = $userInfo['money']; $dataRecharge['new_money'] = $userMoney; $result = Db::name('user_recharge')->insert($dataRecharge); if(!$result){ throw new Exception("结算返水:{$userInfo['username']} 操作失败"); } Db::commit(); $output->writeln("{$userInfo['username']} 成功结算返水:{$amount}元",1); }catch (Exception $e){ $output->writeln($e->getMessage().',行:'.$e->getLine(),1); Db::rollback(); } } } }