package org.dromara.ygpassport.service.impl;

import com.github.yulichang.base.MPJBaseServiceImpl;
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.ygpassport.mapper.TutorNewMapper;
import org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.TutorBo;
import org.dromara.ygpassport.domain.vo.TutorVo;
import org.dromara.ygpassport.domain.Tutor;
import org.dromara.ygpassport.mapper.TutorMapper;
import org.dromara.ygpassport.service.ITutorService;

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

/**
 * 导师组活动Service业务层处理
 *
 * @author Lion Li
 * @date 2024-01-11
 */
@RequiredArgsConstructor
@Service
public class TutorServiceImpl extends MPJBaseServiceImpl<TutorNewMapper, Tutor> implements ITutorService {

    private final TutorMapper baseMapper;

    private final TutorNewMapper newMapper;

    /**
     * 查询导师组活动
     */
    @Override
    public TutorVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询导师组活动列表
     */
    @Override
    public TableDataInfo<TutorVo> queryPageList(TutorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Tutor> lqw = buildQueryWrapper(bo);
        Page<TutorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询导师组活动列表
     */
    @Override
    public List<TutorVo> queryList(TutorBo bo) {
        LambdaQueryWrapper<Tutor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Tutor> buildQueryWrapper(TutorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Tutor> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), Tutor::getStuNo, bo.getStuNo());
        lqw.eq(bo.getSemester() != null, Tutor::getSemester, bo.getSemester());
        lqw.like(StringUtils.isNotBlank(bo.getTutorName()), Tutor::getTutorName, bo.getTutorName());
        lqw.eq(bo.getTutorType() != null, Tutor::getTutorType, bo.getTutorType());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), Tutor::getProjectName, bo.getProjectName());
        lqw.eq(bo.getScore() != null, Tutor::getScore, bo.getScore());
        return lqw;
    }

    /**
     * 新增导师组活动
     */
    @Override
    public Boolean insertByBo(TutorBo bo) {
        Tutor add = MapstructUtils.convert(bo, Tutor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改导师组活动
     */
    @Override
    public Boolean updateByBo(TutorBo bo) {
        Tutor update = MapstructUtils.convert(bo, Tutor.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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