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

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskTodoPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskTodoPageRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.task.BpmTaskExtDO;
import cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceResultEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

@Mapper
public interface BpmTaskExtMapper extends BaseMapperX<BpmTaskExtDO> {

    default void updateByTaskId(BpmTaskExtDO entity) {
        update(entity, new LambdaQueryWrapper<BpmTaskExtDO>().eq(BpmTaskExtDO::getTaskId, entity.getTaskId()));
    }

    default List<BpmTaskExtDO> selectListByTaskIds(Collection<String> taskIds) {
        return selectList(BpmTaskExtDO::getTaskId, taskIds);
    }

    // TODO @海：BpmProcessInstanceResultEnum.CAN_SUB_SIGN_STATUS_LIST) 应该作为条件，mapper 不要有业务
    default List<BpmTaskExtDO> selectProcessListByTaskIds(Collection<String> taskIds) {
        return selectList(new LambdaQueryWrapperX<BpmTaskExtDO>()
                .in(BpmTaskExtDO::getTaskId, taskIds)
                .in(BpmTaskExtDO::getResult, BpmProcessInstanceResultEnum.CAN_SUB_SIGN_STATUS_LIST));
    }

    default BpmTaskExtDO selectByTaskId(String taskId) {
        return selectOne(BpmTaskExtDO::getTaskId, taskId);
    }

    default void updateBatchByTaskIdList(List<String> taskIdList, BpmTaskExtDO updateObj) {
        update(updateObj, new LambdaQueryWrapper<BpmTaskExtDO>().in(BpmTaskExtDO::getTaskId, taskIdList));
    }

    default Long selectListCount(Long userId, String processInstanceId){

        return selectCount(new LambdaQueryWrapperX<BpmTaskExtDO>().eq(BpmTaskExtDO::getAssigneeUserId, userId)
                .eq(BpmTaskExtDO::getProcessInstanceId, processInstanceId)
                .eq(BpmTaskExtDO::getResult, BpmProcessInstanceResultEnum.APPROVE.getResult()));
    }

    default List<BpmTaskExtDO> selectTask(List<String> processInstanceId, Integer result) {

        return selectList(new LambdaQueryWrapperX<BpmTaskExtDO>()
                .inIfPresent(BpmTaskExtDO::getProcessInstanceId, processInstanceId)
                .eqIfPresent(BpmTaskExtDO::getResult, result)
                .orderByDesc(BpmTaskExtDO::getCreateTime));
    }

    IPage<BpmTaskTodoPageRespVO> selectTodoTaskPage(@Param("page") IPage<BpmTaskTodoPageRespVO> page,
                                                    @Param("reqVO") BpmTaskTodoPageReqVO reqVO,
                                                    @Param("userId") Long userId);

    List<Long> selectTodoTaskDept(@Param("reqVO") BpmTaskTodoPageReqVO pageVO,
                                  @Param("userId") Long userId);
}
