diff --git a/Application/Admin/Controller/AutoController.class.php b/Application/Admin/Controller/AutoController.class.php index 85c84dabf..47b0a75a8 100644 --- a/Application/Admin/Controller/AutoController.class.php +++ b/Application/Admin/Controller/AutoController.class.php @@ -8,6 +8,7 @@ */ namespace Admin\Controller; use Admin\Model\SpendModel; +use Admin\Model\WithdrawModel; use Think\Think; use Org\RedisSDK\Redis; @@ -1059,22 +1060,49 @@ public function auto_rrdae(){ $this->promoteWithdraw(2); } + //会长周结月补点 + public function promoteWithdrawRecoup() + { + $this->promoteWithdraw(3); + } + //会长提现 $settlement 1-周结算 2-月结算 private function promoteWithdraw($settlement = 1, $initial = false) { + $withdrawModel = new WithdrawModel(); + $map['id'] = 84; $map['level'] = 1; $map['ver_status'] = 1; - $map['settlement_type'] = $settlement; - $promotes = M('promote', 'tab_')->field('id, account, chain')->where($map)->select(); + $map['settlement_type'] = ($settlement == 3) ? 1 : $settlement; + $promotes = M('promote', 'tab_')->field('id, account, chain, settlement_type')->where($map)->select(); $success = 0; $error_withdraw = 0; $error_spend = 0; $error_balance = 0; + $error_unset = 0; + $error_repeat = 0; if (!empty($promotes)) { foreach ($promotes as $promote) { - $result = $this->promoteWithdrawByPromote($promote, $initial); + if ($settlement == 3) { + $result = $withdrawModel->promoteWithdrawRecoupByPromote($promote); + } else { + switch ($promote['settlement_type']) { + case 1: + $result = $withdrawModel->promoteWithdrawWeeklyByPromote($promote, $initial); + break; + case 2: + $result = $withdrawModel->promoteWithdrawPerMonthByPromote($promote, $initial); + break; + } + } switch ($result) { + case -4: + $error_repeat++; + break; + case -3: + $error_unset++; + break; case -2: $error_balance++; break; @@ -1093,127 +1121,4 @@ public function auto_rrdae(){ echo "success:{$success} error_withdraw:{$error_withdraw} error_spend:{$error_spend} error_balance:{$error_balance}"; } - - private function promoteWithdrawWeeklyByPromote($promote, $initial = false) - { - $promoteMap['chain'] = ['like', "{$promote['chain']}{$promote['id']}/%"]; - $promoteIds = M('promote', 'tab_')->where($promoteMap)->getField('id', true); - $promoteIds[] = $promote['id']; - $settlementType = $promote['settlement_type']; - $settlementEndTime = strtotime(date('Y-m-d')) - 1; - - - } - - private function promoteWithdrawByPromote($promote, $initial = false) - { - $promoteMap['chain'] = ['like', "{$promote['chain']}{$promote['id']}/%"]; - $promoteIds = M('promote', 'tab_')->where($promoteMap)->getField('id', true); - $promoteIds[] = $promote['id']; - $settlementType = $promote['settlement_type']; - $settlementEndTime = strtotime(date('Y-m-d')) - 1; - if ($initial) { - $settlementType = 5; - $settlementBeginTime = 0; - } else { - switch ($promote['settlement_type']) { - case 2: - $settlementBeginTime = strtotime(date('Y-m', strtotime('-1 month', time()))); - break; - case 1: - default: - $settlementBeginTime = strtotime(date('Y-m-d', strtotime('-7 day', time()))); - break; - } - } - - $spendMap['pay_status'] = 1; - $spendMap['selle_status'] = 0; - $spendMap['promote_id'] = ['in', $promoteIds]; - $spendMap['pay_time'] = ['between', [$settlementBeginTime, $settlementEndTime]]; - $spendModel = M('spend', 'tab_'); - - $promoteGameRatioMap['status'] = 1; - $promoteGameRatioMap['promote_id'] = $promote['id']; - $promoteGameRatios = D('promote_game_ratio')->field('game_id, ratio, turnover_ratio, begin_time, end_time') - ->where($promoteGameRatioMap) - ->select(); - if (empty($promoteGameRatios)) { - $balance = $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance") - ->where($spendMap) - ->find()['balance']; - } else { - $balance = 0; - foreach ($promoteGameRatios as $promoteGameRatio) { - if ($settlementType == 1) { - $thisSettlementBeginTime = $settlementBeginTime; - $thisSettlementEndTime = $settlementEndTime; - if ($promoteGameRatio['begin_time'] > $settlementBeginTime) { - $thisSettlementBeginTime = $promoteGameRatio['begin_time']; - } elseif (!empty($promoteGameRatio['end_time']) && $promoteGameRatio['end_time'] < $settlementEndTime) { - $thisSettlementEndTime = $promoteGameRatio['end_time']; - } - $spendMap['pay_time'] = ['between', [$thisSettlementBeginTime, $thisSettlementEndTime]]; - $balance += $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance") - ->where($spendMap) - ->find()['balance']; - } elseif ($settlementType == 2) { - if ($promoteGameRatio['begin_time'] > $settlementBeginTime || (!empty($promoteGameRatio['end_time']) && $promoteGameRatio['end_time'] < $thisTime) || empty($promoteGameRatio['turnover_ratio'])) { - $spendMap['game_id'] = $promoteGameRatio['game_id']; - $balance += $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance") - ->where($spendMap) - ->find()['balance']; - } - if (empty($promoteGameRatio['turnover_ratio'])) { - - } - } - } - - $sumAmount = $spendModel->field("sum(pay_amount) as sum_amount") - ->where($spendMap) - ->find()['sum_amount']; - } - - $balance = bcdiv($balance, 100, 2); - if ($balance < 100) { - return -2;//余额不足 - } - - $thisTime = time(); - $add['sum_money'] = $balance; - $add['promote_id'] = $promote['id']; - $add['promote_account'] = $promote['account']; - $add['create_time'] = $thisTime; - $add['status'] = 0; - $add['widthdraw_number'] = D('withdraw')->produceWithdrawNumber(); - $add['settlement_begin_time'] = $settlementBeginTime; - $add['settlement_end_time'] = $settlementEndTime; - $add['spend_ids'] = ''; - $add['op_id'] = 0; - $add['op_type'] = 3; - $add['last_op_id'] = $add['op_id']; - $add['last_op_type'] = 3; - $add['settlement_type'] = $settlementType; - - M()->startTrans();//开启事物 - $withdrawId = D('withdraw')->add($add); - if (!$withdrawId) { - M()->rollback();//事物回滚 - return 0;//提现失败 - } - - $save['selle_status'] = 1; - $save['selle_time'] = $thisTime; - $save['withdraw_id'] = $withdrawId; - $res = $spendModel->where($spendMap)->save($save); - if ($res === false) { - D('withdraw')->delete($withdrawId); - M()->rollback();//事物回滚 - return -1; - } - - M()->commit();//事物提交 - return 1; - } } diff --git a/Application/Admin/Controller/QueryController.class.php b/Application/Admin/Controller/QueryController.class.php index a92719f2c..8677e9841 100644 --- a/Application/Admin/Controller/QueryController.class.php +++ b/Application/Admin/Controller/QueryController.class.php @@ -31,6 +31,8 @@ class QueryController extends ThinkController public function settlement($p = 0) { + $settlementType = intval(I('settlement_type', 0)); + $settlementTypeList = WithdrawModel::$settlementTypeList; $page = intval(I('p', 1)); //默认显示第一页数据 $row = intval(I('row', 10)); $group = I('group', 1); @@ -39,13 +41,50 @@ class QueryController extends ThinkController $promoteId = I('promote_id', 0); if ($group == 1) { - if (empty($promoteId)) { + unset($settlementTypeList[WithdrawModel::SETTLEMENT_TYPE_OTHER]); + $time = I('time', ''); + if (empty($promoteId) || empty($settlementType) || empty($time)) { + $this->assign('settlementTypeList', $settlementTypeList); $this->display(); } else { $promoteIds = M('promote', 'tab_')->where(['chain' => ['like', "/{$promoteId}/%"]])->getField('id', true); $promoteIds[] = $promoteId; - $beginTime = I('timestart', get_lastweek_name(7)); - $endTime = I('timeend', get_lastweek_name(1)); + + if ($settlementType == 1) { + $time = explode(' 至 ', $time); + if (count($time) != 2) { + $this->display();die; + } + + $beginTime = strtotime($time[0]); + $endTime = strtotime($time[1]) + 3600 * 24 - 1; + } else { + $beginTime = strtotime($time); + $endTime = strtotime('+1 month', $beginTime) - 1; + } + + $promote = M('promote', 'tab_')->find($promoteId); + if (empty($promote) || $promote['level'] != 1 || empty($promote['settlement_type'])) { + $this->error('网络异常'); + } + if ($promote['settlement_type'] == 1) { + unset($settlementTypeList[WithdrawModel::SETTLEMENT_TYPE_MONTH]); + } else { + unset($settlementTypeList[WithdrawModel::SETTLEMENT_TYPE_WEEK]); + unset($settlementTypeList[WithdrawModel::SETTLEMENT_TYPE_RECOUP]); + } + if ($settlementType == 3) { + $withdrawMap['promote_id'] = $promoteId; + $withdrawMap['status'] = ['neq', -2]; + $withdrawMap['settlement_begin_time'] = $beginTime; + $withdrawMap['settlement_end_time'] = $endTime; + $withdraw = D('withdraw')->where($withdrawMap)->find(); + if (!empty($withdraw)) { + $this->assign('settlementTypeList', $settlementTypeList); + $this->display();die; + } + } + $sortBy = I('sort_by', ''); $sortColumn = I('sort_column', ''); $order = 'day'; @@ -59,8 +98,6 @@ class QueryController extends ThinkController } elseif ($sortBy == 2) { $orderType = 'asc'; } - $beginTime = strtotime($beginTime); - $endTime = strtotime($endTime) + 3600 * 24 - 1; $spendMap['tab_spend.pay_status'] = 1; $spendMap['tab_spend.selle_status'] = 0; @@ -96,15 +133,19 @@ class QueryController extends ThinkController } if (!empty($records)) { - $promote = M('promote', 'tab_')->find($promoteId); foreach ($records as &$record) { $record['promote_account'] = $promote['account'] ?? '未知'; $record['settlement_amount'] = bcdiv($record['settlement_amount'], 100, 2); } } - $totalField = 'sum(if(tab_spend.selle_ratio > 0, tab_spend.pay_amount * tab_spend.selle_ratio, 0)) as settlement_amount'; - $zTotal = null_to_0(M('spend', 'tab_')->field($totalField)->where($spendMap)->find()['settlement_amount']);//累计 + if ($settlementType == 3) { + $totalField = 'sum(if(tab_spend.selle_ratio > 0, tab_spend.pay_amount * tab_spend.selle_ratio, 0)) as settlement_amount'; + $zTotal = null_to_0(M('spend', 'tab_')->field($totalField)->where($spendMap)->find()['settlement_amount']);//累计 + } else { + $totalField = 'sum(tab_spend.pay_amount) as sum_amount'; + $zTotal = null_to_0(M('spend', 'tab_')->field($totalField)->where($spendMap)->find()['sum_amount']);//累计 + } $zTotal = bcdiv($zTotal, 100 ,2); if(I('export', 0) == 1) { @@ -139,6 +180,7 @@ class QueryController extends ThinkController if($page) { $this->assign('_page', $page); } + $this->assign('settlementTypeList', $settlementTypeList); $this->assign('records', $records); $this->assign('zTotal', $zTotal); $this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Query/settlement', 'status' => 1])->find()); @@ -151,7 +193,6 @@ class QueryController extends ThinkController $withdrawNumber = I('widthdraw_number', ''); $status = intval(I('status', '')); $opType = intval(I('op_type', 0)); - $settlementType = intval(I('settlement_type', 0)); if ($createTime) { $createTimeEnd = $createTime + 3600 * 24 - 1; $map['create_time'] = ['between', [$createTime, $createTimeEnd]]; @@ -244,6 +285,7 @@ class QueryController extends ThinkController if($page) { $this->assign('_page', $page); } + $this->assign('settlementTypeList', $settlementTypeList); $this->assign('records', $records); $this->display(); } @@ -266,22 +308,22 @@ class QueryController extends ThinkController public function settlementWithdraw() { - $beginTime = strtotime(I('begin_time', '')); - $endTime = strtotime(I('end_time', '')); $promoteId = intval(I('promote_id', 0)); - if ($beginTime == 0 || $endTime == 0) { + $settlementType = intval(I('settlement_type', 0)); + $time = I('time', ''); + if (empty($promoteId)) { $data['status'] = 0; - $data['msg'] = '时间参数错误'; + $data['msg'] = '请选择会长账号'; $this->ajaxReturn($data); } - if ($beginTime > $endTime) { + if (empty($settlementType)) { $data['status'] = 0; - $data['msg'] = '开始时间必须小于等于结束时间'; + $data['msg'] = '请选择提现模式'; $this->ajaxReturn($data); } - if ($promoteId == 0) { + if (empty($time)) { $data['status'] = 0; - $data['msg'] = '请选择会长账号'; + $data['msg'] = '请选择充值时间'; $this->ajaxReturn($data); } @@ -301,78 +343,75 @@ class QueryController extends ThinkController $data['msg'] = '该会长未通过资质认证审核,暂时无法提现'; $this->ajaxReturn($data); } - - $endTime += 3600 * 24 - 1; - $spendModel = M('spend', 'tab_'); - $map['chain'] = ['like', $promote['chain'] . $promote['id'] . '/%']; - $promoteIds = M('promote', 'tab_')->where($map)->getField('id', true); - $promoteIds[] = $promote['id']; - $promoteIds = implode(',', $promoteIds); - - $map = []; - $map['pay_status'] = 1; - $map['selle_status'] = 0; - $map['promote_id'] = ['in', $promoteIds]; - $map['pay_time'] = ['between', [$beginTime, $endTime]]; - $balance = $spendModel->field("sum(pay_amount * selle_ratio) as balance") - ->where($map) - ->find()['balance']; - $balance = bcdiv($balance, 100, 2); - if ($balance < 100) { - $data['status'] = 0; - $data['msg'] = '累计结算低于100元,无法提现!'; - $this->ajaxReturn($data); + if ($promote['settlement_type'] == 1) { + if (!in_array($settlementType, [1, 3])) { + $data['status'] = 0; + $data['msg'] = '参数异常'; + $this->ajaxReturn($data); + } + } else { + if ($settlementType != 2) { + $data['status'] = 0; + $data['msg'] = '参数异常'; + $this->ajaxReturn($data); + } } - $spendIds = $spendModel->where($map)->getField('id', true); - if (empty($spendIds)) { - $data['status'] = 0; - $data['msg'] = '数据异常'; - $this->ajaxReturn($data); + + if ($settlementType == 1) { + $time = explode(' 至 ', $time); + if (count($time) != 2) { + $data['status'] = 0; + $data['msg'] = '参数异常'; + $this->ajaxReturn($data); + } + $beginTime = strtotime($time[0]); + $endTime = strtotime($time[1]) + 3600 * 24 - 1; + } else { + $beginTime = strtotime($time); + $endTime = strtotime('+1 month', $beginTime) - 1; } - $thisTime = time(); - $add['sum_money'] = $balance; - $add['promote_id'] = $promote['id']; - $add['promote_account'] = $promote['account']; - $add['create_time'] = $thisTime; - $add['status'] = 0; - $add['widthdraw_number'] = D('withdraw')->produceWithdrawNumber(); - $add['settlement_begin_time'] = $beginTime; - $add['settlement_end_time'] = $endTime; - $add['spend_ids'] = ''; - $add['op_id'] = is_login(); - $add['op_type'] = 2; - $add['last_op_id'] = $add['op_id']; - $add['last_op_type'] = 2; - $add['settlement_type'] = 3; - $add['game_ratio'] = ''; - - M()->startTrans();//开启事物 - $withdrawId = M('withdraw', 'tab_')->add($add); - if (!$withdrawId) { - M()->rollback();//事物回滚 - $data['status'] = 0; - $data['msg'] = '提现失败'; - $this->ajaxReturn($data); + $withdrawModel = new WithdrawModel(); + $data['begin_time'] = $beginTime; + $data['end_time'] = $endTime; + $data['op_id'] = is_login(); + $data['op_type'] = 2; + switch ($settlementType) { + case 1: + $result = $withdrawModel->promoteWithdrawWeeklyByPromote($promote, false, $data); + break; + case 2: + $result = $withdrawModel->promoteWithdrawPerMonthByPromote($promote, false, $data); + break; + case 3: + $result = $withdrawModel->promoteWithdrawRecoupByPromote($promote, $data); + break; } - $map = []; - $map['id'] = ['in', $spendIds]; - $save['selle_status'] = 1; - $save['selle_time'] = $thisTime; - $save['withdraw_id'] = $withdrawId; - $res = $spendModel->where($map)->save($save); - if ($res === false) { - M('withdraw', 'tab_')->delete($withdrawId); - M()->rollback();//事物回滚 - $data['status'] = 0; - $data['msg'] = '提现失败'; - $this->ajaxReturn($data); + $data = []; + $data['status'] = 0; + switch ($result) { + case -4: + case -3: + $data['msg'] = '无可补点订单'; + break; + case -2: + if ($settlementType == 3) { + $data['msg'] = '无可补点订单'; + } else { + $data['msg'] = '累计结算低于100元,无法提现!'; + } + break; + case -1: + case 0: + $data['msg'] = '操作失败'; + break; + case 1: + $data['status'] = 1; + $data['msg'] = '操作成功'; + break; } - M()->commit();//事物提交 - $data['status'] = 1; - $data['msg'] = '提现成功'; $this->ajaxReturn($data); } @@ -412,43 +451,53 @@ class QueryController extends ThinkController $this->ajaxReturn($data); } - $spendMap['withdraw_id'] = $id; - $spendIds = M('Spend', 'tab_')->where($spendMap)->getField('id', true); - if (empty($spendIds)) { - $data['status'] = 0; - $data['msg'] = '数据异常'; - $this->ajaxReturn($data); - } - $spendIds = implode(',', $spendIds); $time = time(); + $data['status'] = 1; + $data['msg'] = '撤销成功'; $withdrawSave['id'] = $id; $withdrawSave['status'] = -2; - $withdrawSave['spend_ids'] = $spendIds; $withdrawSave['last_up_update_time'] = $time; $withdrawSave['last_op_id'] = is_login(); $withdrawSave['last_op_type'] = 2; + if ($data['settlement_type'] == 3) { + $withdrawRes = M('withdraw', 'tab_')->save($withdrawSave); + if ($withdrawRes === false) { + $data['status'] = 0; + $data['msg'] = '撤销失败'; + $this->ajaxReturn($data); + } + } else { + $spendMap['withdraw_id'] = $id; + $spendIds = M('Spend', 'tab_')->where($spendMap)->getField('id', true); + if (empty($spendIds)) { + $data['status'] = 0; + $data['msg'] = '数据异常'; + $this->ajaxReturn($data); + } + $spendIds = implode(',', $spendIds); + $withdrawSave['spend_ids'] = $spendIds; - M('withdraw', 'tab_')->startTrans();//开启事物 - $withdrawRes = M('withdraw', 'tab_')->save($withdrawSave); - if ($withdrawRes === false) { - M('withdraw', 'tab_')->rollback();//事物回滚 - $data['status'] = 0; - $data['msg'] = '撤销失败'; - $this->ajaxReturn($data); - } + M('withdraw', 'tab_')->startTrans();//开启事物 + $withdrawRes = M('withdraw', 'tab_')->save($withdrawSave); + if ($withdrawRes === false) { + M('withdraw', 'tab_')->rollback();//事物回滚 + $data['status'] = 0; + $data['msg'] = '撤销失败'; + $this->ajaxReturn($data); + } - $spendSave['selle_status'] = 0; - $spendSave['withdraw_id'] = 0; - $spendRes = M('spend', 'tab_')->where($spendMap)->save($spendSave); - if ($spendRes === false) { - M('withdraw', 'tab_')->rollback();//事物回滚 - $data['status'] = 0; - $data['msg'] = '撤销失败'; - } else { - M('withdraw', 'tab_')->commit();//事物提交 - $data['status'] = 1; - $data['msg'] = '撤销成功'; + $spendSave['selle_status'] = 0; + $spendSave['withdraw_id'] = 0; + $spendRes = M('spend', 'tab_')->where($spendMap)->save($spendSave); + if ($spendRes === false) { + M('withdraw', 'tab_')->rollback();//事物回滚 + $data['status'] = 0; + $data['msg'] = '撤销失败'; + } else { + M('withdraw', 'tab_')->commit();//事物提交 + } } + $this->ajaxReturn($data); } @@ -1393,7 +1442,6 @@ class QueryController extends ThinkController } } - public function marketList($p=1) { $page = intval($p); @@ -1501,4 +1549,49 @@ class QueryController extends ThinkController $this->display(); } + public function getPromoteSettlementType() + { + $promoteId = intval(I('post.promote_id', 0)); + $settlementType = []; + if ($promoteId > 0) { + $promote = D('promote')->field('level, settlement_type')->where(array('id' => $promoteId))->find(); + if (empty($promote) || $promote['level'] != 1 || empty($promote['settlement_type'])) { + $settlementType = []; + } else { + $settlementType = WithdrawModel::$settlementTypeList; + unset($settlementType[WithdrawModel::SETTLEMENT_TYPE_OTHER]); + if ($promote['settlement_type'] == 1) { + unset($settlementType[WithdrawModel::SETTLEMENT_TYPE_MONTH]); + } else { + unset($settlementType[WithdrawModel::SETTLEMENT_TYPE_WEEK]); + unset($settlementType[WithdrawModel::SETTLEMENT_TYPE_RECOUP]); + } + } + } + + $this->ajaxReturn(['settlement_type' => $settlementType]); + } + + public function getRecoupAmount() + { + $withdrawModel = new WithdrawModel(); + $promoteId = intval(I('promote_id', 0)); + $settlementType = intval(I('settlement_type', 0)); + $time = I('time', ''); + + if (empty($promoteId) || $settlementType != 3) { + $this->ajaxReturn(['balance' => 0]); + } + + $promote = M('promote', 'tab_')->find($promoteId); + if (empty($promote) || $promote['level'] != 1 || $promote['ver_status'] != 1 || $promote['settlement_type'] != 1 || empty($time)) { + $this->ajaxReturn(['balance' => 0]); + } + + $data['begin_time'] = strtotime($time); + $data['end_time'] = strtotime('+1 month', $data['begin_time']) - 1; + + $balance = $withdrawModel->getRecoupAmount($promote, $data); + $this->ajaxReturn(['balance' => $balance]); + } } diff --git a/Application/Admin/Model/WithdrawModel.class.php b/Application/Admin/Model/WithdrawModel.class.php index aad3366bc..0c03b4703 100644 --- a/Application/Admin/Model/WithdrawModel.class.php +++ b/Application/Admin/Model/WithdrawModel.class.php @@ -23,9 +23,8 @@ class WithdrawModel extends Model{ //结算类型 const SETTLEMENT_TYPE_WEEK = 1; const SETTLEMENT_TYPE_MONTH = 2; - const SETTLEMENT_TYPE_ADMIN = 3; - const SETTLEMENT_TYPE_PROMOTE = 4; - const SETTLEMENT_TYPE_OTHER = 5; + const SETTLEMENT_TYPE_RECOUP = 3; + const SETTLEMENT_TYPE_OTHER = 9; /** * 构造函数 @@ -49,7 +48,7 @@ class WithdrawModel extends Model{ public static $settlementTypeList = [ self::SETTLEMENT_TYPE_WEEK => '周结', self::SETTLEMENT_TYPE_MONTH => '月结', - self::SETTLEMENT_TYPE_ADMIN => '管理员提现', + self::SETTLEMENT_TYPE_RECOUP => '补点', self::SETTLEMENT_TYPE_OTHER => '其他', ]; @@ -124,11 +123,22 @@ class WithdrawModel extends Model{ } - //生成提现单号 - public function produceWithdrawNumber() + //生成提现单号 $type 1-周结 2-月结 3-补点 + public function produceWithdrawNumber($type = 1) { $prefix = 'WD_'; $number = ''; + switch ($type) { + case 1: + $prefix = "{$prefix}W_"; + break; + case 2: + $prefix = "{$prefix}M_"; + break; + case 3: + $prefix = "{$prefix}R_"; + break; + } while (true) { $randomNum = rand(10, 99); $number = $prefix . time() . $randomNum; @@ -140,4 +150,402 @@ class WithdrawModel extends Model{ } return $number; } + + //周结 + public function promoteWithdrawWeeklyByPromote($promote, $initial = false, $data = []) + { + $promoteIds = $this->getPromoteChildren($promote); + $settlementType = $promote['settlement_type']; + $settlementBeginTime = $data['begin_time'] ?? strtotime(date('Y-m-d', strtotime('-7 day', time()))); + $settlementEndTime = $data['end_time'] ?? strtotime(date('Y-m-d')) - 1; + + if ($initial) { + $settlementType = 9; + $settlementBeginTime = 0; + } + + $spendMap['pay_status'] = 1; + $spendMap['selle_status'] = 0; + $spendMap['promote_id'] = ['in', $promoteIds]; + $spendMap['pay_time'] = ['between', [$settlementBeginTime, $settlementEndTime]]; + $spendModel = M('spend', 'tab_'); + + $balance = $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance") + ->where($spendMap) + ->find()['balance']; + $balance = $balance ?? 0; + $balance = bcdiv($balance, 100, 2); + if ($balance < 100) { + return -2;//余额不足 + } + + $gameRatios = []; + if ($initial === false) { + $beginTime = date('Y-m-d', $settlementBeginTime); + $endTime = date('Y-m-d', $settlementEndTime); + $gameRatios = $this->getGameRatio($spendMap, $beginTime, $endTime); + } + + $gameRatios = $gameRatios ? json_encode($gameRatios) : ''; + + $thisTime = time(); + $add['sum_money'] = $balance; + $add['promote_id'] = $promote['id']; + $add['promote_account'] = $promote['account']; + $add['create_time'] = $thisTime; + $add['status'] = 0; + $add['widthdraw_number'] = D('withdraw')->produceWithdrawNumber(1); + $add['settlement_begin_time'] = $settlementBeginTime; + $add['settlement_end_time'] = $settlementEndTime; + $add['spend_ids'] = ''; + $add['op_id'] = $data['op_id'] ?? 0; + $add['op_type'] = $data['op_type'] ?? 3; + $add['last_op_id'] = $add['op_id']; + $add['last_op_type'] = $add['op_type']; + $add['settlement_type'] = $settlementType; + $add['game_ratio'] = $gameRatios; + + M()->startTrans();//开启事物 + $withdrawId = D('withdraw')->add($add); + if (!$withdrawId) { + M()->rollback();//事物回滚 + return 0;//提现失败 + } + + $save['selle_status'] = 1; + $save['selle_time'] = $thisTime; + $save['withdraw_id'] = $withdrawId; + $res = $spendModel->where($spendMap)->save($save); + if ($res === false) { + D('withdraw')->delete($withdrawId); + M()->rollback();//事物回滚 + return -1; + } + + M()->commit();//事物提交 + return 1; + } + + //月结 + public function promoteWithdrawPerMonthByPromote($promote, $initial = false, $data = []) + { + $promoteIds = $this->getPromoteChildren($promote); + $settlementType = $promote['settlement_type']; + $settlementBeginTime = $data['begin_time'] ?? strtotime(date('Y-m', strtotime('-1 month', time()))); + $settlementEndTime = $data['end_time'] ?? strtotime(date('Y-m')) - 1; + + if ($initial) { + $settlementType = 9; + $settlementBeginTime = 0; + } + + $beginTime = date('Y-m-d', $settlementBeginTime); + $endTime = date('Y-m-d', $settlementEndTime); + $spendMap['pay_status'] = 1; + $spendMap['selle_status'] = 0; + $spendMap['promote_id'] = ['in', $promoteIds]; + $spendMap['pay_time'] = ['between', [$settlementBeginTime, $settlementEndTime]]; + $spendWhere = $spendMap; + $spendModel = M('spend', 'tab_'); + + $gameRatios = []; + $promoteGameRatioMap['status'] = 1; + $promoteGameRatioMap['promote_id'] = $promote['id']; + $promoteGameRatios = D('promote_game_ratio')->field('game_id, ratio, turnover_ratio, begin_time, end_time') + ->where($promoteGameRatioMap) + ->order('update_time desc') + ->select(); + + if ($initial === false) { + $beginTime = date('Y-m-d', $settlementBeginTime); + $endTime = date('Y-m-d', $settlementEndTime); + $gameRatios = $this->getGameRatio($spendMap, $beginTime, $endTime); + } + if (empty($promoteGameRatios) || $initial) { + $balance = $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance") + ->where($spendMap) + ->find()['balance']; + $balance = $balance ?? 0; + } else { + $balance = 0; + $notInGameIds = [-1]; + foreach ($promoteGameRatios as $promoteGameRatio) { + $spendWhere['game_id'] = $promoteGameRatio['game_id']; + if (!empty($promoteGameRatio['turnover_ratio']) && $promoteGameRatio['begin_time'] <= $settlementBeginTime && (empty($promoteGameRatio['end_time']) || $promoteGameRatio['end_time'] >= $settlementEndTime)) { + $notInGameIds[] = $promoteGameRatio['game_id']; + $ratio = $promoteGameRatio['ratio']; + $promoteGameRatio['turnover_ratio'] = json_decode($promoteGameRatio['turnover_ratio'], true); + $turnoverRatios = array_reverse($promoteGameRatio['turnover_ratio']); + $sumAmount = $spendModel->field("sum(pay_amount) as sum_amount") + ->where($spendWhere) + ->find()['sum_amount']; + $sumAmount = $sumAmount ?? 0; + foreach ($turnoverRatios as $turnoverRatio) { + if ($sumAmount >= $turnoverRatio['turnover']) { + $ratio = $turnoverRatio['ratio']; + break; + } + } + + $thisBalance = $spendModel->field("sum(pay_amount * {$ratio}) as balance") + ->where($spendWhere) + ->find()['balance']; + $thisBalance = $thisBalance ?? 0; + $balance = bcadd($balance, $thisBalance, 2); + + $gameRatios[$promoteGameRatio['game_id']] = []; + $gameRatios[$promoteGameRatio['game_id']][] = [ + 'selle_ratio' => $ratio, + 'sum_amount' => $sumAmount, + 'begin_time' => $beginTime, + 'end_time' => $endTime, + ]; + } + } + + $spendWhere['game_id'] = ['not in', $notInGameIds]; + $otherBalance = $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance") + ->where($spendWhere) + ->find()['balance']; + $otherBalance = $otherBalance ?? 0; + $balance = bcadd($balance, $otherBalance, 2); + } + + $balance = bcdiv($balance, 100, 2); + if ($balance < 100) { + return -2;//余额不足 + } + + $gameRatios = $gameRatios ? json_encode($gameRatios) : ''; + + $thisTime = time(); + $add['sum_money'] = $balance; + $add['promote_id'] = $promote['id']; + $add['promote_account'] = $promote['account']; + $add['create_time'] = $thisTime; + $add['status'] = 0; + $add['widthdraw_number'] = D('withdraw')->produceWithdrawNumber(2); + $add['settlement_begin_time'] = $settlementBeginTime; + $add['settlement_end_time'] = $settlementEndTime; + $add['spend_ids'] = ''; + $add['op_id'] = $data['op_id'] ?? 0; + $add['op_type'] = $data['op_type'] ?? 3; + $add['last_op_id'] = $add['op_id']; + $add['last_op_type'] = $add['op_type']; + $add['settlement_type'] = $settlementType; + $add['game_ratio'] = $gameRatios; + + M()->startTrans();//开启事物 + $withdrawId = D('withdraw')->add($add); + if (!$withdrawId) { + M()->rollback();//事物回滚 + return 0;//提现失败 + } + + $save['selle_status'] = 1; + $save['selle_time'] = $thisTime; + $save['withdraw_id'] = $withdrawId; + $res = $spendModel->where($spendMap)->save($save); + if ($res === false) { + D('withdraw')->delete($withdrawId); + M()->rollback();//事物回滚 + return -1; + } + + M()->commit();//事物提交 + return 1; + } + + //补点 + public function promoteWithdrawRecoupByPromote($promote, $data = []) + { + $promoteGameRatioMap['status'] = 1; + $promoteGameRatioMap['promote_id'] = $promote['id']; + $promoteGameRatios = D('promote_game_ratio')->field('game_id, ratio, turnover_ratio, begin_time, end_time') + ->where($promoteGameRatioMap) + ->order('update_time desc') + ->select(); + if (empty($promoteGameRatios)) { + return -3; + } + + $promoteIds = $this->getPromoteChildren($promote); + $settlementType = 3; + $settlementBeginTime = $data['begin_time'] ?? strtotime(date('Y-m', strtotime('-1 month', time()))); + $settlementEndTime = $data['end_time'] ?? strtotime(date('Y-m')) - 1; + + $withdrawMap['promote_id'] = $promote['id']; + $withdrawMap['status'] = ['neq', -2]; + $withdrawMap['settlement_begin_time'] = $settlementBeginTime; + $withdrawMap['settlement_end_time'] = $settlementEndTime; + $withdraw = D('withdraw')->where($withdrawMap)->find(); + if (!empty($withdraw)) { + return -4; + } + + $spendMap['pay_status'] = 1; + $spendMap['promote_id'] = ['in', $promoteIds]; + $spendMap['pay_time'] = ['between', [$settlementBeginTime, $settlementEndTime]]; + $spendModel = M('spend', 'tab_'); + + $balance = 0; + $gameRatios = []; + foreach ($promoteGameRatios as $promoteGameRatio) { + if (!empty($promoteGameRatio['turnover_ratio']) && $promoteGameRatio['begin_time'] <= $settlementBeginTime && (empty($promoteGameRatio['end_time']) || $promoteGameRatio['end_time'] >= $settlementEndTime)) { + $spendMap['game_id'] = $promoteGameRatio['game_id']; + $promoteGameRatio['turnover_ratio'] = json_decode($promoteGameRatio['turnover_ratio'], true); + $turnoverRatios = array_reverse($promoteGameRatio['turnover_ratio']); + $sumAmount = $spendModel->field("sum(pay_amount) as sum_amount") + ->where($spendMap) + ->find()['sum_amount']; + $sumAmount = $sumAmount ?? 0; + $ratio = 0; + foreach ($turnoverRatios as $turnoverRatio) { + if ($sumAmount >= $turnoverRatio['turnover']) { + $ratio = $turnoverRatio['ratio']; + break; + } + } + + if ($ratio > 0) { + $ratio = bcsub($ratio, $promoteGameRatio['ratio'], 2); + $thisBalance = bcdiv(bcmul($sumAmount, $ratio, 2), 100, 2); + $balance = bcadd($balance, $thisBalance, 2); + + $gameRatios[$promoteGameRatio['game_id']][] = [ + 'selle_ratio' => $ratio, + 'sum_amount' => $sumAmount, + 'begin_time' => date('Y-m-d', $settlementBeginTime), + 'end_time' => date('Y-m-d', $settlementEndTime), + ]; + } + } + } + + if ($balance == 0) { + return -2;//无余额 + } + + $gameRatios = $gameRatios ? json_encode($gameRatios) : ''; + + $thisTime = time(); + $add['sum_money'] = $balance; + $add['promote_id'] = $promote['id']; + $add['promote_account'] = $promote['account']; + $add['create_time'] = $thisTime; + $add['status'] = 0; + $add['widthdraw_number'] = D('withdraw')->produceWithdrawNumber(3); + $add['settlement_begin_time'] = $settlementBeginTime; + $add['settlement_end_time'] = $settlementEndTime; + $add['spend_ids'] = ''; + $add['op_id'] = $data['op_id'] ?? 0; + $add['op_type'] = $data['op_type'] ?? 3; + $add['last_op_id'] = $add['op_id']; + $add['last_op_type'] = $add['op_type']; + $add['settlement_type'] = $settlementType; + $add['game_ratio'] = $gameRatios; + + $withdrawId = D('withdraw')->add($add); + if (!$withdrawId) { + return 0;//补点失败 + } + + return 1; + } + + public function getRecoupAmount($promote, $data = []) + { + $promoteGameRatioMap['status'] = 1; + $promoteGameRatioMap['promote_id'] = $promote['id']; + $promoteGameRatios = D('promote_game_ratio')->field('game_id, ratio, turnover_ratio, begin_time, end_time') + ->where($promoteGameRatioMap) + ->order('update_time desc') + ->select(); + if (empty($promoteGameRatios)) { + return 0; + } + + $promoteIds = $this->getPromoteChildren($promote); + $settlementBeginTime = $data['begin_time'] ?? strtotime(date('Y-m', strtotime('-1 month', time()))); + $settlementEndTime = $data['end_time'] ?? strtotime(date('Y-m')) - 1; + + $spendMap['pay_status'] = 1; + $spendMap['promote_id'] = ['in', $promoteIds]; + $spendMap['pay_time'] = ['between', [$settlementBeginTime, $settlementEndTime]]; + $spendModel = M('spend', 'tab_'); + + $balance = 0; + foreach ($promoteGameRatios as $promoteGameRatio) { + if (!empty($promoteGameRatio['turnover_ratio']) && $promoteGameRatio['begin_time'] <= $settlementBeginTime && (empty($promoteGameRatio['end_time']) || $promoteGameRatio['end_time'] >= $settlementEndTime)) { + $spendMap['game_id'] = $promoteGameRatio['game_id']; + $promoteGameRatio['turnover_ratio'] = json_decode($promoteGameRatio['turnover_ratio'], true); + $turnoverRatios = array_reverse($promoteGameRatio['turnover_ratio']); + $sumAmount = $spendModel->field("sum(pay_amount) as sum_amount") + ->where($spendMap) + ->find()['sum_amount']; + $sumAmount = $sumAmount ?? 0; + $ratio = 0; + foreach ($turnoverRatios as $turnoverRatio) { + if ($sumAmount >= $turnoverRatio['turnover']) { + $ratio = $turnoverRatio['ratio']; + break; + } + } + + if ($ratio > 0) { + $ratio = bcsub($ratio, $promoteGameRatio['ratio'], 2); + $thisBalance = bcdiv(bcmul($sumAmount, $ratio, 2), 100, 2); + $balance = bcadd($balance, $thisBalance, 2); + } + } + } + + return $balance; + } + + private function getSpendMessageByGame($map) + { + $field = 'game_id, selle_ratio, sum(pay_amount) as sum_amount, pay_time'; + $spendModel = M('spend', 'tab_'); + $gameSpendRatios = $spendModel->field($field) + ->where($map) + ->group('game_id, selle_ratio') + ->order('game_id asc, pay_time asc') + ->select(); + + return $gameSpendRatios; + } + + private function getPromoteChildren($promote) { + $promoteMap['chain'] = ['like', "{$promote['chain']}{$promote['id']}/%"]; + $promoteIds = M('promote', 'tab_')->where($promoteMap)->getField('id', true); + $promoteIds[] = $promote['id']; + + return $promoteIds; + } + + private function getGameRatio($map, $beginTime, $endTime) + { + $gameRatios = []; + $gameSpendRatios = $this->getSpendMessageByGame($map); + foreach ($gameSpendRatios as $gameSpendRatio) { + $gameRatios[$gameSpendRatio['game_id']][] = [ + 'selle_ratio' => $gameSpendRatio['selle_ratio'], + 'sum_amount' => $gameSpendRatio['sum_amount'], + 'begin_time' => date('Y-m-d', $gameSpendRatio['pay_time']), + 'end_time' => date('Y-m-d', $gameSpendRatio['pay_time']), + ]; + } + foreach ($gameRatios as $key => $gameRatio) { + $gameRatios[$key][0]['begin_time'] = $beginTime; + $gameRatios[$key][count($gameRatio) - 1]['end_time'] = $endTime; + if (count($gameRatio) > 1) { + foreach ($gameRatios[$key] as $childKey => $childList) { + $gameRatios[$key][$childKey]['end_time'] = isset($gameRatios[$key][$childKey + 1]) ? $gameRatios[$key][$childKey + 1]['begin_time'] : $gameRatios[$key][$childKey]['end_time']; + } + } + } + + return $gameRatios; + } } diff --git a/Application/Admin/View/Query/settlement.html b/Application/Admin/View/Query/settlement.html index 382e1a222..dc76938b5 100644 --- a/Application/Admin/View/Query/settlement.html +++ b/Application/Admin/View/Query/settlement.html @@ -52,24 +52,9 @@