<?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.AppealInfoMapper">

    <select id="getAppealInfo" resultType="com.lantone.report.dto.GetAppealInfoDTO">
        SELECT
            mai.hospital_id hospitalId,
            mbi.behospital_code behospitalCode,
            mbi.`name`,
            mbi.sex,
            mhp.age,
            mbi.beh_dept_id deptId,
            mbi.beh_dept_name deptName,
            mbi.doctor_id doctorId,
            mbi.doctor_name doctorName,
            mbi.behospital_date behospitalDate,
            mbi.leave_hospital_date leaveHospitalDate,
            mbi.doctor_id doctorId,
            mbi.doctor_name doctorName,
            mai.mode_id modeId,
            mai.mode_name modeName,
            mai.defect_content defectContent,
            mai.qcresult_detail_id qcresultDetailId,
            mai.qcresult_detail_msg qcresultDetailMsg,
            mai.qcresult_detai_value qcresultDetaiValue
        FROM
            med_appeal_info mai
        LEFT JOIN med_behospital_info mbi ON mbi.behospital_code = mai.behospital_code
        AND mbi.hospital_id = mai.hospital_id
        LEFT JOIN med_home_page mhp ON mbi.behospital_code = mhp.behospital_code
        AND mhp.hospital_id = mbi.hospital_id
        WHERE
            1 = 1
        AND mai.id = #{getAppealInfoVO.id}
    </select>
    <select id="getModeName" resultType="java.lang.String">
        SELECT
            `name`
        FROM
            qc_mode
        WHERE
            id = #{id}
        AND is_deleted = 'N'
    </select>
    <select id="getAppealExamineRecords" resultType="com.lantone.report.dto.AppealExamineRecordDTO">
        SELECT
            mai.id,
            mai.cases_entry_id casesEntryId,
            qce.`name` casesEntryName,
            mai.cases_entry_msg casesEntryMsg,
            mai.`value`,
            mai.claimant_id claimantId,
            mai.appeal_type appealType,
            mai.appeal_operation_type appealOperationType,
            mai.appeal_explain appealExplain,
            mai.check_id checkId,
            mai.remark appealRemark,
            mai.gmt_create appealCreateDate,
            mai.is_reject isReject,
            mai.cases_score casesScore,
            mae.example_status exampleStatus,
            mae.example_operation exampleOperation,
            mae.process_result processResult,
            mae.reject_reason rejectReason,
            mae.remark exampleRemark,
            mae.gmt_create appealExamineDate,
            mae.msg,
            mae.`value` score,
            u.linkman claimantName,
            u2.linkman checkName
        FROM
            med_appeal_info mai
        LEFT JOIN med_appeal_examine_info mae ON mai.id = mae.appeal_info_id
        LEFT JOIN qc_cases_entry qce ON mai.cases_entry_id = qce.id
        AND qce.is_deleted = "N"
        LEFT JOIN sys_user u ON mai.claimant_id = u.id
        LEFT JOIN sys_user u2 ON mai.check_id = u2.id
        WHERE
            1 = 1
        AND mai.work_flow_node_id != #{getAppealInfoVO.concelWorkFlowNode}
        <if test="getAppealInfoVO.hospitalId != null ">
            AND mai.hospital_id = #{getAppealInfoVO.hospitalId}
        </if>
        <if test="getAppealInfoVO.behospitalCode != null and getAppealInfoVO.behospitalCode != '' ">
            AND mai.behospital_code = #{getAppealInfoVO.behospitalCode}
        </if>
        <if test="getAppealInfoVO.casesEntryId != null ">
            AND mai.cases_entry_id = #{getAppealInfoVO.casesEntryId}
        </if>
        <if test="getAppealInfoVO.modeId != null ">
            AND mai.mode_id = #{getAppealInfoVO.modeId}
        </if>
        <if test="getAppealInfoVO.appealExplain != null and getAppealInfoVO.appealExplain != '' ">
            AND appeal_operation_type = 2   AND mai.appeal_explain = #{getAppealInfoVO.appealExplain}
        </if>
        ORDER BY
        mai.gmt_create
    </select>
    <select id="getModeIdByName" resultType="java.lang.Long">
        SELECT
            `id`
        FROM
            qc_mode
        WHERE
            name = #{name}
        AND is_deleted = 'N'
    </select>
    <select id="getAppealDept" resultType="com.lantone.report.dto.GetAppealDeptDTO">
        SELECT
            b.beh_dept_id deptId,
            b.beh_dept_name deptName
        FROM
            med_appeal_info a
        LEFT JOIN med_behospital_info b ON a.behospital_code = b.behospital_code
        WHERE
            a.work_flow_node_id != 2
            AND a.hospital_id = #{getAppealDeptVo.hospitalId}
            AND a.hospital_id = b.hospital_id
            <if test="getAppealDeptVo.claimantId != null ">
                AND a.claimant_id = #{getAppealDeptVo.claimantId}
            </if>
            <if test="getAppealDeptVo.checkId != null ">
                AND a.check_id = #{getAppealDeptVo.checkId}
            </if>
            <if test="getAppealDeptVo.isPlacefile != null and getAppealDeptVo.isPlacefile != '' ">
                AND b.is_placefile = #{getAppealDeptVo.isPlacefile}
            </if>
            GROUP BY
            b.beh_dept_id
    </select>
    <select id="getAppealMode" resultType="com.lantone.report.dto.GetAppealModeDTO">
        SELECT
        a.mode_id modeId,
        a.mode_name modeName
        FROM
        med_appeal_info a
        LEFT JOIN med_behospital_info b ON a.behospital_code = b.behospital_code
        WHERE
        a.work_flow_node_id != 2
        AND a.hospital_id = #{getAppealModeVo.hospitalId}
        AND a.hospital_id = b.hospital_id
        AND a.mode_id IS NOT NULL
        <if test="getAppealModeVo.claimantId != null ">
            AND a.claimant_id = #{getAppealModeVo.claimantId}
        </if>
        <if test="getAppealModeVo.checkId != null ">
            AND a.check_id = #{getAppealModeVo.checkId}
        </if>
        <if test="getAppealModeVo.isPlacefile != null and getAppealModeVo.isPlacefile != '' ">
            AND b.is_placefile = #{getAppealModeVo.isPlacefile}
        </if>
        GROUP BY
        a.mode_id
    </select>
</mapper>