package com.loan.system.repository;

import com.loan.system.domain.entity.Contract;
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.Collection;
import java.util.List;

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

    boolean existsByCaseIdAndBusinessAttrAndIsDelete(Long caseId, String businessAttr, boolean b);

    @Query("SELECT c FROM Contract c WHERE c.caseId = :caseId AND c.businessAttr = :businessAttr AND c.isDelete = :isDelete")
    Contract findByCaseIdAndBusinessAttrAndIsDelete(Long caseId, String businessAttr, boolean b);

    @Transactional
    @Modifying
    @Query("UPDATE Contract c SET " +
            "c.businessAttr = CASE WHEN :#{#contract.businessAttr} IS NOT NULL THEN :#{#contract.businessAttr} ELSE c.businessAttr END, " +
            "c.caseId = CASE WHEN :#{#contract.caseId} IS NOT NULL THEN :#{#contract.caseId} ELSE c.caseId END, " +
            "c.customerId = CASE WHEN :#{#contract.customerId} IS NOT NULL THEN :#{#contract.customerId} ELSE c.customerId END, " +
            "c.contractNo = CASE WHEN :#{#contract.contractNo} IS NOT NULL THEN :#{#contract.contractNo} ELSE c.contractNo END, " +
            "c.contractName = CASE WHEN :#{#contract.contractName} IS NOT NULL THEN :#{#contract.contractName} ELSE c.contractName END, " +
            "c.contractVersion = CASE WHEN :#{#contract.contractVersion} IS NOT NULL THEN :#{#contract.contractVersion} ELSE c.contractVersion END, " +
            "c.contractAmount = CASE WHEN :#{#contract.contractAmount} IS NOT NULL THEN :#{#contract.contractAmount} ELSE c.contractAmount END, " +
            "c.interestRate = CASE WHEN :#{#contract.interestRate} IS NOT NULL THEN :#{#contract.interestRate} ELSE c.interestRate END, " +
            "c.loanPeriod = CASE WHEN :#{#contract.loanPeriod} IS NOT NULL THEN :#{#contract.loanPeriod} ELSE c.loanPeriod END, " +
            "c.content = CASE WHEN :#{#contract.content} IS NOT NULL THEN :#{#contract.content} ELSE c.content END, " +
            "c.signedByCustomer = CASE WHEN :#{#contract.signedByCustomer} IS NOT NULL THEN :#{#contract.signedByCustomer} ELSE c.signedByCustomer END, " +
            "c.sifnedId = CASE WHEN :#{#contract.sifnedId} IS NOT NULL THEN :#{#contract.sifnedId} ELSE c.sifnedId END, " +
            "c.signedTime = CASE WHEN :#{#contract.signedTime} IS NOT NULL THEN :#{#contract.signedTime} ELSE c.signedTime END, " +
            "c.updateTime = CASE WHEN :#{#contract.updateTime} IS NOT NULL THEN :#{#contract.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 = :isDelete")
    void updateContractById(Long id, Contract contract, boolean isDelete);

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

    @Transactional
    @Modifying
    @Query("UPDATE Contract c SET c.isPush = true WHERE c.id = :contractId")
    void updateIsPushById(Long contractId);

    @Query("SELECT c FROM Contract c WHERE c.caseId = ?1 AND c.isPush = ?2 AND c.isDelete = ?3 ")
    List<Contract> findByCaseIdAndIsPush(Long caseId,Boolean isPush,boolean isDelete);

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

    @Transactional
    @Modifying
    @Query("UPDATE Contract c SET c.contractNo = ?2 WHERE c.id = ?1")
    void updateContractNoById(Long id, String contractNo);
}