package com.fs.his.mapper;

import java.util.List;
import com.fs.his.domain.FsPatient;
import com.fs.his.param.FsPatientListDParam;
import com.fs.his.vo.FsPatientListDVO;
import com.fs.his.vo.FsPatientVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;

/**
 * 病人Mapper接口
 *
 * @author fs
 * @date 2023-06-07
 */
public interface FsPatientMapper
{
    /**
     * 查询病人
     *
     * @param patientId 病人主键
     * @return 病人
     */
    public FsPatient selectFsPatientByPatientId(Long patientId);

    /**
     * 查询病人列表
     *
     * @param fsPatient 病人
     * @return 病人集合
     */
    public List<FsPatient> selectFsPatientList(FsPatient fsPatient);

    /**
     * 新增病人
     *
     * @param fsPatient 病人
     * @return 结果
     */
    public int insertFsPatient(FsPatient fsPatient);

    /**
     * 修改病人
     *
     * @param fsPatient 病人
     * @return 结果
     */
    public int updateFsPatient(FsPatient fsPatient);

    /**
     * 删除病人
     *
     * @param patientId 病人主键
     * @return 结果
     */
    public int deleteFsPatientByPatientId(Long patientId);

    /**
     * 批量删除病人
     *
     * @param patientIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsPatientByPatientIds(Long[] patientIds);



    @Select({"<script> " +
"select pat.*,us.nick_name ,us.phone from fs_patient pat LEFT JOIN fs_user us ON pat.user_id=us.user_id"+
            " where 1=1 and pat.is_del=0  \n" +
            "            <if test=\"patientName != null  and patientName != ''\"> and pat.patient_name like concat('%', #{patientName}, '%')</if>\n" +
            "            <if test=\"userId != null \"> and pat.user_id = #{userId}</if>\n" +
            "            <if test=\"idCard != null  and idCard != ''\"> and pat.id_card = #{idCard}</if>\n" +
            "            <if test=\"birthday != null \"> and pat.birthday = #{birthday}</if>\n" +
            "            <if test=\"sex != null \"> and pat.sex = #{sex}</if>\n" +
            "            <if test=\"weight != null \"> and pat.weight = #{weight}</if>\n" +
            "            <if test=\"mobile != null  and mobile != ''\"> and pat.mobile like concat('%', #{mobile}, '%')</if>\n" +
            "            <if test=\"isDel != null \"> and pat.is_del = #{isDel}</if>\n" +
            "            <if test=\"status != null \"> and pat.status = #{status}</if>\n" +
            "            <if test=\"nickName != null \"> and (us.nick_name like concat(#{nickName}, '%') or us.phone like concat('%', #{nickName}, '%'))</if>\n" +
            "order by patient_id desc "+
            "</script>"})
    List<FsPatientVO> selectFsPatientListVO(FsPatientVO fsPatient);

    @Update("update fs_patient set is_del=1 where patient_id=#{patientId}")
    int updataFsPatientByPatientId(Long patientId);
    @Select("select ifnull(count(1),0) from fs_patient where is_del=0 and user_id=#{userId}")
    int selectFsPatientCountByUserId(long userId);

    @Select("SELECT * FROM fs_patient WHERE user_id=#{userId} limit 1")
    FsPatientVO selectFsPatientByUserId(Long userId);
    @Select("SELECT * FROM fs_patient WHERE user_id=#{userId}")
    List<FsPatientVO> selectFsPatientListByUserId(Long userId);
    @Select({"<script> " +
            "select DISTINCT p.patient_id, p.patient_name,p.id_card,p.birthday,p.sex,p.weight,p.mobile,o.create_time  from fs_inquiry_order o inner join fs_patient p  on o.patient_id=p.patient_id     " +
            "where  p.status=1 and  o.doctor_id=#{maps.doctorId}  " +
            "<if test = 'maps.patientName != null     '> " +
            "and p.patient_name like CONCAT('%',#{maps.patientName},'%')" +
            "</if>"+
            "<if test = 'maps.idCard != null     '> " +
            "and p.id_card like CONCAT('%',#{maps.idCard},'%')" +
            "</if>"+
            "<if test = 'maps.sex != null     '> " +
            "and p.sex = #{maps.sex}  " +
            "</if>"+

            "<if test = 'maps.mobile != null     '> " +
            "and p.mobile like CONCAT('%',#{maps.mobile},'%')" +
            "</if>"+
            " order by o.create_time desc "+
            "</script>"})
    List<FsPatientListDVO> selectFsPatientListDVO(@Param("maps") FsPatientListDParam param);
    @Select({"<script> " +
            "select count(1) from fs_patient pat LEFT JOIN fs_user us ON pat.user_id=us.user_id"+
            " where 1=1 and pat.is_del=0  \n" +
            "            <if test=\"patientName != null  and patientName != ''\"> and pat.patient_name like concat('%', #{patientName}, '%')</if>\n" +
            "            <if test=\"userId != null \"> and pat.user_id = #{userId}</if>\n" +
            "            <if test=\"idCard != null  and idCard != ''\"> and pat.id_card = #{idCard}</if>\n" +
            "            <if test=\"birthday != null \"> and pat.birthday = #{birthday}</if>\n" +
            "            <if test=\"sex != null \"> and pat.sex = #{sex}</if>\n" +
            "            <if test=\"weight != null \"> and pat.weight = #{weight}</if>\n" +
            "            <if test=\"mobile != null  and mobile != ''\"> and pat.mobile like concat('%', #{mobile}, '%')</if>\n" +
            "            <if test=\"isDel != null \"> and pat.is_del = #{isDel}</if>\n" +
            "            <if test=\"status != null \"> and pat.status = #{status}</if>\n" +
            "            <if test=\"nickName != null \"> and (us.nick_name like concat( #{nickName}, '%') or us.phone like concat('%', #{nickName}, '%'))</if>\n" +
            "order by patient_id desc "+
            "</script>"})
    Long selectFsPatientListVOCount(FsPatientVO fsPatient);

}
