diff --git a/Application/Admin/Controller/AutoController.class.php b/Application/Admin/Controller/AutoController.class.php index 0045ae56b..ff30dda40 100644 --- a/Application/Admin/Controller/AutoController.class.php +++ b/Application/Admin/Controller/AutoController.class.php @@ -1038,7 +1038,90 @@ public function auto_rrdae(){ //会长提现 public function promoteWithdraw() { - $promoteMap['level'] = 1; - $promotes = M('promote', 'tab_')->where(); + $map['level'] = 1; + $map['ver_status'] = 1; + $promotes = M('promote', 'tab_')->field('id, account, chain')->where($map)->select(); + $success = 0; + $error_withdraw = 0; + $error_spend = 0; + $error_balance = 0; + + if (!empty($promotes)) { + foreach ($promotes as $promote) { + $result = $this->promoteWithdrawByPromote($promote); + switch ($result) { + case -2: + $error_balance++; + break; + case -1: + $error_spend++; + break; + case 0: + $error_withdraw++; + break; + case 1: + $success++; + break; + } + } + } + + echo "success:{$success} error_withdraw:{$error_withdraw} error_spend:{$error_spend} error_balance:{$error_balance}"; + } + + private function promoteWithdrawByPromote($promote) + { + $promoteMap['chain'] = ['like', "{$promote['chain']}{$promote['id']}/%"]; + $promoteIds = M('promote', 'tab_')->where($promoteMap)->getField('id', true); + $promoteIds[] = $promote['id']; + $settlementEndTime = strtotime(date('Y-m-d')) - 1; + + $spendMap['pay_status'] = 1; + $spendMap['selle_status'] = 0; + $spendMap['promote_id'] = ['in', $promoteIds]; + $spendMap['pay_time'] = ['elt', $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 = 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'] = 0; + $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; + + 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 ecc99b178..fde9a8bc0 100644 --- a/Application/Admin/Controller/QueryController.class.php +++ b/Application/Admin/Controller/QueryController.class.php @@ -13,7 +13,7 @@ use Admin\Model\WithdrawModel; class QueryController extends ThinkController { //生成提现单号 - private function produceWithdrawNumber() + public function produceWithdrawNumber() { $prefix = 'WD_'; $number = ''; @@ -148,7 +148,7 @@ class QueryController extends ThinkController $records = M('withdraw', 'tab_')->field('id, promote_account, widthdraw_number, sum_money, op_id, op_type, last_op_id, last_op_type, settlement_begin_time, settlement_end_time, create_time, status, respond') ->where($map) - ->order('') + ->order('create_time desc') ->page($page, $row) ->select(); $count = M('withdraw', 'tab_')->field('id, promote_account, widthdraw_number, sum_money, op_id, op_type, last_op_id, last_op_type, settlement_begin_time, settlement_end_time, create_time, status, respond') @@ -247,7 +247,7 @@ class QueryController extends ThinkController $add['promote_account'] = $promote['account']; $add['create_time'] = $thisTime; $add['status'] = 0; - $add['widthdraw_number'] = $this->produceWithdrawNumber(); + $add['widthdraw_number'] = D('withdraw')->produceWithdrawNumber(); $add['settlement_begin_time'] = $beginTime; $add['settlement_end_time'] = $endTime; $add['spend_ids'] = ''; diff --git a/Application/Admin/Model/WithdrawModel.class.php b/Application/Admin/Model/WithdrawModel.class.php index dd9b57847..87d60eb32 100644 --- a/Application/Admin/Model/WithdrawModel.class.php +++ b/Application/Admin/Model/WithdrawModel.class.php @@ -17,6 +17,7 @@ class WithdrawModel extends Model{ const OP_TYPE_PROMOTE = 1; const OP_TYPE_ADMIN = 2; + const OP_TYPE_AUTO = 3; /** * 构造函数 @@ -34,6 +35,7 @@ class WithdrawModel extends Model{ public static $opTypeList = [ self::OP_TYPE_PROMOTE => '推广员', self::OP_TYPE_ADMIN => '管理员', + self::OP_TYPE_AUTO => '自动', ]; /* @@ -107,4 +109,20 @@ class WithdrawModel extends Model{ } + //生成提现单号 + public function produceWithdrawNumber() + { + $prefix = 'WD_'; + $number = ''; + while (true) { + $randomNum = rand(10, 99); + $number = $prefix . time() . $randomNum; + $map['widthdraw_number'] = $number; + $res = $this->where($map)->getField('id'); + if (!$res) { + break; + } + } + return $number; + } } diff --git a/Application/Admin/View/Query/settlement.html b/Application/Admin/View/Query/settlement.html index f059ca622..e704735ff 100644 --- a/Application/Admin/View/Query/settlement.html +++ b/Application/Admin/View/Query/settlement.html @@ -191,16 +191,20 @@ {:get_promote_name($record['op_id'])} - + {:getAdminNickname($record['op_id'])} + + 自动 {:getPromoteWithdrawOpType($record['op_type'])} {:get_promote_name($record['last_op_id'])} - + {:getAdminNickname($record['last_op_id'])} + + 自动 {:getPromoteWithdrawOpType($record['last_op_type'])} diff --git a/Application/Admin/View/Query/withdraw.html b/Application/Admin/View/Query/withdraw.html index 326afd8e5..c675cae0b 100644 --- a/Application/Admin/View/Query/withdraw.html +++ b/Application/Admin/View/Query/withdraw.html @@ -159,16 +159,20 @@ {:get_promote_name($data['op_id'])} - + {:getAdminNickname($data['op_id'])} + + 自动 {:getPromoteWithdrawOpType($data['op_type'])} {:get_promote_name($data['last_op_id'])} - + {:getAdminNickname($data['last_op_id'])} + + 自动 {:getPromoteWithdrawOpType($data['last_op_type'])} diff --git a/Application/Home/Controller/FinanceController.class.php b/Application/Home/Controller/FinanceController.class.php index 1ee9e1c2f..a61cc0b36 100644 --- a/Application/Home/Controller/FinanceController.class.php +++ b/Application/Home/Controller/FinanceController.class.php @@ -430,8 +430,8 @@ class FinanceController extends BaseController $list['settlement_begin_time'] = $list['settlement_begin_time'] ? date('Y-m-d H:i:s', $list['settlement_begin_time']) : '--'; $list['settlement_end_time'] = date('Y-m-d H:i:s', $list['settlement_end_time']); $list['status_name'] = FinanceController::$withdrawStatus[$list['status']]; - $list['op_id'] = ($list['op_type'] == 1) ? get_promote_name($list['op_id']) : getAdminNickname($list['op_id']); - $list['last_op_id'] = ($list['last_op_type'] == 1) ? get_promote_name($list['last_op_id']) : getAdminNickname($list['last_op_id']); + $list['op_id'] = $this->getOpIdByType($list['op_id'], $list['op_type']); + $list['last_op_id'] = $this->getOpIdByType($list['last_op_id'], $list['last_op_type']); $list['op_type'] = getPromoteWithdrawOpType($list['op_type']); $list['last_op_type'] = getPromoteWithdrawOpType($list['last_op_type']); switch ($list['status']) { @@ -460,6 +460,23 @@ class FinanceController extends BaseController $this->display(); } + private function getOpIdByType($opId, $opType) + { + switch ($opType) { + case 1: + $opId = get_promote_name($opId); + break; + case 2: + $opId = getAdminNickname($opId); + break; + case 3: + $opId = '自动'; + break; + } + + return $opId; + } + //提现明细 public function withdrawDtl() { diff --git a/Data/update.sql b/Data/update.sql index 9356224cc..12a8ead50 100644 --- a/Data/update.sql +++ b/Data/update.sql @@ -924,7 +924,7 @@ ADD COLUMN `settlement_begin_time` int(10) NOT NULL DEFAULT 0 COMMENT '结算开 ALTER TABLE `tab_withdraw` ADD COLUMN `op_id` int(11) NOT NULL DEFAULT 0 COMMENT '操作id'; ALTER TABLE `tab_withdraw` -ADD COLUMN `op_type` tinyint(3) NOT NULL DEFAULT 1 COMMENT '操作人:1-推广员 2-管理员'; +ADD COLUMN `op_type` tinyint(3) NOT NULL DEFAULT 1 COMMENT '操作人:1-推广员 2-管理员 3-自动'; ALTER TABLE `tab_withdraw` ADD COLUMN `last_op_id` int(11) NOT NULL DEFAULT 0 COMMENT '最后操作人id', -ADD COLUMN `last_op_type` tinyint(3) NOT NULL DEFAULT 1 COMMENT '最后操作人:1-推广员 2-管理员'; \ No newline at end of file +ADD COLUMN `last_op_type` tinyint(3) NOT NULL DEFAULT 1 COMMENT '最后操作人:1-推广员 2-管理员 3-自动'; \ No newline at end of file