package org.dromara.biz.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.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 org.dromara.biz.domain.bo.ActRuTaskBo;
import org.dromara.biz.domain.vo.ActRuTaskVo;
import org.dromara.biz.domain.ActRuTask;
import org.dromara.biz.mapper.ActRuTaskMapper;
import org.dromara.biz.service.IActRuTaskService;

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

/**
 * 运行时任务Service业务层处理
 *
 * @author chenle
 * @date 2025-05-15
 */
@RequiredArgsConstructor
@Service
public class ActRuTaskServiceImpl implements IActRuTaskService {

    private final ActRuTaskMapper baseMapper;

    /**
     * 查询运行时任务
     *
     * @param id 主键
     * @return 运行时任务
     */
    @Override
    public ActRuTaskVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询运行时任务列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 运行时任务分页列表
     */
    @Override
    public TableDataInfo<ActRuTaskVo> queryPageList(ActRuTaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ActRuTask> lqw = buildQueryWrapper(bo);
        Page<ActRuTaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的运行时任务列表
     *
     * @param bo 查询条件
     * @return 运行时任务列表
     */
    @Override
    public List<ActRuTaskVo> queryList(ActRuTaskBo bo) {
        LambdaQueryWrapper<ActRuTask> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ActRuTask> buildQueryWrapper(ActRuTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ActRuTask> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ActRuTask::getId);
        lqw.eq(bo.getRev() != null, ActRuTask::getRev, bo.getRev());
        lqw.eq(StringUtils.isNotBlank(bo.getExecutionId()), ActRuTask::getExecutionId, bo.getExecutionId());
        lqw.eq(StringUtils.isNotBlank(bo.getProcInstId()), ActRuTask::getProcInstId, bo.getProcInstId());
        lqw.eq(StringUtils.isNotBlank(bo.getProcDefId()), ActRuTask::getProcDefId, bo.getProcDefId());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskDefId()), ActRuTask::getTaskDefId, bo.getTaskDefId());
        lqw.eq(StringUtils.isNotBlank(bo.getScopeId()), ActRuTask::getScopeId, bo.getScopeId());
        lqw.eq(StringUtils.isNotBlank(bo.getSubScopeId()), ActRuTask::getSubScopeId, bo.getSubScopeId());
        lqw.eq(StringUtils.isNotBlank(bo.getScopeType()), ActRuTask::getScopeType, bo.getScopeType());
        lqw.eq(StringUtils.isNotBlank(bo.getScopeDefinitionId()), ActRuTask::getScopeDefinitionId, bo.getScopeDefinitionId());
        lqw.eq(StringUtils.isNotBlank(bo.getPropagatedStageInstId()), ActRuTask::getPropagatedStageInstId, bo.getPropagatedStageInstId());
        lqw.eq(StringUtils.isNotBlank(bo.getName()), ActRuTask::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getParentTaskId()), ActRuTask::getParentTaskId, bo.getParentTaskId());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), ActRuTask::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskDefKey()), ActRuTask::getTaskDefKey, bo.getTaskDefKey());
        lqw.eq(StringUtils.isNotBlank(bo.getOwner()), ActRuTask::getOwner, bo.getOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getAssignee()), ActRuTask::getAssignee, bo.getAssignee());
        lqw.eq(StringUtils.isNotBlank(bo.getDelegation()), ActRuTask::getDelegation, bo.getDelegation());
        lqw.eq(bo.getPriority() != null, ActRuTask::getPriority, bo.getPriority());
        lqw.eq(bo.getDueDate() != null, ActRuTask::getDueDate, bo.getDueDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), ActRuTask::getCategory, bo.getCategory());
        lqw.eq(bo.getSuspensionState() != null, ActRuTask::getSuspensionState, bo.getSuspensionState());
        lqw.eq(StringUtils.isNotBlank(bo.getFormKey()), ActRuTask::getFormKey, bo.getFormKey());
        lqw.eq(bo.getClaimTime() != null, ActRuTask::getClaimTime, bo.getClaimTime());
        lqw.eq(bo.getIsCountEnabled() != null, ActRuTask::getIsCountEnabled, bo.getIsCountEnabled());
        lqw.eq(bo.getVarCount() != null, ActRuTask::getVarCount, bo.getVarCount());
        lqw.eq(bo.getIdLinkCount() != null, ActRuTask::getIdLinkCount, bo.getIdLinkCount());
        lqw.eq(bo.getSubTaskCount() != null, ActRuTask::getSubTaskCount, bo.getSubTaskCount());
        return lqw;
    }

    /**
     * 新增运行时任务
     *
     * @param bo 运行时任务
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ActRuTaskBo bo) {
        ActRuTask add = MapstructUtils.convert(bo, ActRuTask.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改运行时任务
     *
     * @param bo 运行时任务
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ActRuTaskBo bo) {
        ActRuTask update = MapstructUtils.convert(bo, ActRuTask.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除运行时任务信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
