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.dromara.system.domain.SysOperLog;
import org.springframework.stereotype.Service;
import org.dromara.biz.domain.bo.ActHiProcinstBo;
import org.dromara.biz.domain.vo.ActHiProcinstVo;
import org.dromara.biz.domain.ActHiProcinst;
import org.dromara.biz.mapper.ActHiProcinstMapper;
import org.dromara.biz.service.IActHiProcinstService;

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

/**
 * 流程实例Service业务层处理
 *
 * @author chenle
 * @date 2025-05-14
 */
@RequiredArgsConstructor
@Service
public class ActHiProcinstServiceImpl implements IActHiProcinstService {

    private final ActHiProcinstMapper baseMapper;

    /**
     * 查询流程实例
     *
     * @param id 主键
     * @return 流程实例
     */
    @Override
    public ActHiProcinstVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询流程实例列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 流程实例分页列表
     */
    @Override
    public TableDataInfo<ActHiProcinstVo> queryPageList(ActHiProcinstBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ActHiProcinst> lqw = buildQueryWrapper(bo);
        Page<ActHiProcinstVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的流程实例列表
     *
     * @param bo 查询条件
     * @return 流程实例列表
     */
    @Override
    public List<ActHiProcinstVo> queryList(ActHiProcinstBo bo) {
        LambdaQueryWrapper<ActHiProcinst> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ActHiProcinst> buildQueryWrapper(ActHiProcinstBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ActHiProcinst> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ActHiProcinst::getId);
        lqw.eq(bo.getRev() != null, ActHiProcinst::getRev, bo.getRev());
        lqw.eq(StringUtils.isNotBlank(bo.getProcInstId()), ActHiProcinst::getProcInstId, bo.getProcInstId());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessKey()), ActHiProcinst::getBusinessKey, bo.getBusinessKey());
        lqw.eq(StringUtils.isNotBlank(bo.getProcDefId()), ActHiProcinst::getProcDefId, bo.getProcDefId());
        lqw.eq(bo.getStartTime() != null, ActHiProcinst::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, ActHiProcinst::getEndTime, bo.getEndTime());
        lqw.eq(bo.getDuration() != null, ActHiProcinst::getDuration, bo.getDuration());
        lqw.eq(StringUtils.isNotBlank(bo.getStartUserId()), ActHiProcinst::getStartUserId, bo.getStartUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getStartActId()), ActHiProcinst::getStartActId, bo.getStartActId());
        lqw.eq(StringUtils.isNotBlank(bo.getEndActId()), ActHiProcinst::getEndActId, bo.getEndActId());
        lqw.eq(StringUtils.isNotBlank(bo.getSuperProcessInstanceId()), ActHiProcinst::getSuperProcessInstanceId, bo.getSuperProcessInstanceId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteReason()), ActHiProcinst::getDeleteReason, bo.getDeleteReason());
//        lqw.eq(StringUtils.isNotBlank(bo.getTenantId()), ActHiProcinst::getTenantId, bo.getTenantId());
        lqw.eq(StringUtils.isNotBlank(bo.getName()), ActHiProcinst::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCallbackId()), ActHiProcinst::getCallbackId, bo.getCallbackId());
        lqw.eq(StringUtils.isNotBlank(bo.getCallbackType()), ActHiProcinst::getCallbackType, bo.getCallbackType());
        lqw.eq(StringUtils.isNotBlank(bo.getReferenceId()), ActHiProcinst::getReferenceId, bo.getReferenceId());
        lqw.eq(StringUtils.isNotBlank(bo.getReferenceType()), ActHiProcinst::getReferenceType, bo.getReferenceType());
        lqw.eq(StringUtils.isNotBlank(bo.getPropagatedStageInstId()), ActHiProcinst::getPropagatedStageInstId, bo.getPropagatedStageInstId());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessStatus()), ActHiProcinst::getBusinessStatus, bo.getBusinessStatus());
        lqw.between(params.get("beginStartTime") != null && params.get("endStartTime") != null,
                ActHiProcinst::getStartTime, params.get("beginStartTime"), params.get("endStartTime"));
        lqw.between(params.get("beginEndTime") != null && params.get("endEndTime") != null,
                ActHiProcinst::getEndTime, params.get("beginEndTime"), params.get("endEndTime"));
        return lqw;
    }

    /**
     * 新增流程实例
     *
     * @param bo 流程实例
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ActHiProcinstBo bo) {
        ActHiProcinst add = MapstructUtils.convert(bo, ActHiProcinst.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改流程实例
     *
     * @param bo 流程实例
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ActHiProcinstBo bo) {
        ActHiProcinst update = MapstructUtils.convert(bo, ActHiProcinst.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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