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.ProjectPlanBo;
import com.ruoyi.scientific.mapper.ProjectPlanMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.scientific.domain.vo.ProjectPlanVo;
import com.ruoyi.scientific.domain.ProjectPlan;
import com.ruoyi.scientific.service.IProjectPlanService;

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

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

    private final ProjectPlanMapper baseMapper;

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

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

    /**
     * 查询项目申报v2列表
     */
    @Override
    public List<ProjectPlanVo> queryList(ProjectPlanBo bo) {
        LambdaQueryWrapper<ProjectPlan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProjectPlan> buildQueryWrapper(ProjectPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProjectPlan> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPlanName()), ProjectPlan::getPlanName, bo.getPlanName());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanProcId()), ProjectPlan::getPlanProcId, bo.getPlanProcId());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanStatus()), ProjectPlan::getPlanStatus, bo.getPlanStatus());

        lqw.eq(StringUtils.isNotBlank(bo.getPlanLevel()), ProjectPlan::getPlanLevel, bo.getPlanLevel());

        lqw.eq(bo.getProjectAppStTime() != null, ProjectPlan::getProjectAppStTime, bo.getProjectAppStTime());
        lqw.eq(bo.getProjectAppEndTime() != null, ProjectPlan::getProjectAppEndTime, bo.getProjectAppEndTime());

        return lqw;
    }

    /**
     * 新增项目申报v2
     */
    @Override
    public Boolean insertByBo(ProjectPlanBo bo) {
        ProjectPlan add = BeanUtil.toBean(bo, ProjectPlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPlanId(add.getPlanId());
        }
        return flag;
    }

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

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

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

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