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.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.Scoreplan;
import org.dromara.ygpassport.domain.bo.ScoreplanBo;
import org.dromara.ygpassport.domain.vo.ScoreplanVo;
import org.dromara.ygpassport.mapper.ScoreplanMapper;
import org.dromara.ygpassport.service.IScoreplanService;
import org.springframework.stereotype.Service;

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

/**
 * 个人目标与成长计划Service业务层处理
 *
 */
@RequiredArgsConstructor
@Service
public class ScoreplanServiceImpl implements IScoreplanService {

    private final ScoreplanMapper baseMapper;

    /**
     * 查询个人目标与成长计划
     */
    @Override
    public ScoreplanVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询个人目标与成长计划列表
     */
    @Override
    public TableDataInfo<ScoreplanVo> queryPageList(ScoreplanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Scoreplan> lqw = buildQueryWrapper(bo);
        Page<ScoreplanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询个人目标与成长计划列表
     */
    @Override
    public List<ScoreplanVo> queryList(ScoreplanBo bo) {
        LambdaQueryWrapper<Scoreplan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Scoreplan> buildQueryWrapper(ScoreplanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Scoreplan> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), Scoreplan::getStuNo, bo.getStuNo());
        lqw.eq(StringUtils.isNotBlank(bo.getScore()), Scoreplan::getScore, bo.getScore());
        lqw.eq(StringUtils.isNotBlank(bo.getPlan()), Scoreplan::getPlan, bo.getPlan());
        lqw.eq(StringUtils.isNotBlank(bo.getFilePath()), Scoreplan::getFilePath, bo.getFilePath());
        return lqw;
    }

    /**
     * 新增个人目标与成长计划
     */
    @Override
    public Boolean insertByBo(ScoreplanBo bo) {
        Scoreplan add = MapstructUtils.convert(bo, Scoreplan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改个人目标与成长计划
     */
    @Override
    public Boolean updateByBo(ScoreplanBo bo) {
        Scoreplan update = MapstructUtils.convert(bo, Scoreplan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public List<ScoreplanVo> queryListByStuNo(String stuNo) {
        return baseMapper.selectVoListByStuNo(stuNo);
    }
}
