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.ProjectBaseBo;
import com.ruoyi.scientific.domain.bo.UpdateBudgetBo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.scientific.domain.bo.ProjectApplicationBo;
import com.ruoyi.scientific.domain.vo.ProjectApplicationVo;
import com.ruoyi.scientific.domain.ProjectApplication;
import com.ruoyi.scientific.mapper.ProjectApplicationMapper;
import com.ruoyi.scientific.service.IProjectApplicationService;

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

/**
 * 项目申报Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-18
 */
@RequiredArgsConstructor
@Service
public class ProjectApplicationServiceImpl implements IProjectApplicationService {

    private final ProjectApplicationMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<ProjectApplication> buildQueryWrapper(ProjectApplicationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProjectApplication> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), ProjectApplication::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectStatus()), ProjectApplication::getProjectStatus, bo.getProjectStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectAppProcId()), ProjectApplication::getProjectAppProcId, bo.getProjectAppProcId());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectAppStaus()), ProjectApplication::getProjectAppStaus, bo.getProjectAppStaus());
        lqw.eq(bo.getProjectAppStTime() != null, ProjectApplication::getProjectAppStTime, bo.getProjectAppStTime());
        lqw.eq(bo.getProjectAppEndTime() != null, ProjectApplication::getProjectAppEndTime, bo.getProjectAppEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectLeader()), ProjectApplication::getProjectLeader, bo.getProjectLeader());
        lqw.eq(bo.getProjectBudget() != null, ProjectApplication::getProjectBudget, bo.getProjectBudget());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectLeaderPhone()), ProjectApplication::getProjectLeaderPhone, bo.getProjectLeaderPhone());


        // zqjia:自定义条件查询
        lqw.eq(bo.getProjectHandbookId() != null, ProjectApplication::getProjectHandbookId, bo.getProjectHandbookId());
        return lqw;
    }

    /**
     * 新增项目申报
     */
    @Override
    public Boolean insertByBo(ProjectApplicationBo bo) {
        // 新增的时候默认设置为未立项状态
        bo.setProjectAppStaus(ProjectBaseBo.PROJECT_NO_INIT);
        // 新增项目默认设置为未申报状态
        bo.setProjectDeclareStatus(ProjectBaseBo.PROJECT_NO_DECLARE);
        // 数据转换
        ProjectApplication add = BeanUtil.toBean(bo, ProjectApplication.class);
        // 保存实体信息
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        // 如果数据库成功插入，那么就可以获得项目的Id
        if (flag) {
            bo.setProjectId(add.getProjectId());
        }
        return flag;
    }

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

    /**
     * 项目预算追加流程
     */
    @Override
    public Boolean updateBudgetProc(ProjectApplicationBo bo) {
//        ProjectApplication update = BeanUtil.toBean(bo, ProjectApplication.class);
//        validEntityBeforeSave(update);
        // 将项目的表单转换为Map
        UpdateBudgetBo budgetBo = new UpdateBudgetBo();
        budgetBo.setProjectId(bo.getProjectId());
        //budgetBo.se
        return true;
    }

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

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

    /**
     * 根据给定的Id更新预算信息,这个功能用于支持项目预算变更。当用户发起预算变更流程，当流程
     * 完成后，通过监听流程审批是否完成，确定是否更新预算信息
     * @author : zhanli
     */
    public Boolean updateBudgetInfoById(Long projectId, Long newProjectBudget){
        // 获取项目的详细信息
        ProjectApplication projectApplication  = baseMapper.selectById(projectId);
        if (projectApplication == null) {
            return false;
        }
        // 更新预算信息
        projectApplication.setProjectBudget(newProjectBudget);
        // 保存信息
        validEntityBeforeSave(projectApplication);
        // 返回结果取决于是否更新成功
        return baseMapper.updateById(projectApplication) > 0;
    }
}
