490 lines
20 KiB
PHP
490 lines
20 KiB
PHP
<?php
|
||
|
||
namespace app\admin\model;
|
||
|
||
use app\api\model\UserMoney;
|
||
use app\common\library\DatabaseRoute;
|
||
use think\facade\Db;
|
||
use think\Model;
|
||
|
||
|
||
class Order extends Model
|
||
{
|
||
|
||
public static function selectOrdersCountStatisticsByYear($flag, $time, $status)
|
||
{
|
||
return DatabaseRoute::getAllDbData('orders', function ($query)use($flag, $time, $status) {
|
||
// 添加状态条件
|
||
if (!is_null($status) && $status != 0) {
|
||
$query->where('status', $status);
|
||
}
|
||
// 添加时间条件
|
||
if (!is_null($flag)) {
|
||
switch ($flag) {
|
||
case 1:
|
||
// 按日匹配(精确到天)
|
||
$query->whereRaw("date_format(create_time, '%Y-%m-%d') = date_format(?, '%Y-%m-%d')", [$time]);
|
||
break;
|
||
case 2:
|
||
// 按月匹配(精确到月)
|
||
$query->whereRaw("date_format(create_time, '%Y-%m') = date_format(?, '%Y-%m')", [$time]);
|
||
break;
|
||
case 3:
|
||
// 按年匹配(精确到年)
|
||
$query->whereRaw("date_format(create_time, '%Y') = date_format(?, '%Y')", [$time]);
|
||
break;
|
||
default:
|
||
// 无效flag,不添加时间条件
|
||
break;
|
||
}
|
||
}
|
||
return $query;
|
||
})->count();
|
||
}
|
||
|
||
|
||
public static function selectFenXiaoMoney($type, $sysUserId, $flag, $time)
|
||
{
|
||
$result = DatabaseRoute::getAllDbData('orders', function($query)use($type, $sysUserId, $flag, $time) {
|
||
$query->where('status', 1); // 固定条件:订单状态为1(已完成)
|
||
// 2. 根据类型设置聚合字段(处理null值为0.00)
|
||
switch ($type) {
|
||
case 1:
|
||
$query->field('sum(ifnull(one_money, 0.00)) as total'); // 一级分销金额
|
||
break;
|
||
case 2:
|
||
$query->field('sum(ifnull(two_money, 0.00)) as total'); // 二级分销金额
|
||
break;
|
||
case 3:
|
||
$query->field('sum(ifnull(qd_money, 0.00)) as total'); // 渠道分销金额
|
||
break;
|
||
default:
|
||
throw new \InvalidArgumentException("无效的金额类型:{$type}");
|
||
}
|
||
|
||
// 3. 添加系统用户ID条件
|
||
if (!is_null($sysUserId)) {
|
||
$query->where('sys_user_id', $sysUserId);
|
||
}
|
||
|
||
// 4. 添加时间条件
|
||
if (!is_null($flag)) {
|
||
switch ($flag) {
|
||
case 1:
|
||
// 按日匹配
|
||
$query->whereRaw("date_format(create_time, '%Y-%m-%d') = date_format(?, '%Y-%m-%d')", [$time]);
|
||
break;
|
||
case 2:
|
||
// 按月匹配
|
||
$query->whereRaw("date_format(create_time, '%Y-%m') = date_format(?, '%Y-%m')", [$time]);
|
||
break;
|
||
case 3:
|
||
// 按年匹配
|
||
$query->whereRaw("date_format(create_time, '%Y') = date_format(?, '%Y')", [$time]);
|
||
break;
|
||
default:
|
||
throw new \InvalidArgumentException("无效的时间标志:{$flag}");
|
||
}
|
||
}
|
||
return $query;
|
||
})->find();
|
||
return $result['total'] ?? 0.00;
|
||
}
|
||
|
||
|
||
|
||
public static function selectOrdersCount($status, $orderType, $flag, $time, $sysUserId)
|
||
{
|
||
return DatabaseRoute::getAllDbData('orders', function($query)use($status, $orderType, $flag, $time, $sysUserId) {
|
||
$query->where('pay_way', 9);
|
||
// 添加系统用户ID条件(排除sysUserId=1的情况)
|
||
if (!is_null($sysUserId) && $sysUserId != 1) {
|
||
$query->where('sys_user_id', $sysUserId);
|
||
}
|
||
|
||
// 添加订单状态条件
|
||
if (!is_null($status)) {
|
||
$query->where('status', $status);
|
||
}
|
||
|
||
// 添加订单类型条件
|
||
if (!is_null($orderType)) {
|
||
$query->where('orders_type', $orderType);
|
||
}
|
||
|
||
// 添加时间条件
|
||
if (!is_null($flag)) {
|
||
switch ($flag) {
|
||
case 1:
|
||
// 按日匹配(精确到天)
|
||
$query->whereRaw("date_format(create_time, '%Y-%m-%d') = date_format(?, '%Y-%m-%d')", [$time]);
|
||
break;
|
||
case 2:
|
||
// 按月匹配(精确到月)
|
||
$query->whereRaw("date_format(create_time, '%Y-%m') = date_format(?, '%Y-%m')", [$time]);
|
||
break;
|
||
case 3:
|
||
// 按年匹配(精确到年)
|
||
$query->whereRaw("date_format(create_time, '%Y') = date_format(?, '%Y')", [$time]);
|
||
break;
|
||
default:
|
||
// 无效flag,不添加时间条件
|
||
break;
|
||
}
|
||
}
|
||
|
||
return $query;
|
||
})->count();
|
||
}
|
||
public static function selectOrdersMoney($status, $orderType, $flag, $time, $courseId, $sysUserId)
|
||
{
|
||
$result = DatabaseRoute::getAllDbData('orders', function($query)use($status, $orderType, $flag, $time, $sysUserId, $courseId) {
|
||
// 初始化查询
|
||
$query->where('pay_way', 9) // 固定条件:支付方式为9
|
||
->where('status', 1) // 固定条件:订单状态为1
|
||
->field('sum(pay_money) as total'); // 聚合计算总支付金额
|
||
|
||
// 添加系统用户ID条件
|
||
if (!is_null($sysUserId) && $sysUserId != 1) {
|
||
$query->where('sys_user_id', $sysUserId);
|
||
}
|
||
|
||
// 添加订单状态条件
|
||
if (!is_null($status)) {
|
||
$query->where('status', $status);
|
||
}
|
||
|
||
// 添加课程ID条件
|
||
if (!is_null($courseId)) {
|
||
$query->where('course_id', $courseId);
|
||
}
|
||
|
||
// 添加订单类型条件
|
||
if (!is_null($orderType)) {
|
||
$query->where('orders_type', $orderType);
|
||
}
|
||
|
||
// 添加时间条件
|
||
if (!is_null($flag)) {
|
||
switch ($flag) {
|
||
case 1:
|
||
// 按日匹配(精确到天)
|
||
$query->whereRaw("date_format(create_time, '%Y-%m-%d') = date_format(?, '%Y-%m-%d')", [$time]);
|
||
break;
|
||
case 2:
|
||
// 按月匹配(精确到月)
|
||
$query->whereRaw("date_format(create_time, '%Y-%m') = date_format(?, '%Y-%m')", [$time]);
|
||
break;
|
||
case 3:
|
||
// 按年匹配(精确到年)
|
||
$query->whereRaw("date_format(create_time, '%Y') = date_format(?, '%Y')", [$time]);
|
||
break;
|
||
default:
|
||
// 无效flag,不添加时间条件
|
||
break;
|
||
}
|
||
}
|
||
return $query;
|
||
})->find();
|
||
return (float)$result['total'] ?? 0.00;
|
||
}
|
||
public static function selectSignInAwardMoney($flag, $time, $sysUserId)
|
||
{
|
||
$db = Db::connect(get_slave_connect_name());
|
||
// 初始化查询
|
||
$query = $db->name('v_user_money_detail_temp')
|
||
->alias('x')
|
||
->where('x.classify', 6) // 固定条件:分类为6
|
||
->where('x.type', 1) // 固定条件:类型为1
|
||
->where('x.state', 2) // 固定条件:状态为2
|
||
->where('x.user_id', '<>', 1) // 固定条件:用户ID不为1
|
||
->where('x.title', '签到奖励') // 固定条件:标题为"签到奖励"
|
||
->field('ifnull(sum(x.money), 0) as total'); // 聚合计算总金额
|
||
|
||
// 添加系统用户ID条件
|
||
if (!is_null($sysUserId) && $sysUserId != 1) {
|
||
$query = $query->where('x.sys_user_id', $sysUserId);
|
||
}
|
||
// 添加时间条件
|
||
if (!is_null($flag)) {
|
||
switch ($flag) {
|
||
case 1:
|
||
// 按日匹配(精确到天)
|
||
$query = $query->whereRaw("date_format(x.create_time, '%Y-%m-%d') = date_format(?, '%Y-%m-%d')", [$time]);
|
||
break;
|
||
case 2:
|
||
// 按月匹配(精确到月)
|
||
$query = $query->whereRaw("date_format(x.create_time, '%Y-%m') = date_format(?, '%Y-%m')", [$time]);
|
||
break;
|
||
case 3:
|
||
// 按年匹配(精确到年)
|
||
$query = $query->whereRaw("date_format(x.create_time, '%Y') = date_format(?, '%Y')", [$time]);
|
||
break;
|
||
default:
|
||
// 无效flag,不添加时间条件
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 执行查询并返回结果
|
||
$result = $query->find();
|
||
return (float)$result['total'];
|
||
}
|
||
|
||
public static function selectShareAwardMoney($flag, $time, $sysUserId)
|
||
{
|
||
$db = Db::connect(get_slave_connect_name());
|
||
// 初始化查询
|
||
$query = $db->name('v_user_money_detail_temp')
|
||
->alias('x')
|
||
->where('x.classify', 6) // 固定条件:分类为6
|
||
->where('x.type', 1) // 固定条件:类型为1
|
||
->where('x.state', 2) // 固定条件:状态为2
|
||
->where('x.user_id', '<>', 1) // 固定条件:用户ID不为1
|
||
->where('x.title', '分享达标奖励') // 固定条件:标题为"分享达标奖励"
|
||
->field('ifnull(sum(x.money), 0) as total'); // 聚合计算总金额
|
||
|
||
// 添加系统用户ID条件
|
||
if (!is_null($sysUserId) && $sysUserId != 1) {
|
||
$query->where('x.sys_user_id', $sysUserId);
|
||
}
|
||
|
||
// 添加时间条件
|
||
if (!is_null($flag)) {
|
||
switch ($flag) {
|
||
case 1:
|
||
// 按日匹配(精确到天)
|
||
$query->whereRaw("date_format(x.create_time, '%Y-%m-%d') = date_format(?, '%Y-%m-%d')", [$time]);
|
||
break;
|
||
case 2:
|
||
// 按月匹配(精确到月)
|
||
$query->whereRaw("date_format(x.create_time, '%Y-%m') = date_format(?, '%Y-%m')", [$time]);
|
||
break;
|
||
case 3:
|
||
// 按年匹配(精确到年)
|
||
$query->whereRaw("date_format(x.create_time, '%Y') = date_format(?, '%Y')", [$time]);
|
||
break;
|
||
default:
|
||
// 无效flag,不添加时间条件
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 执行查询并返回结果
|
||
$result = $query->find();
|
||
return (float)$result['total'];
|
||
}
|
||
|
||
public static function selectNewUserTaskDoneAwardMoney($flag, $time, $sysUserId)
|
||
{
|
||
$db = Db::connect(get_slave_connect_name());
|
||
// 初始化查询
|
||
$query = $db->name('v_user_money_detail_temp')
|
||
->alias('x')
|
||
->where('x.classify', 7) // 固定条件:分类为7
|
||
->where('x.type', 1) // 固定条件:类型为1
|
||
->where('x.state', 2) // 固定条件:状态为2
|
||
->where('x.user_id', '<>', 1) // 固定条件:用户ID不为1
|
||
->where('x.money_type', 1) // 新增条件:金额类型为1
|
||
->field('ifnull(sum(x.money), 0) as total'); // 聚合计算总金额
|
||
|
||
// 添加系统用户ID条件
|
||
if (!is_null($sysUserId) && $sysUserId != 1) {
|
||
$query->where('x.sys_user_id', $sysUserId);
|
||
}
|
||
|
||
// 添加时间条件
|
||
if (!is_null($flag)) {
|
||
switch ($flag) {
|
||
case 1:
|
||
// 按日匹配(精确到天)
|
||
$query->whereRaw("date_format(x.create_time, '%Y-%m-%d') = date_format(?, '%Y-%m-%d')", [$time]);
|
||
break;
|
||
case 2:
|
||
// 按月匹配(精确到月)
|
||
$query->whereRaw("date_format(x.create_time, '%Y-%m') = date_format(?, '%Y-%m')", [$time]);
|
||
break;
|
||
case 3:
|
||
// 按年匹配(精确到年)
|
||
$query->whereRaw("date_format(x.create_time, '%Y') = date_format(?, '%Y')", [$time]);
|
||
break;
|
||
default:
|
||
// 无效flag,不添加时间条件
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 执行查询并返回结果
|
||
$result = $query->find();
|
||
return (float)$result['total'];
|
||
}
|
||
|
||
|
||
public static function selectInviteTaskDoneAwardMoney($flag, $time, $sysUserId)
|
||
{
|
||
$db = Db::connect(get_slave_connect_name());
|
||
// 初始化查询
|
||
$query = $db->name('v_user_money_detail_temp')
|
||
->alias('x')
|
||
->where('x.classify', 6) // 固定条件:分类为6
|
||
->where('x.type', 1) // 固定条件:类型为1
|
||
->where('x.state', 2) // 固定条件:状态为2
|
||
->where('x.user_id', '<>', 1) // 固定条件:用户ID不为1
|
||
->where('x.title', '[分享达标额外奖励]') // 固定条件:标题为"[分享达标额外奖励]"
|
||
->field('ifnull(sum(x.money), 0) as total'); // 聚合计算总金额
|
||
|
||
// 添加系统用户ID条件
|
||
if (!is_null($sysUserId) && $sysUserId != 1) {
|
||
$query->where('x.sys_user_id', $sysUserId);
|
||
}
|
||
|
||
// 添加时间条件
|
||
if (!is_null($flag)) {
|
||
switch ($flag) {
|
||
case 1:
|
||
// 按日匹配(精确到天)
|
||
$query->whereRaw("date_format(x.create_time, '%Y-%m-%d') = date_format(?, '%Y-%m-%d')", [$time]);
|
||
break;
|
||
case 2:
|
||
// 按月匹配(精确到月)
|
||
$query->whereRaw("date_format(x.create_time, '%Y-%m') = date_format(?, '%Y-%m')", [$time]);
|
||
break;
|
||
case 3:
|
||
// 按年匹配(精确到年)
|
||
$query->whereRaw("date_format(x.create_time, '%Y') = date_format(?, '%Y')", [$time]);
|
||
break;
|
||
default:
|
||
// 无效flag,不添加时间条件
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 执行查询并返回结果
|
||
$result = $query->find();
|
||
return (float)$result['total'];
|
||
}
|
||
|
||
public static function executeExtractCallback(mixed $cashOut, ?array $baseResp)
|
||
{
|
||
$state = 0;
|
||
if (!empty($baseResp['status']) && $baseResp['status'] == 2) {
|
||
$state = 1;
|
||
}else if (!empty($baseResp['status']) && ($baseResp['status'] == 3 || $baseResp['status'] == 99999)) {
|
||
$state = 2;
|
||
}
|
||
|
||
$reason = '';
|
||
if (!empty($baseResp['reason']) && $baseResp['reason'] == '已驳回') {
|
||
$reason = '提现失败,请检查收款账号与收款人姓名后重试。';
|
||
}
|
||
|
||
if ($state == 1) {
|
||
$cashOut['state'] = 1;
|
||
$cashOut['out_at'] = getNormalDate();
|
||
$cashOut['refund'] = null;
|
||
DatabaseRoute::getDb('cash_out', $cashOut['user_id'], true)->where([
|
||
'id' => $cashOut['id']
|
||
])->update($cashOut);
|
||
|
||
$userMoney = UserMoney::selectUserMoneyfind($cashOut['user_id']);
|
||
if ($userMoney) {
|
||
DatabaseRoute::getDb('user_money', $cashOut['user_id'], true)->where([
|
||
'id' => $userMoney['id']
|
||
])->update([
|
||
'cash_count' => $userMoney['cash_count'] + 1,
|
||
'cash_amount' => $userMoney['cash_amount'] ?? 0 + $cashOut['amount'],
|
||
]);
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
if ($state == 2) {
|
||
// 设置失败状态
|
||
$cashOut['out_at'] = date('Y-m-d H:i:s');
|
||
$cashOut['state'] = 2;
|
||
$cashOut['refund'] = $reason;
|
||
|
||
// 企业用户
|
||
if ($cashOut['user_type'] === 2) {
|
||
// 判断是否已经返还过
|
||
$count = DatabaseRoute::getDb('sys_user_money_details', $cashOut['user_id'])->where([
|
||
'user_id' => $cashOut['user_id'],
|
||
'classify' => 4,
|
||
'state' => 2,
|
||
'money_type' => 1,
|
||
'type' => 1,
|
||
'source_id' => $cashOut['id'],
|
||
])->count();
|
||
|
||
if ($count > 0) {
|
||
return 0;
|
||
}
|
||
|
||
// 写入返还明细
|
||
DatabaseRoute::getDb('sys_user_money_details', $cashOut['user_id'], true)->insert([
|
||
'user_id' => $cashOut['user_id'],
|
||
'relation_id'=> $cashOut['user_id'],
|
||
'title' => '提现失败存入余额',
|
||
'classify' => 4,
|
||
'money_type' => 1,
|
||
'state' => 2,
|
||
'money' => $cashOut['money'],
|
||
'content' => '提现失败存入余额' . $cashOut['money'] . '元',
|
||
'type' => 1,
|
||
]);
|
||
|
||
// 更新余额
|
||
|
||
$userMoney = SysUserMoney::selectSysUserMoneyByUserId($cashOut['user_id']);
|
||
Db::name('sys_user_money')->where([
|
||
'id' => $userMoney['id']
|
||
])->dec('money', $cashOut['money'])->update();
|
||
|
||
} else {
|
||
// 普通用户
|
||
$count = DatabaseRoute::getDb('user_money_details', $cashOut['user_id'])->where([
|
||
'user_id' => $cashOut['user_id'],
|
||
'classify' => 4,
|
||
'state' => 2,
|
||
'money_type' => 1,
|
||
'type' => 1,
|
||
'source_id' => $cashOut['id'],
|
||
])->count();
|
||
|
||
if ($count > 0) {
|
||
return 0;
|
||
}
|
||
|
||
// 写入返还明细
|
||
DatabaseRoute::getDb('user_money_details', $cashOut['user_id'], true)->insert([
|
||
'user_id' => $cashOut['user_id'],
|
||
'title' => '提现失败存入余额',
|
||
'classify' => 4,
|
||
'money_type' => 1,
|
||
'state' => 2,
|
||
'money' => $cashOut['money'],
|
||
'content' => '提现失败存入余额' . $cashOut['money'] . '元',
|
||
'type' => 1,
|
||
'create_time'=> date('Y-m-d H:i:s'),
|
||
'source_id' => $cashOut['id'],
|
||
]);
|
||
|
||
// 更新余额
|
||
(new UserMoney())->updateAmount($cashOut['user_id'], $cashOut['money'], false);
|
||
}
|
||
|
||
// 更新提现记录
|
||
DatabaseRoute::getDb('cash_out', $cashOut['user_id'], true)->where('user_id', $cashOut->user_id)
|
||
->where('id', $cashOut['id'])
|
||
->update([
|
||
'out_at' => $cashOut['out_at'],
|
||
'state' => $cashOut['state'],
|
||
'refund' => $cashOut['refund'],
|
||
]);
|
||
}
|
||
|
||
return 0;
|
||
|
||
}
|
||
|
||
} |