package com.ruoyi.oa.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.oa.domain.bo.OaReportSummaryBo;
import com.ruoyi.oa.domain.vo.OaReportSummaryVo;
import com.ruoyi.oa.domain.OaReportSummary;
import com.ruoyi.oa.mapper.OaReportSummaryMapper;
import com.ruoyi.oa.service.IOaReportSummaryService;

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

/**
 * 设计项目汇报概述Service业务层处理
 *
 * @author cpy
 * @date 2025-05-13
 */
@RequiredArgsConstructor
@Service
public class OaReportSummaryServiceImpl implements IOaReportSummaryService {

    private final OaReportSummaryMapper baseMapper;

    /**
     * 查询设计项目汇报概述
     */
    @Override
    public OaReportSummaryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询设计项目汇报概述列表
     */
    @Override
    public TableDataInfo<OaReportSummaryVo> queryPageList(OaReportSummaryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaReportSummary> lqw = buildQueryWrapper(bo);
//        Page<OaReportSummaryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
       Page<OaReportSummaryVo> result = baseMapper.selectVoPageWithProject(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询设计项目汇报概述列表
     */
    @Override
    public List<OaReportSummaryVo> queryList(OaReportSummaryBo bo) {
        LambdaQueryWrapper<OaReportSummary> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaReportSummary> buildQueryWrapper(OaReportSummaryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaReportSummary> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getReportTitle()), OaReportSummary::getReportTitle, bo.getReportTitle());
        lqw.eq(bo.getReportDate() != null, OaReportSummary::getReportDate, bo.getReportDate());
        lqw.like(StringUtils.isNotBlank(bo.getReporter()), OaReportSummary::getReporter, bo.getReporter());
        lqw.eq(bo.getProjectId() != null, OaReportSummary::getProjectId, bo.getProjectId());
        return lqw;
    }

    /**
     * 新增设计项目汇报概述
     */
    @Override
    public Boolean insertByBo(OaReportSummaryBo bo) {
        OaReportSummary add = BeanUtil.toBean(bo, OaReportSummary.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSummaryId(add.getSummaryId());
        }
        return flag;
    }

    /**
     * 修改设计项目汇报概述
     */
    @Override
    public Boolean updateByBo(OaReportSummaryBo bo) {
        OaReportSummary update = BeanUtil.toBean(bo, OaReportSummary.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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