<?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="WebStockSum">
	
	<!-- 현재고 재고품목수, 품목별 재고수, 거래처수, 재고총액 조회 -->
	<select id="selectTotalStockSumCompany" parameterType="hashmap" resultType="hashmap">
		SELECT (SELECT COUNT(1)
		        FROM(
					select MU_PRODUCT_ID
					FROM MU_STOCK_SUM AS MSS
					WHERE MSS.USE_YN='Y'
					GROUP BY MSS.MU_TREATMENT_ID
						   ,MSS.TREATMENT_NAME
						   ,MSS.MU_PRODUCT_ID
						   ,MSS.PRODUCT_CODE
						   ,MSS.PRODUCT_NAME
						   ,MSS.MU_COMPANY_ID
						   ,MSS.COMPANY_NAME) as mpi
			   )                                                                       AS "treatmentCount"
			  ,IFNULL((SELECT GROUP_CONCAT(CONCAT(MCI.CATEGORY_ITEM_NAME, IFNULL(SumQuantities.SumQuantity, 0)))
					   FROM MU_CATEGORY_ITEM AS MCI
								JOIN (
						   SELECT
							   MCI2.CATEGORY_ITEM_NAME
							   ,MU_CATEGORY_ITEM_ID
							   ,CASE
                                  WHEN FLOOR(SUM(MSS.QUANTITY)) = SUM(MSS.QUANTITY) THEN FORMAT(IFNULL(SUM(MSS.QUANTITY),0),0)
                                  ELSE IFNULL(TRIM(TRAILING '0' FROM CAST(FORMAT(SUM(MSS.QUANTITY), 2) AS FLOAT)),0)
                                END                                          AS "SumQuantity"
						   FROM MU_CATEGORY_ITEM AS MCI2
									LEFT JOIN MU_STOCK_SUM AS MSS ON MCI2.MU_CATEGORY_ITEM_ID = MSS.MU_TREATMENT_ID AND MSS.USE_YN = 'Y'
									JOIN MU_CATEGORY ON MU_CATEGORY.MU_CATEGORY_ID = MCI2.MU_CATEGORY_ID AND MU_CATEGORY.CATEGORY_NAME = '재고구분'
						   GROUP BY MCI2.MU_CATEGORY_ITEM_ID
					   ) AS SumQuantities ON MCI.MU_CATEGORY_ITEM_ID = SumQuantities.MU_CATEGORY_ITEM_ID
					   WHERE MCI.USE_YN = 'Y'
			   ),'')                                                                   AS "groupTreatmentCount"
			  ,(SELECT COUNT(MC.MU_COMPANY_ID)
				  FROM MU_COMPANY AS MC
				 WHERE MC.USE_YN = 'Y'
			   )                                                                       AS "companyCount"
			  ,IFNULL((SELECT SUM(MSS.PRICE*MSS.QUANTITY)
				  		 FROM MU_STOCK_SUM AS MSS
				 		WHERE MSS.USE_YN = 'Y'
			   ),0)                                                                    AS "stockAmount"
	</select>
	
	<select id="selectTotalStockSumCompanyCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)						AS "totalCount"
		FROM
		    (SELECT MU_PRODUCT_ID
				FROM MU_STOCK_SUM AS MSS
				WHERE MSS.USE_YN='Y'
				   <if test="stockSumTreatmentId != null and stockSumTreatmentId != ''">
				   AND MSS.MU_TREATMENT_ID IN (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 =#{stockSumTreatmentId})
				   </if>
				   <if test="stockSumCompanyId != null and stockSumCompanyId != ''">
				   AND MSS.MU_COMPANY_ID   = #{stockSumCompanyId}
				   </if>
				   <if test="stockSumSearchKeywordParam != null and stockSumSearchKeywordParam != ''">
			   AND ((MSS.COMPANY_NAME LIKE CONCAT('%',TRIM(#{stockSumSearchKeywordParam}),'%'))
					OR (MSS.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{stockSumSearchKeywordParam}),'%')))
				   </if>
				GROUP BY MSS.MU_TREATMENT_ID
				,MSS.TREATMENT_NAME
				,MSS.MU_PRODUCT_ID
				,MSS.PRODUCT_CODE
				,MSS.PRODUCT_NAME
				,MSS.MU_COMPANY_ID
				,MSS.COMPANY_NAME) as MPI
	</select>
	
	<select id="selectListStockSumCompany" parameterType="hashmap" resultType="hashmap">
		SELECT MSH.*
		FROM (
			SELECT MSH.*
				  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
			FROM (
					SELECT MCI.MU_CATEGORY_ITEM_ID    AS "muTreatmentId"
						  ,CATEGORY_ITEM_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"
						  ,FORMAT(TOTAL_AVG_PRICE, 0) AS "totalAvgPrice"
                          ,CASE
                             WHEN FLOOR(TOTAL_QUANTITY) = TOTAL_QUANTITY THEN FORMAT(IFNULL(TOTAL_QUANTITY,0),0)
                             ELSE TRIM(TRAILING '0' FROM CAST(FORMAT(TOTAL_QUANTITY, 2) AS FLOAT))
                           END                                          AS "totalQuantity"
					FROM (
							SELECT MSS.MU_TREATMENT_ID
								  ,MSS.MU_PRODUCT_ID
								  ,MSS.PRODUCT_CODE
								  ,MSS.PRODUCT_NAME
								  ,MSS.MU_COMPANY_ID
								  ,MSS.COMPANY_NAME
								  ,IFNULL(AVG(MSS.PRICE),0) AS TOTAL_AVG_PRICE
								  ,IFNULL(SUM(MSS.QUANTITY),0) AS TOTAL_QUANTITY
							 FROM MU_STOCK_SUM AS MSS
							 WHERE MSS.USE_YN='Y'
							   <if test="stockSumTreatmentId != null and stockSumTreatmentId != ''">
								AND MSS.MU_TREATMENT_ID IN (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 =#{stockSumTreatmentId})
							   </if>
							   <if test="stockSumCompanyId != null and stockSumCompanyId != ''">
							   AND MSS. MU_COMPANY_ID   = #{stockSumCompanyId}
							   </if>
							   <if test="stockSumSearchKeywordParam != null and stockSumSearchKeywordParam != ''">
							   AND ((MSS.COMPANY_NAME LIKE CONCAT('%',TRIM(#{stockSumSearchKeywordParam}),'%'))
								OR (MSS.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{stockSumSearchKeywordParam}),'%')))
							   </if>
							GROUP BY MSS.MU_TREATMENT_ID
								,MSS.TREATMENT_NAME
								,MSS.MU_PRODUCT_ID
								,MSS.PRODUCT_CODE
								,MSS.PRODUCT_NAME
								,MSS.MU_COMPANY_ID
								,MSS.COMPANY_NAME
					   ) AS MSH
				INNER JOIN MU_CATEGORY_ITEM AS MCI
				ON MCI.MU_CATEGORY_ITEM_ID = MSH.MU_TREATMENT_ID
				AND MCI.USE_YN = 'Y'
				 <choose>
				 <when test="stockSumSort != null and stockSumSort != ''">
			  ORDER BY ${stockSumSort}
				 </when>
				 <otherwise>
			  ORDER BY PRODUCT_NAME ASC
				 </otherwise>
				 </choose>
-- 				 LIMIT ${stockSumStart}, ${stockSumLimit}
			)MSH, (SELECT @RNUM:=0) R
			WHERE 1 = 1
		) MSH
		WHERE 1 = 1
	</select>
	
	<select id="selectListStockSumCompanyExcel" parameterType="hashmap" resultType="hashmap">
		SELECT MCI.MU_CATEGORY_ITEM_ID    AS "muTreatmentId"
						  ,CATEGORY_ITEM_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"
						  ,FORMAT(TOTAL_AVG_PRICE, 0) AS "totalAvgPrice"
                          ,CASE
                            WHEN FLOOR(TOTAL_QUANTITY) = TOTAL_QUANTITY THEN FORMAT(IFNULL(TOTAL_QUANTITY,0),0)
                            ELSE TRIM(TRAILING '0' FROM CAST(FORMAT(TOTAL_QUANTITY, 2) AS FLOAT))
                           END                                          AS "totalQuantity"
					FROM (
							SELECT MSS.MU_TREATMENT_ID
								  ,MSS.MU_PRODUCT_ID
								  ,MSS.PRODUCT_CODE
								  ,MSS.PRODUCT_NAME
								  ,MSS.MU_COMPANY_ID
								  ,MSS.COMPANY_NAME
								  ,IFNULL(AVG(MSS.PRICE),0) AS TOTAL_AVG_PRICE
								  ,IFNULL(SUM(MSS.QUANTITY),0) AS TOTAL_QUANTITY
							 FROM MU_STOCK_SUM AS MSS
							 WHERE MSS.USE_YN='Y'
							   <if test="stockSumTreatmentId != null and stockSumTreatmentId != ''">
								AND MSS.MU_TREATMENT_ID IN (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 =#{stockSumTreatmentId})
							   </if>
							   <if test="stockSumCompanyId != null and stockSumCompanyId != ''">
							   AND MSS. MU_COMPANY_ID   = #{stockSumCompanyId}
							   </if>
							   <if test="stockSumSearchKeywordParam != null and stockSumSearchKeywordParam != ''">
							   AND ((MSS.COMPANY_NAME LIKE CONCAT('%',TRIM(#{stockSumSearchKeywordParam}),'%'))
								OR (MSS.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{stockSumSearchKeywordParam}),'%')))
							   </if>
							GROUP BY MSS.MU_TREATMENT_ID
								,MSS.TREATMENT_NAME
								,MSS.MU_PRODUCT_ID
								,MSS.PRODUCT_CODE
								,MSS.PRODUCT_NAME
								,MSS.MU_COMPANY_ID
								,MSS.COMPANY_NAME
					   ) AS MSH
				INNER JOIN MU_CATEGORY_ITEM AS MCI
				ON MCI.MU_CATEGORY_ITEM_ID = MSH.MU_TREATMENT_ID
				AND MCI.USE_YN = 'Y'
				 <choose>
				 <when test="stockSumSort != null and stockSumSort != ''">
			  ORDER BY ${stockSumSort}
				 </when>
				 <otherwise>
			  ORDER BY PRODUCT_NAME ASC
				 </otherwise>
				 </choose>
	</select>
	
	<!-- 현재고 상세 제품명, 거래처, 평균 입고단가, 재고수량, 재고총액 조회 -->
	<select id="selectTotalStockSumCompanyHistory" parameterType="hashmap" resultType="hashmap">
		SELECT COMPANY_NAME          AS companyName
		      ,PRODUCT_NAME          AS productName
		      ,TREATMENT_NAME        AS treatmentName
		      ,AVG_PRICE             AS avgPrice
		      ,TOTAL_QUANTITY        AS totalQuantity
		      ,PRICE*TOTAL_QUANTITY  AS totalAmount
	      FROM (
				SELECT MSS.COMPANY_NAME                  								    AS "COMPANY_NAME"
					  ,IFNULL((SELECT CONCAT(MP.PRODUCT_NAME, '(', VOLUME,MP.UNIT_NAME, ')')
								 FROM MU_PRODUCT AS MP
								WHERE MP.MU_PRODUCT_ID = MSS.MU_PRODUCT_ID
						),'')                                                               AS "PRODUCT_NAME"
					  ,MSS.TREATMENT_NAME               									AS "TREATMENT_NAME"
					  ,ROUND(AVG(MSS.PRICE))             								    AS "AVG_PRICE"
					  ,SUM(CASE
						   WHEN FLOOR(MSS.QUANTITY) = MSS.QUANTITY THEN FORMAT(IFNULL(MSS.QUANTITY,0),0)
						   ELSE IFNULL(TRIM(TRAILING '0' FROM CAST(FORMAT(MSS.QUANTITY, 2) AS FLOAT)),0)
							END)                                          					AS "TOTAL_QUANTITY"
					  ,SUM(MSS.PRICE)       						   						AS "PRICE"
				  FROM MU_STOCK_SUM AS MSS
				  JOIN MU_PRODUCT MP ON MSS.MU_PRODUCT_ID = MP.MU_PRODUCT_ID
				 WHERE MSS.USE_YN = 'Y'
				   AND MSS.MU_TREATMENT_ID   = #{muTreatmentId}
				   AND MSS.MU_PRODUCT_ID     = #{muProductId}
				   AND MSS.MU_COMPANY_ID     = #{muCompanyId}
			  GROUP BY MSS.MU_TREATMENT_ID
					  ,MSS.MU_PRODUCT_ID
					  ,MSS.MU_COMPANY_ID
		  ) AS MSS
	</select>
	
	<select id="selectTotalStockSumCompanyHistoryCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)                      AS "totalCount"
		  FROM MU_STOCK_SUM AS MSS
		 WHERE MSS.USE_YN            = 'Y'
		   AND MSS.MU_TREATMENT_ID   = #{muTreatmentId}
		   AND MSS.MU_PRODUCT_ID     = #{muProductId}
		   AND MSS.MU_COMPANY_ID     = #{muCompanyId}
	</select>
	
	<select id="selectListStockSumCompanyHistory" parameterType="hashmap" resultType="hashmap">
		SELECT MSS.*
		  FROM (
					SELECT MSS.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MU_STOCK_SUM_ID                                           AS "muStockSumId"
									  ,MU_STOCK_HISTORY_ID                                       AS "muStockHistoryId"
									  ,MU_TREATMENT_ID                                           AS "muTreatmentId"
									  ,IFNULL((SELECT MT.TREATMENT_NAME
												 FROM MU_TREATMENT AS MT
												WHERE MT.USE_YN = 'Y'
												  AND MT.MU_TREATMENT_ID = MSS.MU_TREATMENT_ID
									   ),'')                                                     AS "treatmentName"
									  ,MU_PRODUCT_ID                                             AS "muProductId"
									  ,IFNULL((SELECT MP.PRODUCT_NAME
												 FROM MU_PRODUCT AS MP
												WHERE MP.MU_PRODUCT_ID = MSS.MU_PRODUCT_ID
									   ),'')                                                     AS "productName"
									  ,MU_COMPANY_ID                                             AS "muCompanyId"
									  ,IFNULL((SELECT MC.COMPANY_NAME
												 FROM MU_COMPANY AS MC
												WHERE MC.MU_COMPANY_ID = MSS.MU_COMPANY_ID
									   ),'')                                                     AS "companyName"
									  ,DATE_FORMAT(IN_DATE, '%Y-%m-%d')                          AS "inDate"
									  ,'입고'                                                      AS "typeName"
									  ,FORMAT(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(PRICE*QUANTITY, 0)                                 AS "amount"
									  ,ETC                                                       AS "etc"
								  FROM (
											SELECT MSS.MU_STOCK_SUM_ID
												  ,MSS.MU_STOCK_HISTORY_ID
												  ,MSS.MU_TREATMENT_ID
												  ,MSS.MU_PRODUCT_ID
												  ,MSS.MU_COMPANY_ID
												  ,MSS.IN_DATE
												  ,MSS.PRICE
												  ,MSS.QUANTITY
												  ,MSS.REG_DATE
												  ,MSS.ETC
											  FROM MU_STOCK_SUM AS MSS
											 WHERE MSS.USE_YN            = 'Y'
											   AND MSS.MU_TREATMENT_ID   = #{muTreatmentId}
											   AND MSS.MU_PRODUCT_ID     = #{muProductId}
											   AND MSS.MU_COMPANY_ID     = #{muCompanyId}
									   ) AS MSS
								 WHERE 1=1
								 <choose>
								 <when test="stockSumHistorySort != null and stockSumHistorySort != ''">
							  ORDER BY ${stockSumHistorySort}
								 </when>
								 <otherwise>
							  ORDER BY MSS.IN_DATE DESC, REG_DATE DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MSS, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MSS
		 WHERE 1 = 1
-- 		 LIMIT ${stockSumHistoryStart}, ${stockSumHistoryLimit}
	</select>
	
	
	
	<!-- 제품별 실시간 재고 현황 총갯수 (재고조정 화면) -->
	<select id="selectTotalStockSumAdjustmentCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)                       AS "totalCount"
		  FROM (
					SELECT MSS.MU_TREATMENT_ID 
						  ,MSS.MU_PRODUCT_ID 
						  ,MSS.MU_COMPANY_ID
					  FROM MU_STOCK_SUM AS MSS
					 WHERE MSS.USE_YN = 'Y'
					   <if test="stockSumAdjustmentTreatmentId != null and stockSumAdjustmentTreatmentId != ''">
					   AND MSS.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 =#{stockSumAdjustmentTreatmentId})
					   </if>
					   <if test="stockSumAdjustmentCompanyId != null and stockSumAdjustmentCompanyId != ''">
					   AND MSS.MU_COMPANY_ID   = #{stockSumAdjustmentCompanyId}
					   </if>
					   <if test="stockSumAdjustmentSearchKeywordParam != null and stockSumAdjustmentSearchKeywordParam != ''">
					   AND ((MSS.COMPANY_NAME LIKE CONCAT('%',TRIM(#{stockSumAdjustmentSearchKeywordParam}),'%'))
						OR (MSS.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{stockSumAdjustmentSearchKeywordParam}),'%')))
					   </if>
				  GROUP BY MSS.MU_TREATMENT_ID
						  ,MSS.TREATMENT_NAME 
						  ,MSS.MU_PRODUCT_ID 
						  ,MSS.PRODUCT_CODE
						  ,MSS.PRODUCT_NAME
						  ,MSS.MU_COMPANY_ID
						  ,MSS.COMPANY_NAME
			   ) MSS
		 WHERE 1=1
	</select>
	
	<!-- 제품별 실시간 재고 현황 리스트 (재고조정 화면) -->
	<select id="selectListStockSumAdjustment" parameterType="hashmap" resultType="hashmap">
		SELECT MSS.*
		  FROM (
					SELECT MSS.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT 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"
									  ,CASE
									      WHEN FLOOR(SUM(MSS.QUANTITY)) = SUM(MSS.QUANTITY) THEN FORMAT(IFNULL(SUM(MSS.QUANTITY),0),0)
										  ELSE TRIM(TRAILING '0' FROM CAST(FORMAT(SUM(MSS.QUANTITY), 2) AS FLOAT))
									   END                               							 AS "quantity"
									  ,FORMAT(SUM(MSS.PRICE * MSS.QUANTITY), 0)                      AS "amount"
									  ,IFNULL((SELECT FORMAT(AVG(MSS2.PRICE), 0)
												 FROM MU_STOCK_SUM AS MSS2
												WHERE MSS2.USE_YN = 'Y'
												  AND MSS2.QUANTITY > 0
												  AND MSS2.MU_TREATMENT_ID = MSS.MU_TREATMENT_ID
										 	 	  AND MSS2.MU_PRODUCT_ID   = MSS.MU_PRODUCT_ID  
										 	  	  AND MSS2.MU_COMPANY_ID   = MSS.MU_COMPANY_ID  
									   ), 0)                                                         AS "avgPrice"
								  FROM MU_STOCK_SUM AS MSS
								 WHERE MSS.USE_YN = 'Y'
								   <if test="stockSumAdjustmentTreatmentId != null and stockSumAdjustmentTreatmentId != ''">
								   AND MSS.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 =#{stockSumAdjustmentTreatmentId})
								   </if>
								   <if test="stockSumAdjustmentCompanyId != null and stockSumAdjustmentCompanyId != ''">
								   AND MSS.MU_COMPANY_ID   = #{stockSumAdjustmentCompanyId}
								   </if>
								   <if test="stockSumAdjustmentSearchKeywordParam != null and stockSumAdjustmentSearchKeywordParam != ''">
								   AND ((MSS.COMPANY_NAME LIKE CONCAT('%',TRIM(#{stockSumAdjustmentSearchKeywordParam}),'%'))
									OR (MSS.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{stockSumAdjustmentSearchKeywordParam}),'%')))
								   </if>
							  GROUP BY MSS.MU_TREATMENT_ID
									  ,MSS.TREATMENT_NAME 
									  ,MSS.MU_PRODUCT_ID 
									  ,MSS.PRODUCT_CODE
									  ,MSS.PRODUCT_NAME
									  ,MSS.MU_COMPANY_ID
									  ,MSS.COMPANY_NAME
								 <choose>
								 <when test="stockSumAdjustmentSort != null and stockSumAdjustmentSort != ''">
							  ORDER BY ${stockSumAdjustmentSort}
								 </when>
								 <otherwise>
							  ORDER BY PRODUCT_NAME ASC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MSS, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MSS
		 WHERE 1 = 1
-- 		 LIMIT ${stockSumAdjustmentStart}, ${stockSumAdjustmentLimit}
	</select>
	
	<select id="selectListStockSumAdjustmentExcel" parameterType="hashmap" resultType="hashmap">
		SELECT MSS.*
		  FROM (
					SELECT MSS.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT 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"
									  ,CASE
									      WHEN FLOOR(SUM(MSS.QUANTITY)) = SUM(MSS.QUANTITY) THEN FORMAT(IFNULL(SUM(MSS.QUANTITY),0),0)
										  ELSE TRIM(TRAILING '0' FROM CAST(FORMAT(SUM(MSS.QUANTITY), 2) AS FLOAT))
									   END                               							 AS "quantity"
									  ,FORMAT(SUM(MSS.PRICE * MSS.QUANTITY), 0)                      AS "amount"
									  ,IFNULL((SELECT FORMAT(AVG(MSS2.PRICE), 0)
												 FROM MU_STOCK_SUM AS MSS2
												WHERE MSS2.USE_YN = 'Y'
												  AND MSS2.QUANTITY > 0
												  AND MSS2.MU_TREATMENT_ID = MSS.MU_TREATMENT_ID
										 	 	  AND MSS2.MU_PRODUCT_ID   = MSS.MU_PRODUCT_ID  
										 	  	  AND MSS2.MU_COMPANY_ID   = MSS.MU_COMPANY_ID  
									   ), 0)                                                         AS "avgPrice"
								  FROM MU_STOCK_SUM AS MSS
								 WHERE MSS.USE_YN = 'Y'
								   <if test="stockSumAdjustmentTreatmentId != null and stockSumAdjustmentTreatmentId != ''">
								   AND MSS.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 =#{stockSumAdjustmentTreatmentId})
								   </if>
								   <if test="stockSumAdjustmentCompanyId != null and stockSumAdjustmentCompanyId != ''">
								   AND MSS.MU_COMPANY_ID   = #{stockSumAdjustmentCompanyId}
								   </if>
								   <if test="stockSumAdjustmentSearchKeywordParam != null and stockSumAdjustmentSearchKeywordParam != ''">
								   AND ((MSS.COMPANY_NAME LIKE CONCAT('%',TRIM(#{stockSumAdjustmentSearchKeywordParam}),'%'))
									OR (MSS.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{stockSumAdjustmentSearchKeywordParam}),'%')))
								   </if>
							  GROUP BY MSS.MU_TREATMENT_ID
									  ,MSS.TREATMENT_NAME 
									  ,MSS.MU_PRODUCT_ID 
									  ,MSS.PRODUCT_CODE
									  ,MSS.PRODUCT_NAME
									  ,MSS.MU_COMPANY_ID
									  ,MSS.COMPANY_NAME
								 <choose>
								 <when test="stockSumAdjustmentSort != null and stockSumAdjustmentSort != ''">
							  ORDER BY ${stockSumAdjustmentSort}
								 </when>
								 <otherwise>
							  ORDER BY PRODUCT_NAME ASC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MSS, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MSS
		 WHERE 1 = 1
	</select>
	
	<!-- 제품별 실시간 재고 현황 상세 총갯수 (재고조정 팝업 화면) -->
	<select id="selectTotalStockSumAdjustmentDetailCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)                       AS "totalCount"
		  FROM MU_STOCK_SUM AS MSS
		 WHERE MSS.USE_YN = 'Y'
		   AND MSS.MU_TREATMENT_ID = #{muTreatmentId}
		   AND MSS.MU_PRODUCT_ID   = #{muProductId}
		   AND MSS.MU_COMPANY_ID   = #{muCompanyId}
	</select>
	
	<!-- 제품별 실시간 재고 현황 상세 리스트 (재고조정 팝업 화면) -->
	<select id="selectListStockSumAdjustmentDetail" parameterType="hashmap" resultType="hashmap">
		SELECT MSS.*
		  FROM (
					SELECT MSS.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MSS.MU_STOCK_SUM_ID                          AS "muStockSumId"
									  ,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(MSS.IN_DATE, '%Y-%m-%d')         AS "inDate"
									  ,FORMAT(MSS.PRICE, 0)                         AS "price"
									  ,CASE
										   WHEN FLOOR(MSS.QUANTITY) = MSS.QUANTITY
											   THEN FORMAT(ROUND(MSS.QUANTITY, 0), 0)
										   ELSE TRIM(TRAILING '.00' FROM FORMAT(ROUND(MSS.QUANTITY, 2), 2))
									   END										    AS "quantity"
									  ,FORMAT(MSS.PRICE * MSS.QUANTITY, 0)          AS "amount"
									  ,IFNULL(MSS.ETC, 0)                           AS "etc"
								  FROM MU_STOCK_SUM AS MSS
								 WHERE MSS.USE_YN = 'Y'
								   AND MSS.MU_TREATMENT_ID = #{muTreatmentId}
								   AND MSS.MU_PRODUCT_ID   = #{muProductId}
								   AND MSS.MU_COMPANY_ID   = #{muCompanyId}
							  ORDER BY IN_DATE DESC, TREATMENT_NAME ASC, PRODUCT_NAME ASC, COMPANY_NAME ASC
								 LIMIT 18446744073709551615
						   ) MSS, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MSS
		 WHERE 1 = 1
	</select>
	
	<!-- 현재 재고 확인 -->
	<select id="selectListStockSum" parameterType="hashmap" resultType="hashmap">
		SELECT MSS.MU_STOCK_SUM_ID                AS "muStockSumId"
			  ,MSS.MU_STOCK_HISTORY_ID            AS "muStockHistoryId"
			  ,MSS.COMPANY_NAME            		  AS "companyName"
			  ,MSS.IN_DATE                        AS "inDate"
			  ,MSS.PRICE                          AS "price"
			  ,MSS.QUANTITY                       AS "quantity"
		  FROM MU_STOCK_SUM AS MSS
		 WHERE MSS.USE_YN            = 'Y'
		   AND MSS.QUANTITY          > 0
		   AND MSS.MU_TREATMENT_ID   = #{muTreatmentId}
		   AND MSS.MU_PRODUCT_ID     = #{muProductId}
		   AND MSS.MU_COMPANY_ID     = #{muCompanyId}
	  ORDER BY MSS.IN_DATE ASC, REG_DATE ASC
	</select>
	
	<!-- 현재 재고 수량 확인 -->
	<select id="selectTotalStockSumQuantity" parameterType="hashmap" resultType="hashmap">
		SELECT SUM(MSS.QUANTITY)             AS "totalQuantity"
		  FROM MU_STOCK_SUM AS MSS
		 WHERE MSS.USE_YN            = 'Y'
		   AND MSS.MU_TREATMENT_ID   = #{muTreatmentId}
		   AND MSS.MU_PRODUCT_ID     = #{muProductId}
		   AND MSS.MU_COMPANY_ID     = #{muCompanyId}
	</select>
	
	<!-- 입고일자에 등록된 게 잇는 지 확인 -->
	<select id="checkStockSumIn" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)                           AS "totalCount"
		  FROM MU_STOCK_SUM AS MSS
		 WHERE MSS.USE_YN                           = 'Y'
		   AND MSS.MU_TREATMENT_ID                  = #{muTreatmentId}
		   AND MSS.MU_PRODUCT_ID                    = #{muProductId}
		   AND MSS.MU_COMPANY_ID                    = #{muCompanyId}
		   AND DATE_FORMAT(MSS.IN_DATE, '%Y-%m-%d') = #{inDate}
	</select>
	
	<insert id="insertStockSum" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muStockSumId},LPAD((SELECT NEXTVAL(MU_STOCK_SUM_SEQ)), 10, 0))
		</selectKey>
		INSERT INTO MU_STOCK_SUM(
					 MU_STOCK_SUM_ID
					,MU_STOCK_HISTORY_ID
					,MU_TREATMENT_ID
					,TREATMENT_NAME
					,MU_PRODUCT_ID
					,PRODUCT_CODE
					,PRODUCT_NAME
					,MU_COMPANY_ID
					,COMPANY_NAME
					,IN_DATE
					,PRICE
					,QUANTITY
					,ETC
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muStockHistoryId}
					,#{muTreatmentId}
					,#{treatmentName}
					,#{muProductId}
					,#{productCode}
					,#{productName}
					,#{muCompanyId}
					,#{companyName}
					,#{inDate}
					,#{price}
					,#{quantity}
					,#{etc,jdbcType=VARCHAR}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	
	<insert id="insertStockSum2" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muStockSumId},LPAD((SELECT NEXTVAL(MU_STOCK_SUM_SEQ)), 10, 0))
		</selectKey>
		INSERT INTO MU_STOCK_SUM(
					 MU_STOCK_SUM_ID
					,MU_STOCK_HISTORY_ID
					,MU_TREATMENT_ID
					,TREATMENT_NAME
					,MU_PRODUCT_ID
					,PRODUCT_CODE
					,PRODUCT_NAME
					,MU_COMPANY_ID
					,COMPANY_NAME
					,IN_DATE
					,PRICE
					,QUANTITY
					,ETC
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muStockHistoryId}
					,#{muTreatmentId}
					,#{treatmentName}
					,#{muProductId}
					,#{productCode}
					,#{productName}
					,#{muCompanyId}
					,#{companyName}
					,CURDATE()
					,#{price}
					,#{quantity}
					,#{etc,jdbcType=VARCHAR}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	
	<update id="updateStockSum" parameterType="hashmap">
		UPDATE MU_STOCK_SUM
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,QUANTITY            = #{quantity}
		 WHERE USE_YN           = 'Y'
		   AND MU_STOCK_SUM_ID  = #{muStockSumId}
	</update>
	
	<update id="updateStockSum2" parameterType="hashmap">
		UPDATE MU_STOCK_SUM
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,QUANTITY            = QUANTITY +
			                          (CASE
                                        WHEN #{type} = 'AA' THEN #{quantity}
                                        WHEN #{type} = 'AD' THEN -#{quantity}
                                        ELSE 0
                                       END)
		 WHERE USE_YN           = 'Y'
		   AND MU_STOCK_SUM_ID  = #{muStockSumId}
	</update>
</mapper>