package cn.iocoder.yudao.module.bpm.dal.mysql.task;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionExtDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.task.BpmProcessInstanceExtDO;
import cn.iocoder.yudao.module.bpm.service.task.dto.ProcessInstanceVariablesDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Mapper
public interface BpmProcessInstanceExtMapper extends BaseMapperX<BpmProcessInstanceExtDO> {

    default PageResult<BpmProcessInstanceExtDO> selectCCPage(Long userId, BpmProcessInstanceMyPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BpmProcessInstanceExtDO>()
//                .eqIfPresent(BpmProcessInstanceExtDO::getStartUserId, userId)
                //只能查询流程标识定义为oa开头的流程
                .likeIfPresent(BpmProcessInstanceExtDO::getProcessDefinitionId, "oa_")
                .likeIfPresent(BpmProcessInstanceExtDO::getCcids, "[" + userId + "]")
                .eqIfPresent(BpmProcessInstanceExtDO::getProcessDefinitionId, reqVO.getProcessDefinitionId())
                .eqIfPresent(BpmProcessInstanceExtDO::getCategory, reqVO.getCategory())
                .eqIfPresent(BpmProcessInstanceExtDO::getStatus, reqVO.getStatus())
                .eqIfPresent(BpmProcessInstanceExtDO::getResult, reqVO.getResult())
                .betweenIfPresent(BpmProcessInstanceExtDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BpmProcessInstanceExtDO::getName, reqVO.getName())
                .inIfPresent(BpmProcessInstanceExtDO::getStartUserId, reqVO.getUserIds() == null ? null : Arrays.asList(reqVO.getUserIds()))
                .orderByDesc(BpmProcessInstanceExtDO::getId));
    }

    default PageResult<BpmProcessInstanceExtDO> selectPage(Long userId, BpmProcessInstanceMyPageReqVO reqVO) {
//        return selectPage(reqVO, new LambdaQueryWrapperX<BpmProcessInstanceExtDO>()
//                //只能查询流程标识定义为oa开头的流程
//                .likeIfPresent(BpmProcessInstanceExtDO::getProcessDefinitionId, "oa_")
//                .eqIfPresent(BpmProcessInstanceExtDO::getStartUserId, userId)
//                .likeIfPresent(BpmProcessInstanceExtDO::getName, reqVO.getName())
//                .eqIfPresent(BpmProcessInstanceExtDO::getProcessDefinitionId, reqVO.getProcessDefinitionId())
//                .eqIfPresent(BpmProcessInstanceExtDO::getCategory, reqVO.getCategory())
//                .eqIfPresent(BpmProcessInstanceExtDO::getStatus, reqVO.getStatus())
//                .eqIfPresent(BpmProcessInstanceExtDO::getResult, reqVO.getResult())
//                .betweenIfPresent(BpmProcessInstanceExtDO::getCreateTime, reqVO.getCreateTime())
//                .orderByDesc(BpmProcessInstanceExtDO::getId));
        Long tenantId = TenantContextHolder.getTenantId() ;
        MPJLambdaWrapperX<BpmProcessInstanceExtDO> queryWrapper = new MPJLambdaWrapperX<>();
        queryWrapper.selectAll(BpmProcessInstanceExtDO.class);
        queryWrapper.selectAs("b.BUSINESS_KEY_", BpmProcessInstanceExtDO::getBusinessKey);
        queryWrapper.selectAs("d.name", BpmProcessInstanceExtDO::getTableName);
        queryWrapper.selectAs("d.field_names", BpmProcessInstanceExtDO::getFieldNames);
        queryWrapper.leftJoin(BpmProcessDefinitionExtDO.class,"e", on -> on.
                eq(BpmProcessDefinitionExtDO::getProcessDefinitionId,
                        BpmProcessInstanceExtDO::getProcessDefinitionId)) ;
        //通过流程定义的关联业务配置表单ID,获取业务表，需要显示的中文名及英文字段
        queryWrapper.leftJoin("bpm_business_table_info d on e.business_table_info_id = d.id") ;
        //通过流程实例ID 获取流程业务主键ID
        queryWrapper.leftJoin("ACT_HI_PROCINST b on b.PROC_INST_ID_ = t.process_instance_id") ;
        //通过流程业务主键ID 查询对应的业务表，获取业务数据  业务表名，是通过流程定义获取
        //queryWrapper.leftJoin("(select * from (  select name from bpm_business_table_info where id = e.business_table_info_id ) )  c on b.BUSINESS_KEY_ = c.id") ;
        queryWrapper.eq("t.TENANT_ID", tenantId);
        queryWrapper.likeIfPresent(BpmProcessInstanceExtDO::getProcessDefinitionId, "oa_")
                .eqIfPresent(BpmProcessInstanceExtDO::getStartUserId, userId)
                .likeIfPresent(BpmProcessInstanceExtDO::getName, reqVO.getName())
                .eqIfPresent(BpmProcessInstanceExtDO::getProcessDefinitionId, reqVO.getProcessDefinitionId())
                .eqIfPresent(BpmProcessInstanceExtDO::getCategory, reqVO.getCategory())
                .eqIfPresent(BpmProcessInstanceExtDO::getStatus, reqVO.getStatus())
                .eqIfPresent(BpmProcessInstanceExtDO::getResult, reqVO.getResult())
                .betweenIfPresent(BpmProcessInstanceExtDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BpmProcessInstanceExtDO::getId) ;
        return selectJoinPage(reqVO, BpmProcessInstanceExtDO.class, queryWrapper);
    }

    default BpmProcessInstanceExtDO selectByProcessInstanceId(String processInstanceId) {
        return selectOne(BpmProcessInstanceExtDO::getProcessInstanceId, processInstanceId);
    }

    default void updateByProcessInstanceId(BpmProcessInstanceExtDO updateObj) {
        update(updateObj, new LambdaQueryWrapperX<BpmProcessInstanceExtDO>()
                .eq(BpmProcessInstanceExtDO::getProcessInstanceId, updateObj.getProcessInstanceId()));
    }


    List<BpmProcessInstanceStatisticsRespVO> getProcessInstancesGroupByModelName(BpmProcessInstanceStatisticsReqVO reqVO);

    List<BpmProcessInstanceStatisticsRespVO> getProcessInstancesGroupByResultStatus(BpmProcessInstanceStatisticsReqVO reqVO);

    default PageResult<BpmProcessInstanceExtDO> selectStatisticePage(BpmProcessInstanceMyPageReqVO reqVO) {
        //如果为空，那么查询全部
        return selectPage(reqVO, new LambdaQueryWrapperX<BpmProcessInstanceExtDO>()
                //只能查询流程标识定义为oa开头的流程
                .likeIfPresent(BpmProcessInstanceExtDO::getProcessDefinitionId, "oa_")
                .likeIfPresent(BpmProcessInstanceExtDO::getName, reqVO.getName())
                .eqIfPresent(BpmProcessInstanceExtDO::getProcessDefinitionId, reqVO.getProcessDefinitionId())
                .eqIfPresent(BpmProcessInstanceExtDO::getCategory, reqVO.getCategory())
                .eqIfPresent(BpmProcessInstanceExtDO::getStatus, reqVO.getStatus())
                .eqIfPresent(BpmProcessInstanceExtDO::getResult, reqVO.getResult())
                .betweenIfPresent(BpmProcessInstanceExtDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BpmProcessInstanceExtDO::getId));

    }

    List<BpmProcessFinishStatisticsRespVO> getUserProcessTpo10(BpmProcessInstanceStatisticsReqVO reqVO);

    List<BpmProcessFinishStatisticsRespVO> selectUnfinishProcessCount(BpmProcessInstanceStatisticsReqVO reqVO);

    /**
     *
     * @param pageReqVO
     * @return
     */
    List<BpmProcessInstanceResultStatusStatisticsGroupTimeVO> getProcessInstanceResultStatusStatisticsGroupTime(BpmProcessInstanceStatisticsReqVO pageReqVO);

    @TenantIgnore
    List<ProcessInstanceVariablesDTO> selectFormVariables(@Param("processInstanceId") String processInstanceId);
}
