package com.yihu.jw.patient.dao;

import com.yihu.jw.entity.base.patient.BasePatientDO;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;
import java.util.Map;

/**
 * Created by Trick on 2018/8/31.
 */
public interface BasePatientDao extends PagingAndSortingRepository<BasePatientDO, String>, JpaSpecificationExecutor<BasePatientDO> {

    BasePatientDO findById(String id);

    BasePatientDO findByIdcardAndDel(String Idcard,String del);

    @Query("select id as id,idcard as idcard,name as name,case sex when 1 then '男' when 2 then '女' else '未知' end as sex,mobile as phone,committeeName as committeeName,concat(provinceName,cityName,townName,streetName) as address from BasePatientDO where idcard like ?1")
    List<Map<String,Object>> findByIdcard(String idcard, Pageable pageable);

    @Query("select id as id,idcard as idcard,name as name,case sex when 1 then '男' when 2 then '女' else '未知' end as sex,mobile as phone,committeeName as committeeName,concat(provinceName,cityName,townName,streetName) as address from BasePatientDO where name like ?1")
    List<Map<String,Object>> findByName(String name, Pageable pageable);

    @Query("select id as id,idcard as idcard,name as name,case sex when 1 then '男' when 2 then '女' else '未知' end as sex,mobile as phone,committeeName as committeeName,concat(provinceName,cityName,townName,streetName) as address from BasePatientDO")
    List<Map<String,Object>> findBaseInfo(Pageable pageable);

    boolean existsByIdcardAndIdNotAndDel(String idcard,String id,String del);

    boolean existsByIdcardAndDel(String idcard,String del);

    @Query("from BasePatientDO where mobile = ?1")
    List<BasePatientDO> findByMobile(String mobile);

    @Query("from BasePatientDO where del = 1")
    List<BasePatientDO> findAllByDel();

    @Query("from BasePatientDO where del = 1 and id = ?1")
    List<BasePatientDO> findAllByDelAndId(String user);

    @Query("from BasePatientDO p where p.mobile = ?1 and p.del=?2")
    List<BasePatientDO> findByMobileAndDel(String mobile,String del);
    
    BasePatientDO findByIdcard(String idcard);

//    @Modifying
//    @Query(" update BasePatientDO a set a.openid=?2 where a.id=?1 ")
//    int updateOpenId(String id, String openId);

    BasePatientDO findByIdAndDel(String id,String del);
}
