package com.ruoyi.scientific.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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.springframework.stereotype.Service;
import com.ruoyi.scientific.domain.bo.ActProjectBo;
import com.ruoyi.scientific.domain.vo.ActProjectVo;
import com.ruoyi.scientific.domain.ActProject;
import com.ruoyi.scientific.mapper.ActProjectMapper;
import com.ruoyi.scientific.service.IActProjectService;

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

/**
 * 科研管理Service业务层处理
 *
 * @author zqjia
 * @date 2024-07-14
 */
@RequiredArgsConstructor
@Service
public class ActProjectServiceImpl implements IActProjectService {

    private final ActProjectMapper baseMapper;

    /**
     * 查询科研管理
     */
    @Override
    public ActProjectVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

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

    /**
     * 查询科研管理列表
     */
    @Override
    public List<ActProjectVo> queryList(ActProjectBo bo) {
        LambdaQueryWrapper<ActProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ActProject> buildQueryWrapper(ActProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ActProject> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getProjectId()), ActProject::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookId()), ActProject::getHandbookId, bo.getHandbookId());
        lqw.eq(bo.getCategory() != null, ActProject::getCategory, bo.getCategory());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ActProject::getName, bo.getName());
        lqw.eq(bo.getCreateTime() != null, ActProject::getCreateTime, bo.getCreateTime());
        return lqw;
    }

    /**
     * 新增科研管理
     */
    @Override
    public Boolean insertByBo(ActProjectBo bo) {
        ActProject add = BeanUtil.toBean(bo, ActProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改科研管理
     */
    @Override
    public Boolean updateByBo(ActProjectBo bo) {
        ActProject update = BeanUtil.toBean(bo, ActProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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