package org.dromara.ygpassport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
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.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.SysUser;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.ygpassport.domain.Student;
import org.dromara.ygpassport.domain.StudentList;
import org.dromara.ygpassport.domain.bo.StudentBo;
import org.dromara.ygpassport.domain.bo.StudentListBo;
import org.dromara.ygpassport.domain.vo.StudentListVo;
import org.dromara.ygpassport.mapper.StudentListMapper;
import org.dromara.ygpassport.mapper.StudentMapper;
import org.dromara.ygpassport.service.IStudentListService;
import org.springframework.stereotype.Service;

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

@RequiredArgsConstructor
@Service
public class StudentListServiceImpl implements IStudentListService {

    private final StudentListMapper baseMapper;

    private final StudentMapper studentMapper;

    private final SysUserMapper userMapper;

    /**
     * 查询学生信息
     */
    @Override
    public StudentListVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

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

    @Override
    public StudentListVo queryByStuNoPro(String stuNo){
        return baseMapper.selectVoByStuNoPro(stuNo);
    }

    /**
     * 查询学生信息列表
     */
    @Override
    public TableDataInfo<StudentListVo> queryPageList(StudentListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StudentList> lqw = buildQueryWrapper(bo);
        Page<StudentListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询学生信息列表
     */
    @Override
    public List<StudentListVo> queryList(StudentListBo bo) {
        LambdaQueryWrapper<StudentList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StudentList> buildQueryWrapper(StudentListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StudentList> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getStuName()), StudentList::getStuName, bo.getStuName());
        lqw.eq(bo.getStuSex() != null, StudentList::getStuSex, bo.getStuSex());
        lqw.eq(bo.getStuMajor() != null, StudentList::getStuMajor, bo.getStuMajor());
        lqw.eq(bo.getFaculties() != null, StudentList::getFaculties, bo.getFaculties());
        lqw.eq(StringUtils.isNotBlank(bo.getStuGrade()), StudentList::getStuGrade, bo.getStuGrade());
        lqw.eq(StringUtils.isNotBlank(bo.getStuClass()), StudentList::getStuClass, bo.getStuClass());
        lqw.eq(bo.getStuLevels() != null, StudentList::getStuLevels, bo.getStuLevels());
        return lqw;
    }

    /**
     * 新增学生信息
     */
    @Override
    public Boolean insertByBo(StudentListBo bo) {
        StudentList add = MapstructUtils.convert(bo, StudentList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改学生信息
     */
    @Override
    public Boolean updateByBo(StudentListBo bo) {
        StudentList update = MapstructUtils.convert(bo, StudentList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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