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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.base.constant.SchoolBaseConstants;
import cn.iocoder.yudao.module.base.service.SchoolNotifyMessageSendService;
import cn.iocoder.yudao.module.course.entity.Process;
import cn.iocoder.yudao.module.course.service.ProcessService;
import cn.iocoder.yudao.module.train.entity.Train;
import cn.iocoder.yudao.module.train.mapper.TrainMapper;
import cn.iocoder.yudao.module.train.service.TrainService;
import cn.iocoder.yudao.module.train.vo.TrainVO;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.module.base.utils.DriveSchoolBaseUtil.getSubjectStr;


/**
 * 预约练车 Service 实现类
 *
 * @author 若依
 */
@Service
public class TrainServiceImpl extends ServiceImpl<TrainMapper, Train> implements TrainService {
    @Autowired
    private TrainMapper trainMapper;
    @Autowired
    @Lazy
    private ProcessService processService;
    @Resource
    private SchoolNotifyMessageSendService schoolNotifyMessageSendService;

    /**
     * 分页查询
     *
     * @param pageReqVO TODO
     * @param page      TODO
     * @author vinjor-M
     * @date 15:03 2025/1/14
     **/
    @Override
    public IPage<TrainVO> queryListPage(TrainVO pageReqVO, Page<TrainVO> page) {
        IPage<TrainVO> rtnList = trainMapper.queryListPage(pageReqVO, page);
        return rtnList;
    }

    /**
     * 分页查询训练记录
     *
     * @param pageReqVO TODO
     * @param page      TODO
     * @author vinjor-M
     * @date 15:05 2025/1/14
     **/
    @Override
    public IPage<TrainVO> queryTrainListPage(TrainVO pageReqVO, Page<TrainVO> page) {
        return trainMapper.queryTrainListPage(pageReqVO, page);
    }

    /**
     * 到场打卡
     *
     * @param trainVO
     * @author vinjor-M
     * @date 15:47 2025/1/14
     **/
    @Override
    public void createObj(TrainVO trainVO) {
        trainVO.setTrainDay(DateUtil.format(new Date(), "yyyy-MM-dd"));
        this.save(trainVO);

        // 准备消息内容
        String message = String.format(SchoolBaseConstants.SCHOOL_NOTIFY_MESSAGE_TEMPLATE_MEMBER_ATTENDANCE, DateUtil.format(trainVO.getStartTime(), "yyyy-MM-dd HH:mm:ss"), trainVO.getAddr(), trainVO.getCourseName(), getSubjectStr(trainVO.getSubject()));

        schoolNotifyMessageSendService.sendMessage(trainVO.getUserId(), message, SchoolBaseConstants.SCHOOL_NOTIFY_MESSAGE_TYPE_MEMBER, null);
    }

    /**
     * 离场打卡
     *
     * @param trainVO
     * @author vinjor-M
     * @date 15:49 2025/1/14
     **/
    @Override
    public void updateObj(TrainVO trainVO) {
        //计算累计训练时长
        Double allTrainTime = trainVO.getAllTrainTime()+trainVO.getTrainTime();
        trainVO.setAllTrainTime(allTrainTime);
        this.updateById(trainVO);
        //更新该课程学习进度的 训练时长
        Process process = new Process();
        process.setId(trainVO.getProcessId());
        process.setTrainTime(allTrainTime);
        processService.updateById(process);

        // 准备消息内容
        String message = String.format(SchoolBaseConstants.SCHOOL_NOTIFY_MESSAGE_TEMPLATE_MEMBER_END_ATTENDANCE, DateUtil.format(trainVO.getEndTime(), "yyyy-MM-dd HH:mm:ss"), trainVO.getCourseName(), getSubjectStr(trainVO.getSubject()), trainVO.getTrainTime(), trainVO.getAllTrainTime());

        schoolNotifyMessageSendService.sendMessage(trainVO.getUserId(), message, SchoolBaseConstants.SCHOOL_NOTIFY_MESSAGE_TYPE_MEMBER, null);
    }

    /**
     * 查某个学院某个课程某个科目某天的培训记录-未签退的
     *
     * @param userId   学员用户id
     * @param courseId 课程ID
     * @param subject  科目
     * @param dayStr   日期
     * @return cn.iocoder.yudao.module.train.entity.Train
     * @author vinjor-M
     * @date 10:21 2025/1/17
     **/
    @Override
    public Train getUserTrainData(Long userId, String courseId, Integer subject, String dayStr) {
        LambdaQueryWrapper<Train> queryWrapper = new LambdaQueryWrapper<Train>()
                .eq(Train::getUserId,userId)
                .eq(Train::getCourseId,courseId)
                .eq(Train::getSubject,subject)
                .eq(Train::getTrainDay,dayStr)
                .isNull(Train::getEndTime);
        List<Train> list = this.list(queryWrapper);
        return list.isEmpty()?null:list.get(0);
    }

    /**
     * 查某学生所有培训记录
     *
     * @param userId  学生id
     * @param coachId 教练id，可能为空
     * @return java.util.List<cn.iocoder.yudao.module.train.entity.Train>
     * @author vinjor-M
     * @date 16:26 2025/2/10
     **/
    @Override
    public List<Train> selectByUserIdAndCoachId(Long userId, Long coachId) {
        LambdaQueryWrapper<Train> queryWrapper = new LambdaQueryWrapper<Train>()
                .eq(Train::getUserId,userId);
        if(null!=coachId){
            queryWrapper.eq(Train::getCoachId,coachId);
        }
        queryWrapper.orderByDesc(BaseDO::getCreateTime);
        return this.list(queryWrapper);
    }

    /**
     * 批量查询
     *
     * @param trainIds 训练id集合
     * @return
     */
    @Override
    public List<TrainVO> listJoinBatchByIds(List<String> trainIds) {
        if (CollUtil.isEmpty(trainIds)) return null;
        return trainMapper.listJoinBatchByIds(trainIds);
    }
}