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.ActHiTaskinstBo;
import org.dromara.biz.domain.vo.ActHiTaskinstVo;
import org.dromara.biz.domain.ActHiTaskinst;
import org.dromara.biz.mapper.ActHiTaskinstMapper;
import org.dromara.biz.service.IActHiTaskinstService;

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

/**
 * 历史任务Service业务层处理
 *
 * @author chenle
 * @date 2025-05-19
 */
@RequiredArgsConstructor
@Service
public class ActHiTaskinstServiceImpl implements IActHiTaskinstService {

    private final ActHiTaskinstMapper baseMapper;

    /**
     * 查询历史任务
     *
     * @param id 主键
     * @return 历史任务
     */
    @Override
    public ActHiTaskinstVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询历史任务列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 历史任务分页列表
     */
    @Override
    public TableDataInfo<ActHiTaskinstVo> queryPageList(ActHiTaskinstBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ActHiTaskinst> lqw = buildQueryWrapper(bo);
        Page<ActHiTaskinstVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的历史任务列表
     *
     * @param bo 查询条件
     * @return 历史任务列表
     */
    @Override
    public List<ActHiTaskinstVo> queryList(ActHiTaskinstBo bo) {
        LambdaQueryWrapper<ActHiTaskinst> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ActHiTaskinst> buildQueryWrapper(ActHiTaskinstBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ActHiTaskinst> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ActHiTaskinst::getId);
        lqw.eq(bo.getRev() != null, ActHiTaskinst::getRev, bo.getRev());
        lqw.eq(StringUtils.isNotBlank(bo.getProcDefId()), ActHiTaskinst::getProcDefId, bo.getProcDefId());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskDefId()), ActHiTaskinst::getTaskDefId, bo.getTaskDefId());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskDefKey()), ActHiTaskinst::getTaskDefKey, bo.getTaskDefKey());
        lqw.eq(StringUtils.isNotBlank(bo.getProcInstId()), ActHiTaskinst::getProcInstId, bo.getProcInstId());
        lqw.eq(StringUtils.isNotBlank(bo.getExecutionId()), ActHiTaskinst::getExecutionId, bo.getExecutionId());
        lqw.eq(StringUtils.isNotBlank(bo.getScopeId()), ActHiTaskinst::getScopeId, bo.getScopeId());
        lqw.eq(StringUtils.isNotBlank(bo.getSubScopeId()), ActHiTaskinst::getSubScopeId, bo.getSubScopeId());
        lqw.eq(StringUtils.isNotBlank(bo.getScopeType()), ActHiTaskinst::getScopeType, bo.getScopeType());
        lqw.eq(StringUtils.isNotBlank(bo.getScopeDefinitionId()), ActHiTaskinst::getScopeDefinitionId, bo.getScopeDefinitionId());
        lqw.eq(StringUtils.isNotBlank(bo.getPropagatedStageInstId()), ActHiTaskinst::getPropagatedStageInstId, bo.getPropagatedStageInstId());
        lqw.eq(StringUtils.isNotBlank(bo.getName()), ActHiTaskinst::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getParentTaskId()), ActHiTaskinst::getParentTaskId, bo.getParentTaskId());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), ActHiTaskinst::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getOwner()), ActHiTaskinst::getOwner, bo.getOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getAssignee()), ActHiTaskinst::getAssignee, bo.getAssignee());
        lqw.eq(bo.getStartTime() != null, ActHiTaskinst::getStartTime, bo.getStartTime());
        lqw.eq(bo.getClaimTime() != null, ActHiTaskinst::getClaimTime, bo.getClaimTime());
        lqw.eq(bo.getEndTime() != null, ActHiTaskinst::getEndTime, bo.getEndTime());
        lqw.eq(bo.getDuration() != null, ActHiTaskinst::getDuration, bo.getDuration());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteReason()), ActHiTaskinst::getDeleteReason, bo.getDeleteReason());
        lqw.eq(bo.getPriority() != null, ActHiTaskinst::getPriority, bo.getPriority());
        lqw.eq(bo.getDueDate() != null, ActHiTaskinst::getDueDate, bo.getDueDate());
        lqw.eq(StringUtils.isNotBlank(bo.getFormKey()), ActHiTaskinst::getFormKey, bo.getFormKey());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), ActHiTaskinst::getCategory, bo.getCategory());
        lqw.eq(bo.getLastUpdatedTime() != null, ActHiTaskinst::getLastUpdatedTime, bo.getLastUpdatedTime());
        return lqw;
    }

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

    /**
     * 修改历史任务
     *
     * @param bo 历史任务
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ActHiTaskinstBo bo) {
        ActHiTaskinst update = MapstructUtils.convert(bo, ActHiTaskinst.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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