<?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="com.iwc.shop.modules.shop.dao.ShopProductAttributeDao">
    
	<sql id="columns">
		a.id AS "id",
		a.item_id AS "item.id",
		a.product_id AS "product.id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.del_flag AS "delFlag"
	</sql>

	<sql id="itemColumns">
		i.name AS "item.name",
		i.print_name AS "item.printName",
		i.sort AS "item.sort",
		i.del_flag AS "item.delFlag"
	</sql>
	
	<sql id="joins">
	    INNER JOIN shop_product_attribute_item i ON i.id = a.item_id
	</sql>

	<insert id="insert">
		INSERT INTO shop_product_attribute (
			id,
			item_id,
			product_id,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag)
		VALUES (
			#{id},
			#{item.id},
			#{product.id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag})
	</insert>

	<update id="update">
		UPDATE shop_product_attribute SET
			item_id = #{item.id},
			product_id = #{product.id},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			del_flag = #{delFlag}
		WHERE id = #{id}
	</update>

    <select id="get" resultType="ShopProductAttribute">
        SELECT
        	<include refid="columns"/>
			,<include refid="itemColumns"/>
        FROM shop_product_attribute a
			<include refid="joins"/>
        WHERE a.id = #{id}
    </select>

	<update id="delete">
		UPDATE shop_product_attribute SET
			del_flag = #{delFlag}
		WHERE id = #{id}
	</update>

	<delete id="deleteByProductId">
		DELETE FROM shop_product_attribute
		WHERE product_id = #{productId}
	</delete>
	
	<select id="findList" resultType="ShopProductAttribute">
		SELECT 
			<include refid="columns"/>
			,<include refid="itemColumns"/>
		FROM shop_product_attribute a
			<include refid="joins"/>
		<where>
			a.del_flag = #{delFlag}
			<if test="product != null and product.id != null and product.id != ''">
				AND a.product_id = #{product.id}
			</if>
		</where>
		<choose>
			<when test="page != null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY i.sort
			</otherwise>
		</choose>
	</select>

</mapper>