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.
298 lines
9.9 KiB
298 lines
9.9 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\service\api\apiService;
|
|
|
|
use app\model\campus\Campus;
|
|
use app\model\campus_person_role\CampusPersonRole;
|
|
use app\model\chat_friends\ChatFriends;
|
|
use app\model\chat_messages\ChatMessages;
|
|
use app\model\dict\Dict;
|
|
use app\model\order_table\OrderTable;
|
|
use core\base\BaseApiService;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 订单管理-控制器服务层
|
|
* Class MemberService
|
|
* @package app\service\api\member
|
|
*/
|
|
class OrderTableService extends BaseApiService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
//查询列表
|
|
public function getList(array $where)
|
|
{
|
|
$page_params = $this->getPageParam();//获取请求参数中的页码+分页数
|
|
$page = $page_params['page'];
|
|
$limit = $page_params['limit'];
|
|
|
|
$model = new OrderTable();
|
|
|
|
//员工表id
|
|
if (!empty($where['staff_id'])) {
|
|
$model = $model->where('staff_id', $where['staff_id']);
|
|
}
|
|
|
|
//客户资源表id
|
|
if (!empty($where['resource_id'])) {
|
|
$model = $model->where('resource_id', $where['resource_id']);
|
|
}
|
|
|
|
//学生表id
|
|
if (!empty($where['student_id'])) {
|
|
$model = $model->where('student_id', $where['student_id']);
|
|
}
|
|
|
|
$data = $model
|
|
->append([
|
|
'customerResources',
|
|
'course',
|
|
'classGrade',
|
|
'personnel',
|
|
'studentCourses' // 添加学员课程关联
|
|
])
|
|
->order('created_at','desc') // 使用created_at排序
|
|
->paginate([
|
|
'list_rows' => $limit,
|
|
'page' => $page,
|
|
])->toArray();
|
|
|
|
return $data;
|
|
}
|
|
|
|
//查询详情
|
|
public function getInfo(array $where)
|
|
{
|
|
$model = new OrderTable();
|
|
//判断用没有员工id
|
|
if (!empty($where['staff_id'])) {
|
|
$model = $model->where('staff_id', $where['staff_id']);
|
|
}
|
|
//判断用没有客户资源id
|
|
if (!empty($where['resource_id'])) {
|
|
$model = $model->where('resource_id', $where['resource_id']);
|
|
}
|
|
$data = $model
|
|
->append([
|
|
'customerResources',
|
|
'course',
|
|
'classGrade',
|
|
'personnel'
|
|
])
|
|
->find();
|
|
|
|
|
|
if ($data) {
|
|
$data = $data->toArray();
|
|
$res = [
|
|
'code' => 1,
|
|
'msg' => '操作成功',
|
|
'data' => $data
|
|
];
|
|
return $res;
|
|
} else {
|
|
$res = [
|
|
'code' => 0,
|
|
'msg' => '暂无数据',
|
|
'data' => []
|
|
];
|
|
return $res;
|
|
}
|
|
}
|
|
|
|
//创建订单
|
|
public function addData(array $data)
|
|
{
|
|
$success = OrderTable::create($data);
|
|
|
|
$res = [
|
|
'code' => 1,
|
|
'msg' => '操作成功',
|
|
'data' => []
|
|
];
|
|
if (!$success) {
|
|
$res = [
|
|
'code' => 0,
|
|
'msg' => '操作失败',
|
|
'data' => []
|
|
];
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
//更新订单支付状态
|
|
public function updatePaymentStatus(array $data)
|
|
{
|
|
try {
|
|
$order = OrderTable::where('id', $data['order_id'])->find();
|
|
if (!$order) {
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '订单不存在',
|
|
'data' => []
|
|
];
|
|
}
|
|
|
|
// 准备更新数据
|
|
$updateData = [
|
|
'order_status' => $data['order_status'],
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
];
|
|
|
|
// 如果提供了支付单号,则更新
|
|
if (!empty($data['payment_id'])) {
|
|
$updateData['payment_id'] = $data['payment_id'];
|
|
}
|
|
|
|
// 如果订单状态为已支付,记录支付时间
|
|
if ($data['order_status'] === 'paid') {
|
|
$updateData['payment_time'] = date('Y-m-d H:i:s');
|
|
}
|
|
|
|
$success = $order->save($updateData);
|
|
|
|
if ($success) {
|
|
// 如果订单状态变更为已支付,则自动为学员分配课程
|
|
if ($data['order_status'] === 'paid') {
|
|
$this->assignCourseToStudent($order->toArray());
|
|
}
|
|
|
|
return [
|
|
'code' => 1,
|
|
'msg' => '订单状态更新成功',
|
|
'data' => $order->toArray()
|
|
];
|
|
} else {
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '订单状态更新失败',
|
|
'data' => []
|
|
];
|
|
}
|
|
} catch (\Exception $e) {
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '更新订单状态异常: ' . $e->getMessage(),
|
|
'data' => []
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 支付成功后为学员分配课程
|
|
* @param array $orderData 订单数据
|
|
* @return bool
|
|
*/
|
|
private function assignCourseToStudent(array $orderData)
|
|
{
|
|
try {
|
|
$student_id = $orderData['student_id'];
|
|
$course_id = $orderData['course_id'];
|
|
$resource_id = $orderData['resource_id'];
|
|
|
|
if (empty($student_id) || empty($course_id)) {
|
|
\think\facade\Log::warning('学员分配课程失败:缺少学员ID或课程ID', $orderData);
|
|
return false;
|
|
}
|
|
|
|
// 获取课程信息
|
|
$course = \app\model\course\Course::where('id', $course_id)->find();
|
|
if (!$course) {
|
|
\think\facade\Log::warning('学员分配课程失败:课程不存在', ['course_id' => $course_id]);
|
|
return false;
|
|
}
|
|
$course = $course->toArray();
|
|
|
|
// 检查学员是否已有该课程记录
|
|
$existingCourse = Db::table('school_student_courses')
|
|
->where('student_id', $student_id)
|
|
->where('course_id', $course_id)
|
|
->find();
|
|
|
|
$now = date('Y-m-d H:i:s');
|
|
$start_date = date('Y-m-d');
|
|
$end_date = date('Y-m-d', strtotime('+' . $course['duration'] . ' days'));
|
|
|
|
if ($existingCourse) {
|
|
// 如果已有课程记录,累加课时数量
|
|
$updateData = [
|
|
'total_hours' => $existingCourse['total_hours'] + $course['session_count'],
|
|
'gift_hours' => $existingCourse['gift_hours'] + $course['gift_session_count'],
|
|
'updated_at' => $now
|
|
];
|
|
|
|
// 如果原有课程已过期,更新有效期
|
|
if ($existingCourse['end_date'] < $start_date) {
|
|
$updateData['start_date'] = $start_date;
|
|
$updateData['end_date'] = $end_date;
|
|
} else {
|
|
// 延长有效期
|
|
$updateData['end_date'] = date('Y-m-d',
|
|
strtotime($existingCourse['end_date'] . ' +' . $course['duration'] . ' days')
|
|
);
|
|
}
|
|
|
|
$result = Db::table('school_student_courses')
|
|
->where('id', $existingCourse['id'])
|
|
->update($updateData);
|
|
|
|
\think\facade\Log::info('学员课程更新成功', [
|
|
'student_id' => $student_id,
|
|
'course_id' => $course_id,
|
|
'added_hours' => $course['session_count'],
|
|
'added_gift_hours' => $course['gift_session_count']
|
|
]);
|
|
} else {
|
|
// 创建新的课程记录
|
|
$insertData = [
|
|
'student_id' => $student_id,
|
|
'course_id' => $course_id,
|
|
'total_hours' => $course['session_count'],
|
|
'gift_hours' => $course['gift_session_count'],
|
|
'start_date' => $start_date,
|
|
'end_date' => $end_date,
|
|
'use_total_hours' => 0,
|
|
'use_gift_hours' => 0,
|
|
'single_session_count' => $course['single_session_count'],
|
|
'status' => 1, // 激活状态
|
|
'resource_id' => $resource_id,
|
|
'created_at' => $now,
|
|
'updated_at' => $now
|
|
];
|
|
|
|
$result = Db::table('school_student_courses')->insert($insertData);
|
|
|
|
\think\facade\Log::info('学员课程创建成功', [
|
|
'student_id' => $student_id,
|
|
'course_id' => $course_id,
|
|
'total_hours' => $course['session_count'],
|
|
'gift_hours' => $course['gift_session_count'],
|
|
'start_date' => $start_date,
|
|
'end_date' => $end_date
|
|
]);
|
|
}
|
|
|
|
return $result ? true : false;
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('学员分配课程异常', [
|
|
'order_data' => $orderData,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|