168 lines
7.7 KiB
PHP
168 lines
7.7 KiB
PHP
<?php
|
||
|
||
namespace app\command;
|
||
|
||
use think\console\Command;
|
||
use think\console\Input;
|
||
use think\console\Output;
|
||
use think\Db;
|
||
use think\Exception;
|
||
|
||
class SettleXima extends Command
|
||
{
|
||
protected function configure()
|
||
{
|
||
$this->setName('SettleXima')->setDescription('结算洗码');
|
||
}
|
||
|
||
protected function execute(Input $input, Output $output)
|
||
{
|
||
$this->settleXima($input, $output);
|
||
}
|
||
|
||
// 结算洗码
|
||
private function settleXima($input, $output)
|
||
{
|
||
$settleTime = time() - 86400;
|
||
$where = [];
|
||
$where['x.is_checkout'] = 0;
|
||
$where['x.create_time'] = array('<=',$settleTime);
|
||
$where['u.agent_parent_id'] = array('>',0);
|
||
|
||
$list = Db::name('xima')->alias('x')
|
||
->join('cg_user u','u.id=x.user_id')
|
||
->field('x.user_id,sum(x.maliang) as amount, sum(x.ximaliang) as ximaliang')
|
||
->where($where)
|
||
->group('x.user_id')
|
||
->select();
|
||
foreach($list as $v){
|
||
$userId = $v['user_id'];
|
||
$amount = $v['amount'];
|
||
$ximaliang = $v['ximaliang'];
|
||
|
||
try{
|
||
Db::startTrans();
|
||
|
||
// 用户信息 总代不结算
|
||
$userInfo = Db::name('user')->where('id',$userId)->find();
|
||
if($userInfo['agent_parent_id'] == 0){
|
||
throw new Exception("结算洗码:{$userInfo['username']} 为总代");
|
||
}
|
||
// 上级信息
|
||
$parentId = $userInfo['agent_parent_id'];
|
||
$parent = Db::name('user')->where('id',$parentId)->lock(true)->find();
|
||
if(empty($parent)){
|
||
throw new Exception("结算洗码:{$userInfo['username']} 上级不存在");
|
||
}
|
||
// 上级余额判断
|
||
if(SETTLE_MONEY_EXCEED_PARENT_MONTY != 1 && $amount > $parent['money']){
|
||
throw new Exception("结算洗码:{$userInfo['username']} 上级余额不足");
|
||
}
|
||
|
||
// 更新洗码表状态
|
||
$ximaWhere = [];
|
||
$ximaWhere['is_checkout'] = 0;
|
||
$ximaWhere['user_id'] = $userId;
|
||
$ximaWhere['create_time'] = array('<=',$settleTime);
|
||
$result = Db::name('xima')->where($ximaWhere)->update(['is_checkout'=>1,'checkout_time'=>time()]);
|
||
if(!$result){
|
||
throw new Exception("结算洗码:{$userInfo['username']} 操作失败");
|
||
}
|
||
|
||
// 扣除上级余额
|
||
$parentMoney = $parent['money'] - $amount;
|
||
$result = Db::name('user')->where(['id'=>$parent['id']])->update(['money'=>$parentMoney]);
|
||
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['ximaliang'] = $ximaliang;
|
||
$dataXima['maliang'] = $amount;
|
||
if($userInfo['type_xima'] == 1){
|
||
$dataXima['agent_ximalv'] = $userInfo['agent_ximalv'].'/'.$userInfo['agent_ximalv_dt'].'/'.$userInfo['agent_ximalv_nn'];
|
||
}else{
|
||
$dataXima['agent_ximalv'] = '0/0/0';
|
||
}
|
||
$dataXima['create_time'] = time();
|
||
$dataXima['old_money'] = $userInfo['money'];
|
||
$dataXima['new_money'] = $userMoney;
|
||
$dataXima['connection_old_money'] = $userInfo['money'];
|
||
$dataXima['connection_new_money'] = $parentMoney;
|
||
$dataXima['type'] = 1;
|
||
$result = Db::name('xima_log')->insert($dataXima);
|
||
if(!$result){
|
||
throw new Exception("结算洗码:{$userInfo['username']} 操作失败");
|
||
}
|
||
|
||
|
||
// 处理上分(下级)
|
||
$dataRecharge = array();
|
||
$dataRecharge['type'] = 2;
|
||
$dataRecharge['amount'] = $amount;
|
||
$dataRecharge['mode'] = 1;
|
||
$dataRecharge['agent_or_admin'] = 5;
|
||
$dataRecharge['controller_type'] = '定时结算系统操作';
|
||
$dataRecharge['user_id'] = $userInfo['id'];
|
||
$dataRecharge['user_type'] = $userInfo['agent'];
|
||
$dataRecharge['user_agent_level'] = $userInfo['agent_level'];
|
||
$dataRecharge['username_for'] = $userInfo['username'];
|
||
$dataRecharge['nickname_for'] = $userInfo['nickname'];
|
||
$dataRecharge['user_parent_id'] = $userInfo['agent_parent_id'];
|
||
$dataRecharge['create_time'] = time();
|
||
$dataRecharge['old_money'] = $userInfo['money'];
|
||
$dataRecharge['new_money'] = $userMoney;
|
||
$dataRecharge['controller_old_money'] = $parent['money'];
|
||
$dataRecharge['controller_new_money'] = $parentMoney;
|
||
$dataRecharge['controller_system'] = 1;
|
||
$dataRecharge['remake'] = '洗码上分,超过24小时未接码费自动结算';
|
||
$result = Db::name('recharge')->insert($dataRecharge);
|
||
if(!$result){
|
||
throw new Exception("结算洗码:{$userInfo['username']} 操作失败");
|
||
}
|
||
|
||
// 处理上分(上级)
|
||
$dataRecharge = array();
|
||
$dataRecharge['type'] = 2;
|
||
$dataRecharge['amount'] = $amount;
|
||
$dataRecharge['mode'] = 2;
|
||
$dataRecharge['agent_or_admin'] = 5;
|
||
$dataRecharge['controller_type'] = '定时结算系统操作';
|
||
$dataRecharge['user_id'] = $parent['id'];
|
||
$dataRecharge['user_type'] = $parent['agent'];
|
||
$dataRecharge['user_agent_level'] = $parent['agent_level'];
|
||
$dataRecharge['username_for'] = $parent['username'];
|
||
$dataRecharge['nickname_for'] = $parent['nickname'];
|
||
$dataRecharge['user_parent_id'] = $parent['agent_parent_id'];
|
||
$dataRecharge['create_time'] = time();
|
||
$dataRecharge['old_money'] = $parent['money'];
|
||
$dataRecharge['new_money'] = $parentMoney;
|
||
$dataRecharge['controller_old_money'] = $parent['money'];
|
||
$dataRecharge['controller_new_money'] = $parentMoney;
|
||
$dataRecharge['controller_system'] = 1;
|
||
$dataRecharge['remake'] = '下级洗码上分,超过24小时未接码费自动结算';
|
||
$result = Db::name('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();
|
||
}
|
||
}
|
||
}
|
||
} |