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

	<select id="selectListCompanyOption" parameterType="hashMap" resultType="hashMap">
		SELECT MC.MU_COMPANY_ID            AS "muCompanyId"
			  ,MC.COMPANY_NAME             AS "companyName"
		  FROM MU_COMPANY AS MC
		 WHERE MC.USE_YN     = 'Y'
 	  ORDER BY MC.COMPANY_NAME ASC
	</select>
	
	<select id="selectTotalCompanyCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM MU_COMPANY AS MC
		 WHERE MC.USE_YN = 'Y'
		   <if test="companySearchKeywordParam != null and companySearchKeywordParam != ''">
		   AND ((MC.COMPANY_NAME LIKE CONCAT('%',TRIM(#{companySearchKeywordParam}),'%'))
			OR (MC.USER_NAME LIKE CONCAT('%',TRIM(#{companySearchKeywordParam}),'%')))
		   </if>
	</select>
	
	<select id="selectListCompany" parameterType="hashmap" resultType="hashmap">
		SELECT MC.*
		  FROM (
					SELECT MC.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT 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"
									  ,MC.MEMO                                              AS "memo"
									  ,IFNULL((SELECT GROUP_CONCAT(MP.PRODUCT_NAME)
												 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'
												WHERE MCP.MU_COMPANY_ID = MC.MU_COMPANY_ID
											 ORDER BY MCP.ORDER_NUMBER ASC
									   ),'')                                                AS "productName"
									  ,DATE_FORMAT(MC.WRITE_DATE, '%Y-%m-%d')               AS "writeDate"
								  FROM MU_COMPANY AS MC
								 WHERE MC.USE_YN = 'Y'
								   <if test="companySearchKeywordParam != null and companySearchKeywordParam != ''">
								   AND ((MC.COMPANY_NAME LIKE CONCAT('%',TRIM(#{companySearchKeywordParam}),'%'))
									OR (MC.USER_NAME LIKE CONCAT('%',TRIM(#{companySearchKeywordParam}),'%')))
								   </if>
								 <choose>
								 <when test="companySort != null and companySort != ''">
							  ORDER BY ${companySort}
								 </when>
								 <otherwise>
							  ORDER BY MC.WRITE_DATE DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MC, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MC
		 WHERE 1 = 1
		 LIMIT ${companyStart}, ${companyLimit}
	</select>

	<select id="selectCompany" parameterType="hashmap" resultType="hashmap">
		SELECT 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"
			  ,MC.MEMO                                AS "memo"
			  ,DATE_FORMAT(MC.WRITE_DATE, '%Y-%m-%d') AS "writeDate"
		  FROM MU_COMPANY AS MC
		 WHERE MC.USE_YN         = 'Y'
		   AND MC.MU_COMPANY_ID  = #{muCompanyId}
		 LIMIT 0, 1
	</select>

	<insert id="insertCompany" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muCompanyId},LPAD((SELECT NEXTVAL(MU_COMPANY_SEQ)), 11, 0))
		</selectKey>
		INSERT INTO MU_COMPANY(
					 MU_COMPANY_ID
					,COMPANY_NAME
					,BUSINESS_NUMBER
					,USER_NAME
					,PHONE_NUMBER
					,EMAIL
					,MEMO
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{companyName}
					,#{businessNumber}
					,#{userName}
					,#{phoneNumber}
					,#{email}
					,#{memo,jdbcType=VARCHAR}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	
	<update id="updateCompany" parameterType="hashmap">
		UPDATE MU_COMPANY
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,COMPANY_NAME        = #{companyName}
			  ,BUSINESS_NUMBER     = #{businessNumber}
			  ,USER_NAME           = #{userName}
			  ,PHONE_NUMBER        = #{phoneNumber}
			  ,EMAIL               = #{email}
			  ,MEMO                = #{memo,jdbcType=VARCHAR}
		 WHERE USE_YN         = 'Y'
		   AND MU_COMPANY_ID  = #{muCompanyId}
	</update>
	
	<update id="deleteCompany" parameterType="hashmap">
		UPDATE MU_COMPANY
		   SET MOD_ID            = #{modId}
			  ,MOD_DATE	         = NOW()
			  ,CUD_FLAG          = 'D'
			  ,USE_YN            = 'N'
		 WHERE USE_YN         = 'Y'
		   AND MU_COMPANY_ID  = #{muCompanyId}
	</update>
</mapper>