package cn.hnthyy.thmz.mapper.thmz;

import cn.hnthyy.thmz.entity.thmz.Clinic;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ClinicMapper {

    /**
     * 保存就诊记录
     *
     * @param clinic
     * @return
     */
    @Insert("INSERT INTO t_clinic(patient_id, patient_name, times, receipt_no,dept_code,doctor_code,icd_code,icd_text,op_id,op_day,first_or_not,clinic_status) VALUES " +
            "(#{patientId}, #{patientName}, #{times}, #{receiptNo},#{deptCode},#{doctorCode},#{icdCode},#{icdText},#{opId},#{opDay},#{firstOrNot},#{clinicStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertClinic(Clinic clinic);


    /**
     * 批量保存就诊记录
     *
     * @param clinicList
     * @return
     */
    @Insert({
            "<script>",
            "INSERT INTO  t_clinic(patient_id, patient_name, times, receipt_no,dept_code,doctor_code,icd_code,icd_text,op_id,op_day,first_or_not,clinic_status) VALUES ",
            "<foreach collection='clinicList' item='item' index='index' separator=','>",
            " (#{item.patientId}, #{item.patientName}, #{item.times}, #{item.receiptNo},#{item.deptCode},#{item.doctorCode},#{item.icdCode},#{item.icdText},#{item.opId},#{item.opDay},#{item.firstOrNot},#{item.clinicStatus}) ",
            "</foreach>",
            "</script>"
    })
    int batchInsertClinic(@Param(value = "clinicList") List<Clinic> clinicList);


    /**
     * 根据单个id查询就诊记录
     *
     * @param id
     * @return
     */
    @Select("select id,patient_id, patient_name, times, receipt_no,dept_code,doctor_code,icd_code,icd_text,op_id,op_day,first_or_not,clinic_status from t_clinic where id=#{id,jdbcType=BIGINT}")
    Clinic selectById(@Param("id") Long id);


    /**
     * 分页查询就诊记录
     *
     * @param clinic
     * @return
     */
    @Select({"<script>" +
            "select a.id,patient_id, patient_name, times, receipt_no,dept_code,doctor_code,icd_code,icd_text,op_id,op_day,first_or_not,clinic_status from t_clinic a join  (" +
            "select id from t_clinic where 1=1" +
            "<when test='beginDate!=null'>" +
            " and op_day  <![CDATA[>=]]>#{beginDate} " +
            "</when>" +
            "<when test='endDate!=null'>" +
            " and op_day  <![CDATA[<=]]>#{endDate}" +
            "</when>" +
            "<when test='patientName!=null'>" +
            " and patient_name like #{patientName}" +
            "</when>" +
            "<when test='patientId!=null'>" +
            " and patient_id like #{patientId}" +
            "</when>" +
            "<when test='deptCode!=null'>" +
            " and dept_code = #{deptCode}" +
            "</when>" +
            "<when test='doctorCode!=null'>" +
            " and doctor_code = #{doctorCode}" +
            "</when>" +
            "<when test='clinicStatuses!=null'>" +
            " and clinic_status in" +
            "<foreach item='item' index='index' collection='clinicStatuses' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</when>" +
            "<when test='patientIds!=null'>" +
            " and patient_id in" +
            "<foreach item='item' index='index' collection='patientIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</when>" +
            "<when test='commonParams!=null'>" +
            " and (patient_name like #{commonParams} or patient_id like #{commonParams})" +
            "</when>" +
            " order by id desc limit #{offset},#{pageSize} )b on a.id=b.id"
            + "</script>"})
    List<Clinic> selectClinicWithPage(Clinic clinic);

    /**
     * 按照病人id 和就诊次数与缴费次数查询病人就诊记录
     * @param patientId
     * @param times
     * @param receiptNo
     * @return
     */
    @Select("select * from t_clinic where patient_id = #{patientId}  and times=#{times} and receipt_no=#{receiptNo}")
    Clinic selectByPatientIdAndTimesAndReceiptNo(@Param("patientId") String patientId,@Param("times") Integer times,@Param("receiptNo") Integer receiptNo);

    /**
     * 查询符合条件的总数
     *
     * @param clinic
     * @return
     */
    @Select({"<script>" +
            "select count(1) from t_clinic where 1=1" +
            "<when test='beginDate!=null'>" +
            " and op_day  <![CDATA[>=]]>#{beginDate} " +
            "</when>" +
            "<when test='endDate!=null'>" +
            " and op_day  <![CDATA[<=]]>#{endDate}" +
            "</when>" +
            "<when test='patientName!=null'>" +
            " and patient_name like #{patientName}" +
            "</when>" +
            "<when test='patientId!=null'>" +
            " and patient_id like #{patientId}" +
            "</when>" +
            "<when test='deptCode!=null'>" +
            " and dept_code = #{deptCode}" +
            "</when>" +
            "<when test='doctorCode!=null'>" +
            " and doctor_code = #{doctorCode}" +
            "</when>" +
            "<when test='clinicStatuses!=null'>" +
            " and clinic_status in" +
            "<foreach item='item' index='index' collection='clinicStatuses' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</when>" +
            "<when test='patientIds!=null'>" +
            " and patient_id in" +
            "<foreach item='item' index='index' collection='patientIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</when>" +
            "<when test='commonParams!=null'>" +
            " and (patient_name like #{commonParams} or patient_id like #{commonParams})" +
            "</when>" +
            "</script>"})
    Integer selectCountClinic(Clinic clinic);

    /**
     * 修改就诊状态
     *
     * @param clinic
     * @return
     */
    @Update({"<script>",
            "update t_clinic ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where id = #{id,jdbcType=BIGINT} ' >",
            "<when test='clinicStatus!=null'>",
            ",clinic_status =#{clinicStatus}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateClinic(Clinic clinic);


    /**
     * 修改就诊状态 根据病人就诊次数和id
     *
     * @param clinic
     * @return
     */
    @Update({"<script>",
            "update t_clinic ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where patient_id = #{patientId}  and times=#{times}' >",
            "<when test='receiptNo!=null'>",
            ",receipt_no =#{receiptNo}",
            "</when>",
            "<when test='clinicStatus!=null'>",
            ",clinic_status =#{clinicStatus}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateClinicByPatientIdAndTimes(Clinic clinic);


    /**
     * 修改就诊状态 根据病人就诊次数和id以及缴费次数
     *
     * @param clinic
     * @return
     */
    @Update({"<script>",
            "update t_clinic ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where patient_id = #{patientId}  and times=#{times} and receipt_no=#{receiptNo}' >",
            "<when test='clinicStatus!=null'>",
            ",clinic_status =#{clinicStatus}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateClinicByPatientIdAndTimesAndReceiptNo(Clinic clinic);

    /**
     * 删除就诊记录
     * @param id
     * @return
     */
    @Delete("delete from t_clinic where id = #{id}")
    int deleteById(@Param("id") Long id);
}
