package com.ruoyi.project.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.project.domain.bo.ProjectApplicationBo;
import com.ruoyi.project.domain.vo.ProjectApplicationVo;
import com.ruoyi.project.domain.ProjectApplication;
import com.ruoyi.project.mapper.ProjectApplicationMapper;
import com.ruoyi.project.service.IProjectApplicationService;

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

/**
 * 项目申报Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-14
 */
@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(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());
        return lqw;
    }

    /**
     * 新增项目申报
     */
    @Override
    public Boolean insertByBo(ProjectApplicationBo bo) {
        ProjectApplication add = BeanUtil.toBean(bo, ProjectApplication.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        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;
    }

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

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