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.ProjectHandbookBo;
import com.ruoyi.scientific.domain.vo.ProjectHandbookVo;
import com.ruoyi.scientific.domain.ProjectHandbook;
import com.ruoyi.scientific.mapper.ProjectHandbookMapper;
import com.ruoyi.scientific.service.IProjectHandbookService;

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

/**
 * 项目申报v2Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@RequiredArgsConstructor
@Service
public class ProjectHandbookServiceImpl implements IProjectHandbookService {

    private final ProjectHandbookMapper baseMapper;

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

    /**
     * 查询项目申报v2列表
     */
    @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);
    }

    /**
     * 查询项目申报v2列表
     */
    @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.like(StringUtils.isNotBlank(bo.getHandbookName()), ProjectHandbook::getHandbookName, bo.getHandbookName());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookProcId()), ProjectHandbook::getHandbookProcId, bo.getHandbookProcId());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookStatus()), ProjectHandbook::getHandbookStatus, bo.getHandbookStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookType()), ProjectHandbook::getHandbookType, bo.getHandbookType());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookLevel()), ProjectHandbook::getHandbookLevel, bo.getHandbookLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getDeclareStatus()), ProjectHandbook::getDeclareStatus, bo.getDeclareStatus());
        lqw.eq(bo.getProjectAppStTime() != null, ProjectHandbook::getProjectAppStTime, bo.getProjectAppStTime());
        lqw.eq(bo.getProjectAppEndTime() != null, ProjectHandbook::getProjectAppEndTime, bo.getProjectAppEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookLeader()), ProjectHandbook::getHandbookLeader, bo.getHandbookLeader());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectLeaderPhone()), ProjectHandbook::getProjectLeaderPhone, bo.getProjectLeaderPhone());
        return lqw;
    }

    /**
     * 新增项目申报v2
     */
    @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;
    }

    /**
     * 修改项目申报v2
     */
    @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 做一些数据校验,如唯一约束
    }

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