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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.score.ScoreDataDetail;
import org.dromara.system.domain.score.bo.ScoreDataDetailBo;
import org.dromara.system.domain.score.vo.ScoreDataDetailVo;
import org.dromara.system.mapper.score.ScoreDataDetailMapper;
import org.dromara.system.service.score.IScoreDataDetailService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 成绩_成绩详情idService业务层处理
 *
 * @author boman
 * @date 2023-08-17
 */
@RequiredArgsConstructor
@Service
public class ScoreDataDetailServiceImpl implements IScoreDataDetailService {

    private final ScoreDataDetailMapper baseMapper;

    /**
     * 查询成绩_成绩详情id
     */
    @Override
    public ScoreDataDetailVo queryById(Long scoreDetailId){
        return baseMapper.selectVoById(scoreDetailId);
    }

    /**
     * 查询成绩_成绩详情id列表
     */
    @Override
    public TableDataInfo<ScoreDataDetailVo> queryPageList(ScoreDataDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ScoreDataDetail> lqw = buildQueryWrapper(bo);
        Page<ScoreDataDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询成绩_成绩详情id列表
     */
    @Override
    public List<ScoreDataDetailVo> queryList(ScoreDataDetailBo bo) {
        LambdaQueryWrapper<ScoreDataDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ScoreDataDetail> buildQueryWrapper(ScoreDataDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ScoreDataDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getScoreId() != null, ScoreDataDetail::getScoreId, bo.getScoreId());
        lqw.like(StringUtils.isNotBlank(bo.getScoreDataName()), ScoreDataDetail::getScoreDataName, bo.getScoreDataName());
        lqw.eq(bo.getScoreDataNameId() != null, ScoreDataDetail::getScoreDataNameId, bo.getScoreDataNameId());
        lqw.eq(StringUtils.isNotBlank(bo.getXueke()), ScoreDataDetail::getXueke, bo.getXueke());
        lqw.eq(bo.getScore() != null, ScoreDataDetail::getScore, bo.getScore());
        lqw.eq(bo.getManfen() != null, ScoreDataDetail::getManfen, bo.getManfen());
        return lqw;
    }

    /**
     * 新增成绩_成绩详情id
     */
    @Override
    public Boolean insertByBo(ScoreDataDetailBo bo) {
        ScoreDataDetail add = MapstructUtils.convert(bo, ScoreDataDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setScoreDetailId(add.getScoreDetailId());
        }
        return flag;
    }

    /**
     * 修改成绩_成绩详情id
     */
    @Override
    public Boolean updateByBo(ScoreDataDetailBo bo) {
        ScoreDataDetail update = MapstructUtils.convert(bo, ScoreDataDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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