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 com.github.yulichang.base.MPJBaseServiceImpl;
import lombok.RequiredArgsConstructor;
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.ygpassport.domain.ScoreList;
import org.dromara.ygpassport.domain.bo.ScoreListBo;
import org.dromara.ygpassport.domain.vo.ScoreListVo;
import org.dromara.ygpassport.mapper.ScoreListMapper;
import org.dromara.ygpassport.mapper.ScoreListNewMapper;
import org.dromara.ygpassport.service.IScoreListService;
import org.springframework.stereotype.Service;

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

/**
 * 学生成绩信息列Service业务层处理
 *
 * @author Lion Li
 * @date 2023-10-30
 */
@RequiredArgsConstructor
@Service
public class ScoreListServiceImpl extends MPJBaseServiceImpl<ScoreListNewMapper, ScoreList> implements IScoreListService {

    private final ScoreListMapper baseMapper;

    private final ScoreListNewMapper newMapper;

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

    /**
     * 根据学号查询学生成绩信息列
     */
    @Override
    public List<ScoreListVo> queryListByStuNo(String stuNo){
        return baseMapper.selectListVoByStuNo(stuNo);
    }

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

    /**
     * 查询学生成绩信息列列表
     */
    @Override
    public List<ScoreListVo> queryList(ScoreListBo bo) {
        LambdaQueryWrapper<ScoreList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ScoreList> buildQueryWrapper(ScoreListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ScoreList> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStuNo() != null, ScoreList::getStuNo, bo.getStuNo());
        lqw.eq(bo.getScore() != null, ScoreList::getScore, bo.getScore());
        lqw.like(StringUtils.isNotBlank(bo.getCourseName()), ScoreList::getCourseName, bo.getCourseName());
        lqw.eq(bo.getHourCredit() != null, ScoreList::getHourCredit, bo.getHourCredit());
        lqw.eq(bo.getDeleted() != null, ScoreList::getDeleted, bo.getDeleted());
        return lqw;
    }

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

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

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

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