<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.his.api.db.dao.CheckupReportDao">

    <resultMap id="BaseResultMap" type="com.example.his.api.db.pojo.CheckupReportEntity">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="appointmentId" column="appointment_id" jdbcType="INTEGER"/>
            <result property="resultId" column="result_id" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="TINYINT"/>
            <result property="filePath" column="file_path" jdbcType="VARCHAR"/>
            <result property="waybillCode" column="waybill_code" jdbcType="VARCHAR"/>
            <result property="waybillDate" column="waybill_date" jdbcType="DATE"/>
            <result property="date" column="date" jdbcType="DATE"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

<!--    <sql id="Base_Column_List">-->
<!--        id,appointment_id,result_id,-->
<!--        status,file_path,waybill_code,-->
<!--        waybill_date,date,create_time-->
<!--    </sql>-->

    <insert id="insert" parameterType="Map">
        INSERT INTO tb_checkup_report
        SET appointment_id = (SELECT id FROM tb_appointment WHERE uuid = #{uuid}),
            result_id = #{resultId},
            status = 1,
            date = CURRENT_DATE(),
            create_time = NOW()
    </insert>


    <!-- 体检报告-查询分页 -->
    <select id="searchCheckupReportByPage" parameterType="Map" resultType="HashMap">
        SELECT r.id,
        a.name,
        a.sex,
        TIMESTAMPDIFF(YEAR,a.birthday,NOW()) AS age,
        CONCAT(SUBSTRING(a.pid,1,3),"***********",SUBSTRING(a.pid,15,4)) AS pid,
        a.tel,
        CONCAT(SUBSTRING(a.tel,1,3),"****",SUBSTRING(a.tel,8,4)) AS hideTel,
        a.mailing_address AS mailingAddress,
        a.date,
        r.status,
        r.file_path AS filePath,
        r.waybill_code AS waybillCode,
        r.waybill_date AS waybillDate
        FROM tb_checkup_report r
        JOIN tb_appointment a ON r.appointment_id = a.id
        <where>
            <if test="searchWord!=null">
                OR a.tel LIKE CONCAT("%",#{searchWord},"%")
            </if>
            <if test="searchWord!=null">
                OR a.name LIKE CONCAT("%",#{searchWord},"%")
            </if>
            <if test="searchWord!=null">
                OR r.waybill_code LIKE CONCAT("%",#{searchWord},"%")
            </if>
            <if test="status!=null">
                AND r.status = #{status}
            </if>
        </where>
        ORDER BY r.id DESC
    </select>


    <!-- 体检报告-查询体检报告记录 -->
    <select id="searchCheckupReportById" parameterType="int" resultType="HashMap">
        SELECT appointment_id AS appointmentId,
               result_id      AS resultId,
               status,
               `date`
        FROM tb_checkup_report
        WHERE id = #{id}
    </select>

    <!-- 体检报告-更新体检报告记录的状态 -->
    <update id="updateCheckupReportStatus" parameterType="Map">
        UPDATE tb_checkup_report
        <set>
            <if test="status!=null">
                status = #{status},
            </if>
            <if test="filePath!=null">
                file_path = #{filePath},
            </if>
            <if test="waybillCode!=null">
                waybill_code = #{waybillCode},
            </if>
            <if test="waybillDate!=null">
                waybill_date = #{waybillDate},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 体检报告-查询体检结束十天以上还未生成体检报告的记录分页记录 -->
    <select id="searchWillGenerateReport" resultType="Integer">
        SELECT id
        FROM tb_checkup_report
        WHERE status = 1
          AND DATEDIFF(CURRENT_DATE,date) >= 1
        LIMIT 50
    </select>



</mapper>
