package org.dromara.system.service.impl.score;

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.score.ScoreDataStudent;
import org.dromara.system.domain.score.bo.ScoreDataStudentBo;
import org.dromara.system.domain.score.vo.ScoreDataStudentVo;
import org.dromara.system.mapper.score.ScoreDataStudentMapper;
import org.springframework.stereotype.Service;
import org.dromara.system.service.score.IScoreDataStudentService;

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

/**
 * 成绩_子学生idService业务层处理
 *
 * @author boman
 * @date 2023-08-17
 */
@RequiredArgsConstructor
@Service
public class ScoreDataStudentServiceImpl implements IScoreDataStudentService {

    private final ScoreDataStudentMapper baseMapper;

    /**
     * 查询成绩_子学生id
     */
    @Override
    public ScoreDataStudentVo queryById(Long scoreStudentId){
        return baseMapper.selectVoById(scoreStudentId);
    }

    /**
     * 查询成绩_子学生id列表
     */
    @Override
    public TableDataInfo<ScoreDataStudentVo> queryPageList(ScoreDataStudentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ScoreDataStudent> lqw = buildQueryWrapper(bo);
        Page<ScoreDataStudentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询成绩_子学生id列表
     */
    @Override
    public List<ScoreDataStudentVo> queryList(ScoreDataStudentBo bo) {
        LambdaQueryWrapper<ScoreDataStudent> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ScoreDataStudent> buildQueryWrapper(ScoreDataStudentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ScoreDataStudent> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getScoreId() != null, ScoreDataStudent::getScoreId, bo.getScoreId());
        lqw.like(StringUtils.isNotBlank(bo.getScoreDataName()), ScoreDataStudent::getScoreDataName, bo.getScoreDataName());
        lqw.eq(bo.getScoreDataNameId() != null, ScoreDataStudent::getScoreDataNameId, bo.getScoreDataNameId());
        lqw.eq(bo.getZongfen() != null, ScoreDataStudent::getZongfen, bo.getZongfen());
        lqw.eq(StringUtils.isNotBlank(bo.getAvg()), ScoreDataStudent::getAvg, bo.getAvg());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailsType()), ScoreDataStudent::getDetailsType, bo.getDetailsType());
        return lqw;
    }

    /**
     * 新增成绩_子学生id
     */
    @Override
    public Boolean insertByBo(ScoreDataStudentBo bo) {
        ScoreDataStudent add = MapstructUtils.convert(bo, ScoreDataStudent.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setScoreStudentId(add.getScoreStudentId());
        }
        return flag;
    }

    /**
     * 修改成绩_子学生id
     */
    @Override
    public Boolean updateByBo(ScoreDataStudentBo bo) {
        ScoreDataStudent update = MapstructUtils.convert(bo, ScoreDataStudent.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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