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

import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.base.service.DlDriveSchoolStudentService;
import cn.iocoder.yudao.module.course.entity.Process;
import cn.iocoder.yudao.module.course.service.ProcessService;
import cn.iocoder.yudao.module.train.entity.ReservationCourse;
import cn.iocoder.yudao.module.train.mapper.ReservationCourseMapper;
import cn.iocoder.yudao.module.train.service.ReservationCourseService;
import cn.iocoder.yudao.module.train.service.TrainService;
import cn.iocoder.yudao.module.train.vo.ReservationCourseVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 预约练车 Service 实现类
 *
 * @author 若依
 */
@Service
public class ReservationCourseServiceImpl extends ServiceImpl<ReservationCourseMapper, ReservationCourse> implements ReservationCourseService {
    @Autowired
    private ReservationCourseMapper reservationCourseMapper;
    @Autowired
    private ProcessService processService;
    @Autowired
    private TrainService trainService;
    @Autowired
    private DlDriveSchoolStudentService studentService;

    /**
     * 分页查询
     *
     * @param pageReqVO TODO
     * @param page      TODO
     * @return com.baomidou.mybatisplus.core.metadata.IPage<cn.iocoder.yudao.module.train.vo.ReservationCourseVO>
     * @author vinjor-M
     * @date 15:03 2025/1/14
     **/
    @Override
    public IPage<ReservationCourseVO> queryListPage(ReservationCourseVO pageReqVO, Page<ReservationCourseVO> page) {
        IPage<ReservationCourseVO> rtnList = reservationCourseMapper.queryListPage(pageReqVO, page);
        return rtnList;
    }

    /**
     * 根据学生userId查询学生信息及当天的预约记录
     *
     * @param userId
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author vinjor-M
     * @date 17:27 2025/1/16
     **/
    @SneakyThrows
    @Override
    public Map<String, Object> getUserInfoAndReservation(Long userId,String courseId,Integer subject,String type) {
        Map<String, Object> rtnMap = new HashMap<>();
        //当前教练ID
        Long coachId = SecurityFrameworkUtils.getLoginUserId();
        String nowDayStr = DateUtil.format(new Date(), "yyyy-MM-dd");
        //查预约记录
        LambdaQueryWrapper<ReservationCourse> queryWrapper=new LambdaQueryWrapper<ReservationCourse>()
                .eq(ReservationCourse::getUserId,userId)
                .eq(ReservationCourse::getCoachId,coachId)
                .eq(ReservationCourse::getCourseId,courseId)
                .eq(ReservationCourse::getSubject,subject)
                .eq(ReservationCourse::getIfCancel,false)
                .eq(ReservationCourse::getReservDay,nowDayStr)
                .eq(ReservationCourse::getStatus,1)
                .orderByDesc(BaseDO::getCreateTime);
        List<ReservationCourse> list = this.list(queryWrapper);
        if(!list.isEmpty()){
            rtnMap.put("reservation",list.get(0));
        }
        //查该学员，该科目进度
        Process process = processService.getByStudentAndCourse(userId,courseId,subject,coachId,null);
        if(null==process){
            throw new RuntimeException("暂未查询到该学员信息，请向管理员核实");
        }
        //查当天训练记录--未签退的
        rtnMap.put("train",trainService.getUserTrainData(userId,courseId,subject,nowDayStr));
        rtnMap.put("process",process);
        rtnMap.put("userInfo",studentService.getStudentByUserId(userId));
        return rtnMap;
    }
}
