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 com.ruoyi.scientific.domain.bo.ProjectHandbookBo;
import com.ruoyi.scientific.mapper.ProjectHandbookMapper;
import com.ruoyi.scientific.service.IProjectHandbookService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.scientific.domain.vo.ProjectHandbookVo;
import com.ruoyi.scientific.domain.ProjectHandbook;

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

/**
 * 项目申报指南Service业务层处理
 *
 * @author zqjia
 * @date 2024-07-29
 */
@RequiredArgsConstructor
@Service
public class ProjectHandbookServiceImpl implements IProjectHandbookService {

    private final ProjectHandbookMapper baseMapper;

    /**
     * 查询项目申报指南
     */
    @Override
    public ProjectHandbookVo queryById(Long handbookId){
        return baseMapper.selectVoById(handbookId);
    }

    /**
     * 查询项目申报指南列表
     */
    @Override
    public TableDataInfo<ProjectHandbookVo> queryPageList(ProjectHandbookBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProjectHandbook> lqw = buildQueryWrapper(bo);
        Page<ProjectHandbookVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目申报指南列表
     */
    @Override
    public List<ProjectHandbookVo> queryList(ProjectHandbookBo bo) {
        LambdaQueryWrapper<ProjectHandbook> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProjectHandbook> buildQueryWrapper(ProjectHandbookBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProjectHandbook> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getHandbookId() != null, ProjectHandbook::getHandbookId, bo.getHandbookId());
        lqw.like(StringUtils.isNotBlank(bo.getHandbookName()), ProjectHandbook::getHandbookName, bo.getHandbookName());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookCategory()), ProjectHandbook::getHandbookCategory, bo.getHandbookCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookLevel()), ProjectHandbook::getHandbookLevel, bo.getHandbookLevel());
        lqw.eq(bo.getHandbookDate() != null, ProjectHandbook::getHandbookDate, bo.getHandbookDate());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookNote()), ProjectHandbook::getHandbookNote, bo.getHandbookNote());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookFile()), ProjectHandbook::getHandbookFile, bo.getHandbookFile());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookProcId()), ProjectHandbook::getHandbookProcId, bo.getHandbookProcId());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookStatus()), ProjectHandbook::getHandbookStatus, bo.getHandbookStatus());
        return lqw;
    }

    /**
     * 新增项目申报指南
     */
    @Override
    public Boolean insertByBo(ProjectHandbookBo bo) {
        ProjectHandbook add = BeanUtil.toBean(bo, ProjectHandbook.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setHandbookId(add.getHandbookId());
        }
        return flag;
    }

    /**
     * 修改项目申报指南
     */
    @Override
    public Boolean updateByBo(ProjectHandbookBo bo) {
        ProjectHandbook update = BeanUtil.toBean(bo, ProjectHandbook.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    public Boolean updateHandbookInfoById(Long handbookId, String status){
        // 获取项目的详细信息
        ProjectHandbook update  = baseMapper.selectById(handbookId);
        if (update == null) {
            return false;
        }
        // 更新状态信息
        update.setHandbookStatus(status);
        // 保存信息
        validEntityBeforeSave(update);
        // 返回结果取决于是否更新成功
        return baseMapper.updateById(update) > 0;
    }
}
