<?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="WebUserOriginalChart">

	<select id="selectTotalUserOriginalChartCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)       AS "totalCount"
		  FROM (
		      	SELECT CH.PID                                  AS PID
					  ,CH.CHART_DIVI                           AS CHART_DIVI
					  ,DATE_FORMAT(CH.WRITE_DATE, '%Y-%m-%d')  AS WRITE_DATE
					  ,MU.USER_NAME                            AS USER_NAME
					  ,IFNULL((SELECT CSL.STAFF_NAME
								 FROM CRM_STAFF_LIST AS CSL
								WHERE CSL.PID = CH.CRM_DOC
								LIMIT 0, 1), '')               AS CRM_DOC_NAME
					  ,IFNULL((SELECT CSL.STAFF_NAME
								 FROM CRM_STAFF_LIST AS CSL
								WHERE CSL.PID = CH.CRM_STAFF
								LIMIT 0, 1), '')               AS CRM_STAFF_NAME
					  ,IFNULL(CH.PIC_NAME, '')                 AS PIC_NAME
					  ,IFNULL(CH.PIC_TITLE, '')                AS PIC_TITLE
				  FROM CHART_001 AS CH
			INNER JOIN MU_USER AS MU
					ON CH.CRM_MB_PID = MU.CRM_MEMBER_PID
				   AND CH.LIST_DEL = 'N'
				   AND MU.USE_YN = 'Y'
				   AND MU.MU_USER_ID = #{muUserId}
				 LIMIT 18446744073709551615
		  ) AS CH
		 WHERE 1 = 1
		 <if test="searchChartDivi != '' and searchChartDivi">
		   AND CH.CHART_DIVI = #{searchChartDivi}
		 </if>
	</select>
	
	<select id="selectListUserOriginalChart" parameterType="hashmap" resultType="hashmap">
		SELECT CH.PID                         AS "pId"
		      ,CH.CHART_DIVI                  AS "chartDivi"
		      ,CH.WRITE_DATE                  AS "writeDate"
		      ,CH.USER_NAME                   AS "userName"
		      ,CH.CRM_DOC_NAME                AS "crmDocName"
		      ,CH.CRM_STAFF_NAME              AS "crmStaffName"
		      ,CH.PIC_NAME                    AS "picName"
		      ,CH.PIC_TITLE                   AS "picTitle"
		      ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
		  FROM (
		      	SELECT CH.PID                                  AS PID
					  ,CH.CHART_DIVI                           AS CHART_DIVI
					  ,DATE_FORMAT(CH.WRITE_DATE, '%Y-%m-%d')  AS WRITE_DATE
					  ,MU.USER_NAME                            AS USER_NAME
					  ,IFNULL((SELECT CSL.STAFF_NAME
								 FROM CRM_STAFF_LIST AS CSL
								WHERE CSL.PID = CH.CRM_DOC
								LIMIT 0, 1), '')               AS CRM_DOC_NAME
					  ,IFNULL((SELECT CSL.STAFF_NAME
								 FROM CRM_STAFF_LIST AS CSL
								WHERE CSL.PID = CH.CRM_STAFF
								LIMIT 0, 1), '')               AS CRM_STAFF_NAME
					  ,IFNULL(CH.PIC_NAME, '')                 AS PIC_NAME
					  ,IFNULL(CH.PIC_TITLE, '')                AS PIC_TITLE
				  FROM CHART_001 AS CH
			INNER JOIN MU_USER AS MU
					ON CH.CRM_MB_PID = MU.CRM_MEMBER_PID
				   AND CH.LIST_DEL = 'N'
				   AND MU.USE_YN = 'Y'
				   AND MU.MU_USER_ID = #{muUserId}
				 LIMIT 18446744073709551615
		  ) AS CH, (SELECT @RNUM:=0) R
		 WHERE 1 = 1
		 <if test="searchChartDivi != '' and searchChartDivi">
		   AND CH.CHART_DIVI = #{searchChartDivi}
		 </if>
		 LIMIT ${start}, ${limit}
	</select>

	<select id="selectUserOriginalChart" parameterType="hashmap" resultType="hashmap">
		SELECT CH.PID                                                AS "pId"
			  ,CH.CHART_DIVI                                         AS "chartDivi"
			  ,DATE_FORMAT(CH.WRITE_DATE, '%Y-%m-%d')                AS "writeDate"
			  ,MU.USER_NAME                                          AS "userName"
			  ,IFNULL((SELECT CSL.STAFF_NAME
						 FROM CRM_STAFF_LIST AS CSL
						WHERE CSL.PID = CH.CRM_DOC
						LIMIT 0, 1), '-')                             AS "crmDocName"
			  ,IFNULL((SELECT CSL.STAFF_NAME
						 FROM CRM_STAFF_LIST AS CSL
						WHERE CSL.PID = CH.CRM_STAFF
						LIMIT 0, 1), '-')                             AS "crmStaffName"
			  ,IFNULL(CH.PIC_NAME, '-')                               AS "picName"
			  ,IFNULL(CH.PIC_FILE, '-')                               AS "picFile"
			  ,IFNULL(CH.PIC_TITLE, '-')                              AS "picTitle"
			  ,IFNULL(CH.CHART_MSG_01, '')                           AS "chartMsg01"
			  ,IFNULL(CH.CHART_MSG_03, '')                           AS "chartMsg03"
			  ,IFNULL(DATE_FORMAT(CH.DATE_02, '%Y-%m-%d'), '-')       AS "date02"
			  ,IFNULL(DATE_FORMAT(CH.SURGERY_DATE, '%Y-%m-%d'), '-')  AS "surgeryDate"
			  ,IFNULL(CH.MEDICAL_DIVI, '-')                           AS "medicalDivi"
			  ,IFNULL(CH.SURGERY_MSG, '-')                            AS "surgeryMsg"
		  FROM CHART_001 AS CH
	INNER JOIN MU_USER AS MU
			ON CH.CRM_MB_PID = MU.CRM_MEMBER_PID
		   AND CH.LIST_DEL = 'N'
		   AND MU.USE_YN = 'Y'
		   AND CH.PID = #{pId}
		 LIMIT 0, 1
	</select>
</mapper>