package org.dromara.ygpassport.service.impl;

import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.SysUser;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.ygpassport.domain.StudentList;
import org.dromara.ygpassport.domain.bo.StudentListBo;
import org.dromara.ygpassport.domain.vo.StudentListVo;
import org.dromara.ygpassport.mapper.StudentListMapper;
import org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.StudentBo;
import org.dromara.ygpassport.domain.vo.StudentVo;
import org.dromara.ygpassport.domain.Student;
import org.dromara.ygpassport.mapper.StudentMapper;
import org.dromara.ygpassport.service.IStudentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 学生详情Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-24
 */
@RequiredArgsConstructor
@Service
public class StudentServiceImpl implements IStudentService {

    private final StudentMapper baseMapper;

    private final StudentListMapper studentListMapper;

    private final SysUserMapper userMapper;

    /**
     * 查询学生详情
     */
    @Override
    public StudentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 根据学号查询学生信息
     */
    @Override
    public StudentVo queryByStuNo(String stuNo){
        return baseMapper.selectVoByStuNo(stuNo);
    }

    /**
     * 查询学生详情列表
     */
    @Override
    public TableDataInfo<StudentVo> queryPageList(StudentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Student> lqw = buildQueryWrapper(bo);
        Page<StudentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询学生详情列表
     */
    @Override
    public List<StudentVo> queryList(StudentBo bo) {
        LambdaQueryWrapper<Student> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Student> buildQueryWrapper(StudentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Student> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), Student::getStuNo, bo.getStuNo());
        lqw.eq(bo.getAge() != null, Student::getAge, bo.getAge());
        lqw.eq(bo.getStuSex() != null, Student::getStuSex, bo.getStuSex());
        lqw.eq(bo.getStuCampus() != null, Student::getStuCampus, bo.getStuCampus());
        lqw.eq(bo.getFaculties() != null, Student::getFaculties, bo.getFaculties());
        lqw.eq(bo.getStuMajor() != null, Student::getStuMajor, bo.getStuMajor());
        lqw.eq(StringUtils.isNotBlank(bo.getStuGrade()), Student::getStuGrade, bo.getStuGrade());
        lqw.eq(StringUtils.isNotBlank(bo.getStuClass()), Student::getStuClass, bo.getStuClass());
        lqw.eq(bo.getAcademic() != null, Student::getAcademic, bo.getAcademic());
        lqw.eq(bo.getStuLevels() != null, Student::getStuLevels, bo.getStuLevels());
        lqw.eq(StringUtils.isNotBlank(bo.getImagePath()), Student::getImagePath, bo.getImagePath());
        lqw.eq(StringUtils.isNotBlank(bo.getPinyin()), Student::getPinyin, bo.getPinyin());
        lqw.eq(StringUtils.isNotBlank(bo.getNationality()), Student::getNationality, bo.getNationality());
        lqw.eq(StringUtils.isNotBlank(bo.getOrigin()), Student::getOrigin, bo.getOrigin());
        lqw.eq(StringUtils.isNotBlank(bo.getEthnic()), Student::getEthnic, bo.getEthnic());
        lqw.eq(bo.getBirthTime() != null, Student::getBirthTime, bo.getBirthTime());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), Student::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), Student::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getSpecialty()), Student::getSpecialty, bo.getSpecialty());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), Student::getAddress, bo.getAddress());
        lqw.eq(bo.getMintorId() != null, Student::getMintorId, bo.getMintorId());
        return lqw;
    }

    /**
     * 新增学生详情
     */
    @Override
    public Boolean insertByBo(StudentBo bo) {
        Student add = MapstructUtils.convert(bo, Student.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }

        StudentList studentList = new StudentList();
        studentList.setStuNo(bo.getStuNo());
        studentList.setStuName(bo.getStuName());
        studentList.setStuSex(bo.getStuSex());
        studentList.setStuMajor(bo.getStuMajor());
        studentList.setFaculties(bo.getFaculties());
        studentList.setStuGrade(bo.getStuGrade());
        studentList.setStuClass(bo.getStuClass());
        studentList.setStuLevels(bo.getStuLevels());
        studentList.setStuCampus(bo.getStuCampus());
        studentListMapper.insert(studentList);

        SysUser user = new SysUser();
        user.setUserName(bo.getStuNo());
        user.setNickName(bo.getStuName());
        user.setUserType("stu_user");
        user.setPassword("888888");
        user.setStatus("0");
        user.setCreateTime(DateUtils.getNowDate());
        userMapper.insert(user);

        return flag;
    }

    /**
     * 修改学生详情
     */
    @Override
    public Boolean updateByBo(StudentBo bo) {
        Student update = MapstructUtils.convert(bo, Student.class);
        validEntityBeforeSave(update);

        StudentListVo studentListVo = studentListMapper.selectVoByStuNo(bo.getStuNo());
        StudentList studentList = new StudentList();
        studentList.setId(studentListVo.getId());
        studentList.setStuNo(bo.getStuNo());
        studentList.setStuName(bo.getStuName());
        studentList.setStuSex(bo.getStuSex());
        studentList.setStuMajor(bo.getStuMajor());
        studentList.setFaculties(bo.getFaculties());
        studentList.setStuGrade(bo.getStuGrade());
        studentList.setStuClass(bo.getStuClass());
        studentList.setStuLevels(bo.getStuLevels());
        studentList.setStuCampus(bo.getStuCampus());
        studentListMapper.updateById(studentList);

        return baseMapper.updateById(update) > 0;
    }

    /**
     * 修改学生头像
     */
    @Override
    public Boolean updateAvatarByStuNo(String avatar, String stuNo){
        return baseMapper.updateAvatarByStuNo(avatar, stuNo);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Student entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除学生详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
