package com.loan.system.repository;

import com.loan.system.domain.entity.Collateral;
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 CollateralRepository extends JpaRepository<Collateral,Long> {

    List<Collateral> findByCaseIdAndIsDelete(Long caseId, boolean isDelete);

    @Transactional
    @Modifying
    @Query("UPDATE Collateral c SET " +
            "c.caseId = CASE WHEN :#{#collateral.caseId} IS NOT NULL THEN :#{#collateral.caseId} ELSE c.caseId END, " +
            "c.contractId = CASE WHEN :#{#collateral.contractId} IS NOT NULL THEN :#{#collateral.contractId} ELSE c.contractId END, " +
            "c.collateralName = CASE WHEN :#{#collateral.collateralName} IS NOT NULL THEN :#{#collateral.collateralName} ELSE c.collateralName END, " +
            "c.collateralType = CASE WHEN :#{#collateral.collateralType} IS NOT NULL THEN :#{#collateral.collateralType} ELSE c.collateralType END, " +
            "c.ownerCustomerId = CASE WHEN :#{#collateral.ownerCustomerId} IS NOT NULL THEN :#{#collateral.ownerCustomerId} ELSE c.ownerCustomerId END, " +
            "c.allocatedAmount = CASE WHEN :#{#collateral.allocatedAmount} IS NOT NULL THEN :#{#collateral.allocatedAmount} ELSE c.allocatedAmount END, " +
            "c.address = CASE WHEN :#{#collateral.address} IS NOT NULL THEN :#{#collateral.address} ELSE c.address END, " +
            "c.currentAddress = CASE WHEN :#{#collateral.currentAddress} IS NOT NULL THEN :#{#collateral.currentAddress} ELSE c.currentAddress END, " +
            "c.evalPrice = CASE WHEN :#{#collateral.evalPrice} IS NOT NULL THEN :#{#collateral.evalPrice} ELSE c.evalPrice END, " +
            "c.isInvolvedInLitigation = CASE WHEN :#{#collateral.isInvolvedInLitigation} IS NOT NULL THEN :#{#collateral.isInvolvedInLitigation} ELSE c.isInvolvedInLitigation END, " +
            "c.staus = CASE WHEN :#{#collateral.staus} IS NOT NULL THEN :#{#collateral.staus} ELSE c.staus END, " +
            "c.updateTime = CASE WHEN :#{#collateral.updateTime} IS NOT NULL THEN :#{#collateral.updateTime} ELSE c.updateTime END, " +
            "c.isDelete = CASE WHEN :isDelete IS NOT NULL THEN :isDelete ELSE c.isDelete END " +
            "WHERE c.id = :id and c.isDelete = false")
    void updateCollateralById(Long id, Collateral collateral, boolean isDelete);

    @Query("SELECT c FROM Collateral c WHERE c.collateralName = ?1 AND c.caseId = ?2 AND c.isDelete = ?3")
    Collateral findCollateralByNameAndCaseId(String collateralName, Long caseId, boolean isDelete);

    @Transactional
    @Modifying
    @Query("delete FROM Collateral c WHERE c.caseId = ?1")
    void deleteAllByCaseId(Long caseId);

    //逻辑删除
    @Modifying
    @Transactional
    @Query("UPDATE Collateral c SET c.isDelete = true , c.updateTime = CURRENT_TIMESTAMP WHERE c.id = ?1")
    void deleteById(Long id);

    @Modifying
    @Transactional
    @Query("UPDATE Collateral c SET c.staus = ?2 ,c.updateTime = CURRENT_TIMESTAMP  WHERE c.id = ?1 And c.isDelete= false ")
    void updateCollateralStatusByid(Long id, String status);
}