<?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.lantone.report.mapper.AppealExamineInfoMapper">


    <select id="getComplaintRecord" resultType="com.lantone.report.dto.GetComplaintRecordDTO">
        SELECT
        a.*, b.linkman AS claimant_name,
        c.linkman AS check_name
        FROM
        (
        SELECT
        a.*, c.example_status,
        c.gmt_create AS check_gmt_create,
        c.example_operation exampleOperation,
        c.remark
        FROM
        (
        SELECT
        a.id,
        a.behospital_code,
        a.hospital_id,
        a.qcresult_detail_id,
        a.cases_entry_id,
        a.qcresult_detail_msg,
        a.mode_name,
        a.defect_content,
        a.claimant_id,
        a.check_id,
        a.gmt_create AS claimant_gmt_create,
        b.`name`,
        b.beh_dept_name,
        a.appeal_operation_type,
        a.qcresult_detai_value qcresultDetaiValue,
        a.cases_score,
        a.is_reject,
        a.mode_id
        FROM
        (SELECT
        *
        FROM
        med_appeal_info
        WHERE
        1=1
        <if test="getComplaintRecordVO.complaintDateStart != null ">
            <![CDATA[ AND gmt_create >= #{getComplaintRecordVO.complaintDateStart}]]>
        </if>
        <if test="getComplaintRecordVO.complaintDateEnd != null ">
            <![CDATA[ AND gmt_create <= #{getComplaintRecordVO.complaintDateEnd}]]>
        </if>
        <if test="getComplaintRecordVO.modeName != null and getComplaintRecordVO.modeName != '' ">
            <![CDATA[ AND mode_name = #{getComplaintRecordVO.modeName}]]>
        </if>
        <if test="getComplaintRecordVO.operationType != null and getComplaintRecordVO.operationType != '' ">
            <![CDATA[ AND appeal_operation_type = #{getComplaintRecordVO.operationType}]]>
        </if>
        AND
        (gmt_create IN (
        SELECT
        max(gmt_create)
        FROM
        med_appeal_info
        WHERE
        cases_entry_id IS NOT NULL
        AND hospital_id = #{getComplaintRecordVO.hospitalId}
        AND claimant_id = #{getComplaintRecordVO.complaintId}
        AND work_flow_node_id != 2
        GROUP BY
        hospital_id,
        behospital_code,
        cases_entry_id
        )
        OR
        gmt_create IN (
        SELECT
        max(gmt_create)
        FROM
        med_appeal_info
        WHERE
        cases_entry_id IS NULL
        AND hospital_id = #{getComplaintRecordVO.hospitalId}
        AND appeal_operation_type = 2
        AND claimant_id = #{getComplaintRecordVO.complaintId}
        AND work_flow_node_id != 2
        GROUP BY
        hospital_id,
        behospital_code,
        mode_id,
        appeal_explain
        ))
        ) a
        LEFT JOIN med_behospital_info b ON a.behospital_code = b.behospital_code
        WHERE
        a.hospital_id = b.hospital_id
        <if test="getComplaintRecordVO.isPlacefile != null and getComplaintRecordVO.isPlacefile != '' ">
            <![CDATA[ AND b.is_placefile = #{getComplaintRecordVO.isPlacefile}]]>
        </if>
        <if test="getComplaintRecordVO.deptName != null and getComplaintRecordVO.deptName != '' ">
            <![CDATA[ AND b.beh_dept_name = #{getComplaintRecordVO.deptName}]]>
        </if>
        <if test="getComplaintRecordVO.patientName != null and getComplaintRecordVO.patientName != '' ">
            <![CDATA[ AND b.name like concat('%',#{getComplaintRecordVO.patientName},'%')]]>
        </if>
        ) a
        LEFT JOIN med_appeal_examine_info c ON a.id = c.appeal_info_id
        WHERE
        1=1
        <if test="getComplaintRecordVO.state != null and getComplaintRecordVO.state != '' ">
            <![CDATA[ AND c.example_status = #{getComplaintRecordVO.state}]]>
        </if>
        ) a
        LEFT JOIN sys_user b ON a.claimant_id = b.id
        LEFT JOIN sys_user c ON a.check_id = c.id
        WHERE b.is_deleted='N'
        AND c.is_deleted='N'
        <if test="getComplaintRecordVO.name != null and getComplaintRecordVO.name != '' ">
            <![CDATA[ AND c.linkman like concat('%',#{getComplaintRecordVO.name},'%')]]>
        </if>
    </select>

    <select id="getAppealReview" resultType="com.lantone.report.dto.GetAppealReviewDTO">
        SELECT
        a.*, b.linkman AS claimant_name,
        c.linkman AS check_name
        FROM
        (
        SELECT
        a.*, c.example_status,
        c.gmt_create AS check_gmt_create
        FROM
        (
        SELECT
        a.id,
        a.behospital_code,
        a.hospital_id,
        a.qcresult_detail_id,
        a.cases_entry_id,
        a.qcresult_detail_msg,
        a.mode_id,
        a.mode_name,
        a.defect_content,
        a.claimant_id,
        a.check_id,
        a.gmt_create AS claimant_gmt_create,
        b.`name`,
        b.beh_dept_name,
        a.appeal_operation_type
        FROM
        (
        SELECT
        *
        FROM
        `med_appeal_info`
        WHERE
        (
        gmt_create IN (
        SELECT
        max(gmt_create)
        FROM
        med_appeal_info
        WHERE
        cases_entry_id IS NOT NULL
        AND work_flow_node_id != 2
        GROUP BY
        hospital_id,
        behospital_code,
        cases_entry_id,
        check_id
        )
        OR gmt_create IN (
        SELECT
        max(gmt_create)
        FROM
        med_appeal_info
        WHERE
        cases_entry_id IS NULL
        AND work_flow_node_id != 2
        GROUP BY
        appeal_explain,
        mode_id,
        behospital_code,
        check_id
        )
        )
        AND hospital_id = #{getAppealReviewVO.hospitalId}
        AND check_id = #{getAppealReviewVO.appealId}
        <if test="getAppealReviewVO.complaintDateStart != null ">
            <![CDATA[ AND gmt_create >= #{getAppealReviewVO.complaintDateStart}]]>
        </if>
        <if test="getAppealReviewVO.complaintDateEnd != null ">
            <![CDATA[ AND gmt_create <= #{getAppealReviewVO.complaintDateEnd}]]>
        </if>
        <if test="getAppealReviewVO.modeName != null and getAppealReviewVO.modeName != '' ">
            <![CDATA[ AND mode_name = #{getAppealReviewVO.modeName}]]>
        </if>
        <if test="getAppealReviewVO.operationType != null and getAppealReviewVO.operationType != '' ">
            <![CDATA[ AND appeal_operation_type = #{getAppealReviewVO.operationType}]]>
        </if>
        ) a
        LEFT JOIN med_behospital_info b ON a.behospital_code = b.behospital_code
        WHERE
        a.hospital_id = b.hospital_id
        <if test="getAppealReviewVO.isPlacefile != null and getAppealReviewVO.isPlacefile != '' ">
            <![CDATA[ AND b.is_placefile = #{getAppealReviewVO.isPlacefile}]]>
        </if>

        <if test="getAppealReviewVO.deptName != null and getAppealReviewVO.deptName != '' ">
            <![CDATA[ AND b.beh_dept_name = #{getAppealReviewVO.deptName}]]>
        </if>
        ) a
        LEFT JOIN med_appeal_examine_info c ON a.id = c.appeal_info_id
        WHERE
        1=1
        <if test="getAppealReviewVO.state != null and getAppealReviewVO.state != '' ">
            <![CDATA[ AND c.example_status = #{getAppealReviewVO.state}]]>
        </if>
        ) a
        LEFT JOIN sys_user b ON a.claimant_id = b.id AND b.is_deleted='N'
        LEFT JOIN sys_user c ON a.check_id = c.id
        WHERE c.is_deleted='N'
        <if test="getAppealReviewVO.name != null and getAppealReviewVO.name != '' ">
            <![CDATA[ AND b.linkman like concat('%',#{getAppealReviewVO.name},'%')]]>
        </if>
    </select>


    <select id="getAuditNumber" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            (
                SELECT
                    b.behospital_code,
                    b.hospital_id
                FROM
                    med_appeal_examine_info a,
                    med_appeal_info b
                WHERE
                    a.is_deleted = 'N'
                AND a.example_status = 0
                AND a.appeal_info_id = b.id
                AND a.check_id = #{getAuditNumberVO.id}
            ) a ,
        med_behospital_info c
        where a.behospital_code = c.behospital_code
        and a.hospital_id  = c.hospital_id
        and c.hospital_id=#{getAuditNumberVO.hospitalId}
        and c.is_deleted='N'
        and c.is_placefile=#{getAuditNumberVO.analyzeType}
    </select>
</mapper>