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.ScoreDataMf;
import org.dromara.system.domain.score.bo.ScoreDataMfBo;
import org.dromara.system.domain.score.vo.ScoreDataMfVo;
import org.dromara.system.mapper.score.ScoreDataMfMapper;
import org.springframework.stereotype.Service;
import org.dromara.system.service.IScoreDataMfService;

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

/**
 * 成绩满分Service业务层处理
 *
 * @author boman
 * @date 2023-08-22
 */
@RequiredArgsConstructor
@Service
public class ScoreDataMfServiceImpl implements IScoreDataMfService {

    private final ScoreDataMfMapper baseMapper;

    /**
     * 查询成绩满分
     */
    @Override
    public ScoreDataMfVo queryById(Long mfId){
        return baseMapper.selectVoById(mfId);
    }

    /**
     * 查询成绩满分列表
     */
    @Override
    public TableDataInfo<ScoreDataMfVo> queryPageList(ScoreDataMfBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ScoreDataMf> lqw = buildQueryWrapper(bo);
        Page<ScoreDataMfVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询成绩满分列表
     */
    @Override
    public List<ScoreDataMfVo> queryList(ScoreDataMfBo bo) {
        LambdaQueryWrapper<ScoreDataMf> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ScoreDataMf> buildQueryWrapper(ScoreDataMfBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ScoreDataMf> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getScoreId() != null, ScoreDataMf::getScoreId, bo.getScoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getXueke()), ScoreDataMf::getXueke, bo.getXueke());
        lqw.eq(StringUtils.isNotBlank(bo.getManfen()), ScoreDataMf::getManfen, bo.getManfen());
        lqw.eq(StringUtils.isNotBlank(bo.getAvg()), ScoreDataMf::getAvg, bo.getAvg());
        return lqw;
    }

    /**
     * 新增成绩满分
     */
    @Override
    public Boolean insertByBo(ScoreDataMfBo bo) {
        ScoreDataMf add = MapstructUtils.convert(bo, ScoreDataMf.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMfId(add.getMfId());
        }
        return flag;
    }

    /**
     * 修改成绩满分
     */
    @Override
    public Boolean updateByBo(ScoreDataMfBo bo) {
        ScoreDataMf update = MapstructUtils.convert(bo, ScoreDataMf.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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