<?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.xmomen.module.product.mapper.ProductMapper" >
	<select id="getProductList" resultType="com.xmomen.module.product.model.ProductModel" 
		parameterType="com.xmomen.module.product.model.ProductQuery">
        SELECT
            item.ID,
            item.ITEM_CODE,
            ifnull(s.stock_num,0) as stockNum,
            ifnull(o.sell_num,0) as sellNum,
            item.CD_CATEGORY_ID as categoryId,
            category.CATEGORY_NAME as categoryName,
            item.ITEM_NAME,
            item.ITEM_DESCRIBE,
            item.YIELDLY,
            item.SPEC,
            item.BASE_PRICE,
            item.MEMBER_PRICE,
            item.PRICING_MANNER,
            item.SELL_STATUS,
            item.SELL_UNIT,
            item.SELL_PRICE,
            item.BAO_ZHI_QI,
            item.SHI_YI_REN_QUN,
            item.XIAN_SHI_QIANG_GOU,
            item.XIN_PIN_CHANG_XIAN,
            item.RE_MAI_TUI_JIAN,
            cr.PATH as picUrl
        FROM
          cd_item item
        INNER JOIN
          cd_category category
        ON
          item.CD_CATEGORY_ID = category.id
        left join cd_stock s
        on s.item_id = item.id
        left join (
        select count(1) as sell_num,item_id from tb_order_item oi left join tb_order o on o.ORDER_NO = oi.ORDER_NO where o.ORDER_STATUS != 9 group by oi.ITEM_CODE
        ) o on o.item_id = item.id
        LEFT JOIN
          cd_resource cr
        ON
          item.id = cr.entity_id and cr.entity_type = 'cd_item'
          AND cr.resource_type = 'PICTURE' AND cr.is_default = 1
        <where>
        	item.SELL_STATUS = 1
            <if test="categoryId">
                  AND item.CD_CATEGORY_ID IN (
                  	SELECT id from cd_category where cd_category.id = #{categoryId} or cd_category.parent_id = #{categoryId}) 
            </if>
            <if test="keyword">
                  AND (item.ITEM_NAME LIKE CONCAT('%', #{keyword}, '%'))
            </if>
            <if test="filterLabels">
                  AND 
                <foreach collection="filterLabels" item="label" index="index" open="(" separator=" OR " close=")">
                   item.${label} = 1
                </foreach>
            </if>
            <if test="productIds">
                AND item.ID IN
                <foreach collection="productIds" item="item" index="index" open="(" separator="," close=")">
                    ${item}
                </foreach>
            </if>
        </where>
        <if test="orderField">
            ORDER BY item.${orderField} <choose><when test="isAsc">ASC</when><otherwise>DESC</otherwise></choose>
        </if>
    </select>
    <select id="getProductDetail" resultType="com.xmomen.module.product.model.ProductModel" 
		parameterType="java.lang.Integer">
        SELECT
            item.ID,
            item.ITEM_CODE,
            ifnull(s.stock_num,0) as stockNum,
            ifnull(o.sell_num,0) as sellNum,
            item.CD_CATEGORY_ID as categoryId,
            category.CATEGORY_NAME as categoryName,
            item.ITEM_NAME,
            item.ITEM_DESCRIBE,
            item.YIELDLY,
            item.SPEC,
            item.BASE_PRICE,
            item.MEMBER_PRICE,
            item.PRICING_MANNER,
            item.SELL_STATUS,
            item.SELL_UNIT,
            item.SELL_PRICE,
            item.BAO_ZHI_QI,
            item.SHI_YI_REN_QUN,
            item.XIAN_SHI_QIANG_GOU,
            item.XIN_PIN_CHANG_XIAN,
            item.RE_MAI_TUI_JIAN,
            detail.ITEM_DETAIL_CONTENT as detailContent,
            cr.PATH as picUrl,
            cr.IS_DEFAULT as isDefaultPath
        FROM
          cd_item item
        INNER JOIN
          cd_category category
        ON
          item.CD_CATEGORY_ID = category.id
          left join cd_stock s
        on s.item_id = item.id
        left join (
          select count(1) as sell_num,item_id from tb_order_item oi left join tb_order o on o.ORDER_NO = oi.ORDER_NO where o.ORDER_STATUS != 9 group by oi.ITEM_CODE
        ) o on o.item_id = item.id
        LEFT JOIN
          cd_item_detail detail
        ON
          item.ID = detail.CD_ITEM_ID
        LEFT JOIN
          cd_resource cr
        ON
          item.id = cr.entity_id and cr.entity_type = 'cd_item'
          AND cr.resource_type = 'PICTURE'
        WHERE item.iD = #{id}
    </select>
    
    <select id="getProductsByIds" resultType="com.xmomen.module.product.model.ProductModel" 
		parameterType="com.xmomen.module.product.model.ProductQuery">
    	SELECT
            item.ID,
            item.ITEM_CODE,
            item.CD_CATEGORY_ID as categoryId,
            category.CATEGORY_NAME as categoryName,
            item.ITEM_NAME,
            item.ITEM_DESCRIBE,
            item.YIELDLY,
            item.SPEC,
            item.BASE_PRICE,
            item.MEMBER_PRICE,
            item.PRICING_MANNER,
            item.SELL_STATUS,
            item.SELL_UNIT,
            item.SELL_PRICE,
            item.BAO_ZHI_QI,
            item.SHI_YI_REN_QUN,
            item.XIAN_SHI_QIANG_GOU,
            item.XIN_PIN_CHANG_XIAN,
            item.RE_MAI_TUI_JIAN,
            cr.PATH as picUrl
        FROM
          cd_item item
        INNER JOIN
          cd_category category
        ON
          item.CD_CATEGORY_ID = category.id
        LEFT JOIN
          cd_resource cr
        ON
          item.id = cr.entity_id and cr.entity_type = 'cd_item'
          AND cr.resource_type = 'PICTURE' AND cr.is_default = 1
        <where>
        	item.ID in 
        	<foreach collection="productIds" item="productId" index="index" open="(" separator=" , " close=")">
                   #{productId}
            </foreach>
        </where>
    </select>
</mapper>