package com.ruoyi.system.service.columns.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.school.course.domain.ScClaTime;
import com.ruoyi.school.course.domain.ScClaTimeRule;
import com.ruoyi.school.course.service.IScClaTimeRuleService;
import com.ruoyi.school.course.service.IScClaTimeService;
import com.ruoyi.school.member.domain.ScMemberCardTypes;
import com.ruoyi.school.member.service.ScMemberCardTypesService;
import com.ruoyi.system.domain.columns.SysTeacher;
import com.ruoyi.system.mapper.colums.SysTeacherMapper;
import com.ruoyi.system.service.columns.ISysTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-28
 */
@Service
public class SysTeacherServiceImpl implements ISysTeacherService
{
    @Autowired
    private SysTeacherMapper sysTeacherMapper;


    @Autowired
    private IScClaTimeRuleService claTimeRuleService;
    @Autowired
    private ScMemberCardTypesService cardTypesService;

    @Autowired
    private IScClaTimeService claTimeService;


    /**
     * 查询员工信息
     *
     * @param userId 员工信息主键
     * @return 员工信息
     */
    @Override
    public SysTeacher selectSysTeacherByUserId(Long userId)
    {
        return sysTeacherMapper.selectSysTeacherByUserId(userId);
    }

    /**
     * 查询员工信息列表
     *
     * @param sysTeacher 员工信息
     * @return 员工信息
     */
    @Override
    public List<SysTeacher> selectSysTeacherList(SysTeacher sysTeacher)
    {
        return sysTeacherMapper.selectSysTeacherList(sysTeacher);
    }

    /**
     * 未生成教练简介的，教练员工
     * @param sysTeacher
     * @return
     */
    @Override
    public List<SysUser> getUserForTeacher(SysTeacher sysTeacher)
    {
        return sysTeacherMapper.getUserForTeacher(sysTeacher);
    }

    /**
     * 新增员工信息
     *
     * @param sysTeacher 员工信息
     * @return 结果
     */
    @Override
    public int insertSysTeacher(SysTeacher sysTeacher)
    {
        sysTeacher.setCreateTime(LocalDateTime.now());
        return sysTeacherMapper.insertSysTeacher(sysTeacher);
    }

    /**
     * 修改员工信息
     *
     * @param sysTeacher 员工信息
     * @return 结果
     */
    @Override
    public int updateSysTeacher(SysTeacher sysTeacher)
    {
        sysTeacher.setUpdateTime(LocalDateTime.now());
        return sysTeacherMapper.updateSysTeacher(sysTeacher);
    }

    /**
     * 批量删除员工信息
     *
     * @param userIds 需要删除的员工信息主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteSysTeacherByUserIds(List<Long> userIds)
    {

        if (isUsed(userIds)){
            return AjaxResult.error("该教练已经排课，或在会员卡项中被单独定义，无法删除！");
        }
        sysTeacherMapper.deleteSysTeacherByUserIds(userIds);
        return AjaxResult.success();
    }

    /**
     * 删除员工信息信息
     *
     * @param userId 员工信息主键
     * @return 结果
     */
    @Override
    public int deleteSysTeacherByUserId(Long userId)
    {

        return sysTeacherMapper.deleteSysTeacherByUserId(userId);
    }



    public boolean isUsed(List<Long> userIds){

        int timeRuleCount=claTimeRuleService.count(new QueryWrapper<ScClaTimeRule>().in("teacher_id",userIds));
        if (timeRuleCount>0 ){
            return true;
        }
        String arrayAsString = userIds.stream()
                .map(Object::toString) // 将Long转换为String
                .collect(Collectors.joining(",")); // 使用逗号和空格连接字符串
        int cardTypesCount=cardTypesService.count(new QueryWrapper<ScMemberCardTypes>()
                .last(" and JSON_CONTAINS(teacher_list, CAST("+arrayAsString+" AS JSON))")
        );
        if (cardTypesCount>0 ){
            return true;
        }
        int claTimeCount=claTimeService.count(new QueryWrapper<ScClaTime>().in("teacher_id",userIds));
        if (claTimeCount>0 ){
            return true;
        }

        return false;
    }
}
