<?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="WebCompanyProduct">
	
	<select id="selectListCompanyProduct" parameterType="hashmap" resultType="hashmap">
		SELECT MP.MU_PRODUCT_ID                               AS "muProductId"
			  ,MP.PRODUCT_NAME                                AS "productName"
			  ,MP.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 = MP.MU_TREATMENT_ID
						  LIMIT 0, 1
			   ),'')                                          AS "treatmentName"
			  ,MP.VOLUME                                      AS "volume"
			  ,MP.UNIT_CODE                                   AS "unitCode"
			  ,MP.UNIT_NAME                                   AS "unitName"
			  ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')         AS "writeDate"
			  ,MP.USAGE_VOLUME                                AS "usage_volume"
			  ,MP.USAGE_UNIT_CODE                             AS "usage_unitCode"
			  ,MP.USAGE_UNIT_CODE                             AS "usage_unitName"
		  FROM MU_PRODUCT AS MP
	INNER JOIN MU_COMPANY_PRODUCT AS MCP
			ON MP.MU_PRODUCT_ID = MCP.MU_PRODUCT_ID
		   AND MP.USE_YN  = 'Y'
		   AND MCP.USE_YN = 'Y'
		   AND MCP.MU_COMPANY_ID = #{muCompanyId}
	  ORDER BY MCP.ORDER_NUMBER ASC
	</select>
	
	<select id="selectCompanyProduct" parameterType="hashmap" resultType="hashmap">
		SELECT MP.MU_PRODUCT_ID                               AS "muProductId"
			  ,MP.PRODUCT_CODE                                AS "productCode"
			  ,MP.PRODUCT_NAME                                AS "productName"
			  ,MP.VOLUME                                      AS "volume"
			  ,MP.UNIT_CODE                                   AS "unitCode"
			  ,MP.UNIT_NAME                                   AS "unitName"
			  ,MP.MU_TREATMENT_ID                             AS "muTreatmentId"
			  ,MP.USAGE_VOLUME                                AS "usage_volume"
			  ,MP.USAGE_UNIT_CODE                             AS "usage_unitCode"
			  ,MP.USAGE_UNIT_CODE                             AS "usage_unitName"
			  ,IFNULL((SELECT MCI.CATEGORY_ITEM_NAME
						 FROM MU_CATEGORY_ITEM AS MCI
						WHERE MCI.USE_YN      = 'Y'
			 			  AND MCI.MU_CATEGORY_ITEM_ID = MP.MU_TREATMENT_ID
			 			LIMIT 0, 1
			   ),'')                                          AS "treatmentName"
			  ,MC.MU_COMPANY_ID                               AS "muCompanyId"
			  ,MC.COMPANY_NAME                                AS "companyName"
			  ,MC.BUSINESS_NUMBER                             AS "businessNumber"
			  ,MC.USER_NAME                                   AS "userName"
			  ,MC.PHONE_NUMBER                                AS "phoneNumber"
			  ,MC.EMAIL                                       AS "email"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.USE_YN      = 'Y'
			 			  AND MM.MU_MEMBER_ID = #{loginMemberId}
			 			LIMIT 0, 1
			   ),'')                                          AS "memberName"
		  FROM MU_PRODUCT AS MP
	INNER JOIN MU_COMPANY_PRODUCT AS MCP
			ON MP.MU_PRODUCT_ID = MCP.MU_PRODUCT_ID
	INNER JOIN MU_COMPANY AS MC
			ON MC.MU_COMPANY_ID = MCP.MU_COMPANY_ID
		   AND MP.USE_YN  = 'Y'
		   AND MC.USE_YN  = 'Y'
		   AND MP.MU_TREATMENT_ID = #{muTreatmentId}
		   AND MP.MU_PRODUCT_ID   = #{muProductId}
		   AND MC.MU_COMPANY_ID   = #{muCompanyId}
		 LIMIT 0, 1
	</select>
	
	<insert id="insertCompanyProduct" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muCompanyProductId},LPAD((SELECT NEXTVAL(MU_COMPANY_PRODUCT_SEQ)), 10, 0))
		</selectKey>
		INSERT INTO MU_COMPANY_PRODUCT(
					 MU_COMPANY_PRODUCT_ID
					,MU_COMPANY_ID
					,MU_PRODUCT_ID
					,ORDER_NUMBER
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muCompanyId}
					,#{muProductId}
					,#{orderNumber}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	
	<update id="deleteCompanyProduct" parameterType="hashmap">
		UPDATE MU_COMPANY_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_COMPANY_ID  = #{muCompanyId}
	</update>
</mapper>