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.Goal;
import org.dromara.ygpassport.domain.bo.GoalBo;
import org.dromara.ygpassport.domain.vo.GoalVo;
import org.dromara.ygpassport.mapper.GoalMapper;
import org.dromara.ygpassport.service.IGoalService;
import org.springframework.stereotype.Service;

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

/**
 * 目标信息Service业务层处理
 *
 */
@RequiredArgsConstructor
@Service
public class GoalServiceImpl implements IGoalService {

    private final GoalMapper baseMapper;

    /**
     * 查询目标信息
     */
    @Override
    public GoalVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询目标信息列表
     */
    @Override
    public TableDataInfo<GoalVo> queryPageList(GoalBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Goal> lqw = buildQueryWrapper(bo);
        Page<GoalVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询目标信息列表
     */
    @Override
    public List<GoalVo> queryList(GoalBo bo) {
        LambdaQueryWrapper<Goal> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Goal> buildQueryWrapper(GoalBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Goal> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), Goal::getStuNo, bo.getStuNo());
        lqw.eq(StringUtils.isNotBlank(bo.getGoal()), Goal::getGoal, bo.getGoal());
        return lqw;
    }

    /**
     * 新增目标信息
     */
    @Override
    public Boolean insertByBo(GoalBo bo) {
        Goal add = MapstructUtils.convert(bo, Goal.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改目标信息
     */
    @Override
    public Boolean updateByBo(GoalBo bo) {
        Goal update = MapstructUtils.convert(bo, Goal.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

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