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.
248 lines
9.0 KiB
248 lines
9.0 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\api\controller\apiController;
|
|
|
|
use app\Request;
|
|
use app\service\api\apiService\CampusService;
|
|
use app\service\api\apiService\ChatService;
|
|
use app\service\api\apiService\CommonService;
|
|
use app\service\api\apiService\CourseService;
|
|
use app\service\api\apiService\OrderTableService;
|
|
use core\base\BaseApiService;
|
|
|
|
/**
|
|
* 订单-控制器相关接口
|
|
* Class Personnel
|
|
* @package app\api\controller\apiController
|
|
*/
|
|
class OrderTable extends BaseApiService
|
|
{
|
|
|
|
//订单-列表
|
|
public function index(Request $request)
|
|
{
|
|
$resource_id = $request->param('resource_id', '');//客户资源表school_customer_resources表id
|
|
$staff_id = $request->param('staff_id', '');//员工表school_personnel表id
|
|
$student_id = $request->param('student_id', '');//学生表school_student表id
|
|
|
|
// 至少需要一个查询条件
|
|
if (empty($resource_id) && empty($staff_id) && empty($student_id)) {
|
|
return fail('缺少查询参数');
|
|
}
|
|
|
|
$where = [
|
|
'resource_id' => $resource_id,
|
|
'staff_id' => $staff_id,
|
|
'student_id' => $student_id,
|
|
];
|
|
|
|
$res = (new OrderTableService())->getList($where);
|
|
|
|
return success($res);
|
|
}
|
|
|
|
//订单-详情
|
|
public function info(Request $request)
|
|
{
|
|
$resource_id = $request->param('resource_id', '');//客户资源表school_customer_resources表id(两个参数2选1)
|
|
$staff_id = $request->param('staff_id', '');//员工表school_personnel表id(两个参数2选1)
|
|
if (empty($resource_id) && empty($staff_id)) {
|
|
return fail('缺少参数');
|
|
}
|
|
|
|
$where = [
|
|
'resource_id' => $resource_id,
|
|
'staff_id' => $staff_id,
|
|
];
|
|
|
|
$res = (new OrderTableService())->getInfo($where);
|
|
|
|
if (!$res['code']) {
|
|
return fail($res['msg']);
|
|
}
|
|
|
|
return success($res['data']);
|
|
}
|
|
|
|
//订单-创建
|
|
public function add(Request $request)
|
|
{
|
|
// 获取当前登录的员工ID
|
|
$staff_id = $request->memberId() ?? 0;
|
|
$params = $request->params([
|
|
["payment_type", ""], // 付款类型必填验证
|
|
["course_id", ""], // 课程ID必填验证
|
|
["class_id", ""], // 班级ID必填验证
|
|
["staff_id", ""], // 员工ID(可选)
|
|
["resource_id", ""], // 客户资源表ID必填验证
|
|
["order_type", ""], // 订单类型必填验证
|
|
["student_id", ""], // 学生ID必填验证
|
|
["order_amount", ""], // 订单金额(可选,会从课程获取)
|
|
["gift_id", ""], // 赠品ID(可选)
|
|
["gift_type", ""], // 赠品核销类型(可选):1-减现, 2-赠课
|
|
["remark", ""] // 备注(可选)
|
|
]);
|
|
|
|
// 验证必要参数
|
|
$missing_params = [];
|
|
if(empty($params['payment_type'])) $missing_params[] = 'payment_type(支付方式)';
|
|
if(empty($params['course_id'])) $missing_params[] = 'course_id(课程ID)';
|
|
if(empty($params['class_id'])) $missing_params[] = 'class_id(班级ID)';
|
|
if(empty($params['resource_id'])) $missing_params[] = 'resource_id(客户资源ID)';
|
|
if(empty($params['order_type'])) $missing_params[] = 'order_type(订单类型)';
|
|
if(empty($params['student_id'])) $missing_params[] = 'student_id(学生ID)';
|
|
|
|
if(!empty($missing_params)) {
|
|
return fail('缺少必要参数: ' . implode(', ', $missing_params));
|
|
}
|
|
|
|
// 验证赠品相关参数
|
|
if (!empty($params['gift_id']) && empty($params['gift_type'])) {
|
|
return fail('选择赠品时必须指定核销类型');
|
|
}
|
|
|
|
if (!empty($params['gift_type']) && !in_array($params['gift_type'], ['1', '2'])) {
|
|
return fail('无效的赠品核销类型,只能是1(减现)或2(赠课)');
|
|
}
|
|
|
|
// 如果前端没提供员工ID,使用当前登录的员工ID
|
|
if(empty($params['staff_id'])) {
|
|
if(empty($staff_id)) {
|
|
return fail('无法获取员工信息');
|
|
}
|
|
$params['staff_id'] = $staff_id;
|
|
}
|
|
|
|
// 获取班级信息以查询campus_id
|
|
$class = \app\model\class_grade\ClassGrade::where('id', $params['class_id'])->find();
|
|
if (!$class) {
|
|
return fail('班级不存在');
|
|
}
|
|
$class = $class->toArray();
|
|
$campus_id = $class['campus_id'] ?? 0;
|
|
|
|
if(empty($campus_id)) {
|
|
return fail('班级没有关联校区');
|
|
}
|
|
|
|
$course = \app\model\course\Course::where('id', $params['course_id'])->find();
|
|
if (!$course) {
|
|
return fail('课程不存在');
|
|
}
|
|
$course = $course->toArray();
|
|
$order_amount = $course['price'];//课程的价格
|
|
|
|
$data = [
|
|
'payment_type' => $params['payment_type'],//付款类型: cash-现金支付, scan_code-扫码支付, subscription-订阅支付
|
|
'order_amount' => $order_amount,//订单金额
|
|
'course_id' => $params['course_id'],//课程ID
|
|
'class_id' => $params['class_id'],//班级ID
|
|
'staff_id' => $params['staff_id'],//员工表ID
|
|
'resource_id' => $params['resource_id'],//客户资源表id
|
|
'campus_id' => $campus_id,//校区ID
|
|
'order_type' => $params['order_type'],
|
|
'student_id' => $params['student_id'],//学生ID
|
|
'gift_id' => !empty($params['gift_id']) ? $params['gift_id'] : null,//赠品ID
|
|
'gift_type' => !empty($params['gift_type']) ? $params['gift_type'] : null,//赠品核销类型:1-减现, 2-赠课
|
|
'remark' => $params['remark'],//备注
|
|
'order_status' => 'pending',//订单状态,默认为待支付
|
|
];
|
|
|
|
$res = (new OrderTableService())->addData($data);
|
|
|
|
if (!$res['code']) {
|
|
return fail($res['msg']);
|
|
}
|
|
|
|
return success([]);
|
|
}
|
|
|
|
//订单-更新支付状态
|
|
public function updatePaymentStatus(Request $request)
|
|
{
|
|
$params = $request->params([
|
|
["order_id", ""], // 订单ID必填
|
|
["order_status", ""], // 订单状态必填: pending-待支付, paid-已支付, partial-部分支付, cancelled-已取消
|
|
["payment_id", ""], // 支付单号(可选)
|
|
]);
|
|
|
|
$order = new \app\model\order_table\OrderTable();
|
|
|
|
$info = $order->where(['id' => $params['order_id']])->find();
|
|
|
|
if(!$params['order_status']){
|
|
$params['order_status'] = $info['order_status'] == 'pending' ? 'paid' : $info['order_status'];
|
|
}
|
|
// 验证必要参数
|
|
if(empty($params['order_id']) || empty($params['order_status'])) {
|
|
return fail('缺少必要参数');
|
|
}
|
|
|
|
// 验证订单状态值
|
|
$allowedStatus = ['pending', 'paid', 'partial', 'cancelled', 'completed', 'refunded'];
|
|
if(!in_array($params['order_status'], $allowedStatus)) {
|
|
return fail('无效的订单状态');
|
|
}
|
|
|
|
$res = (new OrderTableService())->updatePaymentStatus($params);
|
|
|
|
if (!$res['code']) {
|
|
return fail($res['msg']);
|
|
}
|
|
|
|
return success($res['data']);
|
|
}
|
|
|
|
//查询订单支付状态
|
|
public function checkOrderPaymentStatus(Request $request)
|
|
{
|
|
$order_no = $request->param('order_no', ''); // 订单号
|
|
|
|
if (empty($order_no)) {
|
|
return fail('缺少订单号参数');
|
|
}
|
|
|
|
// 查询订单状态
|
|
$order = \app\model\order_table\OrderTable::where('payment_id', $order_no)->find();
|
|
|
|
if (!$order) {
|
|
return fail('订单不存在');
|
|
}
|
|
|
|
$orderData = $order->toArray();
|
|
|
|
return success([
|
|
'order_id' => $orderData['id'],
|
|
'order_no' => $order_no,
|
|
'order_status' => $orderData['order_status'],
|
|
'order_amount' => $orderData['order_amount'],
|
|
'payment_type' => $orderData['payment_type'],
|
|
'updated_at' => $orderData['updated_at']
|
|
]);
|
|
}
|
|
|
|
public function updateOrderPaymentVoucher(Request $request)
|
|
{
|
|
$params = $request->params([
|
|
["order_id", ""], // 订单ID必填
|
|
["payment_voucher", ""], // 支付凭证
|
|
]);
|
|
|
|
$res = (new OrderTableService())->updateOrderPaymentVoucher($params);
|
|
|
|
if (!$res['code']) {
|
|
return fail($res['msg']);
|
|
}
|
|
|
|
return success($res['data']);
|
|
}
|
|
}
|
|
|