package com.xkrs.dao;

import com.xkrs.model.entity.BusinessEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @Author: XinYi Song
 * @Date: 2021/12/14 9:37
 */
public interface BusinessDao extends JpaRepository<BusinessEntity,Long>, JpaSpecificationExecutor<BusinessEntity> {

    /**
     * 根据商家名称查询商家信息
     * @param name
     * @return
     */
    BusinessEntity findByBusinessName(String name);

    /**
     * 根据手机号查询商家入驻信息
     * @param phone
     * @return
     */
    BusinessEntity findByBusinessPhone(String phone);

    /**
     * 根据商家入驻状态查询待审核，审核通过，审核不通过商家的信息
     * @param type
     * @return
     */
    List<BusinessEntity> findAllByBusinessType(String type);

    /**
     * 根据省查询审核通过的商家的信息
     * @param businessPro
     * @param type
     * @return
     */
    List<BusinessEntity> findAllByBusinessProAndAndBusinessType(String businessPro, String type);

    /**
     * 根据市查询审核通过的商家的信息
     * @param businessCity
     * @param type
     * @return
     */
    List<BusinessEntity> findAllByBusinessCityAndAndBusinessType(String businessCity, String type);

    /**
     * 根据区县查询审核通过的商家的信息
     * @param businessCounty
     * @param type
     * @return
     */
    List<BusinessEntity> findAllByBusinessCountryAndAndBusinessType(String businessCounty, String type);

    /**
     * 根据商家用户id查询自己的入驻信息
     * @param id
     * @param businessType
     * @return
     */
    BusinessEntity findByBusinessIdAndBusinessType(Integer id,String businessType);

    /**
     * 对商家入驻进行审核（1审核通过  2审核不通过）
     * @param id
     * @param time
     */
    @Modifying(clearAutomatically=true)
    @Query(value = "update business set business_type = '1',settle_in_time = ?2,business_id = ?3 where id = ?1",nativeQuery = true)
    void updatePassBusiness(Integer id,String time,Integer businessId);

    /**
     * 商家入驻审核不通过
     * @param id
     */
    @Modifying(clearAutomatically=true)
    @Query(value = "update business set business_type = '2' where id = ?1",nativeQuery = true)
    void updateNotPassBusiness(Integer id);

    /**
     * 根据商家id查询商家信息
     * @param id
     * @return
     */
    BusinessEntity findById(Integer id);


}
