package com.ankaibei.workFlow.webadmin.ankaibei.mapper;

import com.ankaibei.workFlow.webadmin.ankaibei.entity.TaskInfoEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.Map;

/**
 * @author
 * @version 1.0
 * @description:
 * @date 2022/9/14 17:19
 */
public interface TaskInfoMapper extends JpaRepository<TaskInfoEntity, Integer>, JpaSpecificationExecutor<TaskInfoEntity> {


    @Query(value = "SELECT t.task_id taskId,t.target_task targetTask,t.task_priority taskPriority,t.task_participant taskParticipant,t.start_time startTime,t.end_time endTime,\n" +
            "t.task_status taskStatus,t.task_creator taskCreator,t.task_index taskIndex,t.task_standard taskStandard,t.task_statements\n" +
            "taskStatements,t.project_id projectId,t.process_instance_id processInstanceId,t.flow_task_status flowTaskStatus,t.process_definition_id processDefinitionId  from task_info t WHERE if( ?1 !='', t.target_task = ?1 , 1=1) AND if( ?2 !='', t.task_status = ?2 , 1=1) and is_delete='1' order by t.create_date desc ",
            countQuery = "select count(1)from task_info t WHERE if( ?1 !='', t.target_task = ?1 , 1=1) AND if( ?2 !='', t.task_status = ?2 , 1=1)", nativeQuery = true)
    Page<Map<String, String>> findTaskInfo(String targetTask, String taskStatus, Pageable page);

    /**
     * 查询任务名称数量
     *
     * @param
     * @param targetTask
     * @return ResMsg
     */
    @Query
    int countByTargetTaskEqualsAndIsDeleteEquals(String targetTask, String isDelete);

    @Transactional
    @Modifying
    @Query(value = "UPDATE task_info t SET t.task_status = 4 WHERE t.task_id = ?1 ", nativeQuery = true)
    int performTask(Integer taskId);

    @Transactional
    @Modifying
    @Query(value = "update task_info SET flow_task_status = ?1 WHERE task_id = ?2", nativeQuery = true)
    void updateTaskFlowStatus(String flowTaskStatus, Integer taskId);

    @Transactional
    @Modifying
    @Query(value = "update task_info SET is_delete='-1' WHERE task_id = ?1", nativeQuery = true)
    void deleteTask(Integer taskId);
}
