package com.cxy.classroll.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxy.classroll.model.entity.Lesson;
import com.cxy.classroll.model.entity.Stu_lesson;
import com.cxy.classroll.model.entity.User;
import com.cxy.classroll.model.vo.CsvData;
import com.cxy.classroll.model.vo.FileParam;
import com.cxy.classroll.student.mapper.LessonInfoMapper;
import com.cxy.classroll.student.mapper.StuLessonMapper;
import com.cxy.classroll.student.mapper.UserMapper;
import com.cxy.classroll.student.service.CommonService;
import com.cxy.classroll.student.service.LessonInfoService;
import com.cxy.classroll.student.service.StuLessonService;
import com.cxy.classroll.student.util.CsvUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class StuLessonServiceImpl extends ServiceImpl<StuLessonMapper, Stu_lesson> implements StuLessonService {

    @Autowired
    CommonService commonService;

    @Autowired
    LessonInfoService lessonInfoService;

    @Autowired
    StuLessonMapper stuLessonMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    LessonInfoMapper lessonInfoMapper;

    @Override
    public List<Lesson> getStudentLessons(Long id) {
        List<Lesson> lessons_list = new ArrayList<>();
        User userInfo = commonService.getUserInfo(id);
        String no = userInfo.getNo();
        LambdaQueryWrapper<Stu_lesson> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Stu_lesson::getStudent_no,no);
        List<Stu_lesson> stuLessons = stuLessonMapper.selectList(wrapper);
        for (Stu_lesson stuLesson : stuLessons) {
            if (stuLesson != null) {
                System.out.println(stuLesson);
                String lessonName = stuLesson.getLesson_name();
                Lesson lesson = lessonInfoService.getLessonInfoByLessonName(lessonName);
                lessons_list.add(lesson);
            }
        }
        return lessons_list;
    }

    @Override
    public Boolean uploadCsvData(MultipartFile file,Long teacher_id,String lesson_name) {
        int dotIndex = lesson_name.lastIndexOf('.');
        if (dotIndex>0){
            lesson_name=lesson_name.substring(0,dotIndex);
        }
        LambdaQueryWrapper<Lesson> lessonQueryWrapper = new LambdaQueryWrapper<>();
        lessonQueryWrapper.eq(Lesson::getLesson_name,lesson_name);
        lessonQueryWrapper.eq(Lesson::getTeacher_id,teacher_id);
        Lesson lesson = lessonInfoMapper.selectOne(lessonQueryWrapper);
        if (lesson == null){
            Lesson lesson1 = new Lesson();
            lesson1.setRoll(0);
            lesson1.setStatus(0);
            lesson1.setLesson_name(lesson_name);
            lesson1.setTeacher_id(teacher_id);
            int insert = lessonInfoMapper.insert(lesson1);
            if (insert == 0){
                return false;
            }
        }
        CsvUtil csvUtil = new CsvUtil();
        List<CsvData> csvDataList = csvUtil.getCsvDataManually(file);
        for (CsvData csvData: csvDataList){
            Stu_lesson stuLesson = new Stu_lesson();
            LambdaQueryWrapper<Stu_lesson> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Stu_lesson::getStudent_no,csvData.getStudent_no());
            wrapper.eq(Stu_lesson::getLesson_name,lesson_name);
            Stu_lesson res = stuLessonMapper.selectOne(wrapper);
            if (res == null){
                stuLesson.setLesson_name(lesson_name);
                stuLesson.setStudent_no(csvData.getStudent_no());
                int insert = stuLessonMapper.insert(stuLesson);
                if (insert == 0){
                    return false;
                }
            }
        }

//        for (CsvData csvData: csvDataList){
//            Random random = new Random();
//            User user = new User();
//            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(User::getNo,csvData.getStudent_no());
//            wrapper.eq(User::getName,csvData.getName());
//            User res = userMapper.selectOne(wrapper);
//            if (res == null){
//                user.setName(csvData.getName());
//                user.setIdentify(1);
//                user.setNo(csvData.getStudent_no());
//                user.setSchool("福州大学");
//                user.setScore(random.nextInt(100) + 1);
//                int insert = userMapper.insert(user);
//                if (insert == 0){
//                    return false;
//                }
//            }
//        }
        return true;
    }

    @Override
    public List<Stu_lesson> getStuLessonListByLessonName(String lesson_name) {
        LambdaQueryWrapper<Stu_lesson> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Stu_lesson::getLesson_name, lesson_name);
        return stuLessonMapper.selectList(wrapper);
    }
}
