<?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="WebStockClose">
	
	<select id="selectTotalStockCloseCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM MU_STOCK_CLOSE AS MSC
		 WHERE MSC.USE_YN = 'Y'
		   <if test="stockCloseSearchStartDate != null and stockCloseSearchStartDate != '' and stockCloseSearchEndDate != null and stockCloseSearchEndDate != ''">
		   AND DATE_FORMAT(MSC.CLOSE_DATE, '%Y-%m-%d') &gt;= #{stockCloseSearchStartDate}
		   AND DATE_FORMAT(MSC.CLOSE_DATE, '%Y-%m-%d') &lt;= #{stockCloseSearchEndDate}
		   </if>
	</select>
	<select id="selectIdBeforeUpdate" resultType="hashmap" parameterType="hashmap">
		SELECT MU_STOCK_CLOSE_ID AS muStockCloseId
		FROM MU_STOCK_CLOSE
		WHERE USE_YN = 'Y'
		  AND DATE_FORMAT(END_DATE, '%Y-%m') = DATE_FORMAT(#{endDate}, '%Y-%m');
	</select>
	<update id="updateStockClose" parameterType="hashmap">
		UPDATE MU_STOCK_CLOSE
		SET MOD_ID = #{modId},
		MOD_DATE = NOW(),
		CUD_FLAG = 'D',
		USE_YN = 'N'
		WHERE USE_YN = 'Y'
		AND DATE_FORMAT(END_DATE, '%Y-%m') = DATE_FORMAT(#{endDate}, '%Y-%m')
	</update>
	<update id="updateStockCloseDetail" parameterType="hashmap">
			UPDATE MU_STOCK_CLOSE_DETAIL
			SET MOD_ID = #{modId},
			MOD_DATE = NOW(),
			CUD_FLAG = 'D',
			USE_YN = 'N'
			WHERE USE_YN = 'Y'
			AND MU_STOCK_CLOSE_ID = #{muStockCloseId}
	</update>
	<select id="selectListStockClose" parameterType="hashmap" resultType="hashmap">
		SELECT MSC.*
		  FROM (
					SELECT MSC.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MSC.MU_STOCK_CLOSE_ID                      AS "muStockCloseId"
									  ,DATE_FORMAT(MSC.END_DATE, '%Y-%m')      	  AS "endDate"
									  ,DATE_FORMAT(MSC.CLOSE_DATE, '%Y-%m-%d')    AS "closeDate"
									  ,FORMAT(MSC.COMPANY_COUNT, 0)               AS "companyCount"
									  ,FORMAT(MSC.PRODUCT_COUNT, 0)               AS "productCount"
									  ,FORMAT(MSC.TOTAL_AMOUNT, 0)                AS "totalAmount"
								  FROM MU_STOCK_CLOSE AS MSC
								 WHERE MSC.USE_YN = 'Y'
								   <if test="stockCloseSearchStartDate != null and stockCloseSearchStartDate != '' and stockCloseSearchEndDate != null and stockCloseSearchEndDate != ''">
								   AND DATE_FORMAT(MSC.CLOSE_DATE, '%Y-%m-%d') &gt;= #{stockCloseSearchStartDate}
								   AND DATE_FORMAT(MSC.CLOSE_DATE, '%Y-%m-%d') &lt;= #{stockCloseSearchEndDate}
								   </if>
								 <choose>
								 <when test="stockCloseSort != null and stockCloseSort != ''">
							  ORDER BY ${stockCloseSort}
								 </when>
								 <otherwise>
							  ORDER BY MSC.CLOSE_DATE DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MSC, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MSC
		 WHERE 1 = 1
		 LIMIT ${stockCloseStart}, ${stockCloseLimit}
	</select>
	
	<select id="selectListStockCloseExcel" parameterType="hashmap" resultType="hashmap">
		SELECT MSC.*
		  FROM (
					SELECT MSC.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MSC.MU_STOCK_CLOSE_ID                      AS "muStockCloseId"
									  ,DATE_FORMAT(MSC.END_DATE, '%Y-%m')         AS "endDate"
									  ,DATE_FORMAT(MSC.CLOSE_DATE, '%Y-%m-%d')    AS "closeDate"
									  ,FORMAT(MSC.COMPANY_COUNT, 0)               AS "companyCount"
									  ,FORMAT(MSC.PRODUCT_COUNT, 0)               AS "productCount"
									  ,FORMAT(MSC.TOTAL_AMOUNT, 0)                AS "totalAmount"
								  FROM MU_STOCK_CLOSE AS MSC
								 WHERE MSC.USE_YN = 'Y'
								   <if test="stockCloseSearchStartDate != null and stockCloseSearchStartDate != '' and stockCloseSearchEndDate != null and stockCloseSearchEndDate != ''">
								   AND DATE_FORMAT(MSC.CLOSE_DATE, '%Y-%m-%d') &gt;= #{stockCloseSearchStartDate}
								   AND DATE_FORMAT(MSC.CLOSE_DATE, '%Y-%m-%d') &lt;= #{stockCloseSearchEndDate}
								   </if>
								 <choose>
								 <when test="stockCloseSort != null and stockCloseSort != ''">
							  ORDER BY ${stockCloseSort}
								 </when>
								 <otherwise>
							  ORDER BY MSC.CLOSE_DATE DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MSC, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MSC
		 WHERE 1 = 1
	</select>

	<insert id="insertStockClose" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muStockCloseId},LPAD((SELECT NEXTVAL(MU_STOCK_CLOSE_SEQ)), 10, 0))
		</selectKey>
		INSERT INTO MU_STOCK_CLOSE(
					 MU_STOCK_CLOSE_ID
					,END_DATE
					,COMPANY_COUNT
					,PRODUCT_COUNT
					,TOTAL_AMOUNT
					,CLOSE_DATE
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{endDate}
					,#{companyCount}
					,#{productCount}
					,#{totalAmount}
					,CURDATE()
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
</mapper>