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.
1094 lines
38 KiB
1094 lines
38 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 app\model\student\Student;
|
|
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)
|
|
{
|
|
try {
|
|
// 如果选择了赠品,验证赠品是否可用
|
|
if (!empty($data['gift_id'])) {
|
|
$giftValidation = $this->validateGift($data['gift_id'], $data['resource_id']);
|
|
if (!$giftValidation['valid']) {
|
|
return [
|
|
'code' => 0,
|
|
'msg' => $giftValidation['message'],
|
|
'data' => []
|
|
];
|
|
}
|
|
}
|
|
|
|
$success = OrderTable::create($data);
|
|
|
|
// 如果订单创建成功且使用了赠品,更新赠品状态
|
|
if ($success && !empty($data['gift_id'])) {
|
|
$this->updateGiftStatus($data['gift_id'], $success->id);
|
|
}
|
|
|
|
$res = [
|
|
'code' => 1,
|
|
'msg' => '操作成功',
|
|
'data' => ['order_id' => $success->id ?? null]
|
|
];
|
|
if (!$success) {
|
|
$res = [
|
|
'code' => 0,
|
|
'msg' => '操作失败',
|
|
'data' => []
|
|
];
|
|
}
|
|
return $res;
|
|
} catch (\Exception $e) {
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '创建订单异常: ' . $e->getMessage(),
|
|
'data' => []
|
|
];
|
|
}
|
|
}
|
|
|
|
//更新订单支付状态
|
|
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') {
|
|
$orderArray = $order->toArray();
|
|
$this->handlePaymentSuccess($orderArray);
|
|
}
|
|
|
|
return [
|
|
'code' => 1,
|
|
'msg' => '订单状态更新成功',
|
|
'data' => $order->toArray()
|
|
];
|
|
} else {
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '订单状态更新失败',
|
|
'data' => []
|
|
];
|
|
}
|
|
} catch (\Exception $e) {
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '更新订单状态异常: ' . $e->getMessage(),
|
|
'data' => []
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理支付成功后的业务
|
|
*/
|
|
public function handlePaymentSuccess(array $orderArray)
|
|
{
|
|
if($orderArray['payment_type'] !== 'deposit'){
|
|
// 1. 为学员分配课程
|
|
$this->assignCourseToStudent($orderArray);
|
|
|
|
// 2. 创建合同签署记录
|
|
$this->createContractSign($orderArray);
|
|
|
|
// 4.处理赠品
|
|
$this->handleGift($orderArray);
|
|
}
|
|
|
|
|
|
if($orderArray['payment_type'] == 'deposit'){
|
|
// 3. 创建支付记录
|
|
$this->createPaymentRecord($orderArray);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 支付成功后为学员分配课程
|
|
* @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();
|
|
Student::where('id', $student_id)->update(['status' => 1, 'pay_status' => 1]);
|
|
// 检查学员是否已有该课程记录
|
|
$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) {
|
|
// 如果已有课程记录,计算新课程的开始时间
|
|
// 新课程从现有课程结束后的第二天开始
|
|
$new_start_date = date('Y-m-d', strtotime($existingCourse['end_date'] . ' +1 day'));
|
|
$new_end_date = date('Y-m-d', strtotime($new_start_date . ' +' . $course['duration'] . ' days'));
|
|
|
|
// 创建新的课程记录
|
|
$insertData = [
|
|
'student_id' => $student_id,
|
|
'course_id' => $course_id,
|
|
'total_hours' => $course['session_count'],
|
|
'gift_hours' => $course['gift_session_count'],
|
|
'start_date' => $new_start_date,
|
|
'end_date' => $new_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, true);
|
|
|
|
// 更新订单表的课程计划ID
|
|
if ($result) {
|
|
OrderTable::where('id', $orderData['id'])->update(['course_plan_id' => $result]);
|
|
}
|
|
|
|
\think\facade\Log::info('学员课程新增成功', [
|
|
'student_id' => $student_id,
|
|
'course_id' => $course_id,
|
|
'existing_end_date' => $existingCourse['end_date'],
|
|
'new_start_date' => $new_start_date,
|
|
'new_end_date' => $new_end_date,
|
|
'new_hours' => $course['session_count'],
|
|
'new_gift_hours' => $course['gift_session_count'],
|
|
'course_plan_id' => $result
|
|
]);
|
|
} 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, true);
|
|
|
|
// 更新订单表的课程计划ID
|
|
if ($result) {
|
|
OrderTable::where('id', $orderData['id'])->update(['course_plan_id' => $result]);
|
|
}
|
|
|
|
\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,
|
|
'course_plan_id' => $result
|
|
]);
|
|
}
|
|
|
|
return $result ? true : false;
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('学员分配课程异常', [
|
|
'order_data' => $orderData,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 支付成功后创建合同签署记录
|
|
* @param array $orderData 订单数据
|
|
* @return bool
|
|
*/
|
|
private function createContractSign(array $orderData)
|
|
{
|
|
try {
|
|
$student_id = $orderData['student_id'];
|
|
$course_id = $orderData['course_id'];
|
|
|
|
if (empty($student_id) || empty($course_id)) {
|
|
\think\facade\Log::warning('创建合同签署记录失败:缺少学员ID或课程ID', $orderData);
|
|
return false;
|
|
}
|
|
|
|
// 获取课程的合同模板ID
|
|
$course = \app\model\course\Course::where('id', $course_id)->find();
|
|
if (!$course || empty($course['contract_id'])) {
|
|
\think\facade\Log::warning('创建合同签署记录失败:课程无合同模板', ['course_id' => $course_id]);
|
|
return false;
|
|
}
|
|
|
|
// 获取学员的user_id
|
|
$student = \app\model\student\Student::where('id', $student_id)->find();
|
|
if (!$student || empty($student['user_id'])) {
|
|
\think\facade\Log::warning('创建合同签署记录失败:学员无user_id', ['student_id' => $student_id]);
|
|
return false;
|
|
}
|
|
|
|
$now = date('Y-m-d H:i:s');
|
|
$insertData = [
|
|
'contract_id' => $course['contract_id'],
|
|
'personnel_id' => $student['user_id'], // 用户合同场景下存储学员的user_id
|
|
'student_id' => $student_id,
|
|
'status' => 1, // 默认状态
|
|
'source_type' => 'auto_course', // 来源类型:自动课程购买
|
|
'source_id' => $orderData['id'], // 订单ID
|
|
'type' => 2, // 标识为用户购买课程合同
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
'deleted_at' => 0
|
|
];
|
|
|
|
$result = Db::table('school_contract_sign')->insert($insertData, true);
|
|
|
|
if ($result) {
|
|
// 更新订单表的合同模板ID和合同签署记录ID
|
|
OrderTable::where('id', $orderData['id'])->update([
|
|
'contract_id' => $course['contract_id'], // 合同模板ID
|
|
'contract_sign_id' => $result // 合同签署记录ID
|
|
]);
|
|
// 创建签署记录成功后,为甲乙双方生成数据源配置记录
|
|
$this->createDocumentDataSourceConfig($course['contract_id'], $result, $orderData);
|
|
|
|
\think\facade\Log::info('合同签署记录创建成功', [
|
|
'student_id' => $student_id,
|
|
'contract_id' => $course['contract_id'],
|
|
'order_id' => $orderData['id']
|
|
]);
|
|
}
|
|
|
|
return $result ? true : false;
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('创建合同签署记录异常', [
|
|
'order_data' => $orderData,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 为甲乙双方创建文档数据源配置记录
|
|
* @param int $contract_id 合同模板ID
|
|
* @param int $contract_sign_id 合同签署记录ID
|
|
* @param array $orderData 订单数据
|
|
* @return bool
|
|
*/
|
|
private function createDocumentDataSourceConfig($contract_id, $contract_sign_id, $orderData)
|
|
{
|
|
try {
|
|
// 获取合同模板的占位符配置
|
|
$contract = Db::table('school_contract')->where('id', $contract_id)->find();
|
|
if (!$contract || empty($contract['placeholder_config'])) {
|
|
\think\facade\Log::warning('合同模板无占位符配置', ['contract_id' => $contract_id]);
|
|
return false;
|
|
}
|
|
|
|
$placeholder_config = json_decode($contract['placeholder_config'], true);
|
|
if (!is_array($placeholder_config)) {
|
|
\think\facade\Log::warning('占位符配置格式错误', ['contract_id' => $contract_id]);
|
|
return false;
|
|
}
|
|
|
|
$insert_data = [];
|
|
$now = date('Y-m-d H:i:s');
|
|
|
|
// 获取学员信息,用于数据库字段值处理
|
|
$student_info = $this->getStudentInfoForContract($orderData['student_id']);
|
|
if (!$student_info) {
|
|
\think\facade\Log::warning('获取学员信息失败', ['student_id' => $orderData['student_id']]);
|
|
return false;
|
|
}
|
|
|
|
\think\facade\Log::info('开始处理占位符配置', [
|
|
'contract_id' => $contract_id,
|
|
'config_structure' => array_keys($placeholder_config),
|
|
'config_count' => count($placeholder_config)
|
|
]);
|
|
|
|
// 处理JSON对象格式的占位符配置:{"占位符名": {配置}}
|
|
foreach ($placeholder_config as $placeholder_name => $config) {
|
|
// 确保$config是数组
|
|
if (!is_array($config)) {
|
|
\think\facade\Log::warning('跳过无效配置项', [
|
|
'placeholder' => $placeholder_name,
|
|
'config' => $config
|
|
]);
|
|
continue;
|
|
}
|
|
|
|
// 处理实际值:根据数据类型获取对应的值
|
|
$processed_value = $this->getProcessedFieldValue($config, $student_info, $orderData);
|
|
|
|
// 获取占位符的显示名称,如果配置中有label则使用label,否则使用键名
|
|
$placeholder_display_name = $config['label'] ?? $config['placeholder'] ?? $placeholder_name;
|
|
|
|
$data_source_record = [
|
|
'contract_id' => $contract_id,
|
|
'contract_sign_id' => $contract_sign_id, // 关联到具体的签署记录
|
|
'placeholder' => $placeholder_display_name, // 使用配置的显示名称或键名
|
|
'data_type' => $config['data_type'] ?? 'user_input',
|
|
'system_function' => $config['system_function'] ?? '',
|
|
'table_name' => $config['table_name'] ?? '',
|
|
'field_name' => $config['field_name'] ?? '',
|
|
'field_type' => $config['field_type'] ?? 'text',
|
|
'is_required' => $config['is_required'] ?? 0,
|
|
'default_value' => $processed_value, // 使用处理过的实际值
|
|
'sign_party' => $config['sign_party'] ?? '',
|
|
'validation_rule' => $config['validation_rule'] ?? '',
|
|
'created_at' => $now,
|
|
'updated_at' => $now
|
|
];
|
|
|
|
$insert_data[] = $data_source_record;
|
|
|
|
\think\facade\Log::info('处理占位符字段', [
|
|
'placeholder_key' => $placeholder_name,
|
|
'placeholder_display' => $placeholder_display_name,
|
|
'data_type' => $config['data_type'] ?? 'user_input',
|
|
'processed_value' => $processed_value
|
|
]);
|
|
}
|
|
|
|
if (!empty($insert_data)) {
|
|
$result = Db::table('school_document_data_source_config')->insertAll($insert_data);
|
|
|
|
if ($result) {
|
|
\think\facade\Log::info('文档数据源配置创建成功', [
|
|
'contract_id' => $contract_id,
|
|
'contract_sign_id' => $contract_sign_id,
|
|
'order_id' => $orderData['id'],
|
|
'records_count' => count($insert_data)
|
|
]);
|
|
}
|
|
|
|
return $result ? true : false;
|
|
}
|
|
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('创建文档数据源配置异常', [
|
|
'contract_id' => $contract_id,
|
|
'contract_sign_id' => $contract_sign_id,
|
|
'order_data' => $orderData,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取学员信息用于合同处理
|
|
* @param int $student_id 学员ID
|
|
* @return array|null
|
|
*/
|
|
private function getStudentInfoForContract($student_id)
|
|
{
|
|
try {
|
|
$student = Db::table('school_student')
|
|
->where('id', $student_id)
|
|
->where('status', 1)
|
|
->field([
|
|
'id', 'name', 'gender', 'age', 'birthday',
|
|
'emergency_contact', 'contact_phone', 'member_label',
|
|
'user_id', 'campus_id', 'created_at'
|
|
])
|
|
->find();
|
|
|
|
return $student ? $student : null;
|
|
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('获取学员信息失败', [
|
|
'student_id' => $student_id,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取处理过的字段值
|
|
* 根据字段的数据类型获取相应的值
|
|
* @param array $config 字段配置
|
|
* @param array $student_info 学员信息
|
|
* @param array $order_data 订单数据
|
|
* @return string
|
|
*/
|
|
private function getProcessedFieldValue($config, $student_info, $order_data)
|
|
{
|
|
try {
|
|
$data_type = $config['data_type'] ?? 'user_input';
|
|
|
|
switch ($data_type) {
|
|
case 'database':
|
|
// 数据库类型:从相关表获取数据
|
|
return $this->getDatabaseFieldValueForConfig($config, $student_info, $order_data);
|
|
|
|
case 'system':
|
|
// 系统函数类型:调用系统函数获取值
|
|
return $this->getSystemFunctionValueForConfig($config);
|
|
|
|
case 'user_input':
|
|
// 用户输入类型:使用配置的默认值
|
|
return $config['default_value'] ?? '';
|
|
|
|
case 'signature':
|
|
case 'sign_img':
|
|
// 签名类型:无默认值,需要用户操作
|
|
return '';
|
|
|
|
default:
|
|
return $config['default_value'] ?? '';
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('获取处理字段值失败', [
|
|
'config' => $config,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
return '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 从数据库获取字段值
|
|
* @param array $config 字段配置
|
|
* @param array $student_info 学员信息
|
|
* @param array $order_data 订单数据
|
|
* @return string
|
|
*/
|
|
private function getDatabaseFieldValueForConfig($config, $student_info, $order_data)
|
|
{
|
|
try {
|
|
$table_name = $config['table_name'] ?? '';
|
|
$field_name = $config['field_name'] ?? '';
|
|
|
|
if (empty($table_name) || empty($field_name)) {
|
|
return '';
|
|
}
|
|
|
|
$value = '';
|
|
|
|
switch ($table_name) {
|
|
case 'school_student':
|
|
// 学员表:直接从学员信息获取
|
|
$value = $student_info[$field_name] ?? '';
|
|
break;
|
|
|
|
case 'school_customer_resources':
|
|
// 用户表:通过学员的user_id关联查询
|
|
if (!empty($student_info['user_id'])) {
|
|
$value = Db::table('school_customer_resources')
|
|
->where('id', $student_info['user_id'])
|
|
->value($field_name) ?? '';
|
|
}
|
|
break;
|
|
|
|
case 'school_order_table':
|
|
// 订单表:使用当前订单数据
|
|
$value = $order_data[$field_name] ?? '';
|
|
break;
|
|
|
|
default:
|
|
\think\facade\Log::warning('不支持的数据库表', [
|
|
'table_name' => $table_name,
|
|
'field_name' => $field_name
|
|
]);
|
|
break;
|
|
}
|
|
|
|
// 对特殊字段进行格式化处理
|
|
$value = $this->formatFieldValueForContract($field_name, $value);
|
|
|
|
return (string)$value;
|
|
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('获取数据库字段值失败', [
|
|
'config' => $config,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
return '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取系统函数值
|
|
* @param array $config 字段配置
|
|
* @return string
|
|
*/
|
|
private function getSystemFunctionValueForConfig($config)
|
|
{
|
|
try {
|
|
$system_function = $config['system_function'] ?? '';
|
|
|
|
if (empty($system_function)) {
|
|
return '';
|
|
}
|
|
|
|
// 预定义的系统函数
|
|
switch ($system_function) {
|
|
case 'current_date':
|
|
return date('Y-m-d');
|
|
case 'current_time':
|
|
return date('H:i:s');
|
|
case 'current_datetime':
|
|
return date('Y-m-d H:i:s');
|
|
case 'current_year':
|
|
return date('Y');
|
|
case 'current_month':
|
|
return date('m');
|
|
case 'current_day':
|
|
return date('d');
|
|
default:
|
|
\think\facade\Log::warning('未知的系统函数', [
|
|
'function_name' => $system_function
|
|
]);
|
|
return '';
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('获取系统函数值失败', [
|
|
'config' => $config,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
return '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 格式化字段值
|
|
* @param string $field_name 字段名
|
|
* @param mixed $value 原始值
|
|
* @return string
|
|
*/
|
|
private function formatFieldValueForContract($field_name, $value)
|
|
{
|
|
if (empty($value)) {
|
|
return '';
|
|
}
|
|
|
|
// 根据字段名进行特殊处理
|
|
switch (true) {
|
|
case str_contains($field_name, 'date') || str_contains($field_name, 'time'):
|
|
// 日期时间字段格式化
|
|
if (is_numeric($value)) {
|
|
return date('Y-m-d', $value);
|
|
} elseif (strtotime($value)) {
|
|
return date('Y-m-d', strtotime($value));
|
|
}
|
|
break;
|
|
|
|
case str_contains($field_name, 'amount') || str_contains($field_name, 'price'):
|
|
// 金额字段格式化
|
|
return number_format((float)$value, 2);
|
|
|
|
case str_contains($field_name, 'phone'):
|
|
// 手机号格式化
|
|
return (string)$value;
|
|
|
|
default:
|
|
// 默认返回字符串
|
|
return (string)$value;
|
|
}
|
|
|
|
return (string)$value;
|
|
}
|
|
|
|
/**
|
|
* 支付成功后创建支付记录
|
|
* @param array $orderData 订单数据
|
|
* @return bool
|
|
*/
|
|
private function createPaymentRecord(array $orderData)
|
|
{
|
|
try {
|
|
$order_id = $orderData['id'];
|
|
$payment_id = $orderData['payment_id'] ?? '';
|
|
$payment_type = $orderData['payment_type'] ?? '';
|
|
$order_amount = $orderData['order_amount'] ?? 0;
|
|
|
|
if (empty($order_id) || empty($payment_id)) {
|
|
\think\facade\Log::warning('创建支付记录失败:缺少订单ID或支付单号', $orderData);
|
|
return false;
|
|
}
|
|
|
|
// 获取课程名称用于支付描述
|
|
$course_name = '未知课程';
|
|
if (!empty($orderData['course_id'])) {
|
|
$course = \app\model\course\Course::where('id', $orderData['course_id'])->find();
|
|
if ($course) {
|
|
$course_name = $course['course_name'];
|
|
}
|
|
}
|
|
$status = Db::table('school_pay')->where('out_trade_no', $payment_id)->find();
|
|
if ($status) {
|
|
\think\facade\Log::warning('创建支付记录失败:支付单号已存在', ['order_id' => $order_id, 'payment_id' => $payment_id]);
|
|
return false;
|
|
}
|
|
$now = time();
|
|
$insertData = [
|
|
'main_id' => $order_id, // 订单ID
|
|
'from_main_id' => 0,
|
|
'out_trade_no' => $payment_id, // 支付单号
|
|
'trade_type' => 'order_payment', // 交易类型
|
|
'trade_id' => $order_id,
|
|
'trade_no' => $payment_id,
|
|
'body' => '课程购买-' . $course_name, // 支付描述
|
|
'money' => $order_amount, // 支付金额
|
|
'voucher' => '',
|
|
'status' => 1, // 支付状态:已支付
|
|
'json' => '',
|
|
'create_time' => $now, // 创建时间
|
|
'pay_time' => $now, // 支付时间
|
|
'cancel_time' => 0,
|
|
'type' => $payment_type, // 支付方式:cash, scan_code等
|
|
'mch_id' => '',
|
|
'main_type' => 'order', // 主类型
|
|
'channel' => '',
|
|
'fail_reason' => ''
|
|
];
|
|
|
|
$result = Db::table('school_pay')->insert($insertData);
|
|
|
|
if ($result) {
|
|
\think\facade\Log::info('支付记录创建成功', [
|
|
'order_id' => $order_id,
|
|
'payment_id' => $payment_id,
|
|
'amount' => $order_amount,
|
|
'type' => $payment_type
|
|
]);
|
|
}
|
|
|
|
return $result ? true : false;
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('创建支付记录异常', [
|
|
'order_data' => $orderData,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理赠品
|
|
*/
|
|
private function handleGift(array $orderData)
|
|
{
|
|
try {
|
|
// 如果订单没有使用赠品,跳过处理
|
|
if (empty($orderData['gift_id']) || empty($orderData['gift_type'])) {
|
|
return true;
|
|
}
|
|
|
|
$gift_id = $orderData['gift_id'];
|
|
$gift_type = $orderData['gift_type'];
|
|
$order_amount = $orderData['order_amount'];
|
|
|
|
// 验证赠品是否存在且可用
|
|
$gift = Db::table('shcool_resources_gift')
|
|
->where('id', $gift_id)
|
|
->where('gift_status', 1)
|
|
->where('order_id', $orderData['id']) // 必须是被当前订单绑定的赠品
|
|
->find();
|
|
|
|
if (!$gift) {
|
|
\think\facade\Log::warning('处理赠品失败:赠品不存在或不可用', [
|
|
'gift_id' => $gift_id,
|
|
'order_id' => $orderData['id']
|
|
]);
|
|
return false;
|
|
}
|
|
|
|
// 根据赠品类型进行处理
|
|
switch ($gift_type) {
|
|
case '1': // 减现
|
|
$this->handleGiftCash($orderData, $gift);
|
|
break;
|
|
case '2': // 赠课
|
|
$this->handleGiftCourse($orderData, $gift);
|
|
break;
|
|
default:
|
|
\think\facade\Log::warning('处理赠品失败:未知的赠品类型', [
|
|
'gift_type' => $gift_type,
|
|
'order_id' => $orderData['id']
|
|
]);
|
|
return false;
|
|
}
|
|
|
|
// 更新赠品状态为已使用
|
|
Db::table('shcool_resources_gift')
|
|
->where('id', $gift_id)
|
|
->update([
|
|
'gift_status' => 2, // 已使用
|
|
'use_time' => time(),
|
|
'update_time' => time()
|
|
]);
|
|
|
|
\think\facade\Log::info('赠品处理成功', [
|
|
'gift_id' => $gift_id,
|
|
'gift_type' => $gift_type,
|
|
'order_id' => $orderData['id']
|
|
]);
|
|
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('处理赠品异常', [
|
|
'order_data' => $orderData,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 验证赠品是否可用
|
|
*/
|
|
private function validateGift($gift_id, $resource_id)
|
|
{
|
|
try {
|
|
$gift = Db::table('shcool_resources_gift')
|
|
->where('id', $gift_id)
|
|
->where('resource_id', $resource_id)
|
|
->where('gift_status', 1)
|
|
->where('order_id', 0) // 必须是未使用的赠品
|
|
->find();
|
|
|
|
if (!$gift) {
|
|
return [
|
|
'valid' => false,
|
|
'message' => '赠品不存在或已被使用'
|
|
];
|
|
}
|
|
|
|
return [
|
|
'valid' => true,
|
|
'message' => '赠品验证通过'
|
|
];
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('验证赠品异常', [
|
|
'gift_id' => $gift_id,
|
|
'resource_id' => $resource_id,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
|
|
return [
|
|
'valid' => false,
|
|
'message' => '赠品验证异常: ' . $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新赠品状态,绑定到订单
|
|
*/
|
|
private function updateGiftStatus($gift_id, $order_id)
|
|
{
|
|
try {
|
|
$result = Db::table('shcool_resources_gift')
|
|
->where('id', $gift_id)
|
|
->where('gift_status', 1)
|
|
->where('order_id', 0)
|
|
->update([
|
|
'order_id' => $order_id,
|
|
'update_time' => time()
|
|
]);
|
|
|
|
if ($result) {
|
|
\think\facade\Log::info('赠品状态更新成功', [
|
|
'gift_id' => $gift_id,
|
|
'order_id' => $order_id
|
|
]);
|
|
} else {
|
|
\think\facade\Log::warning('赠品状态更新失败', [
|
|
'gift_id' => $gift_id,
|
|
'order_id' => $order_id
|
|
]);
|
|
}
|
|
|
|
return $result;
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('更新赠品状态异常', [
|
|
'gift_id' => $gift_id,
|
|
'order_id' => $order_id,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理减现赠品
|
|
*/
|
|
private function handleGiftCash($orderData, $gift)
|
|
{
|
|
// 减现赠品的具体逻辑可以根据业务需求实现
|
|
// 例如:修改订单金额、创建退款记录等
|
|
\think\facade\Log::info('处理减现赠品', [
|
|
'order_id' => $orderData['id'],
|
|
'gift_id' => $gift['id'],
|
|
'gift_name' => $gift['gift_name']
|
|
]);
|
|
|
|
// TODO: 具体的减现逻辑实现
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 处理赠课赠品
|
|
*/
|
|
private function handleGiftCourse($orderData, $gift)
|
|
{
|
|
// 赠课赠品的具体逻辑可以根据业务需求实现
|
|
// 例如:增加学员课时、创建额外课程记录等
|
|
\think\facade\Log::info('处理赠课赠品', [
|
|
'order_id' => $orderData['id'],
|
|
'gift_id' => $gift['id'],
|
|
'gift_name' => $gift['gift_name']
|
|
]);
|
|
|
|
// TODO: 具体的赠课逻辑实现
|
|
return true;
|
|
}
|
|
|
|
public function updateOrderPaymentVoucher($params)
|
|
{
|
|
try {
|
|
// 验证必要参数
|
|
if (empty($params['order_id'])) {
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '缺少订单ID参数',
|
|
'data' => []
|
|
];
|
|
}
|
|
|
|
if (empty($params['payment_voucher'])) {
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '缺少支付凭证参数',
|
|
'data' => []
|
|
];
|
|
}
|
|
|
|
// 查询订单
|
|
$order = OrderTable::where('id', $params['order_id'])->find();
|
|
if (!$order) {
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '订单不存在',
|
|
'data' => []
|
|
];
|
|
}
|
|
|
|
// 更新支付凭证
|
|
$result = $order->save([
|
|
'payment_voucher' => $params['payment_voucher'],
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
if ($result) {
|
|
\think\facade\Log::info('订单支付凭证更新成功', [
|
|
'order_id' => $params['order_id'],
|
|
'payment_voucher' => $params['payment_voucher']
|
|
]);
|
|
|
|
return [
|
|
'code' => 1,
|
|
'msg' => '支付凭证提交成功',
|
|
'data' => $order->toArray()
|
|
];
|
|
} else {
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '支付凭证提交失败',
|
|
'data' => []
|
|
];
|
|
}
|
|
} catch (\Exception $e) {
|
|
\think\facade\Log::error('更新订单支付凭证异常', [
|
|
'params' => $params,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '更新订单支付凭证异常: ' . $e->getMessage(),
|
|
'data' => []
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|