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

import com.ankaibei.workFlow.webadmin.ankaibei.entity.CostInfoEntity;
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.Date;
import java.util.Map;

/**
 * @author lwq
 * @version 1.0
 * @description:
 * @date 2022/9/26 14:27
 */
public interface CostInfoMapper extends JpaRepository<CostInfoEntity, Integer>, JpaSpecificationExecutor<CostInfoEntity> {

    @Query(value = "SELECT co.cost_id costId,co.cost_type cosType,co.cost_status costStatus,co.relevance_user relevanceUser,co.perform_amount performAmount\n" +
            ",co.happen_date happenDate,co.payment_date paymentDate,co.project_name projectName,co.flow_task_status flowTaskStatus,co.process_instance_id processInstanceId,co.process_definition_id processDefinitionId FROM cost_info co WHERE if( ?1 !='', co.flow_task_status = ?1 , 1=1) and if( ?2 !='', co.happen_date >=?2 , 1=1)  and if( ?3 !='', co.payment_date <=?3 , 1=1) order by co.create_date desc "
            , countQuery = "SELECT count(1) from cost_info co WHERE if( ?1 !='', co.flow_task_status = ?1 , 1=1) and if( ?2 !='', co.happen_date >=?2 , 1=1)  and if( ?3 !='', co.payment_date <=?3 , 1=1) "
            , nativeQuery = true)
    Page<Map<String, String>> findCostInfoAll(Integer flowTaskStatus, Date happenDate, String paymentDate, Pageable page);


    @Transactional
    @Modifying
    @Query(value = "update cost_info SET flow_task_status = ?1 WHERE apply_cost_id = ?2 and cost_type=?3 ", nativeQuery = true)
    void updateCostInfoFlowStatus(String flowTaskStatus, Integer applyCostId, String costType);
}
