package com.ywt.outpatient.domain.entity.center;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * Created by daiyihua
 */
public interface UserUnionsRepository extends JpaRepository<UserUnions, Integer> {

    /**
     *  type:1-药商代表关联；
     *  2-药商、代表和医生关联；
     *  3-医生和患者关联（统一userId存放医生ID，union_user_id存放用户ID）；
     *  4-患者关注医生（统一userId存放用户ID，union_user_id存放医生ID）
     */


    /**
     * 根据药商ID得到代表人数
     * @param id
     * @return
     */
    @Query(value = "select count(1) from user_unions where user_id = ? and type = 1 and deleted = 0 ", nativeQuery = true)
    int getAgentAmountByProviderId(int id);

    /**
     * 根据经销商ID得到扫码关注医生的数量（必须要填写基本信息才行）
     * @param userId
     * @return
     */
    @Query(value = "select count(1) from user_unions where user_id = ? and type = 2 and base_status = 1 and deleted = 0 ", nativeQuery = true)
    int getScanDoctorAmount(int userId);

    /**
     * 根据药商ID或代表ID得到所有医生
     * @param userId
     * @return
     */
    @Query(value = "select * from user_unions where user_id = ? and type = 2 and deleted = 0 ", nativeQuery = true)
    List<UserUnions> getAllDoctor(int userId);


    /**
     * 根据药商ID或代表ID得到医生数量，在一段时间内
     * @param userId
     * @return
     */
    @Query(value = "select count(1) from user_unions where user_id = ? and type = 2  and create_time > ? and deleted = 0 ", nativeQuery = true)
    int getDoctorAmountByDate(int userId, String date);


    /**
     * 根据医生ID得到添加的患者数量
     * @param userId
     * @return
     */
    @Query(value = "select count(1) from user_unions where user_id = ? and type = 3 and deleted = 0 ", nativeQuery = true)
    int getPatientAmount(int userId);

    /**
     * 根据药商ID和代表ID查找药商添加的代表记录
     * @param userId
     * @param unionUserId
     * @return
     */
    @Query(value = "select * from user_unions where user_id = ? and union_user_id = ? and type = 1 and deleted = 0 limit 1", nativeQuery = true)
    UserUnions findAgentByProviderId(int userId, int unionUserId);

    /**
     * 根据代理ID查找药商
     * @param agentId
     */
    @Query(value = "select * from user_unions where union_user_id = ? and type = 1 and deleted = 0 limit 1", nativeQuery = true)
    UserUnions getUserUnionByAgent(int agentId);

    /**
     * 判断是否关联
     * @param userId
     * @param unionUserId
     * @return
     */
    @Query(value = "select count(1) from user_unions where user_id = ?  and union_user_id = ? and type = ? and deleted = 0 limit 1", nativeQuery = true)
    int checkUnion(int userId, int unionUserId, int type);

    /**
     * 判断是否（和医生）发生关联
     * @param unionUserId
     * @return
     */
    @Query(value = "select count(*) from user_unions where union_user_id = ? and type = ? and deleted = 0 limit 1", nativeQuery = true)
    int checkDoctorUnion(int unionUserId, int type);

    /**
     * 得到关联关系
     * @param userId
     * @param unionUserId
     * @return
     */
    @Query(value = "select * from user_unions where user_id = ?  and union_user_id = ? and type = ? and deleted = 0 limit 1", nativeQuery = true)
    UserUnions getUnion(int userId, int unionUserId, int type);

    @Query(value = "select count(0) from user_unions where user_id = ? and create_time >= ? and create_time < ? and type = ? and deleted = 0;", nativeQuery = true)
    int getCount(int userId, String startDate, String endDate, int type);

    /**
     * 判断是否关联
     * @param userId
     * @param unionUserId
     * @return
     */
    @Query(value = "select count(1) from user_unions where user_id = ?  and union_user_id = ? and type = ? and status = ? and deleted = 0 limit 1", nativeQuery = true)
    int checkUnion(int userId, int unionUserId, int type, int status);

    /**
     * 根据医生ID得到经销商邀请该医生的记录（每个医生只能被一位经销商邀请）
     *
     * @param doctorId
     */
    @Query(value = "select * from user_unions where union_user_id = ? and type = 2 and deleted = 0 limit 1", nativeQuery = true)
    UserUnions getDoctorBaseStatus(int doctorId);

    @Query(value = "select * from user_unions where ((user_id = ? and type = 3) or (union_user_id = ? and type = 4)) and deleted = 0", nativeQuery = true)
    List<UserUnions> getAllUnionPatients(int doctorId, int doctorId2);


    /**
     * 获取 UserUnions
     * Copy from taihe-rpc
     * @param userId
     * @param unionUserId
     * @param type
     * @return
     */
    @SuppressWarnings("javadoc")
    UserUnions getFirstByUserIdAndUnionUserIdAndTypeAndDeletedFalse(int userId, int unionUserId, int type);
}
