package com.loan.system.repository;

import com.loan.system.domain.entity.Customer;
import com.loan.system.domain.entity.Step;
import com.loan.system.domain.vo.StepVO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author EdwinXu
 * @date 2020/9/2 - 15:35
 * @Description
 */
public interface StepRepository extends JpaRepository<Step,Long> {

    @Transactional
    @Modifying
    @Query("update Step set status = ?1, " +
            "beginTime = CASE WHEN ?1 = '进行中' THEN ?4 ELSE beginTime END ," +
            "updateTime = CASE WHEN ?1 = '已完成' THEN ?4 ELSE updateTime END " +
            "where caseId = ?3 and stepName = ?2")
    void updateStatusByCaseIdAndStepName(String msg, String stepName, Long caseId,String currentTime);

    Step findByStepNameAndCaseId(String stepName, Long caseId);

    @Transactional
    @Modifying
    @Query("update Step set userId1 = ?2 where caseId = ?3 and  stepName = ?1")
    void updateUser1ByCaseIdAndStepName(String stepName, Long approverId, Long caseId);

    @Transactional
    @Modifying
    @Query("update Step set userId2 = ?2 where caseId = ?3 and  stepName = ?1")
    void updateUser2ByCaseIdAndStepName(String stepName, Long approverId, Long caseId);

    List<Step> findByCaseId(Long caseId);

    @Modifying
    @Transactional
    @Query("update Step set userId1 = ?2 where caseId = ?3 and  stepName = ?1")
    void updateUser1ByCaseIdAndStepName(String stepName, Long approverId, Long caseId,String currentTime);

    @Modifying
    @Transactional
    @Query("update Step set userId2 = ?2 where caseId = ?3 and  stepName = ?1")
    void updateUser2ByCaseIdAndStepName(String stepName, Long approverId, Long caseId,String currentTime);

    @Modifying
    @Transactional
    @Query("UPDATE Step SET updateTime = ?3, isDelete = ?2 WHERE id = ?1 AND isDelete = false")
    void logic_delete(Long id, boolean isDelete, String updateTime);
}
