package cn.iocoder.yudao.module.jx.service.impl;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.domain.*;
import cn.iocoder.yudao.module.jx.payment.entity.DriveSchoolPay;
import cn.iocoder.yudao.module.jx.payment.mapper.DrivePayMapper;
import cn.iocoder.yudao.module.jx.payment.mapper.DriveSchoolPayMapper;
import cn.iocoder.yudao.module.jx.payment.utils.PhoneValidator;
import cn.iocoder.yudao.module.jx.payment.utils.SendSmsUtil;
import cn.iocoder.yudao.module.jx.utils.DateUtils;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.jx.mapper.*;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolReservationCourseService;
import cn.iocoder.yudao.module.jx.vo.DriveSchoolInfoVO;
import cn.iocoder.yudao.module.jx.vo.StatisticsVo;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thoughtworks.xstream.core.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预约练车Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-08
 */
@Service
public class DriveSchoolReservationCourseServiceImpl implements IDriveSchoolReservationCourseService
{
    @Resource
    private DriveSchoolReservationCourseMapper driveSchoolReservationCourseMapper;

//    @Resource
//    private SysUserMapper sysUserMapper;

    @Resource
    private DriveSchoolCoachMapper driveSchoolCoachMapper;

    @Autowired
    private DriveSchoolPayMapper driveSchoolPayMapper;

    @Autowired
    private DriveSchoolCourseMapper driveSchoolCourseMapper;

    @Resource
    private DriveSchoolExamPassMapper driveSchoolExamPassMapper;

    @Resource
    private DriveSchoolInfoMapper driveSchoolInfoMapper;

    /**
     * 查询预约练车
     *
     * @param id 预约练车主键
     * @return 预约练车
     */
    @Override
    public DriveSchoolReservationCourse selectDriveSchoolReservationCourseById(Long id)
    {
        DriveSchoolReservationCourse driveSchoolReservationCourse = driveSchoolReservationCourseMapper.selectDriveSchoolReservationCourseById(id);
        if (ObjectUtils.isNotEmpty(driveSchoolReservationCourse.getUserId())){
           // SysUser sysUserData = sysUserMapper.findSysUserById(driveSchoolReservationCourse.getUserId());
            AdminUserRespDTO sysUserData = userApi.getUser(driveSchoolReservationCourse.getUserId());
            DriveSchoolPay driveSchoolPay = driveSchoolPayMapper.selectByUserId(driveSchoolReservationCourse.getUserId());
            driveSchoolReservationCourse.setUserName(driveSchoolPay.getName());
        }
        return driveSchoolReservationCourse;
    }

    @Resource
    private AdminUserApi userApi;
    /**
     * 查询预约练车列表
     *
     * @param driveSchoolReservationCourse 预约练车
     * @return 预约练车
     */
    @Override
    public IPage<DriveSchoolReservationCourse> selectDriveSchoolReservationCourseList(DriveSchoolReservationCourse driveSchoolReservationCourse,Page<DriveSchoolReservationCourse> page)
    {
        IPage<DriveSchoolReservationCourse> driveSchoolReservationCourseIPage = driveSchoolReservationCourseMapper.selectDriveSchoolReservationCourseList(driveSchoolReservationCourse, page);
        List<DriveSchoolReservationCourse> courseList = driveSchoolReservationCourseIPage.getRecords();

        courseList.forEach(item -> {
            if (item.getTimePeriod().equals("1")){
                item.setTimePeriod("上午");
            }else{
                item.setTimePeriod("下午");
            }
            AdminUserRespDTO sysUserData = userApi.getUser(item.getUserId());
            AdminUserDO adminUserDO = new AdminUserDO();
            BeanUtils.copyProperties(sysUserData,adminUserDO);
           // SysUser sysUserData = sysUserMapper.findSysUserById(item.getUserId());
            item.setSysUser(adminUserDO);
            item.setUserName(sysUserData.getNickname());
        });
        return driveSchoolReservationCourseIPage;
    }


    /**
     * 查询预约练车列表
     *
     * @param driveSchoolReservationCourse 预约练车
     * @return 预约练车
     */
    @Override
    public IPage<DriveSchoolReservationCourse> selectDriveSchoolReservationCoursePclist(DriveSchoolReservationCourse driveSchoolReservationCourse, Page<DriveSchoolReservationCourse> page)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolReservationCourse.setDeptId(deptId);
        IPage<DriveSchoolReservationCourse> driveSchoolReservationCourseIPage = driveSchoolReservationCourseMapper.selectDriveSchoolReservationCourseList(driveSchoolReservationCourse, page);
        List<DriveSchoolReservationCourse> courseList = driveSchoolReservationCourseIPage.getRecords();

        courseList.forEach(item -> {
            if (item.getTimePeriod().equals("1")){
                item.setTimePeriod("上午");
            }else{
                item.setTimePeriod("下午");
            }

//            SysUser sysUserData = sysUserMapper.findSysUserById(item.getUserId());
//            item.setSysUser(sysUserData);
            AdminUserRespDTO sysUserData = userApi.getUser(item.getUserId());
            if (ObjectUtils.isNotEmpty(sysUserData)){
                AdminUserDO adminUserDO = new AdminUserDO();
                BeanUtils.copyProperties(sysUserData,adminUserDO);
                item.setSysUser(adminUserDO);
            }

            // SysUser sysUserData = sysUserMapper.findSysUserById(item.getUserId());
            DriveSchoolPay driveSchoolPay = driveSchoolPayMapper.selectByUserId(item.getUserId());
            if (ObjectUtils.isNotEmpty(driveSchoolPay)){
                item.setUserName(driveSchoolPay.getName());
            }
        });
        return driveSchoolReservationCourseIPage;
    }

//    @Autowired
//    private ISysUserService userService;

    @Resource
    private DrivePayMapper drivePayMapper;
    /**
     * 新增预约练车
     *
     * @param driveSchoolReservationCourse 预约练车
     * @return 结果
     */
    @Override
    public int insertDriveSchoolReservationCourse(DriveSchoolReservationCourse driveSchoolReservationCourse) throws Exception {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        //预约之前先查询
        if (driveSchoolReservationCourse.getAccountType().equals("3")){
            Integer accountType = Integer.parseInt(driveSchoolReservationCourse.getAccountType());
            String temp = (accountType - 1)+"";
            List<DriveSchoolExamPass> driveSchoolExamPasses = driveSchoolExamPassMapper.selectDriveSchoolExamPassByUserId(userId);
            List<String> collect = driveSchoolExamPasses.stream().map(DriveSchoolExamPass::getCourseSubject).collect(Collectors.toList());
            if (!collect.contains(temp)){
                throw new Exception("未通过科目"+temp+"考试无法预约");
            }
        }
        //立即练车
        if (driveSchoolReservationCourse.getType().equals("2")){
            if (ObjectUtils.isNotEmpty(userId)){
                AdminUserRespDTO sysUserData = userApi.getUser(userId);
                //SysUser sysUserData = sysUserMapper.findSysUserById(userId);
                driveSchoolReservationCourse.setUserName(sysUserData.getNickname());
                driveSchoolReservationCourse.setUserId(userId);
            }

            Integer jlId = drivePayMapper.selectByUserId(userId);
            DriveSchoolCoach driveSchoolCoach = driveSchoolCoachMapper.selectDriveSchoolCoachById(Long.valueOf(jlId));
            driveSchoolReservationCourse.setCoachName(driveSchoolCoach.getInstructorName());
            driveSchoolReservationCourse.setCoachId(driveSchoolCoach.getId());
            driveSchoolReservationCourse.setDeptId(driveSchoolCoach.getDeptId());
            driveSchoolReservationCourse.setOrderTime(new Date());
            driveSchoolReservationCourseMapper.insertDriveSchoolReservationCourse(driveSchoolReservationCourse);
            //SysUser user = userService.getById(userId);
            AdminUserRespDTO user = userApi.getUser(userId);
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentDate = sdf.format(date);

            LocalDateTime now = LocalDateTime.now();
            int hour = now.getHour();
            String timeOfDay = (hour >= 0 && hour < 12) ? "上午" : "下午";
            //发送短信
            if (PhoneValidator.isValid(driveSchoolCoach.getPhone())) {
                SendSmsUtil.sendMsgCommon(new String[]{user.getNickname()+"  "+user.getNickname(),currentDate+" "+(timeOfDay)}, driveSchoolCoach.getPhone(),"1400852709","蓝安汽车小程序","2143603");
            }

            //更新预约练车时间
            driveSchoolReservationCourse.setStuSigninTime(DateUtils.getNowDate());
            driveSchoolReservationCourseMapper.updateDriveSchoolReservationCourse(driveSchoolReservationCourse);
            return 1;
        }

        Calendar calendar = Calendar.getInstance();
        String currentDate = driveSchoolReservationCourse.getCurrentDate();
        String[] parts = currentDate.split("-");
        int month = Integer.parseInt(parts[0]);
        int day = Integer.parseInt(parts[1]);

        currentDate = LocalDate.of(calendar.get(Calendar.YEAR), month, day).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

//        DriveSchoolCoach selfJl = driveSchoolCoachMapper.findSelfJl(SecurityFrameworkUtils.getLoginUserId());
       // SysUser user = userService.getById(userId);
        AdminUserRespDTO user = userApi.getUser(userId);
        if (com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isEmpty(user) && StringUtils.isNotEmpty(user.getNickname())){

        }

        if (ObjectUtils.isNotEmpty(userId)){
           // SysUser sysUserData = sysUserMapper.findSysUserById(userId);
            AdminUserRespDTO sysUserData = userApi.getUser(userId);
            driveSchoolReservationCourse.setUserName(sysUserData.getNickname());
            driveSchoolReservationCourse.setUserId(userId);
        }

        Integer jlId = drivePayMapper.selectByUserId(userId);
        DriveSchoolCoach driveSchoolCoach = driveSchoolCoachMapper.selectDriveSchoolCoachById(Long.valueOf(jlId));
        driveSchoolReservationCourse.setCoachName(driveSchoolCoach.getInstructorName());
        driveSchoolReservationCourse.setCoachId(driveSchoolCoach.getId());
        driveSchoolReservationCourse.setDeptId(driveSchoolCoach.getDeptId());
        driveSchoolReservationCourse.setOrderTime(new Date());
        driveSchoolReservationCourseMapper.insertDriveSchoolReservationCourse(driveSchoolReservationCourse);
        //发送短信
        if (PhoneValidator.isValid(driveSchoolCoach.getPhone())) {
            SendSmsUtil.sendMsgCommon(new String[]{user.getNickname()+"  "+user.getNickname(),currentDate+" "+(Integer.parseInt(driveSchoolReservationCourse.getTimePeriod())==1?"上午":"下午")}, driveSchoolCoach.getPhone(),"1400852709","蓝安汽车小程序","2143603");
        }
        return 1;
    }

    /**
     * 修改预约练车
     *
     * @param driveSchoolReservationCourse1 预约练车
     * @return 结果
     */
    @Override
    public int updateDriveSchoolReservationCourse(DriveSchoolReservationCourse driveSchoolReservationCourse1)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        DriveSchoolReservationCourse driveSchoolReservationCourse = driveSchoolReservationCourseMapper.selectDriveSchoolReservationCourseById(driveSchoolReservationCourse1.getId());

        if (ObjectUtils.isNotEmpty(driveSchoolReservationCourse1.getStuSignState())){
            driveSchoolReservationCourse.setStuSignState(driveSchoolReservationCourse1.getStuSignState());
        }

        if (ObjectUtils.isNotEmpty(driveSchoolReservationCourse1.getStuSignState())){
            driveSchoolReservationCourse.setCoachSignState(driveSchoolReservationCourse1.getCoachSignState());

        }
        AdminUserRespDTO sysUser = userApi.getUser(userId);
       // SysUser sysUser = sysUserMapper.selectUserById(userId);
        Long deptId1 = sysUser.getDeptId();
        deptId1 = 100L;
        //更新学员签到签退时间
        if (ObjectUtils.isNotEmpty(driveSchoolReservationCourse) && ObjectUtils.isNotEmpty(driveSchoolReservationCourse.getStuSignState()) && ObjectUtils.isNotEmpty(deptId1) && deptId1==100 && driveSchoolReservationCourse.getStuSignState().equals("1")){
                driveSchoolReservationCourse.setStuSigninTime(DateUtils.getNowDate());
                driveSchoolReservationCourseMapper.updateDriveSchoolReservationCourse(driveSchoolReservationCourse);
            }
        if (ObjectUtils.isNotEmpty(driveSchoolReservationCourse)  && ObjectUtils.isNotEmpty(driveSchoolReservationCourse.getStuSignState()) && ObjectUtils.isNotEmpty(deptId1) && deptId1==100 && driveSchoolReservationCourse.getStuSignState().equals("2")){
            driveSchoolReservationCourse.setStuSignoutTime(DateUtils.getNowDate());
            driveSchoolReservationCourseMapper.updateDriveSchoolReservationCourse(driveSchoolReservationCourse);
            Date tempStartTime = new Date();
            Date tempEndTime = new Date();

            Date stuSigninTime = driveSchoolReservationCourse.getStuSigninTime();
            Date stuSignoutTime = driveSchoolReservationCourse.getStuSignoutTime();


            //如果教练签退时间不为空
            if (ObjectUtils.isNotEmpty(driveSchoolReservationCourse.getCoachSignoutTime()) && ObjectUtils.isNotEmpty(driveSchoolReservationCourse.getCoachSigninTime())){
                Date coachSigninTime = driveSchoolReservationCourse.getCoachSigninTime();
                Date coachSignoutTime = driveSchoolReservationCourse.getCoachSignoutTime();

                if (stuSigninTime.compareTo(coachSigninTime)> 0){
                    tempStartTime = coachSigninTime;
                }else {
                    tempStartTime = stuSigninTime;
                }

                if (stuSignoutTime.compareTo(coachSignoutTime)> 0){
                    tempEndTime =  coachSignoutTime;
                }else {
                    tempEndTime = stuSignoutTime;
                }

                long differenceInMilliss = Math.abs(tempEndTime.getTime() - tempStartTime.getTime()); // 计算时间差（毫秒）
                double coachTime = differenceInMilliss / 3600000.0;
                DecimalFormat df = new DecimalFormat("#.00");
                String formattedNumber = df.format(coachTime);
                double result = Double.parseDouble(formattedNumber);
                    driveSchoolReservationCourse.setDriveTime(result);
            }else if (ObjectUtils.isNotEmpty(driveSchoolReservationCourse.getCoachSigninTime())){
                if (stuSigninTime.compareTo(driveSchoolReservationCourse.getCoachSigninTime())> 0){
                    tempStartTime = driveSchoolReservationCourse.getCoachSigninTime();
                }else {
                    tempStartTime = stuSigninTime;
                }

                long differenceInMilliss = Math.abs(stuSignoutTime.getTime() -tempStartTime .getTime()); // 计算时间差（毫秒）
                double coachTime = differenceInMilliss / 3600000.0;
                DecimalFormat df = new DecimalFormat("#.00");
                String formattedNumber = df.format(coachTime);
                double result = Double.parseDouble(formattedNumber);

                driveSchoolReservationCourse.setDriveTime(result);
            }else {
                long differenceInMilliss = Math.abs(stuSignoutTime.getTime() - stuSigninTime.getTime()); // 计算时间差（毫秒）
                double coachTime = differenceInMilliss / 3600000.0;
                DecimalFormat df = new DecimalFormat("#.00");
                String formattedNumber = df.format(coachTime);
                double result = Double.parseDouble(formattedNumber);
                driveSchoolReservationCourse.setDriveTime(result);
            }
        }
        //更新教练签到签退时间
        if (ObjectUtils.isNotEmpty(driveSchoolReservationCourse)  && ObjectUtils.isNotEmpty(driveSchoolReservationCourse.getCoachSignState()) && deptId1!=100 && driveSchoolReservationCourse.getCoachSignState().equals("1")){
                driveSchoolReservationCourse.setCoachSigninTime(DateUtils.getNowDate());
                driveSchoolReservationCourseMapper.updateDriveSchoolReservationCourse(driveSchoolReservationCourse);
        }
        if (ObjectUtils.isNotEmpty(driveSchoolReservationCourse)  && ObjectUtils.isNotEmpty(driveSchoolReservationCourse.getCoachSignState()) && deptId1!=100 && driveSchoolReservationCourse.getCoachSignState().equals("2")){
            driveSchoolReservationCourse.setCoachSignoutTime(DateUtils.getNowDate());
            driveSchoolReservationCourseMapper.updateDriveSchoolReservationCourse(driveSchoolReservationCourse);

            Date tempStartTime = new Date();
            Date tempEndTime = new Date();

            Date coachSigninTime1 = driveSchoolReservationCourse.getCoachSigninTime();
            Date coachSignoutTime1 = driveSchoolReservationCourse.getCoachSignoutTime();

            //如果学员签退时间不为空
            if (ObjectUtils.isNotEmpty(driveSchoolReservationCourse.getStuSignoutTime()) && ObjectUtils.isNotEmpty(driveSchoolReservationCourse.getStuSigninTime())){
                Date stuSigninTime = driveSchoolReservationCourse.getStuSigninTime();
                Date stuSignoutTime = driveSchoolReservationCourse.getStuSignoutTime();

                if (stuSigninTime.compareTo(coachSigninTime1)> 0){
                    tempStartTime = coachSigninTime1;
                }else {
                    tempStartTime = stuSigninTime;
                }

                if (stuSignoutTime.compareTo(coachSignoutTime1)> 0){
                    tempEndTime =  coachSignoutTime1;
                }else {
                    tempEndTime = stuSignoutTime;
                }

                long differenceInMilliss = Math.abs(tempEndTime.getTime() - tempStartTime.getTime()); // 计算时间差（毫秒）
                double coachTime = differenceInMilliss / 3600000.0;
                DecimalFormat df = new DecimalFormat("#.00");
                String formattedNumber = df.format(coachTime);
                double result = Double.parseDouble(formattedNumber);
                driveSchoolReservationCourse.setDriveTime(result);
            }else if (ObjectUtils.isNotEmpty(driveSchoolReservationCourse.getStuSigninTime())){
                if (coachSigninTime1.compareTo(driveSchoolReservationCourse.getStuSigninTime())> 0){
                    tempStartTime = driveSchoolReservationCourse.getStuSigninTime();
                }else {
                    tempStartTime = coachSigninTime1;
                }

                long differenceInMilliss = Math.abs(coachSignoutTime1.getTime() - tempStartTime.getTime()); // 计算时间差（毫秒）
                double coachTime = differenceInMilliss / 3600000.0;
                DecimalFormat df = new DecimalFormat("#.00");
                String formattedNumber = df.format(coachTime);
                double result = Double.parseDouble(formattedNumber);
                driveSchoolReservationCourse.setDriveTime(result);
            }else {
                long differenceInMilliss = Math.abs(coachSignoutTime1.getTime() - coachSigninTime1.getTime()); // 计算时间差（毫秒）
                double coachTime = differenceInMilliss / 3600000.0;
                DecimalFormat df = new DecimalFormat("#.00");
                String formattedNumber = df.format(coachTime);
                double result = Double.parseDouble(formattedNumber);
                driveSchoolReservationCourse.setDriveTime(result);
            }

        }
        //driveSchoolReservationCourse.setUpdateTime(DateUtils.getNowDate());
        driveSchoolReservationCourse.setAuditStatus(driveSchoolReservationCourse1.getAuditStatus());
        return driveSchoolReservationCourseMapper.updateDriveSchoolReservationCourse(driveSchoolReservationCourse);
    }

    /**
     * 批量删除预约练车
     *
     * @param ids 需要删除的预约练车主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolReservationCourseByIds(Long[] ids)
    {
        return driveSchoolReservationCourseMapper.deleteDriveSchoolReservationCourseByIds(ids);
    }

    /**
     * 删除预约练车信息
     *
     * @param id 预约练车主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolReservationCourseById(Long id)
    {
        return driveSchoolReservationCourseMapper.deleteDriveSchoolReservationCourseById(id);
    }

    @Override
    public void updateCourseById(DriveSchoolReservationCourse driveSchoolReservationCourse) {
        driveSchoolReservationCourseMapper.updateCourseById(driveSchoolReservationCourse);
    }

    @Override
    public StatisticsVo getHomeData() {
        StatisticsVo statisticsVo = new StatisticsVo();
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();

        DriveSchoolInfoVO schoolInfoByDeptId = driveSchoolInfoMapper.getSchoolInfoByDeptId(deptId);
        //教练
        /*Integer coachNumber = driveSchoolCoachMapper.getCoachNumber(deptId);
        statisticsVo.setCoachNumber(coachNumber);*/
        //学员
        //Integer studentNumber = sysUserMapper.getStudentNumber(deptId);
        AdminUserRespDTO sysUser = userApi.getUser(userId);
        //List<String> studentNumber1 = sysUserMapper.getStudentNumber(schoolInfoByDeptId.getId());
        List<String> studentNumber1 = userService.getStudentNumber(schoolInfoByDeptId.getId());
        statisticsVo.setStuNumber(studentNumber1.size());
        //预约科目二
        Integer reservationNumber = driveSchoolReservationCourseMapper.getReservationNumber(deptId);
        statisticsVo.setReverseNumber(reservationNumber);

        //预约科目三
        Integer reservationNumbers = driveSchoolReservationCourseMapper.getReservationNumberThree(deptId);
        statisticsVo.setReverseNumberThree(reservationNumbers);
        //已报名
        Integer recordNumber = driveSchoolReservationCourseMapper.getIsRecdNumber(schoolInfoByDeptId.getId());
        statisticsVo.setIdRecordNumber(recordNumber);
        //已付款

        Integer payNumber = driveSchoolReservationCourseMapper.getPaydNumber(schoolInfoByDeptId.getId());

        statisticsVo.setIsPayNumber(recordNumber);
        //已面签
        Integer signedNumber = driveSchoolReservationCourseMapper.getSignedNumber(schoolInfoByDeptId.getId());
        statisticsVo.setIsSignNumber(studentNumber1.size());
        return statisticsVo;
    }

    @Override
    public IPage<DriveSchoolReservationCourse> getReservationCourseList(Page page) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        IPage<DriveSchoolReservationCourse> reservationCourseList = driveSchoolReservationCourseMapper.getReservationCourseList(page,deptId);
        for (DriveSchoolReservationCourse driveSchoolReservationCourse : reservationCourseList.getRecords()) {
            AdminUserRespDTO sysUserData = userApi.getUser(userId);
            //SysUser sysUserData = sysUserMapper.findSysUserById(driveSchoolReservationCourse.getUserId());

            List<DriveSchoolPay> infoByUserId = driveSchoolPayMapper.getInfoByUserId(driveSchoolReservationCourse.getUserId());
            if(ObjectUtils.isNotEmpty(infoByUserId)){
                DriveSchoolPay driveSchoolPay = infoByUserId.get(0);

                DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(driveSchoolPay.getCourseId());
                driveSchoolPay.setDriveSchoolCourse(driveSchoolCourse);
                driveSchoolReservationCourse.setDriveSchoolPay(driveSchoolPay);
            }

            DriveSchoolPay driveSchoolPay1 = driveSchoolPayMapper.selectByUserId(driveSchoolReservationCourse.getUserId());
            if (ObjectUtils.isNotEmpty(driveSchoolPay1)){
                driveSchoolReservationCourse.setUserName(driveSchoolPay1.getName());
            }
        }
        return reservationCourseList;
    }

    @Override
    public List<DriveSchoolReservationCourse> selectDriveSchoolReservationGetListUser(DriveSchoolReservationCourse driveSchoolReservationCourse) {
        List<DriveSchoolReservationCourse> driveSchoolReservationCourses = driveSchoolReservationCourseMapper.selectDriveSchoolReservationGetListUser(driveSchoolReservationCourse);
        for (DriveSchoolReservationCourse driveSchoolReservationCours : driveSchoolReservationCourses) {

            List<DriveSchoolPay> infoByUserId = driveSchoolPayMapper.getInfoByUserId(driveSchoolReservationCours.getUserId());
            DriveSchoolPay driveSchoolPay = infoByUserId.get(0);

            DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(driveSchoolPay.getCourseId());
            driveSchoolReservationCours.setDriveSchoolCourse(driveSchoolCourse);
        }
        return driveSchoolReservationCourses;
    }

    @Override
    public DriveSchoolReservationCourse getDriveTime() {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        DriveSchoolReservationCourse driveTime = driveSchoolReservationCourseMapper.getDriveTime(deptId);
        return driveTime;
    }

    @Override
    public List<DriveSchoolReservationCourse> getOftenDriveStudents(Long jlId) {


        List<DriveSchoolReservationCourse> list = driveSchoolReservationCourseMapper.getOftenDriveStudents(jlId);

        for (DriveSchoolReservationCourse driveSchoolReservationCourse : list) {
            DriveSchoolPay infoByUserId = driveSchoolPayMapper.selectByUserId(driveSchoolReservationCourse.getUserId());

            DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(infoByUserId.getCourseId());
            driveSchoolReservationCourse.setCourseName(driveSchoolCourse.getName());
        }
        return list;
    }

    @Resource
    private AdminUserService userService;
    @Override
    public StatisticsVo getIndexData() {

        //获取教练id
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO sysUser = userApi.getUser(userId);
        //SysUser sysUser = sysUserMapper.selectUserById(userId);
        String phonenumber = sysUser.getMobile();
        DriveSchoolCoach driveSchoolCoach = driveSchoolCoachMapper.selectByPhonenumber(phonenumber);
        Long jlId = 0L;
        if (ObjectUtils.isNotEmpty(driveSchoolCoach)){
            jlId = driveSchoolCoach.getId();
        }

        StatisticsVo statisticsVo = new StatisticsVo();
        //学员
        //Integer studentNumber = sysUserMapper.getStudentNumberByUserId(jlId);
        Integer studentNumber = userService.getStudentNumberByUserId(jlId);
        statisticsVo.setStuNumber(studentNumber);
        //预约
        Integer reservationNumber = driveSchoolReservationCourseMapper.getReservationNumberByUserId(jlId);
        statisticsVo.setIdRecordNumber(reservationNumber);
        //账单数
        Integer integer = driveSchoolPayMapper.selectByCoachId(jlId);
        statisticsVo.setIsPayNumber(integer);
        return statisticsVo;
        //return null;
    }

    @Override
    public List<DriveSchoolReservationCourse> getReservationCourseListByUserId() {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        //SysUser sysUser = sysUserMapper.selectUserById(userId);
        AdminUserRespDTO sysUser = userApi.getUser(userId);
        String phonenumber = sysUser.getMobile();
        DriveSchoolCoach driveSchoolCoach = driveSchoolCoachMapper.selectByPhonenumber(phonenumber);
        List<DriveSchoolReservationCourse> reservationCourseListByUserId = driveSchoolReservationCourseMapper.getReservationCourseListByUserId(userId);
       // List<DriveSchoolReservationCourse> reservationCourseListByUserId = driveSchoolReservationCourseMapper.getReservationCourseListByUserId(driveSchoolCoach.getId());

        for (DriveSchoolReservationCourse driveSchoolReservationCourse : reservationCourseListByUserId) {
            Long userId1 = driveSchoolReservationCourse.getUserId();
            DriveSchoolPay infoByUserId = driveSchoolPayMapper.selectByUserId(userId1);
            if (ObjectUtils.isNotEmpty(infoByUserId)){
                DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(infoByUserId.getCourseId());
                driveSchoolReservationCourse.setUserName(infoByUserId.getName());
                driveSchoolReservationCourse.setCourseName(driveSchoolCourse.getName());
            }
        }
        return reservationCourseListByUserId;
    }


    @Override
    public List<DriveSchoolReservationCourse> getAppReservationCourseListByUserId(DriveSchoolReservationCourse driveSchoolReservationCourse) {

        //获取教练id
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        //SysUser sysUser = sysUserMapper.selectUserById(userId);
        AdminUserRespDTO sysUser = userApi.getUser(userId);
        String phonenumber = sysUser.getMobile();
        DriveSchoolCoach driveSchoolCoach = driveSchoolCoachMapper.selectByPhonenumber(phonenumber);
        Long jlId = 0L;
        if (ObjectUtils.isNotEmpty(driveSchoolCoach)){
            jlId = driveSchoolCoach.getId();

        }
        driveSchoolReservationCourse.setUserId(jlId);
        List<DriveSchoolReservationCourse> reservationCourseListByUserId = driveSchoolReservationCourseMapper.getAppReservationCourseListByUserId(driveSchoolReservationCourse);
        for (DriveSchoolReservationCourse driveSchoolReservationCourses : reservationCourseListByUserId) {
            Long userId1 = driveSchoolReservationCourses.getUserId();
            DriveSchoolPay infoByUserId = driveSchoolPayMapper.selectByUserId(userId1);
            if (ObjectUtils.isNotEmpty(infoByUserId)){
                DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(infoByUserId.getCourseId());
                driveSchoolReservationCourses.setUserName(infoByUserId.getName());
                driveSchoolReservationCourses.setCourseName(driveSchoolCourse.getName());
                driveSchoolReservationCourses.setPhone(infoByUserId.getPhone());
            }
        }
        return reservationCourseListByUserId;
    }

    @Override
    public List<DriveSchoolReservationCourse> getAppReservationCourseListByUserIds(DriveSchoolReservationCourse driveSchoolReservationCourse) {

        //获取教练id
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        //SysUser sysUser = sysUserMapper.selectUserById(userId);
        AdminUserRespDTO sysUser = userApi.getUser(userId);
        String phonenumber = sysUser.getMobile();
        DriveSchoolCoach driveSchoolCoach = driveSchoolCoachMapper.selectByPhonenumber(phonenumber);
        Long jlId = 0L;
        if (ObjectUtils.isNotEmpty(driveSchoolCoach)){
            jlId = driveSchoolCoach.getId();

        }
        driveSchoolReservationCourse.setUserId(jlId);
        List<DriveSchoolReservationCourse> reservationCourseListByUserId = driveSchoolReservationCourseMapper.getAppReservationCourseListByUserIds(driveSchoolReservationCourse);
        for (DriveSchoolReservationCourse driveSchoolReservationCourses : reservationCourseListByUserId) {
            Long userId1 = driveSchoolReservationCourses.getUserId();
            DriveSchoolPay infoByUserId = driveSchoolPayMapper.selectByUserId(userId1);
            if (ObjectUtils.isNotEmpty(infoByUserId)){
                DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(infoByUserId.getCourseId());
                driveSchoolReservationCourses.setUserName(infoByUserId.getName());
                driveSchoolReservationCourses.setCourseName(driveSchoolCourse.getName());
                driveSchoolReservationCourses.setPhone(infoByUserId.getPhone());
            }
        }
        return reservationCourseListByUserId;
    }

    @Override
    public int updateDriveSchoolReservationCourseByCoach(DriveSchoolReservationCourse driveSchoolReservationCourse) {
        return driveSchoolReservationCourseMapper.updateDriveSchoolReservationCourseByCoach(driveSchoolReservationCourse);
    }

}
