|
|
<?php
|
|
|
|
|
|
namespace app\distribution\logic;
|
|
|
|
|
|
use app\distribution\model\DistributionCommissionLog;
|
|
|
use app\distribution\model\DistributionCommissionMy;
|
|
|
use app\distribution\model\DistributionCommissionWithdraw;
|
|
|
use app\distribution\model\DistributionConfig;
|
|
|
use app\distribution\model\DistributionUserRelation;
|
|
|
use think\facade\Db;
|
|
|
|
|
|
class Commission extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 获取我的佣金
|
|
|
* @date 2022-11-04
|
|
|
*/
|
|
|
public function getMyCommission()
|
|
|
{
|
|
|
$my_model = new DistributionCommissionMy();
|
|
|
$log_model = new DistributionCommissionLog();
|
|
|
|
|
|
// 获取当前用户分销佣金
|
|
|
$my = $my_model->getOneData([
|
|
|
['uid', '=', $this->mid],
|
|
|
['user_id', '=', $this->userId]
|
|
|
], 'commission,total_commission');
|
|
|
|
|
|
// 获取今日佣金数据
|
|
|
$my['today'] = $log_model->where([
|
|
|
['user_id', '=', $this->userId],
|
|
|
// type 包含在统计类型
|
|
|
['type', 'in', $log_model->getJoinStatisticsTypes()],
|
|
|
['commission', '>', 0],
|
|
|
// 今天一天
|
|
|
['create_time', 'between time', [date('Y-m-d', time()), date('Y-m-d', strtotime('+1 days'))]]
|
|
|
])->sum('commission');
|
|
|
|
|
|
// 获取最近7天佣金数据
|
|
|
$my['seven_day'] = $log_model->where([
|
|
|
['user_id', '=', $this->userId],
|
|
|
// type 包含在统计类型
|
|
|
['type', 'in', $log_model->getJoinStatisticsTypes()],
|
|
|
['commission', '>', 0],
|
|
|
// 近7天(包含今天)
|
|
|
['create_time', 'between time', [date('Y-m-d', strtotime('-6 days')), date('Y-m-d H:i:s', time())]]
|
|
|
])->sum('commission');
|
|
|
|
|
|
$my_commission = [
|
|
|
// 可提现佣金
|
|
|
'commission' => !empty($my['commission']) ? formatNumber($my['commission'], 2) : '0.00',
|
|
|
// 累计总获得
|
|
|
'total_commission' => !empty($my['total_commission']) ? formatNumber($my['total_commission'], 2) : '0.00',
|
|
|
// 今日获得
|
|
|
'today' => formatNumber($my['today'], 2),
|
|
|
// 近7天获得
|
|
|
'seven_day' => formatNumber($my['seven_day'], 2)
|
|
|
];
|
|
|
|
|
|
return sendSuccessArray([
|
|
|
// 我的佣金数据
|
|
|
'my' => $my_commission
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 插入提现订单
|
|
|
* @param float $withdraw_money 提现金额
|
|
|
* @param int $type 提现方式 1--提现到微信 2--提现到支付宝
|
|
|
* @date 2022-11-04
|
|
|
*/
|
|
|
public function insertWithdrawOrder($withdraw_money, $type)
|
|
|
{
|
|
|
$withdraw_model = new DistributionCommissionWithdraw();
|
|
|
$commission_service = new \app\distribution\service\Commission();
|
|
|
$config_model = new DistributionConfig();
|
|
|
|
|
|
|
|
|
// 提现设置
|
|
|
$config = $config_model->getOneData([
|
|
|
['uid', '=', $this->mid]
|
|
|
], 'withdraw_cost,is_need_withdraw_examine,withdraw_min');
|
|
|
|
|
|
// 最小提现金额限制
|
|
|
if ($withdraw_money < $config['withdraw_min']) {
|
|
|
return sendErrorArray(3001, '最小提现金额为' . $config['withdraw_min'] . '元');
|
|
|
}
|
|
|
|
|
|
// 检查用户信息是否完整
|
|
|
$res = $commission_service->checkInfo($this->userId, $type);
|
|
|
if ($res['code'] != 0) {
|
|
|
return $res;
|
|
|
}
|
|
|
|
|
|
Db::startTrans();
|
|
|
$order_number = $withdraw_model->createOrderNumber('C');
|
|
|
$data = [
|
|
|
'uid' => $this->mid,
|
|
|
'user_id' => $this->userId,
|
|
|
'user_agent' => $this->userAgent,
|
|
|
'order_number' => $order_number,
|
|
|
'withdraw_type' => $type,
|
|
|
// 提现金钱
|
|
|
'withdraw_money' => $withdraw_money,
|
|
|
// 提现手续费 %
|
|
|
'withdraw_cost' => $config['withdraw_cost'],
|
|
|
// 实际到账金额
|
|
|
'money' => round($withdraw_money * (1 - $config['withdraw_cost'] / 100), 2)
|
|
|
];
|
|
|
|
|
|
// 提现金额是否满足平台需求
|
|
|
if ($withdraw_money == 1 && $data['money'] < 1) {
|
|
|
return sendErrorArray(3002, '微信最小提现金额为1元');
|
|
|
} else if ($withdraw_money == 2 && $data['money'] < 0.1) {
|
|
|
return sendErrorArray(3003, '支付宝最小提现金额为0.1元');
|
|
|
}
|
|
|
|
|
|
// 更新提现表
|
|
|
$withdraw_id = $withdraw_model->dataUpdate($data);
|
|
|
if (!$withdraw_id) {
|
|
|
Db::rollback();
|
|
|
return sendErrorArray(3004, '提现失败');
|
|
|
}
|
|
|
|
|
|
// 更新用户佣金并记录明细
|
|
|
$res = $commission_service->change($this->userId, 7, -$withdraw_money, '用户申请提现', $order_number, [
|
|
|
'distribution_commission_withdraw_id' => $withdraw_id
|
|
|
]);
|
|
|
if ($res['code'] != 0) {
|
|
|
Db::rollback();
|
|
|
return $res;
|
|
|
}
|
|
|
Db::commit();
|
|
|
|
|
|
// 如果无需审核则自动提现成功
|
|
|
if ($config['is_need_withdraw_examine'] == 0) {
|
|
|
// 提现金额到账
|
|
|
$result = $commission_service->withdraw($withdraw_id);
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
return sendSuccessArray([], '提现申请已提交,请等待工作人员进行审核,后台审核通过后,将提现金额自动打款到选择的第三方账户。');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 我的提现记录
|
|
|
* @param int $page 第X页
|
|
|
* @date 2022-11-08
|
|
|
*/
|
|
|
public function listWithdraw($page)
|
|
|
{
|
|
|
$withdraw_model = new DistributionCommissionWithdraw();
|
|
|
|
|
|
$where = [
|
|
|
['uid', '=', $this->mid],
|
|
|
['user_id', '=', $this->userId]
|
|
|
];
|
|
|
$field = 'id,money,status,withdraw_type,create_time';
|
|
|
$list = $withdraw_model->listWithdrawWithPage($where, $page, $field, 'id desc', 50);
|
|
|
|
|
|
return sendSuccessArray([
|
|
|
// 提现列表
|
|
|
'list' => $list
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 我的佣金记录
|
|
|
* @param int $index 类型 0--全部 1--收入 2--支出
|
|
|
* @param int $page 第X页
|
|
|
* @date 2022-11-08
|
|
|
*/
|
|
|
public function listCommissionLog($index, $page)
|
|
|
{
|
|
|
$log_model = new DistributionCommissionLog();
|
|
|
|
|
|
$where = [
|
|
|
['uid', '=', $this->mid],
|
|
|
['user_id', '=', $this->userId]
|
|
|
];
|
|
|
if ($index == 1) {
|
|
|
$where[] = ['commission', '>', 0];
|
|
|
} else if ($index == 2) {
|
|
|
$where[] = ['commission', '<', 0];
|
|
|
}
|
|
|
$field = 'id,user_id,show_text,commission,after_commission,create_time';
|
|
|
$list = $log_model->listLogWithPage($where, $page, $field, 'id desc', 50);
|
|
|
|
|
|
return sendSuccessArray([
|
|
|
// 佣金明细记录列表
|
|
|
'list' => $list
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取上三级以及分销佣金
|
|
|
* @param float $money 产生分销关系的金钱
|
|
|
* @param int $user_id 产生分销关系的用户
|
|
|
* @param array $percent 分销比例
|
|
|
* @date 2021-03-01
|
|
|
*/
|
|
|
public function getCommission($money, $user_id, $percent = [])
|
|
|
{
|
|
|
$retation_model = new DistributionUserRelation();
|
|
|
|
|
|
// 查找上三级
|
|
|
$distibutor = $retation_model->getOneData([
|
|
|
['uid', '=', $this->mid],
|
|
|
['user_id', '=', $user_id]
|
|
|
], 'first_user_id,second_user_id,third_user_id');
|
|
|
|
|
|
// 定义初始化上三级以及分销佣金
|
|
|
$commission = [];
|
|
|
if (!empty($distibutor)) {
|
|
|
if (!empty($distibutor['first_user_id'])) {
|
|
|
$commission[0] = [
|
|
|
'user_id' => $distibutor['first_user_id'],
|
|
|
'commission' => !empty($percent['first_percent']) ? round($money * $percent['first_percent'] / 100, 2) : $percent['first_commission'],
|
|
|
];
|
|
|
}
|
|
|
if (!empty($distibutor['second_user_id'])) {
|
|
|
$commission[1] = [
|
|
|
'user_id' => $distibutor['second_user_id'],
|
|
|
'commission' => !empty($percent['second_percent']) ? round($money * $percent['second_percent'] / 100, 2) : $percent['second_commission'],
|
|
|
];
|
|
|
}
|
|
|
if (!empty($distibutor['third_user_id'])) {
|
|
|
$commission[2] = [
|
|
|
'user_id' => $distibutor['third_user_id'],
|
|
|
'commission' => !empty($percent['third_percent']) ? round($money * $percent['third_percent'] / 100, 2) : $percent['third_commission'],
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return $commission;
|
|
|
}
|
|
|
|
|
|
}
|