package com.loan.system.repository;

import com.loan.system.domain.dto.BizRecommenderDTO;
import com.loan.system.domain.entity.BizRecommender;
import com.loan.system.domain.entity.User;
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 RecommenderRepository extends JpaRepository<BizRecommender,Long> {
    List<BizRecommender> findByIsDelete(Boolean isDelete);

    @Transactional
    @Modifying
    @Query(value = "UPDATE BizRecommender r SET " +
            "r.recommenderCode = CASE WHEN :#{#dto.recommenderCode} IS NOT NULL THEN :#{#dto.recommenderCode} ELSE r.recommenderCode END, " +
            "r.recommenderName = CASE WHEN :#{#dto.recommenderName} IS NOT NULL THEN :#{#dto.recommenderName} ELSE r.recommenderName END, " +
            "r.mobile = CASE WHEN :#{#dto.mobile} IS NOT NULL THEN :#{#dto.mobile} ELSE r.mobile END, " +
            "r.channelName = CASE WHEN :#{#dto.channelName} IS NOT NULL THEN :#{#dto.channelName} ELSE r.channelName END, " +
            "r.group = CASE WHEN :#{#dto.group} IS NOT NULL THEN :#{#dto.group} ELSE r.group END, " +
            "r.idCard = CASE WHEN :#{#dto.idCard} IS NOT NULL THEN :#{#dto.idCard} ELSE r.idCard END, " +
            "r.bankAccount = CASE WHEN :#{#dto.bankAccount} IS NOT NULL THEN :#{#dto.bankAccount} ELSE r.bankAccount END, " +
            "r.commissionRate = CASE WHEN :#{#dto.commissionRate} IS NOT NULL THEN :#{#dto.commissionRate} ELSE r.commissionRate END, " +
            "r.company = CASE WHEN :#{#dto.company} IS NOT NULL THEN :#{#dto.company} ELSE r.company END, " +
            "r.remark = CASE WHEN :#{#dto.remark} IS NOT NULL THEN :#{#dto.remark} ELSE r.remark END, " +
            "r.isDelete = CASE WHEN :#{#dto.isDelete} IS NOT NULL THEN :#{#dto.isDelete} ELSE r.isDelete END " +
            "WHERE r.id = :id")
    void updateRecommenderById(BizRecommenderDTO dto, Long id);

    @Query(value = "SELECT r FROM BizRecommender r WHERE r.id= :id and r.isDelete = :isDelete")
    BizRecommender findByIdAndIsDelete(Long id,boolean isDelete);
}
