<?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.xingxi.web.controller.master.merchant.mapper.MerchantProdVoMapper">
    <resultMap type="MerchantProdVo"    id="MerchantProdResult">
        <result property="mercProdId"   column="mercProdId"     />
        <result property="mercId"       column="mercId"         />
        <result property="prodId"       column="prodId"         />
        <result property="prodAttrId"   column="prodAttrId"     />
        <result property="shelfTime"    column="shelfTime"      />
        <result property="shelfFlag"    column="shelfFlag"      />
        <result property="delFlag"      column="delFlag"        />
        <result property="createUser"   column="createUser"     />
        <result property="createTime"   column="createTime"     />
        <result property="updateUser"   column="updateUser"     />
        <result property="updateTime"   column="updateTime"     />
        <result property="prodName"     column="prodName"       />
        <result property="attrName"     column="attrName"       />
    </resultMap>

    <sql id="selectMerchantProdVo">
        select m_merchant_prod.mercProdId
             , m_merchant_prod.mercId
             , m_merchant_prod.prodId
             , m_merchant_prod.prodAttrId
             , m_merchant_prod.shelfTime
             , m_merchant_prod.shelfFlag
             , m_prod.prodName
             , m_prod_attr.attrName
        from m_merchant_prod
        inner join m_prod on m_prod.id = m_merchant_prod.prodId
        inner join m_prod_attr on m_prod_attr.prodAttrId = m_merchant_prod.prodAttrId
    </sql>

    <select id="selectMerchantProdVoByMercId" parameterType="Long" resultMap="MerchantProdResult">
        <include refid="selectMerchantProdVo"/>
        <where>
            and m_merchant_prod.delFlag = 0
            and m_merchant_prod.mercId = #{mercId}
        </where>
    </select>

    <select id="selectMerchantProdVoList" parameterType="MerchantProdVo" resultMap="MerchantProdResult">
        <include refid="selectMerchantProdVo"/>
        <where>
            <if test="mercProdId != null "> and m_merchant_prod.mercProdId = #{mercProdId}</if>
            <if test="mercId != null "> and m_merchant_prod.mercId = #{mercId}</if>
            <if test="prodId != null "> and m_merchant_prod.prodId = #{prodId}</if>
            <if test="prodAttrId != null "> and m_merchant_prod.prodAttrId = #{prodAttrId}</if>
            <if test="shelfFlag != null "> and m_merchant_prod.shelfFlag = #{shelfFlag}</if>
            <if test="params.shelfTimeFrom != null and params.shelfTimeFrom != ''"><!-- 开始时间检索 -->
                and date_format(m_merchant_prod.shelfTime,'%y%m%d') &gt;= date_format(#{shelfTimeFrom},'%y%m%d')
            </if>
            <if test="params.shelfTimeTo != null and params.shelfTimeTo != ''"><!-- 开始时间检索 -->
                and date_format(m_merchant_prod.shelfTime,'%y%m%d') &gt;= date_format(#{shelfTimeTo},'%y%m%d')
            </if>
            <if test="delFlag != null and delFlag != ''"> and m_merchant_prod.delFlag = #{delFlag}</if>
            <if test="prodName != null and prodName != ''"> and m_prod.prodName like concat('%', #{prodName}, '%')</if>
            <if test="attrName != null  and attrName != ''"> and m_prod_attr.attrName like concat('%', #{attrName}, '%')</if>
        </where>
    </select>

    <insert id="batchInsertMerchantProd">
        insert into m_merchant_prod( mercId, prodId, prodAttrId, shelfTime, shelfFlag, delFlag, createUser, createTime, updateUser, updateTime) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.mercId}, (select prodId from m_prod_attr where prodAttrId = #{item.prodAttrId}), #{item.prodAttrId}, #{item.shelfTime}, #{item.shelfFlag}, #{item.delFlag}, #{item.createUser}, #{item.createTime}, #{item.updateUser}, #{item.updateTime})
        </foreach>
    </insert>
</mapper>