package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.OaScheduleTemplateStepBo;
import com.ruoyi.oa.domain.vo.OaScheduleTemplateStepVo;
import com.ruoyi.oa.domain.OaScheduleTemplateStep;
import com.ruoyi.oa.mapper.OaScheduleTemplateStepMapper;
import com.ruoyi.oa.service.IOaScheduleTemplateStepService;

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

/**
 * 进度模板步骤Service业务层处理
 *
 * @author hdka
 * @date 2025-05-08
 */
@RequiredArgsConstructor
@Service
public class OaScheduleTemplateStepServiceImpl implements IOaScheduleTemplateStepService {

    private final OaScheduleTemplateStepMapper baseMapper;

    /**
     * 查询进度模板步骤
     */
    @Override
    public OaScheduleTemplateStepVo queryById(Long stepId){
        return baseMapper.selectVoById(stepId);
    }

    /**
     * 查询进度模板步骤列表
     */
    @Override
    public TableDataInfo<OaScheduleTemplateStepVo> queryPageList(OaScheduleTemplateStepBo bo, PageQuery pageQuery) {
        QueryWrapper<OaScheduleTemplateStep> lqw = buildQueryWrapperSelect(bo);
        Page<OaScheduleTemplateStepVo> result = baseMapper.selectVoPagePlus(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询进度模板步骤列表
     */
    @Override
    public List<OaScheduleTemplateStepVo> queryList(OaScheduleTemplateStepBo bo) {
        QueryWrapper<OaScheduleTemplateStep> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<OaScheduleTemplateStep> buildQueryWrapper(OaScheduleTemplateStepBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<OaScheduleTemplateStep> lqw = Wrappers.query();
        lqw.eq(bo.getTemplateId() != null, "template_id", bo.getTemplateId());
        lqw.eq(bo.getStepOrder() != null, "step_order", bo.getStepOrder());
        lqw.like(StringUtils.isNotBlank(bo.getStepName()), "step_name", bo.getStepName());
        lqw.eq(bo.getExpectedDays() != null,"expected_days", bo.getExpectedDays());
        lqw.eq(StringUtils.isNotBlank(bo.getHeader()), "header", bo.getHeader());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), "description", bo.getDescription());
        lqw.orderByAsc("step_order");
        return lqw;
    }

    private QueryWrapper<OaScheduleTemplateStep> buildQueryWrapperSelect(OaScheduleTemplateStepBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<OaScheduleTemplateStep> lqw = Wrappers.query();

        lqw.eq(bo.getTemplateId() != null, "template_id", bo.getTemplateId());
        lqw.eq(bo.getStepOrder() != null, "step_order", bo.getStepOrder());
        lqw.like(StringUtils.isNotBlank(bo.getStepName()), "step_name", bo.getStepName());
        lqw.eq(bo.getExpectedDays() != null,"expected_days", bo.getExpectedDays());
        lqw.eq(StringUtils.isNotBlank(bo.getHeader()), "header", bo.getHeader());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), "description", bo.getDescription());
        lqw.groupBy("step_name","header","expected_days","description");
        return lqw;
    }

    /**
     * 新增进度模板步骤
     */
    @Override
    public Boolean insertByBo(OaScheduleTemplateStepBo bo) {
        OaScheduleTemplateStep add = BeanUtil.toBean(bo, OaScheduleTemplateStep.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setStepId(add.getStepId());
        }
        return flag;
    }

    /**
     * 修改进度模板步骤
     */
    @Override
    public Boolean updateByBo(OaScheduleTemplateStepBo bo) {
        OaScheduleTemplateStep update = BeanUtil.toBean(bo, OaScheduleTemplateStep.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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