<?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="WebProduct">
	
	<select id="selectTotalProductOptionCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM (
				SELECT MU_PRODUCT_ID                               AS "muProductId"
					  ,PRODUCT_NAME                                AS "productName"
					  ,MU_COMPANY_ID                               AS "muCompanyId"
					  ,COMPANY_NAME                                AS "companyName"
					  ,MU_TREATMENT_ID                             AS "muTreatmentId"
					  ,CATEGORY_ITEM_NAME                          AS "treatmentName"
					  ,FORMAT(IFNULL(price,0),0)               	   AS "price"
					  ,CASE
						WHEN FLOOR(QUANTITY) = QUANTITY THEN FORMAT(IFNULL(QUANTITY,0),0)
						ELSE IFNULL(TRIM(TRAILING '0' FROM CAST(FORMAT(QUANTITY, 2) AS FLOAT)),0)
					   END                                         AS "quantity"
					  ,VOLUME                                      AS "volume"
					  ,UNIT_CODE                                   AS "unitCode"
					  ,UNIT_NAME                                   AS "unitName"
					  ,DATE_FORMAT(WRITE_DATE, '%Y-%m-%d')         AS "writeDate"
				  FROM (
						SELECT MP.MU_PRODUCT_ID
							  ,MP.PRODUCT_NAME
							  ,MC.MU_COMPANY_ID
							  ,MC.COMPANY_NAME
							  ,MP.MU_TREATMENT_ID
							  ,CATEGORY_ITEM_NAME
							  ,MSS.PRICE
							  ,SUM(MSS.QUANTITY) AS QUANTITY
							  ,MP.VOLUME
							  ,MP.UNIT_CODE
							  ,MP.UNIT_NAME
							  ,MP.WRITE_DATE
						  FROM MU_PRODUCT AS MP
					INNER JOIN MU_COMPANY_PRODUCT AS MCP
							ON MP.MU_PRODUCT_ID = MCP.MU_PRODUCT_ID
					LEFT JOIN MU_STOCK_SUM MSS
							ON MP.MU_PRODUCT_ID = MSS.MU_PRODUCT_ID
						   AND MSS.USE_YN='Y'
					INNER JOIN MU_CATEGORY_ITEM AS MCI
							ON MCI.MU_CATEGORY_ITEM_ID = MP.MU_TREATMENT_ID
						   AND MCI.USE_YN = 'Y'
					LEFT JOIN MU_COMPANY AS MC
							ON MC.MU_COMPANY_ID = MCP.MU_COMPANY_ID
						 WHERE MP.USE_YN  = 'Y'
					   <if test="productSearchKeywordParam != null and productSearchKeywordParam != ''">
						   AND MP.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{productSearchKeywordParam}),'%')
					   </if>
					  GROUP BY MU_PRODUCT_ID
				  ) AS MCP
		  ) AS MCP
		 WHERE 1 = 1
		   AND MCP.quantity > 0
	</select>
	
	<select id="selectListProductOption" parameterType="hashmap" resultType="hashmap">
		SELECT MCP.*
		  FROM (
				SELECT MCP.*
					  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
				  FROM (
						SELECT MU_PRODUCT_ID                               AS "muProductId"
							  ,PRODUCT_NAME                                AS "productName"
							  ,MU_COMPANY_ID                               AS "muCompanyId"
							  ,COMPANY_NAME                                AS "companyName"
							  ,MU_TREATMENT_ID                             AS "muTreatmentId"
							  ,CATEGORY_ITEM_NAME                          AS "treatmentName"
							  ,FORMAT(IFNULL(price,0),0)               	   AS "price"
							  ,CASE
								WHEN FLOOR(QUANTITY) = QUANTITY THEN FORMAT(IFNULL(QUANTITY,0),0)
								ELSE IFNULL(TRIM(TRAILING '0' FROM CAST(FORMAT(QUANTITY, 2) AS FLOAT)),0)
							   END                                         AS "quantity"
							  ,VOLUME                                      AS "volume"
							  ,UNIT_CODE                                   AS "unitCode"
							  ,UNIT_NAME                                   AS "unitName"
							  ,DATE_FORMAT(WRITE_DATE, '%Y-%m-%d')         AS "writeDate"
						  FROM (
								SELECT MP.MU_PRODUCT_ID
									  ,MP.PRODUCT_NAME
									  ,MC.MU_COMPANY_ID
									  ,MC.COMPANY_NAME
									  ,MP.MU_TREATMENT_ID
									  ,CATEGORY_ITEM_NAME
									  ,MSS.PRICE
									  ,SUM(MSS.QUANTITY) AS QUANTITY
									  ,MP.VOLUME
									  ,MP.UNIT_CODE
									  ,MP.UNIT_NAME
									  ,MP.WRITE_DATE
								  FROM MU_PRODUCT AS MP
							INNER JOIN MU_COMPANY_PRODUCT AS MCP
									ON MP.MU_PRODUCT_ID = MCP.MU_PRODUCT_ID
							LEFT JOIN MU_STOCK_SUM MSS
									ON MP.MU_PRODUCT_ID = MSS.MU_PRODUCT_ID
								   AND MSS.USE_YN='Y'
							INNER JOIN MU_CATEGORY_ITEM AS MCI
									ON MCI.MU_CATEGORY_ITEM_ID = MP.MU_TREATMENT_ID
								   AND MCI.USE_YN = 'Y'
							LEFT JOIN MU_COMPANY AS MC
									ON MC.MU_COMPANY_ID = MCP.MU_COMPANY_ID
								 WHERE MP.USE_YN  = 'Y'
							   <if test="productSearchKeywordParam != null and productSearchKeywordParam != ''">
								   AND MP.PRODUCT_NAME LIKE CONCAT('%',TRIM(#{productSearchKeywordParam}),'%')
							   </if>
							  GROUP BY MU_PRODUCT_ID
						  ) AS MCP
						 <choose>
						 <when test="productSort != null and productSort != ''">
					  ORDER BY ${productSort}
						 </when>
						 <otherwise>
					  ORDER BY MCP.CATEGORY_ITEM_NAME ASC
						 </otherwise>
						 </choose>
						 LIMIT 18446744073709551615
				  ) AS MCP, (SELECT @RNUM:=0) R
				 WHERE 1 = 1
				   AND MCP.quantity > 0
		  ) AS MCP
		 WHERE 1 = 1
	</select>
	
	<select id="checkProduct" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)      AS "totalCount"
		  FROM MU_PRODUCT AS MP
		 WHERE MP.USE_YN            = 'Y'
		   AND MP.MU_PRODUCT_ID     = #{muProductId}
		   AND MP.MU_TREATMENT_ID   = #{muTreatmentId}
		   AND MP.PRODUCT_NAME      = #{productName}
		   AND MP.VOLUME            = #{volume}
		   AND MP.UNIT_CODE         = #{unitCode}
		   AND MP.UNIT_NAME         = #{unitName}
		   AND MP.USAGE_VOLUME      = #{usage_volume}
		   AND MP.USAGE_UNIT_CODE   = #{usage_unitName}
	</select>
	
	<select id="selectProductCountSeq" parameterType="hashmap" resultType="hashmap">
		SELECT CONCAT('P', DATE_FORMAT(CURRENT_DATE() ,'%Y%m%d'), LPAD((SELECT NEXTVAL(MU_PRODUCT_COUNT_SEQ)), 4, 0)) AS "productCode"
	</select>
	
	<insert id="insertProduct" parameterType="hashmap">
		<selectKey resultType="hashmap" keyProperty="id" order="BEFORE">
			SELECT
				 productId
				,categoryId
			FROM (
				SELECT
					CONCAT(#{muProductId}, LPAD((SELECT NEXTVAL(MU_PRODUCT_SEQ)), 11, 0)) AS productId,
				(
				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 =#{categoryCode}  OR MCI.MU_CATEGORY_ITEM_ID = #{categoryCode}) AS categoryId
			)as subquery
		</selectKey>
		INSERT INTO MU_PRODUCT(
					 MU_PRODUCT_ID
					,PRODUCT_CODE
					,PRODUCT_NAME
					,MU_TREATMENT_ID
					,VOLUME
					,UNIT_CODE
					,UNIT_NAME
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
					,USAGE_VOLUME
					,USAGE_UNIT_CODE
		)VALUES(
					 #{id.productId}
					,#{productCode}
					,#{productName}
					,#{id.categoryId}
					,#{volume}
					,#{unitCode}
					,#{unitName}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
					,#{usage_volume}
					,#{usage_unitName}
		)
	</insert>
	
	<update id="deleteProduct" parameterType="hashmap">
		UPDATE MU_PRODUCT
		   SET MOD_ID            = #{modId}
			  ,MOD_DATE	         = NOW()
			  ,CUD_FLAG          = 'D'
			  ,USE_YN            = 'N'
		 WHERE USE_YN         = 'Y'
		   <if test="selectId != null and selectId != ''">
		   AND MU_PRODUCT_ID  NOT IN (${selectId})
		   </if>
		   AND MU_PRODUCT_ID  IN (SELECT MCP.MU_PRODUCT_ID
									FROM MU_COMPANY AS CM
							  INNER JOIN MU_COMPANY_PRODUCT AS MCP
									  ON CM.MU_COMPANY_ID = MCP.MU_COMPANY_ID
									 AND CM.USE_YN        = 'Y'
									 AND MCP.USE_YN       = 'Y'
									 AND CM.MU_COMPANY_ID = #{muCompanyId}
								 )
	</update>
</mapper>