You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/store/model/recharge/Order.php

149 lines
5.7 KiB

<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\store\model\recharge;
use app\common\model\recharge\Order as OrderModel;
use app\common\enum\order\refund\AuditStatus as AuditStatusEnum;
use app\common\enum\payment\Method as PaymentMethodEnum;
use app\common\enum\recharge\order\PayStatus as PayStatusEnum;
use app\api\model\User as UserModel;
use app\common\enum\user\balanceLog\Scene as SceneEnum;
use app\api\model\user\BalanceLog as BalanceLogModel;
use app\common\model\UploadFile;
/**
* 用户充值订单模型
* Class Order
* @package app\store\model\recharge
*/
class Order extends OrderModel
{
/**
* 获取订单列表
* @param array $param
* @return \think\Paginator
*/
public function getList(array $param = []): \think\Paginator
{
// 设置查询条件
$filter = $this->getFilter($param);
// 获取列表数据
$list = $this->alias('order')
->field('order.*')
->where($filter)
->join('user', 'user.user_id = order.user_id')
->order(['order.create_time' => 'desc'])
->paginate(15);
// 加载订单的关联数据
return static::preload($list, ['user.avatar', 'order_plan']);
}
/**
* 设置查询条件
* @param array $param
* @return array
*/
private function getFilter(array $param): array
{
// 设置默认的检索数据
$params = $this->setQueryDefaultValue($param, [
'user_id' => 0, // 用户ID
'search' => '', // 查询内容
'recharge_type' => 0, // 充值方式
'pay_status' => 0, // 支付状态
'betweenTime' => [] // 起止时间
]);
// 检索查询条件
$filter = [];
// 用户ID
$params['user_id'] > 0 && $filter[] = ['order.user_id', '=', $params['user_id']];
// 用户昵称/订单号
!empty($params['search']) && $filter[] = ['order.order_no|user.nick_name', 'like', "%{$params['search']}%"];
// 充值方式
$params['recharge_type'] > 0 && $filter[] = ['order.recharge_type', '=', (int)$params['recharge_type']];
// 支付状态
$params['pay_status'] > 0 && $filter[] = ['order.pay_status', '=', (int)$params['pay_status']];
// 起止时间
if (!empty($params['betweenTime'])) {
$times = between_time($params['betweenTime']);
$filter[] = ['order.pay_time', '>=', $times['start_time']];
$filter[] = ['order.pay_time', '<', $times['end_time'] + 86400];
}
return $filter;
}
/**
* 获取售后单详情
* @param int $rechargeOrderId
* @return OrderRefund|false|null
*/
public function getDetail(int $rechargeOrderId)
{
$detail = static::detail($rechargeOrderId, [
'user','orderPlan',
]) ?: false;
if (!$detail) {
return $detail;
}
if ($detail->upload_images) {
$img_ids = json_decode($detail->upload_images, true);
$files = UploadFile::getFileList($img_ids);
$detail->upload_images = !$files->isEmpty() ? $files->toArray() : [];
}
return $detail;
}
/**
* 商家审核
* @param array $data
* @return bool
*/
public function audit(int $rechargeOrderId, array $data): bool
{
if ($data['audit_status'] == AuditStatusEnum::REJECTED && empty($data['refuse_desc'])) {
$this->error = '请输入拒绝原因';
return false;
}
$this->transaction(function () use ($data, $rechargeOrderId) {
if ($data['audit_status'] == AuditStatusEnum::REJECTED) {
$data['pay_status'] = PayStatusEnum::PENDING;
} elseif ($data['audit_status'] == AuditStatusEnum::REVIEWED) {
$data['pay_status'] = PayStatusEnum::SUCCESS;
$data['pay_method'] = PaymentMethodEnum::VOUCHER;
$data['pay_time'] = time();
}
// var_dump($data);
// exit();
// 更新售后单状态
$this->save($data);
// 同意售后申请, 记录退货地址
if ($data['audit_status'] == AuditStatusEnum::REVIEWED) {
$orderInfo = self::detail($rechargeOrderId);
//新增用户充值记录和给用户加余额
// 累积用户余额
UserModel::setIncBalance((int)$orderInfo['user_id'], (float)$orderInfo['actual_money']);
// 用户余额变动明细
BalanceLogModel::add(SceneEnum::RECHARGE, [
'user_id' => $orderInfo['user_id'],
'money' => $orderInfo['actual_money'],
'store_id' => empty($orderInfo['store_id'])?$this->getStoreId():$orderInfo['store_id'],
], ['order_no' => $orderInfo['order_no']],$orderInfo['store_id']);
}
});
return true;
}
}