<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.no1.wms.product.ProductMapper">
	<resultMap id="productResultMap" type="ProductDto">
		<id column="id" 					property="id" />
		<result column="name" 				property="name" />
		<result column="company_name" 		property="company_name" />
		<result column="kan_code" 			property="kan_code" />
		<result column="vendor_id" 			property="vendor_id" />
		<result column="registration_date" 	property="registration_date" />
		<result column="manager_id" 		property="manager_id" />
		<result column="activation" 		property="activation" />
		<association property="vendorDto" 	javaType="VendorDto">
			<id column="id" 					property="id" />
			<result column="vendor_name" 		property="name" />
			<result column="president_name" property="president_name" />
		</association>
		<association property="accountDto" 	javaType="AccountDto">
			<id column="id" 					property="id" />
			<result column="account_name" 		property="name" />
		</association>
		<association property="categoryDto" javaType="CategoryDto">
			<id column="kan_code" 					property="kan_code" />
			<result column="cls_nm_1" property="cls_nm_1" />
			<result column="cls_nm_2" property="cls_nm_2" />
			<result column="cls_nm_3" property="cls_nm_3" />
			<result column="cls_nm_4" property="cls_nm_4" />
		</association>
	</resultMap>

	<!-- insert -->
		<insert id="createProcess" parameterType="ProductDto">
		INSERT INTO product (id, name, company_name, kan_code, vendor_id, registration_date, manager_id, activation) 
		VALUES (UUID(), #{name}, #{company_name}, #{kan_code}, #{vendor_id}, CURDATE(), #{manager_id}, 1)
		</insert>
		
		
		
		

	<!-- update -->
		<update id="updateById" parameterType="ProductDto">
			UPDATE product 
			SET name = #{name}, company_name = #{company_name},kan_code = #{kan_code}, 
			vendor_id = #{vendor_id}, manager_id = #{manager_id}, registration_date = CURDATE()
			WHERE id = #{id}
		</update>
		
		<update id="deactivateById" parameterType="String">
		UPDATE product
		SET activation = 0
		WHERE id = #{id}
		</update>
		
	<!-- delete -->


	<!-- select -->
	<!--  
	<select id="productList" resultMap="productResultMap" parameterType="map" >
		SELECT
		p.id, p.name, p.company_name, p.kan_code, p.vendor_id, p.registration_date,
		p.manager_id, p.activation,
		v.name as vendor_name, v.president_name,
		a.name as account_name,
		pc.cls_nm_1, pc.cls_nm_2, pc.cls_nm_3, pc.cls_nm_4
		FROM product as p
		left join vendor as v on p.vendor_id = v.id
		left join account as a on p.manager_id = a.id
		left join product_category as pc on p.kan_code = pc.kan_code
		WHERE p.activation != 0
		ORDER BY p.registration_date limit #{start} , #{count}
	</select>
	-->
	
	<select id="selectById" parameterType="String" resultMap="productResultMap">
		SELECT
		p.id, p.name, p.company_name, p.kan_code, p.vendor_id, p.registration_date,
		p.manager_id, p.activation,
		v.name as vendor_name, v.president_name,
		a.name as account_name,
		pc.cls_nm_1, pc.cls_nm_2, pc.cls_nm_3, pc.cls_nm_4
		FROM product as p
		left join vendor as v on p.vendor_id = v.id
		left join account as a on p.manager_id = a.id
		left join product_category as pc on p.kan_code = pc.kan_code
		WHERE p.id = #{id}
		
	</select>
	
	<select id="count" parameterType="map" resultType="int">
	    SELECT COUNT(*)
	
	    FROM product as p
		left join vendor as v on p.vendor_id = v.id
		left join account as a on p.manager_id = a.id
		left join product_category as pc on p.kan_code = pc.kan_code
	    <where>
	        <choose>
	            <when test="searchn == 0">
	                p.activation = 1 AND p.name LIKE CONCAT('%',#{search},'%')
	            </when>
	            <when test="searchn == 1">
	                p.activation = 1 AND p.company_name LIKE CONCAT('%',#{search},'%')
	            </when>
	            <when test="searchn == 2">
	                p.activation = 1 AND p.manager_id IN (SELECT id FROM account WHERE name LIKE CONCAT('%',#{search},'%'))
	            </when>
	            <when test="searchn == 3">
	                p.activation = 1 AND p.kan_code LIKE CONCAT('%',#{search},'%')
	            </when>
	        </choose>
	    </where>
	</select>
	
	
	<select id="productList" parameterType="map" resultMap="productResultMap">
	    SELECT
	    p.id, p.name, p.company_name, p.kan_code, p.vendor_id, p.registration_date,
		p.manager_id, p.activation,
		v.name as vendor_name, v.president_name,
		a.name as account_name,
		pc.cls_nm_1, pc.cls_nm_2, pc.cls_nm_3, pc.cls_nm_4
	
	    FROM product as p
		left join vendor as v on p.vendor_id = v.id
		left join account as a on p.manager_id = a.id
		left join product_category as pc on p.kan_code = pc.kan_code
	    <where>
	        <choose>
	            
	            <when test="searchn == 0">
	                p.activation = 1 AND p.name LIKE CONCAT('%',#{search},'%')
	            </when>
	            <when test="searchn == 1">
	                p.activation = 1 AND p.company_name LIKE CONCAT('%',#{search},'%')
	            </when>
	            <when test="searchn == 2">
	                p.activation = 1 AND p.kan_code IN (SELECT kan_code FROM product_category WHERE cls_nm_4 LIKE CONCAT('%',#{search},'%'))
	            </when>
	            <when test="searchn == 3">
	                p.activation = 1 AND p.manager_id IN (SELECT id FROM account WHERE name LIKE CONCAT('%',#{search},'%'))
	            </when>
	            
	        </choose>
	    </where>
	    ORDER BY p.registration_date LIMIT #{start} , #{perPage}
	</select>
	

</mapper>