package com.loan.system.repository;


import com.loan.system.domain.entity.CollateralPlan;
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;

public interface CollateralPlanRepository extends JpaRepository<CollateralPlan,Long> {

    @Query("select c from CollateralPlan c where c.collateralId = ?1 and c.isDelete = ?2 and c.status='待审批'")
    CollateralPlan findByCollateralIdAndIsDelete(Long collateralId, boolean isDelete);

    @Query("select c from CollateralPlan c where c.id = ?1 and c.isDelete = ?2")
    CollateralPlan findByIdAndIsDelete(Long id, Boolean isDelete);

    @Query("select c from CollateralPlan c where c.collateralId = ?1 and c.isDelete = ?2")
    List<CollateralPlan> findsByCollateralIdAndIsDelete(Long collateralId, boolean isDelete);

    @Query("select c from CollateralPlan c where c.caseId = ?1 and c.isDelete = ?2")
    List<CollateralPlan> findByCaseIdAndIsDelete(Long caseId, boolean isDelete);

    @Modifying
    @Transactional
    @Query("update CollateralPlan c set c.isDelete = true where c.id = ?1")
    void logic_delete(Long id);

    @Modifying
    @Transactional
    @Query("update CollateralPlan c set c.userId = ?2 , c.updateTime=CURRENT_TIMESTAMP where c.collateralId = ?1")
    void updateCollateralUserIdeByid(Long collateralId, Long userId);

    @Modifying
    @Transactional
    @Query("update CollateralPlan c set c.status = ?2 , c.updateTime=CURRENT_TIMESTAMP where c.id = ?1")
    void updateCollateralStatusByid(Long collateralId, String status);

    @Modifying
    @Transactional
    @Query("update CollateralPlan c set c.operatorId = ?2 , c.operatorName = ?3 , c.updateTime=CURRENT_TIMESTAMP where c.id = ?1")
    void updateCollateraloperatorIdByid(Long collateralId, Long operatorId,String operatorName);

}
