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.
282 lines
9.0 KiB
282 lines
9.0 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\service\admin\student;
|
|
|
|
use app\model\class_resources_rel\ClassResourcesRel;
|
|
use app\model\student\Student;
|
|
use app\model\customer_resources\CustomerResources;
|
|
use app\model\campus\Campus;
|
|
use app\model\class_grade\ClassGrade;
|
|
|
|
use app\model\student_label\StudentLabel;
|
|
use app\service\admin\student_label\StudentLabelService;
|
|
use core\base\BaseAdminService;
|
|
|
|
|
|
/**
|
|
* 学员服务层
|
|
* Class StudentService
|
|
* @package app\service\admin\student
|
|
*/
|
|
class StudentService extends BaseAdminService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->model = new Student();
|
|
}
|
|
|
|
/**
|
|
* 获取学员列表
|
|
* @param array $where
|
|
* @return array
|
|
*/
|
|
public function getPage(array $data = [])
|
|
{
|
|
$field = 'id,user_id,campus_id,class_id,name,gender,age,birthday,member_label,emergency_contact,contact_phone,note,status,created_at,updated_at,deleted_at';
|
|
$order = 'id desc';
|
|
|
|
$where = [];
|
|
if($data['campus_id']){
|
|
$where[] = ['campus_id','=',$data['campus_id']];
|
|
}
|
|
|
|
if($data['name']){
|
|
$where[] = ['name','=',$data['name']];
|
|
}
|
|
|
|
if($data['emergency_contact']){
|
|
$where[] = ['emergency_contact','=',$data['emergency_contact']];
|
|
}
|
|
|
|
if($data['contact_phone']){
|
|
$where[] = ['contact_phone','=',$data['contact_phone']];
|
|
}
|
|
|
|
if($data['class_id']){
|
|
$where[] = ['class_id','=',$data['class_id']];
|
|
}
|
|
|
|
if ($data['member_label']) {
|
|
$where[] = ['a.member_label', 'like', "%" . $data['member_label'] . "%"];
|
|
}
|
|
|
|
|
|
$search_model = $this->model->where($where)->with(['customerResources', 'campus', 'classGrade'])->field($field)->order($order);
|
|
|
|
|
|
if (isset($data['created_at'][0]) && isset($data['created_at'][1])) {
|
|
$search_model->whereBetweenTime('created_at', $data['created_at'][0] . "00:00:00", $data['created_at'][1] . "23:59:59");
|
|
}
|
|
|
|
|
|
return $this->pageQuery($search_model, function ($item, $key) {
|
|
$item = $this->makeUp($item);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 获取学员信息
|
|
* @param int $id
|
|
* @return array
|
|
*/
|
|
public function getInfo(int $id)
|
|
{
|
|
$field = 'id,user_id,campus_id,class_id,name,gender,age,birthday,emergency_contact,member_label,contact_phone,note,status,created_at,updated_at,deleted_at';
|
|
|
|
$info = $this->makeUp($this->model->field($field)->where([['id', "=", $id]])->with(['customerResources', 'campus', 'classGrade'])->findOrEmpty()->toArray());
|
|
$info['getInfoRel'] = $this->getInfoRel($info['user_id']);
|
|
return $info;
|
|
}
|
|
|
|
|
|
public function getInfoRel(int $resource_id)
|
|
{
|
|
$rel = new ClassResourcesRel();
|
|
$field = 'id,class_id,resource_id,campus_id,source_id,source_type,join_time,out_time,status,create_time,update_time';
|
|
|
|
$info = $rel->field($field)->where([['resource_id', "=", $resource_id]])->with(['classGrade','customerResources','campus'])->findOrEmpty()->toArray();
|
|
return $info;
|
|
}
|
|
|
|
|
|
/**
|
|
* 组合整理数据
|
|
* @param $data
|
|
*/
|
|
public function makeUp($data)
|
|
{
|
|
//会员标签
|
|
if (!empty($data['member_label'])) {
|
|
// 确保member_label是数组格式
|
|
$labelIds = $data['member_label'];
|
|
if (is_string($labelIds)) {
|
|
// 如果是JSON字符串,解析为数组
|
|
$labelIds = json_decode($labelIds, true) ?: [];
|
|
} elseif (is_numeric($labelIds)) {
|
|
// 如果是数字,转换为数组
|
|
$labelIds = [$labelIds];
|
|
} elseif (!is_array($labelIds)) {
|
|
// 其他情况设为空数组
|
|
$labelIds = [];
|
|
}
|
|
|
|
$data['member_label_array'] = (new StudentLabelService())->getMemberLabelListByLabelIds($labelIds);
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 添加学员
|
|
* @param array $data
|
|
* @return mixed
|
|
*/
|
|
public function add(array $data)
|
|
{
|
|
$res = $this->model->create($data);
|
|
return $res->id;
|
|
|
|
}
|
|
|
|
/**
|
|
* 学员编辑
|
|
* @param int $id
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
public function edit(int $id, array $data)
|
|
{
|
|
|
|
$this->model->where([['id', '=', $id]])->update($data);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除学员
|
|
* @param int $id
|
|
* @return bool
|
|
*/
|
|
public function del(int $id)
|
|
{
|
|
$model = $this->model->where([['id', '=', $id]])->find();
|
|
$res = $model->delete();
|
|
return $res;
|
|
}
|
|
|
|
|
|
public function getCustomerResourcesAll($params = [])
|
|
{
|
|
$customerResourcesModel = new CustomerResources();
|
|
|
|
// 构建查询条件
|
|
$where = [];
|
|
if (!empty($params['name'])) {
|
|
$where[] = ['name', 'like', '%' . $params['name'] . '%'];
|
|
}
|
|
if (!empty($params['phone_number'])) {
|
|
$where[] = ['phone_number', 'like', '%' . $params['phone_number'] . '%'];
|
|
}
|
|
|
|
// 查询客户资源,关联学员课程信息
|
|
$customerResources = $customerResourcesModel
|
|
->where($where)
|
|
->field('id,name,phone_number,age,member_id')
|
|
->select()
|
|
->toArray();
|
|
|
|
// 批量查询学员课程信息,减少数据库查询次数
|
|
$memberIds = array_column($customerResources, 'member_id');
|
|
$memberIds = array_filter($memberIds); // 移除空值
|
|
|
|
$courseInfo = [];
|
|
if (!empty($memberIds)) {
|
|
// 查询学员课程信息表
|
|
$courses = \think\facade\Db::table('school_student_courses')
|
|
->alias('sc')
|
|
->leftJoin('school_course c', 'sc.course_id = c.id')
|
|
->where([['sc.resource_id', 'in', $memberIds]])
|
|
->field('sc.resource_id as member_id,
|
|
(sc.total_hours + sc.gift_hours - sc.use_total_hours - sc.use_gift_hours) as remaining_hours,
|
|
sc.end_date as expiry_date,
|
|
c.course_name,
|
|
sc.status')
|
|
->select()
|
|
->toArray();
|
|
|
|
// 按 member_id 分组
|
|
foreach ($courses as $course) {
|
|
$courseInfo[$course['member_id']][] = $course;
|
|
}
|
|
}
|
|
|
|
// 合并数据并判断正式学员状态
|
|
foreach ($customerResources as &$customer) {
|
|
$customer['is_formal_student'] = false;
|
|
$customer['course_info'] = [];
|
|
|
|
if (!empty($customer['member_id']) && isset($courseInfo[$customer['member_id']])) {
|
|
$customer['course_info'] = $courseInfo[$customer['member_id']];
|
|
|
|
// 判断是否为正式学员
|
|
$currentTime = time();
|
|
foreach ($customer['course_info'] as $course) {
|
|
$isValid = true;
|
|
|
|
// 检查到期时间
|
|
if (!empty($course['expiry_date'])) {
|
|
$expiryTime = strtotime($course['expiry_date']);
|
|
$isValid = $expiryTime >= $currentTime;
|
|
}
|
|
|
|
// 检查剩余课时
|
|
if ($isValid && $course['remaining_hours'] > 0) {
|
|
$customer['is_formal_student'] = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $customerResources;
|
|
}
|
|
|
|
public function getCampusAll()
|
|
{
|
|
$campusModel = new Campus();
|
|
return $campusModel->select()->toArray();
|
|
}
|
|
|
|
public function getClassGradeAll()
|
|
{
|
|
$classGradeModel = new ClassGrade();
|
|
return $classGradeModel->select()->toArray();
|
|
}
|
|
|
|
public function label_all()
|
|
{
|
|
$field = 'label_id, label_name';
|
|
return (new StudentLabel())->where([['label_id', '>', 0]])->field($field)->order('sort desc,create_time desc')->select()->toArray();
|
|
|
|
}
|
|
|
|
public function getStudentByName($name)
|
|
{
|
|
$query = $this->model->with(['customer_resources'])->where([['name', 'like', '%' . $name . '%']]);
|
|
$list = $query->select()->toArray();
|
|
foreach ($list as &$item) {
|
|
$item['client_name'] = $item['name'] . '(家长:' . $item['customer_resources']['name'] . ' 电话:' . $item['customer_resources']['phone_number'] . ')';
|
|
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
}
|
|
|