<?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.business.ProdInventory.mapper.ProdInventoryVoMapper">
    <resultMap type="ProdInventoryVo"    id="ProdInventoryVoResult">
        <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="selectProdInventoryVo">
        select t_prod_inventory.prodInventoryId
             , m_merchant_prod.prodId
             , m_merchant_prod.prodAttrId
             , m_merchant_prod.mercId
             , ifnull(t_prod_inventory.quantity, 0) as quantity
             , ifnull(t_prod_inventory.availQty, 0) as availQty
             , m_prod.prodName
             , m_prod_attr.attrName
        from m_merchant
        inner join m_merchant_prod on m_merchant_prod.mercId = m_merchant.mercId
        inner join m_prod on m_prod.id = m_merchant_prod.prodId
        inner join m_prod_attr on m_prod_attr.prodId = m_merchant_prod.prodId and m_prod_attr.prodAttrId = m_merchant_prod.prodAttrId
        left join t_prod_inventory on t_prod_inventory.mercId = m_merchant.mercId and t_prod_inventory.prodId = m_merchant_prod.prodId and t_prod_inventory.prodAttrId = m_merchant_prod.prodAttrId
    </sql>

    <select id="selectProdInventoryVoByMercId" parameterType="Long" resultMap="ProdInventoryVoResult">
        <include refid="selectProdInventoryVo"/>
        <where>
            and t_prod_inventory.delFlag = 0
            and t_prod_inventory.mercId = #{mercId}
        </where>
    </select>

    <select id="selectProdInventoryVoByProdInventoryId" parameterType="Long" resultMap="ProdInventoryVoResult">
        <include refid="selectProdInventoryVo"/>
        <where>
            and t_prod_inventory.delFlag = 0
            and t_prod_inventory.prodInventoryId = #{prodInventoryId}
        </where>
    </select>

    <select id="selectProdInventoryVoList" parameterType="ProdInventoryVo" resultMap="ProdInventoryVoResult">
        <include refid="selectProdInventoryVo"/>
        <where>
            <if test="prodInventoryId != null "> and t_prod_inventory.prodInventoryId = #{prodInventoryId}</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="mercId != null "> and m_merchant_prod.mercId = #{mercId}</if>
            <if test="delFlag != null and delFlag != ''"> and t_prod_inventory.delFlag = #{delFlag}</if>
            <if test="mercName != null and mercName != ''"> and m_merchant.mercName like concat('%', #{mercName}, '%')</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>

    <update id="updateProdInventoryVo" parameterType="ProdInventoryVo">
        update t_prod_inventory
        <trim prefix="SET" suffixOverrides=",">
            <if test="prodId != null">prodId = #{prodId},</if>
            <if test="prodAttrId != null">prodAttrId = #{prodAttrId},</if>
            <if test="mercId != null">mercId = #{mercId},</if>
            <if test="ioFlag == 'stockin'">
                <if test="addInvQty != null">quantity = quantity + #{addInvQty},</if>
                <if test="addInvQty != null">availQty = availQty + #{addInvQty},</if>
            </if>
            <if test="ioFlag == 'stockout'">
                <if test="addInvQty != null">quantity = quantity - #{addInvQty},</if>
                <if test="addInvQty != null">availQty = availQty - #{addInvQty},</if>
            </if>
            <if test="updateUser != null">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where prodInventoryId = #{prodInventoryId}
    </update>
</mapper>