package cn.hnthyy.thmz.mapper.his.mz;

import cn.hnthyy.thmz.entity.his.mz.MzPatientMi;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface MzPatientMiMapper {
    /**
     * 按照卡号 或者病人ID 精确查询病人信息
     * @param icCardNo
     * @return
     */
    @Select("select rtrim(social_no) social_no,rtrim(hic_no) hic_no,rtrim(name) name,rtrim(sex) sex,lv_date,rtrim(patient_id) patient_id,rtrim(p_bar_code) p_bar_code," +
            "rtrim(baddebt_mark) baddebt_mark,rtrim(response_type) response_type,rtrim(charge_type) charge_type,times,rtrim(reqdel_mark) reqdel_mark,age,birth_day," +
            "rtrim(ic_card_no) ic_card_no,rtrim(phone_no) phone_no,rtrim(adress) address,rtrim(yb_card_no) yb_card_no,first_date,rtrim(rel_name) rel_name,rtrim(contract_name) contract_name from mz_patient_mi  where  ic_card_no =#{icCardNo} or patient_id=#{icCardNo} ")
    MzPatientMi selectByIcCardNo(@Param("icCardNo") String icCardNo);


    /**
     * 按照病人姓名模糊查询病人信息
     * @param name
     * @return
     */
    @Select({"<script>",
            " select top ${pageSize} rtrim(social_no) social_no,rtrim(hic_no) hic_no,rtrim(name) name,rtrim(sex) sex,lv_date,rtrim(patient_id) patient_id,rtrim(p_bar_code) p_bar_code,",
            "rtrim(baddebt_mark) baddebt_mark,rtrim(response_type) response_type,rtrim(charge_type) charge_type,times,rtrim(reqdel_mark) reqdel_mark,age,birth_day," ,
            "rtrim(ic_card_no) ic_card_no,rtrim(phone_no) phone_no,rtrim(adress) address,rtrim(yb_card_no) yb_card_no,first_date,rtrim(rel_name) rel_name," +
                    "rtrim(contract_name) contract_name from   " ,
            "(SELECT ROW_NUMBER() OVER (ORDER BY mz_patient_mi.ic_card_no desc) AS RowNumber,* from mz_patient_mi ",
            "<when test='name!=null'>",
            "  where  name like #{name,jdbcType=VARCHAR}",
            "</when>",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc",
            "</script>"})
    List<MzPatientMi> selectByNameFuzzy(@Param("name") String name,@Param("pageIndex") Integer pageIndex,@Param("pageSize") Integer pageSize);


    /**
     * 按照id查询病人信息
     * @param patientId
     * @return
     */
    @Select("select rtrim(social_no) social_no,rtrim(hic_no) hic_no,rtrim(name) name,rtrim(sex) sex,lv_date,rtrim(patient_id) patient_id,rtrim(p_bar_code) p_bar_code," +
            "rtrim(baddebt_mark) baddebt_mark,rtrim(response_type) response_type,rtrim(charge_type) charge_type,times,rtrim(reqdel_mark) reqdel_mark,age,birth_day," +
            "rtrim(ic_card_no) ic_card_no,rtrim(phone_no) phone_no,rtrim(adress) address,rtrim(yb_card_no) yb_card_no,first_date,rtrim(rel_name) rel_name,rtrim(contract_name) contract_name from mz_patient_mi  where  patient_id =#{patientId}")
    MzPatientMi selectByPatientId(@Param("patientId") String patientId);

    /**
     * 保存病人信息
     * @param mzPatientMi
     * @return
     */
    @Insert("if  not exists (select  patient_id from mz_patient_mi where ic_card_no=#{icCardNo,jdbcType=CHAR}) " +
            "INSERT INTO mz_patient_mi(social_no,name,sex,lv_date,patient_id,response_type,charge_type,times,age,birth_day,ic_card_no,phone_no,adress,rel_name,contract_name,contract_hz) " +
            "VALUES(#{socialNo,jdbcType=CHAR},#{name,jdbcType=CHAR},#{sex,jdbcType=CHAR},#{lvDate,jdbcType=TIMESTAMP},#{patientId,jdbcType=CHAR},#{responseType,jdbcType=CHAR}," +
            "#{chargeType,jdbcType=CHAR},#{times,jdbcType=INTEGER},#{age,jdbcType=TINYINT},#{birthDay,jdbcType=TIMESTAMP},#{icCardNo,jdbcType=CHAR},#{phoneNo,jdbcType=CHAR}," +
            "#{address,jdbcType=CHAR},#{relName,jdbcType=VARCHAR},#{contractName,jdbcType=VARCHAR},#{contractHz,jdbcType=VARCHAR}) ")
    int insertMzPatientMi(MzPatientMi mzPatientMi);


    /**
     * 更新病人信息
     * @param mzPatientMi
     * @return
     */
    @Update({"<script>",
            "update mz_patient_mi ",
              "<trim prefix='set' prefixOverrides=',' suffix=' where patient_id =#{patientId,jdbcType=CHAR}' >",
                 "<when test='socialNo!=null'>",
                    "social_no =#{socialNo,jdbcType=CHAR}",
                 "</when>",
                 "<when test='name!=null'>",
                    ",name =#{name,jdbcType=CHAR}",
                 "</when>",
                 "<when test='sex!=null'>",
                   ",sex =#{sex,jdbcType=CHAR}",
                 "</when>",
                 "<when test='lvDate!=null'>",
                    ",lv_date =#{lvDate,jdbcType=TIMESTAMP}",
                 "</when>",
                 "<when test='responseType!=null'>",
                    ",response_type =#{responseType,jdbcType=CHAR}",
                 "</when>",
                 "<when test='chargeType!=null'>",
                    ",charge_type =#{chargeType,jdbcType=CHAR}",
                 "</when>",
                 "<when test='times!=null'>",
                    ",times =#{times,jdbcType=CHAR}",
                 "</when>",
                 "<when test='age!=null'>",
                    ",age =#{age,jdbcType=TINYINT}",
                 "</when>",
                 "<when test='birthDay!=null'>",
                    ",birth_day =#{birthDay,jdbcType=TIMESTAMP}",
                 "</when>",
                 "<when test='icCardNo!=null'>",
                    ",ic_card_no =#{icCardNo,jdbcType=CHAR}",
                 "</when>",
                 "<when test='phoneNo!=null'>",
                    ",phone_no =#{phoneNo,jdbcType=CHAR}",
                 "</when>",
                 "<when test='address!=null'>",
                    ",adress =#{address,jdbcType=CHAR}",
                 "</when>",
                 "<when test='relName!=null'>",
                    ",rel_name =#{relName,jdbcType=CHAR}",
                 "</when>",
                 "<when test='contractName!=null'>",
                    ",contract_name =#{contractName,jdbcType=CHAR}",
                 "</when>",
              "</trim>"
            ,"</script>"})
    int updateMzPatientMi(MzPatientMi mzPatientMi);

    /**
     * 销卡
     * @param patientId
     * @return
     */
    @Update("update mz_patient_mi set ic_card_no ='' where patient_id =#{patientId} ")
    int clearIcCardNo(@Param("patientId") String patientId);

    /**
     * 按照身份证号码查询病人信息,如果病人id参数不为空，则是查询是否存在除当前编号以外的相同身份证号码
     * @param socialNo 身份证号码
     * @param patientId 病人id
     * @return
     */
    @Select({"<script>",
            "select rtrim(social_no) social_no,rtrim(hic_no) hic_no,rtrim(name) name,rtrim(sex) sex,lv_date,rtrim(patient_id) patient_id,rtrim(p_bar_code) p_bar_code," ,
            "rtrim(baddebt_mark) baddebt_mark,rtrim(response_type) response_type,rtrim(charge_type) charge_type,times,rtrim(reqdel_mark) reqdel_mark,age,birth_day," ,
            "rtrim(ic_card_no) ic_card_no,rtrim(phone_no) phone_no,rtrim(adress) address,rtrim(yb_card_no) yb_card_no,first_date,rtrim(rel_name) rel_name," +
                    "rtrim(contract_name) contract_name,rtrim(contract_hz) contract_hz from mz_patient_mi  where  social_no =#{socialNo}",
               "<when test='patientId!=null'>",
                   " and patient_id <![CDATA[<>]]> #{patientId,jdbcType=CHAR}",
               "</when>",
            " order by lv_date desc ",
            "</script>"})
    List<MzPatientMi> selectBySocialNo(@Param("socialNo") String socialNo,@Param("patientId") String patientId);

    /**
     * 根据病人id集合查询病人信息
     * @param patientIds
     * @return
     */
    @Select({"<script>",
            "select rtrim(social_no) social_no,rtrim(hic_no) hic_no,rtrim(name) name,rtrim(sex) sex,lv_date,rtrim(patient_id) patient_id,rtrim(p_bar_code) p_bar_code," ,
            "rtrim(baddebt_mark) baddebt_mark,rtrim(response_type) response_type,rtrim(charge_type) charge_type,times,rtrim(reqdel_mark) reqdel_mark,age,birth_day," ,
            "rtrim(ic_card_no) ic_card_no,rtrim(phone_no) phone_no,rtrim(adress) address,rtrim(yb_card_no) yb_card_no,first_date,rtrim(rel_name) rel_name," +
                    "rtrim(contract_name) contract_name,rtrim(contract_hz) contract_hz from mz_patient_mi  where  patient_id in",
            "<foreach item='item' index='index' collection='patientIds' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<MzPatientMi> selectByPatientIds(@Param("patientIds") List<String> patientIds);


    /**
     * 按照手机号查询病人信息
     * @param phoneNo
     * @return
     */
    @Select("select rtrim(social_no) social_no,rtrim(hic_no) hic_no,rtrim(name) name,rtrim(sex) sex,lv_date,rtrim(patient_id) patient_id,rtrim(p_bar_code) p_bar_code," +
            "rtrim(baddebt_mark) baddebt_mark,rtrim(response_type) response_type,rtrim(charge_type) charge_type,times,rtrim(reqdel_mark) reqdel_mark,age,birth_day," +
            "rtrim(ic_card_no) ic_card_no,rtrim(phone_no) phone_no,rtrim(adress) address,rtrim(yb_card_no) yb_card_no,first_date,rtrim(rel_name) rel_name," +
            "rtrim(contract_name) contract_name,rtrim(contract_hz) contract_hz from mz_patient_mi  where  phone_no =#{phoneNo}")
    List<MzPatientMi> selectByPhoneNo(@Param("phoneNo") String phoneNo);



    /**
     * 按照卡号 或者病人ID 或者病人姓名 模糊 查询病人信息
     * @param commonParams
     * @return
     */
    @Select("select rtrim(social_no) social_no,rtrim(hic_no) hic_no,rtrim(name) name,rtrim(sex) sex,lv_date,rtrim(patient_id) patient_id,rtrim(p_bar_code) p_bar_code," +
            "rtrim(baddebt_mark) baddebt_mark,rtrim(response_type) response_type,rtrim(charge_type) charge_type,times,rtrim(reqdel_mark) reqdel_mark,age,birth_day," +
            "rtrim(ic_card_no) ic_card_no,rtrim(phone_no) phone_no,rtrim(adress) address,rtrim(yb_card_no) yb_card_no,first_date,rtrim(rel_name) rel_name," +
            "rtrim(contract_name) contract_name,rtrim(contract_hz) contract_hz from mz_patient_mi  where  ic_card_no like #{commonParams} or patient_id like #{commonParams} or name like #{commonParams}")
    List<MzPatientMi> selectByCommonParams(@Param("commonParams") String commonParams);

}
