<?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="WebStockHistory">
	
	<!-- 현재까지 전체 입고 수량/금액, 출고 수량/금액 확인 -->
	<select id="selectTotalStockStatus" parameterType="hashmap" resultType="hashmap">
		SELECT FORMAT(IFNULL(SUM(IN_TOTAL_QUANTITY),0), 0)         AS "inTotalQuantity"
			  ,FORMAT(IFNULL(SUM(IN_TOTAL_AMOUNT),0), 0)           AS "inTotalAmount"
			  ,CASE
			    WHEN FLOOR(SUM(OUT_TOTAL_QUANTITY)) = SUM(OUT_TOTAL_QUANTITY) THEN FORMAT(IFNULL(SUM(OUT_TOTAL_QUANTITY),0),0)
			    ELSE TRIM(TRAILING '0' FROM CAST(FORMAT(SUM(OUT_TOTAL_QUANTITY), 2) AS FLOAT))
			   END                                     			  AS "outTotalQuantity"
			 ,FORMAT(IFNULL(SUM(OUT_TOTAL_AMOUNT),0), 0)          AS "outTotalAmount"
		FROM (
			 SELECT SUM(MSH.QUANTITY)                 AS IN_TOTAL_QUANTITY
				  ,SUM(MSH.PRICE*MSH.QUANTITY)       AS IN_TOTAL_AMOUNT
				  ,0                                 AS OUT_TOTAL_QUANTITY
				  ,0                                 AS OUT_TOTAL_AMOUNT
				  ,IN_OUT_DATE
				  ,MU_TREATMENT_ID
				  ,MU_COMPANY_ID
				  ,COMPANY_NAME
				  ,PRODUCT_NAME
			 FROM MU_STOCK_HISTORY AS MSH
			 WHERE MSH.USE_YN = 'Y'
			   AND MSH.TYPE IN ('I', 'AA')
		AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockStatusSearchStartDate}
		AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockStatusSearchEndDate}
		<if test="stockStatusTreatmentId != null and stockStatusTreatmentId != ''">
			AND MSH.MU_TREATMENT_ID = (SELECT MU_CATEGORY_ITEM_ID
			FROM MU_CATEGORY_ITEM MCI
			JOIN MU_CATEGORY MC
			ON MCI.MU_CATEGORY_ID = MC.MU_CATEGORY_ID
			WHERE MC.CATEGORY_NAME = '재고구분'
			AND MCI.CATEGORY_ITEM_CODE =#{stockStatusTreatmentId})
		</if>
		<if test="stockStatusCompanyId != null and stockStatusCompanyId != ''">
			AND MSH.MU_COMPANY_ID   = #{stockStatusCompanyId}
		</if>
		<if test="stockStatusSearchKeywordParam != null and stockStatusSearchKeywordParam != ''">
			AND ((MSH.COMPANY_NAME LIKE CONCAT('%',TRIM(#{stockStatusSearchKeywordParam}),'%'))
			OR (MSH.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{stockStatusSearchKeywordParam}),'%')))
		</if>
			 UNION ALL
			 SELECT 0                                 AS IN_TOTAL_QUANTITY
				  ,0                                 AS IN_TOTAL_AMOUNT
				  ,SUM(MSH.QUANTITY)                 AS OUT_TOTAL_QUANTITY
				  ,SUM(MSH.PRICE*MSH.QUANTITY)       AS OUT_TOTAL_AMOUNT
				  ,IN_OUT_DATE
				  ,MU_TREATMENT_ID
				  ,MU_COMPANY_ID
				  ,COMPANY_NAME
				  ,PRODUCT_NAME
			 FROM MU_STOCK_HISTORY AS MSH
			 WHERE MSH.USE_YN = 'Y'
			   AND MSH.TYPE IN ('S', 'R', 'D', 'U', 'E', 'AD')
				AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockStatusSearchStartDate}
				AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockStatusSearchEndDate}
				<if test="stockStatusTreatmentId != null and stockStatusTreatmentId != ''">
					AND MSH.MU_TREATMENT_ID = (SELECT MU_CATEGORY_ITEM_ID
					FROM MU_CATEGORY_ITEM MCI
					JOIN MU_CATEGORY MC
					ON MCI.MU_CATEGORY_ID = MC.MU_CATEGORY_ID
					WHERE MC.CATEGORY_NAME = '재고구분'
					AND MCI.CATEGORY_ITEM_CODE =#{stockStatusTreatmentId})
				</if>
				<if test="stockStatusCompanyId != null and stockStatusCompanyId != ''">
					AND MSH.MU_COMPANY_ID   = #{stockStatusCompanyId}
				</if>
				<if test="stockStatusSearchKeywordParam != null and stockStatusSearchKeywordParam != ''">
					AND ((MSH.COMPANY_NAME LIKE CONCAT('%',TRIM(#{stockStatusSearchKeywordParam}),'%'))
					OR (MSH.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{stockStatusSearchKeywordParam}),'%')))
				</if>
    	 ) AS MSH
		WHERE 1=1
	</select>
	
	<select id="selectTotalStockStatusCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*) AS "totalCount"
		FROM (
				 SELECT MP.MU_TREATMENT_ID
					  , MP.MU_PRODUCT_ID
					  , MP.PRODUCT_CODE
					  , MP.PRODUCT_NAME
					  , MC.MU_COMPANY_ID
					  , MC.COMPANY_NAME
				 FROM MU_COMPANY AS MC
						  INNER JOIN MU_COMPANY_PRODUCT AS MCP
									 ON MC.MU_COMPANY_ID = MCP.MU_COMPANY_ID
						  INNER JOIN MU_PRODUCT AS MP
									 ON MCP.MU_PRODUCT_ID = MP.MU_PRODUCT_ID
				 WHERE MC.USE_YN = 'Y'
				   AND MP.USE_YN = 'Y'
			    <if test="stockStatusTreatmentId != null and stockStatusTreatmentId != ''">
				AND MP.MU_TREATMENT_ID = (SELECT MU_CATEGORY_ITEM_ID
											FROM MU_CATEGORY_ITEM MCI
											JOIN MU_CATEGORY MC
											  ON MCI.MU_CATEGORY_ID = MC.MU_CATEGORY_ID
										   WHERE MC.CATEGORY_NAME = '재고구분'
											 AND MCI.CATEGORY_ITEM_CODE = #{stockStatusTreatmentId})
				</if>
				<if test="stockStatusCompanyId != null and stockStatusCompanyId != ''">
				AND MC.MU_COMPANY_ID = #{stockStatusCompanyId}
				</if>
				<if test="stockStatusSearchKeywordParam != null and stockStatusSearchKeywordParam != ''">
				AND ((MC.COMPANY_NAME LIKE CONCAT('%', TRIM(#{stockStatusSearchKeywordParam}), '%'))
				  OR (MP.PRODUCT_NAME LIKE CONCAT('%', TRIM(#{stockStatusSearchKeywordParam}), '%')))
				</if>
		) AS count_query
	</select>
	
	<select id="selectListStockStatus" parameterType="hashmap" resultType="hashmap">
		SELECT MSH.*
		  FROM (
					SELECT MSH.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MU_TREATMENT_ID                                           AS "muTreatmentId"
									  ,IFNULL((SELECT MCI.CATEGORY_ITEM_NAME
												 FROM MU_CATEGORY_ITEM AS MCI
												WHERE MCI.USE_YN = 'Y'
												  AND MCI.MU_CATEGORY_ITEM_ID = MSH.MU_TREATMENT_ID
									   ),'')                                                     AS "treatmentName"
									  ,MU_PRODUCT_ID                                             AS "muProductId"
									  ,PRODUCT_CODE                                              AS "productCode"
									  ,PRODUCT_NAME                                              AS "productName"
									  ,MU_COMPANY_ID                                             AS "muCompanyId"
									  ,COMPANY_NAME                                              AS "companyName"
									  ,FORMAT(IN_TOTAL_QUANTITY, 0)                              AS "inTotalQuantity"
									  ,FORMAT(IN_TOTAL_AMOUNT, 0)                                AS "inTotalAmount"
									  ,CASE
									   	WHEN FLOOR(OUT_TOTAL_QUANTITY) = OUT_TOTAL_QUANTITY THEN FORMAT(IFNULL(OUT_TOTAL_QUANTITY,0),0)
										ELSE TRIM(TRAILING '0' FROM CAST(FORMAT(OUT_TOTAL_QUANTITY, 2) AS FLOAT))
									   END                                          			 AS "outTotalQuantity"
									  ,FORMAT(OUT_TOTAL_AMOUNT, 0)                               AS "outTotalAmount"
								  FROM (
											SELECT MP.MU_TREATMENT_ID
												  ,MP.MU_PRODUCT_ID
												  ,MP.PRODUCT_CODE
												  ,MP.PRODUCT_NAME
												  ,MC.MU_COMPANY_ID
												  ,MC.COMPANY_NAME
												  ,IFNULL((SELECT SUM(MSH.QUANTITY)
															 FROM MU_STOCK_HISTORY AS MSH
															WHERE MSH.USE_YN = 'Y'
															  AND MSH.TYPE IN ('I', 'AA')
															  AND MSH.MU_TREATMENT_ID = MP.MU_TREATMENT_ID
															  AND MSH.MU_PRODUCT_ID   = MP.MU_PRODUCT_ID
															  AND MSH.MU_COMPANY_ID   = MC.MU_COMPANY_ID
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockStatusSearchStartDate}
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockStatusSearchEndDate}
														 GROUP BY MSH.MU_TREATMENT_ID
														 		 ,MSH.MU_PRODUCT_ID
														 		 ,MSH.MU_COMPANY_ID
												   ),0)                                                    AS IN_TOTAL_QUANTITY
												  ,IFNULL((SELECT SUM(MSH.PRICE*MSH.QUANTITY)
															 FROM MU_STOCK_HISTORY AS MSH
															WHERE MSH.USE_YN = 'Y'
															  AND MSH.TYPE IN ('I', 'AA')
															  AND MSH.MU_TREATMENT_ID = MP.MU_TREATMENT_ID
															  AND MSH.MU_PRODUCT_ID   = MP.MU_PRODUCT_ID
															  AND MSH.MU_COMPANY_ID   = MC.MU_COMPANY_ID
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockStatusSearchStartDate}
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockStatusSearchEndDate}
														 GROUP BY MSH.MU_TREATMENT_ID
														 		 ,MSH.MU_PRODUCT_ID
														 		 ,MSH.MU_COMPANY_ID
												   ),0)                                                    AS IN_TOTAL_AMOUNT
												  ,IFNULL((SELECT SUM(MSH.QUANTITY)
															 FROM MU_STOCK_HISTORY AS MSH
															WHERE MSH.USE_YN = 'Y'
															  AND MSH.TYPE IN ('S', 'R', 'D', 'U', 'E', 'AD')
															  AND MSH.MU_TREATMENT_ID = MP.MU_TREATMENT_ID
															  AND MSH.MU_PRODUCT_ID   = MP.MU_PRODUCT_ID
															  AND MSH.MU_COMPANY_ID   = MC.MU_COMPANY_ID
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockStatusSearchStartDate}
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockStatusSearchEndDate}
														 GROUP BY MSH.MU_TREATMENT_ID
														 		 ,MSH.MU_PRODUCT_ID
														 		 ,MSH.MU_COMPANY_ID
												   ),0)                                                    AS OUT_TOTAL_QUANTITY
												  ,IFNULL((SELECT SUM(MSH.PRICE*MSH.QUANTITY)
															 FROM MU_STOCK_HISTORY AS MSH
															WHERE MSH.USE_YN = 'Y'
															  AND MSH.TYPE IN ('S', 'R', 'D', 'U', 'E', 'AD')
															  AND MSH.MU_TREATMENT_ID = MP.MU_TREATMENT_ID
															  AND MSH.MU_PRODUCT_ID   = MP.MU_PRODUCT_ID
															  AND MSH.MU_COMPANY_ID   = MC.MU_COMPANY_ID
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockStatusSearchStartDate}
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockStatusSearchEndDate}
														 GROUP BY MSH.MU_TREATMENT_ID
														 		 ,MSH.MU_PRODUCT_ID
														 		 ,MSH.MU_COMPANY_ID
												   ),0)                                                    AS OUT_TOTAL_AMOUNT
											  FROM MU_COMPANY AS MC
										INNER JOIN MU_COMPANY_PRODUCT AS MCP
												ON MC.MU_COMPANY_ID = MCP.MU_COMPANY_ID
										INNER JOIN MU_PRODUCT AS MP
												ON MCP.MU_PRODUCT_ID = MP.MU_PRODUCT_ID  
											   AND MC.USE_YN   = 'Y'
											   AND MP.USE_YN   = 'Y'
											   <if test="stockStatusTreatmentId != null and stockStatusTreatmentId != ''">
											   AND MP.MU_TREATMENT_ID = (SELECT MU_CATEGORY_ITEM_ID
																		   FROM MU_CATEGORY_ITEM MCI
																		   JOIN MU_CATEGORY MC
																		   ON MCI.MU_CATEGORY_ID = MC.MU_CATEGORY_ID
																		   WHERE MC.CATEGORY_NAME = '재고구분'
																		   AND MCI.CATEGORY_ITEM_CODE =#{stockStatusTreatmentId})
											   </if>
											   <if test="stockStatusCompanyId != null and stockStatusCompanyId != ''">
											   AND MC.MU_COMPANY_ID   = #{stockStatusCompanyId}
											   </if>
											   <if test="stockStatusSearchKeywordParam != null and stockStatusSearchKeywordParam != ''">
											   AND ((MC.COMPANY_NAME LIKE CONCAT('%',TRIM(#{stockStatusSearchKeywordParam}),'%'))
												OR (MP.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{stockStatusSearchKeywordParam}),'%')))
											   </if>
									   ) AS MSH
								 WHERE 1=1
								 <choose>
								 <when test="stockStatusSort != null and stockStatusSort != ''">
							  ORDER BY ${stockStatusSort}
								 </when>
								 <otherwise>
							  ORDER BY PRODUCT_NAME ASC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MSH, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MSH
		 WHERE 1 = 1
-- 		 LIMIT ${stockStatusStart}, ${stockStatusLimit}
	</select>
	
	<select id="selectListStockStatusExcel" parameterType="hashmap" resultType="hashmap">
		SELECT MSH.*
		  FROM (
					SELECT MSH.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MU_TREATMENT_ID                                           AS "muTreatmentId"
									  ,IFNULL((SELECT MCI.CATEGORY_ITEM_NAME
												 FROM MU_CATEGORY_ITEM AS MCI
												WHERE MCI.USE_YN = 'Y'
												  AND MCI.MU_CATEGORY_ITEM_ID = MSH.MU_TREATMENT_ID
									   ),'')                                                     AS "treatmentName"
									  ,MU_PRODUCT_ID                                             AS "muProductId"
									  ,PRODUCT_CODE                                              AS "productCode"
									  ,PRODUCT_NAME                                              AS "productName"
									  ,MU_COMPANY_ID                                             AS "muCompanyId"
									  ,COMPANY_NAME                                              AS "companyName"
									  ,FORMAT(IN_TOTAL_QUANTITY, 0)                              AS "inTotalQuantity"
									  ,FORMAT(IN_TOTAL_AMOUNT, 0)                                AS "inTotalAmount"
									  ,CASE
										 WHEN FLOOR(OUT_TOTAL_QUANTITY) = OUT_TOTAL_QUANTITY THEN FORMAT(IFNULL(OUT_TOTAL_QUANTITY,0),0)
										 ELSE TRIM(TRAILING '0' FROM CAST(FORMAT(OUT_TOTAL_QUANTITY, 2) AS FLOAT))
									   END                                          			 AS "outTotalQuantity"
									  ,FORMAT(OUT_TOTAL_AMOUNT, 0)                               AS "outTotalAmount"
								  FROM (
											SELECT MP.MU_TREATMENT_ID
												  ,MP.MU_PRODUCT_ID
												  ,MP.PRODUCT_CODE
												  ,MP.PRODUCT_NAME
												  ,MC.MU_COMPANY_ID
												  ,MC.COMPANY_NAME
												  ,IFNULL((SELECT SUM(MSH.QUANTITY)
															 FROM MU_STOCK_HISTORY AS MSH
															WHERE MSH.USE_YN = 'Y'
															  AND MSH.TYPE IN ('I', 'AA')
															  AND MSH.MU_TREATMENT_ID = MP.MU_TREATMENT_ID
															  AND MSH.MU_PRODUCT_ID   = MP.MU_PRODUCT_ID
															  AND MSH.MU_COMPANY_ID   = MC.MU_COMPANY_ID
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockStatusSearchStartDate}
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockStatusSearchEndDate}
														 GROUP BY MSH.MU_TREATMENT_ID
														 		 ,MSH.MU_PRODUCT_ID
														 		 ,MSH.MU_COMPANY_ID
												   ),0)                                                    AS IN_TOTAL_QUANTITY
												  ,IFNULL((SELECT SUM(MSH.PRICE*MSH.QUANTITY)
															 FROM MU_STOCK_HISTORY AS MSH
															WHERE MSH.USE_YN = 'Y'
															  AND MSH.TYPE IN ('I', 'AA')
															  AND MSH.MU_TREATMENT_ID = MP.MU_TREATMENT_ID
															  AND MSH.MU_PRODUCT_ID   = MP.MU_PRODUCT_ID
															  AND MSH.MU_COMPANY_ID   = MC.MU_COMPANY_ID
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockStatusSearchStartDate}
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockStatusSearchEndDate}
														 GROUP BY MSH.MU_TREATMENT_ID
														 		 ,MSH.MU_PRODUCT_ID
														 		 ,MSH.MU_COMPANY_ID
												   ),0)                                                    AS IN_TOTAL_AMOUNT
												  ,IFNULL((SELECT SUM(MSH.QUANTITY)
															 FROM MU_STOCK_HISTORY AS MSH
															WHERE MSH.USE_YN = 'Y'
															  AND MSH.TYPE IN ('S', 'R', 'D', 'U', 'E', 'AD')
															  AND MSH.MU_TREATMENT_ID = MP.MU_TREATMENT_ID
															  AND MSH.MU_PRODUCT_ID   = MP.MU_PRODUCT_ID
															  AND MSH.MU_COMPANY_ID   = MC.MU_COMPANY_ID
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockStatusSearchStartDate}
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockStatusSearchEndDate}
														 GROUP BY MSH.MU_TREATMENT_ID
														 		 ,MSH.MU_PRODUCT_ID
														 		 ,MSH.MU_COMPANY_ID
												   ),0)                                                    AS OUT_TOTAL_QUANTITY
												  ,IFNULL((SELECT SUM(MSH.PRICE*MSH.QUANTITY)
															 FROM MU_STOCK_HISTORY AS MSH
															WHERE MSH.USE_YN = 'Y'
															  AND MSH.TYPE IN ('S', 'R', 'D', 'U', 'E', 'AD')
															  AND MSH.MU_TREATMENT_ID = MP.MU_TREATMENT_ID
															  AND MSH.MU_PRODUCT_ID   = MP.MU_PRODUCT_ID
															  AND MSH.MU_COMPANY_ID   = MC.MU_COMPANY_ID
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockStatusSearchStartDate}
															  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockStatusSearchEndDate}
														 GROUP BY MSH.MU_TREATMENT_ID
														 		 ,MSH.MU_PRODUCT_ID
														 		 ,MSH.MU_COMPANY_ID
												   ),0)                                                    AS OUT_TOTAL_AMOUNT
											  FROM MU_COMPANY AS MC
										INNER JOIN MU_COMPANY_PRODUCT AS MCP
												ON MC.MU_COMPANY_ID = MCP.MU_COMPANY_ID
										INNER JOIN MU_PRODUCT AS MP
												ON MCP.MU_PRODUCT_ID = MP.MU_PRODUCT_ID  
											   AND MC.USE_YN   = 'Y'
											   AND MP.USE_YN   = 'Y'
											   <if test="stockStatusTreatmentId != null and stockStatusTreatmentId != ''">
											   AND MP.MU_TREATMENT_ID = (SELECT MU_CATEGORY_ITEM_ID
																		   FROM MU_CATEGORY_ITEM MCI
																		   JOIN MU_CATEGORY MC
																		   ON MCI.MU_CATEGORY_ID = MC.MU_CATEGORY_ID
																		   WHERE MC.CATEGORY_NAME = '재고구분'
																		   AND MCI.CATEGORY_ITEM_CODE =#{stockStatusTreatmentId})
											   </if>
											   <if test="stockStatusCompanyId != null and stockStatusCompanyId != ''">
											   AND MC.MU_COMPANY_ID   = #{stockStatusCompanyId}
											   </if>
											   <if test="stockStatusSearchKeywordParam != null and stockStatusSearchKeywordParam != ''">
											   AND ((MC.COMPANY_NAME LIKE CONCAT('%',TRIM(#{stockStatusSearchKeywordParam}),'%'))
												OR (MP.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{stockStatusSearchKeywordParam}),'%')))
											   </if>
									   ) AS MSH
								 WHERE 1=1
								 <choose>
								 <when test="stockStatusSort != null and stockStatusSort != ''">
							  ORDER BY ${stockStatusSort}
								 </when>
								 <otherwise>
							  ORDER BY PRODUCT_NAME ASC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MSH, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MSH
		 WHERE 1 = 1
	</select>
	
	<!-- 입출고내역 상단 각 타입별 수량/금액 조회 -->
	<select id="selectTotalStockHistoryType" parameterType="hashmap" resultType="hashmap">
		SELECT FORMAT(SUM(IN_TOTAL_QUANTITY), 0)         AS "inTotalQuantity"
			  ,FORMAT(SUM(IN_TOTAL_AMOUNT), 0)           AS "inTotalAmount"
			  ,CASE
				   WHEN FLOOR(SUM(U_OUT_TOTAL_QUANTITY)) = SUM(U_OUT_TOTAL_QUANTITY) THEN FORMAT(IFNULL(SUM(U_OUT_TOTAL_QUANTITY),0),0)
				   ELSE TRIM(TRAILING '0' FROM CAST(FORMAT(SUM(U_OUT_TOTAL_QUANTITY), 2) AS FLOAT))
			   END                                     AS "uOutTotalQuantity"
			  ,FORMAT(SUM(U_OUT_TOTAL_AMOUNT), 0)        AS "uOutTotalAmount"
			  ,FORMAT(SUM(S_OUT_TOTAL_QUANTITY), 0)      AS "sOutTotalQuantity"
			  ,FORMAT(SUM(S_OUT_TOTAL_AMOUNT), 0)        AS "sOutTotalAmount"
			  ,FORMAT(SUM(R_OUT_TOTAL_QUANTITY), 0)      AS "rOutTotalQuantity"
			  ,FORMAT(SUM(R_OUT_TOTAL_AMOUNT), 0)        AS "rOutTotalAmount"
			  ,FORMAT(SUM(D_OUT_TOTAL_QUANTITY), 0)      AS "dOutTotalQuantity"
			  ,FORMAT(SUM(D_OUT_TOTAL_AMOUNT), 0)        AS "dOutTotalAmount"
			  ,FORMAT(SUM(E_OUT_TOTAL_QUANTITY), 0)      AS "eOutTotalQuantity"
			  ,FORMAT(SUM(E_OUT_TOTAL_AMOUNT), 0)        AS "eOutTotalAmount"
			  ,FORMAT(SUM(AA_IN_TOTAL_QUANTITY), 0)      AS "aaInTotalQuantity"
			  ,FORMAT(SUM(AA_IN_TOTAL_AMOUNT), 0)        AS "aaInTotalAmount"
			  ,FORMAT(SUM(AD_OUT_TOTAL_QUANTITY), 0)     AS "adOutTotalQuantity"
			  ,FORMAT(SUM(AD_OUT_TOTAL_AMOUNT), 0)       AS "adOutTotalAmount"
		  FROM (
					SELECT SUM(MSH.QUANTITY)                 AS IN_TOTAL_QUANTITY
						  ,SUM(MSH.PRICE*MSH.QUANTITY)       AS IN_TOTAL_AMOUNT
						  ,0                                 AS U_OUT_TOTAL_QUANTITY
						  ,0                                 AS U_OUT_TOTAL_AMOUNT
						  ,0                                 AS S_OUT_TOTAL_QUANTITY
						  ,0                                 AS S_OUT_TOTAL_AMOUNT
						  ,0                                 AS R_OUT_TOTAL_QUANTITY
						  ,0                                 AS R_OUT_TOTAL_AMOUNT
						  ,0                                 AS D_OUT_TOTAL_QUANTITY
						  ,0                                 AS D_OUT_TOTAL_AMOUNT
						  ,0                                 AS E_OUT_TOTAL_QUANTITY
						  ,0                                 AS E_OUT_TOTAL_AMOUNT
						  ,0                                 AS AA_IN_TOTAL_QUANTITY
						  ,0                                 AS AA_IN_TOTAL_AMOUNT
						  ,0                                 AS AD_OUT_TOTAL_QUANTITY
						  ,0                                 AS AD_OUT_TOTAL_AMOUNT
					  FROM MU_STOCK_HISTORY AS MSH
					 WHERE MSH.USE_YN          = 'Y'
					   AND MSH.TYPE            = 'I'
					   AND MSH.MU_TREATMENT_ID = #{muTreatmentId}
					   AND MSH.MU_PRODUCT_ID   = #{muProductId}
					   AND MSH.MU_COMPANY_ID   = #{muCompanyId}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockHistorySearchStartDate}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockHistorySearchEndDate}
					 UNION ALL
					SELECT 0                                 AS IN_TOTAL_QUANTITY
						  ,0                                 AS IN_TOTAL_AMOUNT
						  ,SUM(MSH.QUANTITY)                 AS U_OUT_TOTAL_QUANTITY
						  ,SUM(MSH.PRICE*MSH.QUANTITY)       AS U_OUT_TOTAL_AMOUNT
						  ,0                                 AS S_OUT_TOTAL_QUANTITY
						  ,0                                 AS S_OUT_TOTAL_AMOUNT
						  ,0                                 AS R_OUT_TOTAL_QUANTITY
						  ,0                                 AS R_OUT_TOTAL_AMOUNT
						  ,0                                 AS D_OUT_TOTAL_QUANTITY
						  ,0                                 AS D_OUT_TOTAL_AMOUNT
						  ,0                                 AS E_OUT_TOTAL_QUANTITY
						  ,0                                 AS E_OUT_TOTAL_AMOUNT
						  ,0                                 AS AA_IN_TOTAL_QUANTITY
						  ,0                                 AS AA_IN_TOTAL_AMOUNT
						  ,0                                 AS AD_OUT_TOTAL_QUANTITY
						  ,0                                 AS AD_OUT_TOTAL_AMOUNT
					  FROM MU_STOCK_HISTORY AS MSH
					 WHERE MSH.USE_YN          = 'Y'
					   AND MSH.TYPE            = 'U'
					   AND MSH.MU_TREATMENT_ID = #{muTreatmentId}
					   AND MSH.MU_PRODUCT_ID   = #{muProductId}
					   AND MSH.MU_COMPANY_ID   = #{muCompanyId}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockHistorySearchStartDate}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockHistorySearchEndDate}
					 UNION ALL
					SELECT 0                                 AS IN_TOTAL_QUANTITY
						  ,0                                 AS IN_TOTAL_AMOUNT
						  ,0                                 AS U_OUT_TOTAL_QUANTITY
						  ,0                                 AS U_OUT_TOTAL_AMOUNT
						  ,SUM(MSH.QUANTITY)                 AS S_OUT_TOTAL_QUANTITY
						  ,SUM(MSH.PRICE*MSH.QUANTITY)       AS S_OUT_TOTAL_AMOUNT
						  ,0                                 AS R_OUT_TOTAL_QUANTITY
						  ,0                                 AS R_OUT_TOTAL_AMOUNT
						  ,0                                 AS D_OUT_TOTAL_QUANTITY
						  ,0                                 AS D_OUT_TOTAL_AMOUNT
						  ,0                                 AS E_OUT_TOTAL_QUANTITY
						  ,0                                 AS E_OUT_TOTAL_AMOUNT
						  ,0                                 AS AA_IN_TOTAL_QUANTITY
						  ,0                                 AS AA_IN_TOTAL_AMOUNT
						  ,0                                 AS AD_OUT_TOTAL_QUANTITY
						  ,0                                 AS AD_OUT_TOTAL_AMOUNT
					  FROM MU_STOCK_HISTORY AS MSH
					 WHERE MSH.USE_YN          = 'Y'
					   AND MSH.TYPE            = 'S'
					   AND MSH.MU_TREATMENT_ID = #{muTreatmentId}
					   AND MSH.MU_PRODUCT_ID   = #{muProductId}
					   AND MSH.MU_COMPANY_ID   = #{muCompanyId}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockHistorySearchStartDate}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockHistorySearchEndDate}
					 UNION ALL
					SELECT 0                                 AS IN_TOTAL_QUANTITY
						  ,0                                 AS IN_TOTAL_AMOUNT
						  ,0                                 AS U_OUT_TOTAL_QUANTITY
						  ,0                                 AS U_OUT_TOTAL_AMOUNT
						  ,0                                 AS S_OUT_TOTAL_QUANTITY
						  ,0                                 AS S_OUT_TOTAL_AMOUNT
						  ,SUM(MSH.QUANTITY)                 AS R_OUT_TOTAL_QUANTITY
						  ,SUM(MSH.PRICE*MSH.QUANTITY)       AS R_OUT_TOTAL_AMOUNT
						  ,0                                 AS D_OUT_TOTAL_QUANTITY
						  ,0                                 AS D_OUT_TOTAL_AMOUNT
						  ,0                                 AS E_OUT_TOTAL_QUANTITY
						  ,0                                 AS E_OUT_TOTAL_AMOUNT
						  ,0                                 AS AA_IN_TOTAL_QUANTITY
						  ,0                                 AS AA_IN_TOTAL_AMOUNT
						  ,0                                 AS AD_OUT_TOTAL_QUANTITY
						  ,0                                 AS AD_OUT_TOTAL_AMOUNT
					  FROM MU_STOCK_HISTORY AS MSH
					 WHERE MSH.USE_YN          = 'Y'
					   AND MSH.TYPE            = 'R'
					   AND MSH.MU_TREATMENT_ID = #{muTreatmentId}
					   AND MSH.MU_PRODUCT_ID   = #{muProductId}
					   AND MSH.MU_COMPANY_ID   = #{muCompanyId}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockHistorySearchStartDate}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockHistorySearchEndDate}
					 UNION ALL
					SELECT 0                                 AS IN_TOTAL_QUANTITY
						  ,0                                 AS IN_TOTAL_AMOUNT
						  ,0                                 AS U_OUT_TOTAL_QUANTITY
						  ,0                                 AS U_OUT_TOTAL_AMOUNT
						  ,0                                 AS S_OUT_TOTAL_QUANTITY
						  ,0                                 AS S_OUT_TOTAL_AMOUNT
						  ,0                                 AS R_OUT_TOTAL_QUANTITY
						  ,0                                 AS R_OUT_TOTAL_AMOUNT
						  ,SUM(MSH.QUANTITY)                 AS D_OUT_TOTAL_QUANTITY
						  ,SUM(MSH.PRICE*MSH.QUANTITY)       AS D_OUT_TOTAL_AMOUNT
						  ,0                                 AS E_OUT_TOTAL_QUANTITY
						  ,0                                 AS E_OUT_TOTAL_AMOUNT
						  ,0                                 AS AA_IN_TOTAL_QUANTITY
						  ,0                                 AS AA_IN_TOTAL_AMOUNT
						  ,0                                 AS AD_OUT_TOTAL_QUANTITY
						  ,0                                 AS AD_OUT_TOTAL_AMOUNT
					  FROM MU_STOCK_HISTORY AS MSH
					 WHERE MSH.USE_YN          = 'Y'
					   AND MSH.TYPE            = 'D'
					   AND MSH.MU_TREATMENT_ID = #{muTreatmentId}
					   AND MSH.MU_PRODUCT_ID   = #{muProductId}
					   AND MSH.MU_COMPANY_ID   = #{muCompanyId}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockHistorySearchStartDate}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockHistorySearchEndDate}
					 UNION ALL
					SELECT 0                                 AS IN_TOTAL_QUANTITY
						  ,0                                 AS IN_TOTAL_AMOUNT
						  ,0                                 AS U_OUT_TOTAL_QUANTITY
						  ,0                                 AS U_OUT_TOTAL_AMOUNT
						  ,0                                 AS S_OUT_TOTAL_QUANTITY
						  ,0                                 AS S_OUT_TOTAL_AMOUNT
						  ,0                                 AS R_OUT_TOTAL_QUANTITY
						  ,0                                 AS R_OUT_TOTAL_AMOUNT
						  ,0                                 AS D_OUT_TOTAL_QUANTITY
						  ,0                                 AS D_OUT_TOTAL_AMOUNT
						  ,SUM(MSH.QUANTITY)                 AS E_OUT_TOTAL_QUANTITY
						  ,SUM(MSH.PRICE*MSH.QUANTITY)       AS E_OUT_TOTAL_AMOUNT
						  ,0                                 AS AA_IN_TOTAL_QUANTITY
						  ,0                                 AS AA_IN_TOTAL_AMOUNT
						  ,0                                 AS AD_OUT_TOTAL_QUANTITY
						  ,0                                 AS AD_OUT_TOTAL_AMOUNT
					  FROM MU_STOCK_HISTORY AS MSH
					 WHERE MSH.USE_YN          = 'Y'
					   AND MSH.TYPE            = 'E'
					   AND MSH.MU_TREATMENT_ID = #{muTreatmentId}
					   AND MSH.MU_PRODUCT_ID   = #{muProductId}
					   AND MSH.MU_COMPANY_ID   = #{muCompanyId}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockHistorySearchStartDate}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockHistorySearchEndDate}
					 UNION ALL
					SELECT 0                                 AS IN_TOTAL_QUANTITY
						  ,0                                 AS IN_TOTAL_AMOUNT
						  ,0                                 AS U_OUT_TOTAL_QUANTITY
						  ,0                                 AS U_OUT_TOTAL_AMOUNT
						  ,0                                 AS S_OUT_TOTAL_QUANTITY
						  ,0                                 AS S_OUT_TOTAL_AMOUNT
						  ,0                                 AS R_OUT_TOTAL_QUANTITY
						  ,0                                 AS R_OUT_TOTAL_AMOUNT
						  ,0                                 AS D_OUT_TOTAL_QUANTITY
						  ,0                                 AS D_OUT_TOTAL_AMOUNT
						  ,0                                 AS E_OUT_TOTAL_QUANTITY
						  ,0                                 AS E_OUT_TOTAL_AMOUNT
						  ,SUM(MSH.QUANTITY)                 AS AA_IN_TOTAL_QUANTITY
						  ,SUM(MSH.PRICE*MSH.QUANTITY)       AS AA_IN_TOTAL_AMOUNT
						  ,0                                 AS AD_OUT_TOTAL_QUANTITY
						  ,0                                 AS AD_OUT_TOTAL_AMOUNT
					  FROM MU_STOCK_HISTORY AS MSH
					 WHERE MSH.USE_YN          = 'Y'
					   AND MSH.TYPE            = 'AA'
					   AND MSH.MU_TREATMENT_ID = #{muTreatmentId}
					   AND MSH.MU_PRODUCT_ID   = #{muProductId}
					   AND MSH.MU_COMPANY_ID   = #{muCompanyId}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockHistorySearchStartDate}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockHistorySearchEndDate}
					 UNION ALL
					SELECT 0                                 AS IN_TOTAL_QUANTITY
						  ,0                                 AS IN_TOTAL_AMOUNT
						  ,0                                 AS U_OUT_TOTAL_QUANTITY
						  ,0                                 AS U_OUT_TOTAL_AMOUNT
						  ,0                                 AS S_OUT_TOTAL_QUANTITY
						  ,0                                 AS S_OUT_TOTAL_AMOUNT
						  ,0                                 AS R_OUT_TOTAL_QUANTITY
						  ,0                                 AS R_OUT_TOTAL_AMOUNT
						  ,0                                 AS D_OUT_TOTAL_QUANTITY
						  ,0                                 AS D_OUT_TOTAL_AMOUNT
						  ,0                                 AS E_OUT_TOTAL_QUANTITY
						  ,0                                 AS E_OUT_TOTAL_AMOUNT
						  ,0                                 AS AA_IN_TOTAL_QUANTITY
						  ,0                                 AS AA_IN_TOTAL_AMOUNT
						  ,SUM(MSH.QUANTITY)                 AS AD_OUT_TOTAL_QUANTITY
						  ,SUM(MSH.PRICE*MSH.QUANTITY)       AS AD_OUT_TOTAL_AMOUNT
					  FROM MU_STOCK_HISTORY AS MSH
					 WHERE MSH.USE_YN          = 'Y'
					   AND MSH.TYPE            = 'AD'
					   AND MSH.MU_TREATMENT_ID = #{muTreatmentId}
					   AND MSH.MU_PRODUCT_ID   = #{muProductId}
					   AND MSH.MU_COMPANY_ID   = #{muCompanyId}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockHistorySearchStartDate}
					   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockHistorySearchEndDate}
			   ) AS MSH
		 WHERE 1=1
	</select>
	
	<select id="selectTotalStockHistoryCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)                       AS "totalCount"
		  FROM MU_STOCK_HISTORY AS MSH
		 WHERE MSH.USE_YN          = 'Y'
		   AND MSH.MU_TREATMENT_ID = #{muTreatmentId}
		   AND MSH.MU_PRODUCT_ID   = #{muProductId}
		   AND MSH.MU_COMPANY_ID   = #{muCompanyId}
		   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockHistorySearchStartDate}
		   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockHistorySearchEndDate}
		   <if test="stockHistorySearchType != null and stockHistorySearchType != ''">
		   AND MSH.TYPE = #{stockHistorySearchType}
		   </if>
	</select>
	
	<select id="selectListStockHistory" parameterType="hashmap" resultType="hashmap">
		SELECT MSH.*
		  FROM (
					SELECT MSH.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MSH.MU_STOCK_HISTORY_ID                                  AS "muStockHistoryId"
									  ,MSH.MU_TREATMENT_ID                                      AS "muTreatmentId"
									  ,MSH.TREATMENT_NAME                                       AS "treatmentName"
									  ,MSH.MU_PRODUCT_ID                                        AS "muProductId"
									  ,MSH.PRODUCT_CODE                                         AS "productCode"
									  ,MSH.PRODUCT_NAME                                         AS "productName"
									  ,MSH.MU_COMPANY_ID                                        AS "muCompanyId"
									  ,MSH.FILE_NAME                                            AS "filename"
									  ,MSH.FILE_PATH                                            AS "filePath"
									  ,MSH.ORIGINAL_FILE_NAME                                   AS "originalFileName"
									  ,MSH.COMPANY_NAME                                         AS "companyName"
									  ,DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d')                 AS "inDate"
									  ,MSH.TYPE                                                 AS "type"
									  ,CASE MSH.TYPE
											WHEN 'I' THEN '입고'
											WHEN 'S' THEN '판매'
											WHEN 'R' THEN '반품'
											WHEN 'D' THEN '폐기'
											WHEN 'U' THEN '사용'
											WHEN 'E' THEN '기타'
											WHEN 'AA' THEN '재고조정-추가'
											WHEN 'AD' THEN '재고조정-감소'
									   END                                                      AS "typeName"
									  ,FORMAT(MSH.PRICE, 0)                                     AS "price"
									  ,CASE
									  	 WHEN FLOOR(QUANTITY) = QUANTITY THEN FORMAT(IFNULL(QUANTITY,0),0)
									  	 ELSE TRIM(TRAILING '0' FROM CAST(FORMAT(QUANTITY, 2) AS FLOAT))
									   END                                          			 AS "quantity"
									  ,FORMAT(MSH.PRICE * MSH.QUANTITY, 0)                      AS "totalAmount"
									  ,IFNULL(MSH.ETC, '')                                      AS "etc"
								  FROM MU_STOCK_HISTORY AS MSH
								 WHERE MSH.USE_YN          = 'Y'
								   AND MSH.MU_TREATMENT_ID = #{muTreatmentId}
								   AND MSH.MU_PRODUCT_ID   = #{muProductId}
								   AND MSH.MU_COMPANY_ID   = #{muCompanyId}
								   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockHistorySearchStartDate}
								   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockHistorySearchEndDate}
								   <if test="stockHistorySearchType != null and stockHistorySearchType != ''">
								   AND MSH.TYPE = #{stockHistorySearchType}
								   </if>
								 <choose>
								 <when test="stockStatusSort != null and stockStatusSort != ''">
							  ORDER BY ${stockStatusSort}
								 </when>
								 <otherwise>
							  ORDER BY MSH.IN_OUT_DATE DESC, MSH.WRITE_DATE DESC, MSH.WRITE_TIME DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MSH, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MSH
		 WHERE 1 = 1
-- 		 LIMIT ${stockHistoryStart}, ${stockHistoryLimit}
	</select>
	
	<select id="selectListStockHistoryExcel" parameterType="hashmap" resultType="hashmap">
		SELECT MSH.*
		  FROM (
					SELECT MSH.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MSH.MU_STOCK_HISTORY_ID                                  AS "muStockHistoryId"
									  ,MSH.MU_TREATMENT_ID                                      AS "muTreatmentId"
									  ,MSH.TREATMENT_NAME                                       AS "treatmentName"
									  ,MSH.MU_PRODUCT_ID                                        AS "muProductId"
									  ,MSH.PRODUCT_CODE                                         AS "productCode"
									  ,MSH.PRODUCT_NAME                                         AS "productName"
									  ,MSH.MU_COMPANY_ID                                        AS "muCompanyId"
									  ,MSH.FILE_NAME                                            AS "filename"
									  ,MSH.FILE_PATH                                            AS "filePath"
									  ,MSH.ORIGINAL_FILE_NAME                                   AS "originalFileName"
									  ,MSH.COMPANY_NAME                                         AS "companyName"
									  ,DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d')                 AS "inDate"
									  ,MSH.TYPE                                                 AS "type"
									  ,CASE MSH.TYPE
											WHEN 'I' THEN '입고'
											WHEN 'S' THEN '판매'
											WHEN 'R' THEN '반품'
											WHEN 'D' THEN '폐기'
											WHEN 'U' THEN '사용'
											WHEN 'E' THEN '기타'
											WHEN 'AA' THEN '재고조정-추가'
											WHEN 'AD' THEN '재고조정-감소'
									   END                                                      AS "typeName"
									  ,FORMAT(MSH.PRICE, 0)                                     AS "price"
									  ,CASE
									  	 WHEN FLOOR(QUANTITY) = QUANTITY THEN FORMAT(IFNULL(QUANTITY,0),0)
									  	 ELSE TRIM(TRAILING '0' FROM CAST(FORMAT(QUANTITY, 2) AS FLOAT))
									   END                                          			 AS "quantity"
									  ,FORMAT(MSH.PRICE * MSH.QUANTITY, 0)                      AS "totalAmount"
									  ,IFNULL(MSH.ETC, '')                                      AS "etc"
								  FROM MU_STOCK_HISTORY AS MSH
								 WHERE MSH.USE_YN          = 'Y'
								   AND MSH.MU_TREATMENT_ID = #{muTreatmentId}
								   AND MSH.MU_PRODUCT_ID   = #{muProductId}
								   AND MSH.MU_COMPANY_ID   = #{muCompanyId}
								   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &gt;= #{stockHistorySearchStartDate}
								   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{stockHistorySearchEndDate}
								   <if test="stockHistorySearchType != null and stockHistorySearchType != ''">
								   AND MSH.TYPE = #{stockHistorySearchType}
								   </if>
								 <choose>
								 <when test="stockStatusSort != null and stockStatusSort != ''">
							  ORDER BY ${stockStatusSort}
								 </when>
								 <otherwise>
							  ORDER BY MSH.IN_OUT_DATE DESC, MSH.WRITE_DATE DESC, MSH.WRITE_TIME DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MSH, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MSH
		 WHERE 1 = 1
	</select>
	
	<!-- 사용안함 -->
	<select id="checkStockHistory" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)                       AS "totalCount"
		  FROM MU_STOCK_HISTORY AS MSH
		 WHERE MSH.USE_YN                               = 'Y'
		   AND MSH.MU_TREATMENT_ID                      = #{muTreatmentId}
		   AND MSH.MU_PRODUCT_ID                        = #{muProductId}
		   AND MSH.MU_COMPANY_ID                        = #{muCompanyId}
		   AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') = #{inOutDate}
	</select>
	
	<!-- 현재 거래처수, 품목수, 재고총액 조회 -->
	<select id="selectTotalStockHistoryAmount" parameterType="hashmap" resultType="hashmap">
		SELECT SUM(MSH2.IN_TOTAL_AMOUNT-MSH2.OUT_TOTAL_AMOUNT)                 AS "totalAmount"
			 ,(SELECT COUNT(*)
				 FROM MU_COMPANY AS MC
				WHERE MC.USE_YN = 'Y'
				  AND DATE_FORMAT(MC.WRITE_DATE, '%Y-%m-%d') &lt;= #{endDate}
			   )                                                               AS "companyCount"
			  ,(SELECT COUNT(1)
				FROM(SELECT MU_PRODUCT_ID
					 FROM MU_STOCK_HISTORY AS MP
					 WHERE MP.USE_YN  = 'Y'
					 AND MP.IN_OUT_DATE &lt;= #{endDate}
					 GROUP BY MU_PRODUCT_ID) AS MMPI
		   )                                                                  AS "productCount"
		  FROM (
				SELECT SUM(IN_TOTAL_QUANTITY)  AS IN_TOTAL_QUANTITY,
					SUM(IN_TOTAL_AMOUNT)    AS IN_TOTAL_AMOUNT,
					SUM(OUT_TOTAL_QUANTITY) AS OUT_TOTAL_QUANTITY,
					SUM(OUT_TOTAL_AMOUNT)   AS OUT_TOTAL_AMOUNT
				FROM (SELECT CASE
                                 WHEN FLOOR(SUM(MSH.QUANTITY)) = SUM(MSH.QUANTITY) THEN FORMAT(IFNULL(SUM(MSH.QUANTITY),0),0)
                                 ELSE IFNULL(TRIM(TRAILING '0' FROM CAST(FORMAT(SUM(MSH.QUANTITY), 2) AS FLOAT)),0)
                             END                          AS IN_TOTAL_QUANTITY,
							 SUM(MSH.PRICE * MSH.QUANTITY) AS IN_TOTAL_AMOUNT,
							 0                             AS OUT_TOTAL_QUANTITY,
							 0                             AS OUT_TOTAL_AMOUNT
					   FROM MU_STOCK_HISTORY AS MSH
					   JOIN MU_PRODUCT MP
                         ON MSH.MU_PRODUCT_ID = MP.MU_PRODUCT_ID
					   WHERE MSH.USE_YN = 'Y'
					   AND (MSH.TYPE IN ('I', 'AA')
					        OR (MSH.TYPE = 'AA' AND DAY(CURDATE()) &lt;= 10))
					   AND (DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{endDate}
					        OR (MSH.TYPE = 'AA' AND DAY(CURDATE()) &lt;= 10
					        AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')))
                       AND DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d') &lt;=#{endDate}
					   UNION ALL
					  SELECT 0                              AS IN_TOTAL_QUANTITY,
					    	 0                              AS IN_TOTAL_AMOUNT,
							 CASE
                                 WHEN FLOOR(SUM(MSH.QUANTITY)) = SUM(MSH.QUANTITY) THEN FORMAT(IFNULL(SUM(MSH.QUANTITY),0),0)
                                 ELSE IFNULL(TRIM(TRAILING '0' FROM CAST(FORMAT(SUM(MSH.QUANTITY), 2) AS FLOAT)),0)
                             END                           AS OUT_TOTAL_QUANTITY,
							 SUM(MSH.PRICE * MSH.QUANTITY) AS OUT_TOTAL_AMOUNT
					    FROM MU_STOCK_HISTORY AS MSH
					    JOIN MU_PRODUCT MP
                          ON MSH.MU_PRODUCT_ID = MP.MU_PRODUCT_ID
					    WHERE MSH.USE_YN = 'Y'
					    AND (MSH.TYPE IN ('S', 'R', 'D', 'U', 'E', 'AD')
					        OR (MSH.TYPE = 'AD' AND DAY(CURDATE()) &lt;= 10))
					    AND (DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{endDate}
					     OR (MSH.TYPE = 'AD' AND DAY(CURDATE()) &lt;= 10
                            AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')))
                         AND DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d') &lt;=#{endDate}) AS combined
			   ) AS MSH2
		 WHERE 1=1
		 LIMIT 0, 1
	</select>
	
	<!-- 해당 일자까지의 재고마감 상세 -->
	<select id="selectListStockHistoryDetail" parameterType="hashmap" resultType="hashmap">
		SELECT MU_STOCK_HISTORY_ID                       AS "muStockHistoryId"
			 , MU_TREATMENT_ID                           AS "muTreatmentId"
			 , TREATMENT_NAME                            AS "treatmentName"
			 , MU_PRODUCT_ID                             AS "muProductId"
			 , PRODUCT_CODE                              AS "productCode"
			 , PRODUCT_NAME                              AS "productName"
			 , MU_COMPANY_ID                             AS "muCompanyId"
			 , COMPANY_NAME                              AS "companyName"
			 , DATE_FORMAT(IN_OUT_DATE, '%Y-%m-%d')      AS "inDate"
			 , PRICE                                     AS "price"
			 , IFNULL(TOTAL_QUANTITY, 0)                 AS "quantity"
			 , IFNULL(TOTAL_AMOUNT, 0)                   AS "totalAmount"
		  FROM (
				   SELECT IN_MSH.MU_STOCK_HISTORY_ID
						, IN_MSH.MU_TREATMENT_ID
						, IN_MSH.TREATMENT_NAME
						, IN_MSH.MU_PRODUCT_ID
						, IN_MSH.PRODUCT_CODE
						, IN_MSH.PRODUCT_NAME
						, IN_MSH.MU_COMPANY_ID
						, IN_MSH.COMPANY_NAME
						, IN_MSH.IN_OUT_DATE
						, IN_MSH.PRICE
						, IN_MSH.QUANTITY
						, (IN_MSH.QUANTITY-IFNULL(OUT_TOTAL_QUANTITY, 0)) AS TOTAL_QUANTITY
						, IFNULL(OUT_TOTAL_AMOUNT, 0)                     AS OUT_TOTAL_AMOUNT
						, (PRICE*QUANTITY)-IFNULL(OUT_TOTAL_AMOUNT, 0)    AS TOTAL_AMOUNT
					 FROM (
							  SELECT MSH.MU_STOCK_HISTORY_ID
								   , MSH.MU_TREATMENT_ID
								   , MSH.TREATMENT_NAME
								   , MSH.MU_PRODUCT_ID
								   , MSH.PRODUCT_CODE
								   , MSH.PRODUCT_NAME
								   , MSH.MU_COMPANY_ID
								   , MSH.COMPANY_NAME
								   , MSH.IN_OUT_DATE
								   , MSH.PRICE
								   , MSH.QUANTITY
								FROM MU_STOCK_HISTORY AS MSH
								JOIN MU_PRODUCT MP
								  ON MSH.MU_PRODUCT_ID = MP.MU_PRODUCT_ID
							   WHERE MSH.USE_YN = 'Y'
								 AND (MSH.TYPE IN ('I', 'AA')
									  OR (MSH.TYPE = 'AA' AND DAY(CURDATE()) &lt;= 10))
								 AND (DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{endDate}
									  OR (MSH.TYPE = 'AA' AND DAY(CURDATE()) &lt;= 10
									  AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')))
								 AND DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d') &lt;=#{endDate}
						   )AS IN_MSH
			    LEFT JOIN (
								SELECT MSH.MU_STOCK_HISTORY_ID2
									  ,MSH.MU_TREATMENT_ID
									  ,MSH.MU_PRODUCT_ID
									  ,MSH.MU_COMPANY_ID
									  ,SUM(MSH.QUANTITY)                  AS OUT_TOTAL_QUANTITY
									  ,SUM(MSH2.PRICE*MSH.QUANTITY)       AS OUT_TOTAL_AMOUNT
								  FROM MU_STOCK_HISTORY AS MSH
								  JOIN MU_PRODUCT MP
									ON MSH.MU_PRODUCT_ID = MP.MU_PRODUCT_ID
							INNER JOIN MU_STOCK_HISTORY AS MSH2
									ON MSH.MU_STOCK_HISTORY_ID2 = MSH2.MU_STOCK_HISTORY_ID
								   AND MSH.USE_YN = 'Y'
								   AND (MSH.TYPE IN ('S', 'R', 'D', 'U', 'E', 'AD')
										 OR (MSH.TYPE = 'AD' AND DAY(CURDATE()) &lt;= 10))
								   AND (DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m-%d') &lt;= #{endDate}
										OR (MSH.TYPE = 'AD' AND DAY(CURDATE()) &lt;= 10
										AND DATE_FORMAT(MSH.IN_OUT_DATE, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')))
								   AND DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d') &lt;=#{endDate}
							  GROUP BY MSH.MU_TREATMENT_ID
									  ,MSH.MU_PRODUCT_ID
									  ,MSH.MU_COMPANY_ID
									  ,MSH.MU_STOCK_HISTORY_ID2
						   ) AS OUT_MSH
						ON IN_MSH.MU_TREATMENT_ID = OUT_MSH.MU_TREATMENT_ID
					   AND IN_MSH.MU_PRODUCT_ID = OUT_MSH.MU_PRODUCT_ID
					   AND IN_MSH.MU_COMPANY_ID = OUT_MSH.MU_COMPANY_ID
					   AND IN_MSH.MU_STOCK_HISTORY_ID = OUT_MSH.MU_STOCK_HISTORY_ID2
		       ) AS MSH
		 WHERE 1=1
	</select>

	<insert id="insertStockHistory" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muStockHistoryId},LPAD((SELECT NEXTVAL(MU_STOCK_HISTORY_SEQ)), 10, 0))
		</selectKey>
		INSERT INTO MU_STOCK_HISTORY(
					 MU_STOCK_HISTORY_ID
					,MU_TREATMENT_ID
					,TREATMENT_NAME
					,MU_PRODUCT_ID
					,PRODUCT_CODE
					,PRODUCT_NAME
					,MU_COMPANY_ID
					,COMPANY_NAME
					,IN_OUT_DATE
					,TYPE
					,PRICE
					,QUANTITY
					<if test="filePath != null and filePath != ''">
						,FILE_PATH
					</if>
					<if test="fileName != null and fileName != ''">
						,FILE_NAME
					</if>
					<if test="originalFileName != null and originalFileName != ''">
						,ORIGINAL_FILE_NAME
					</if>
					,ETC
					,MU_STOCK_HISTORY_ID2
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muTreatmentId}
					,#{treatmentName}
					,#{muProductId}
					,#{productCode}
					,#{productName}
					,#{muCompanyId}
					,#{companyName}
					,#{inOutDate}
					,#{type}
					,#{price}
					,#{quantity}
					<if test="filePath != null and filePath != ''">
						,#{filePath}
					</if>
					<if test="fileName != null and fileName != ''">
						,#{fileName}
					</if>
					<if test="originalFileName != null and originalFileName != ''">
						,#{originalFileName}
					</if>
					,#{etc,jdbcType=VARCHAR}
					,#{muStockHistoryId2,jdbcType=VARCHAR}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>

	<insert id="insertStockHistory2" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muStockHistoryId},LPAD((SELECT NEXTVAL(MU_STOCK_HISTORY_SEQ)), 10, 0))
		</selectKey>
		INSERT INTO MU_STOCK_HISTORY(
					 MU_STOCK_HISTORY_ID
					,MU_TREATMENT_ID
					,TREATMENT_NAME
					,MU_PRODUCT_ID
					,PRODUCT_CODE
					,PRODUCT_NAME
					,MU_COMPANY_ID
					,COMPANY_NAME
					,IN_OUT_DATE
					,TYPE
					,PRICE
					,QUANTITY
					,ETC
					,MU_STOCK_HISTORY_ID2
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muTreatmentId}
					,#{treatmentName}
					,#{muProductId}
					,#{productCode}
					,#{productName}
					,#{muCompanyId}
					,#{companyName}
					,CURDATE()
					,#{type}
					,#{price}
					,#{quantity}
					,#{etc,jdbcType=VARCHAR}
					,#{muStockHistoryId2,jdbcType=VARCHAR}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
</mapper>