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.Research;
import org.dromara.ygpassport.domain.bo.ResearchBo;
import org.dromara.ygpassport.domain.vo.ResearchVo;
import org.dromara.ygpassport.mapper.ResearchMapper;
import org.dromara.ygpassport.service.IResearchService;
import org.springframework.stereotype.Service;

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

/**
 * 科研Service业务层处理
 *
 */
@RequiredArgsConstructor
@Service
public class ResearchServiceImpl implements IResearchService {

    private final ResearchMapper baseMapper;

    /**
     * 查询科研
     */
    @Override
    public ResearchVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询科研列表
     */
    @Override
    public TableDataInfo<ResearchVo> queryPageList(ResearchBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Research> lqw = buildQueryWrapper(bo);
        Page<ResearchVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询科研列表
     */
    @Override
    public List<ResearchVo> queryList(ResearchBo bo) {
        LambdaQueryWrapper<Research> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Research> buildQueryWrapper(ResearchBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Research> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), Research::getStuNo, bo.getStuNo());
        lqw.like(StringUtils.isNotBlank(bo.getResearchName()), Research::getResearchName, bo.getResearchName());
        lqw.eq(StringUtils.isNotBlank(bo.getResearchNo()), Research::getResearchNo, bo.getResearchNo());
        lqw.eq(bo.getIsOnetow() != null, Research::getIsOnetow, bo.getIsOnetow());
        lqw.eq(bo.getPublishDate() != null, Research::getPublishDate, bo.getPublishDate());
        lqw.eq(StringUtils.isNotBlank(bo.getResearchFilePath()), Research::getResearchFilePath, bo.getResearchFilePath());
        return lqw;
    }

    /**
     * 新增科研
     */
    @Override
    public Boolean insertByBo(ResearchBo bo) {
        Research add = MapstructUtils.convert(bo, Research.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改科研
     */
    @Override
    public Boolean updateByBo(ResearchBo bo) {
        Research update = MapstructUtils.convert(bo, Research.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

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