<?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.mq.server.product.mapper.ProdStockVoMapper">
    
    <resultMap type="ProdStockVo" id="ProdStockVoResult">
        <result property="stockId"    column="stockId"    />
        <result property="prodId"    column="prodId"    />
        <result property="pprId"    column="pprId"    />
        <result property="vendorId"    column="vendorId"    />
        <result property="stockQty"    column="stockQty"    />
        <result property="availQty"    column="availQty"    />
        <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="prodkindId"    column="prodkindId"    />
        <result property="prodkindName"    column="prodkindName"    />
        <result property="prodName"    column="prodName"    />
        <result property="pprName"    column="pprName"    />

    </resultMap>

    <sql id="selectProdStockVo">
        select
            p.stockId,
            p.prodId,
            p.pprId,
            p.vendorId,
            p.stockQty,
            p.availQty,
            p.delFlag,
            p.createUser,
            p.createTime,
            p.updateUser,
            p.updateTime,
            pp.prodName,
            mp.prodkindId,
            pk.prodkindName,
            ppr.pprName
        from t_prod_stock p
            inner join m_prod pp on p.prodId = pp.prodId
            inner join m_merc_prod mp on p.prodId = mp.prodId and p.vendorId = mp.mercId
            inner join m_prodkind pk on mp.prodkindId = pk.prodkindId
            inner join m_prod_prop_relation ppr on p.pprId = ppr.pprId
    </sql>

    <select id="selectProdStockVoList" parameterType="ProdStockVo" resultMap="ProdStockVoResult">
        <include refid="selectProdStockVo"/>
        <where>
            <if test="prodkindId != null "> and mp.prodkindId = #{prodkindId}</if>
            <if test="prodId != null "> and p.prodId = #{prodId}</if>
            <if test="prodName != null  and prodName != ''"> and pp.prodName like concat('%', #{prodName}, '%')</if>
            <if test="pprId != null "> and p.pprId = #{pprId}</if>
            <if test="vendorId != null "> and p.vendorId = #{vendorId}</if>
            <if test="stockQty != null "> and p.stockQty = #{stockQty}</if>
            <if test="availQty != null "> and p.availQty = #{availQty}</if>
            <if test="delFlag != null  and delFlag != ''"> and p.delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and p.createUser = #{createUser}</if>
            <if test="createTime != null "> and p.createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and p.updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and p.updateTime = #{updateTime}</if>
        </where>
    </select>

    <select id="selectProdStockVoByStockId" parameterType="Long" resultMap="ProdStockVoResult">
        <include refid="selectProdStockVo"/>
        where p.stockId = #{stockId}
    </select>

    <select id="selectProdStockVoByUnionId" parameterType="ProdStockVo" resultMap="ProdStockVoResult">
        select stockId, prodId, pprId, vendorId, stockQty, availQty, delFlag, createUser, createTime, updateUser, updateTime
        from t_prod_stock
        where prodId = #{prodId} and pprId = #{pprId} and vendorId = #{vendorId}
        limit 1
    </select>

    <update id="updateProdStockQty" parameterType="ProdStockVo">
        update t_prod_stock
        <trim prefix="SET" suffixOverrides=",">
            <if test='billType != null and billType == "1"  and stockQty != null'>
                stockQty = ifnull(stockQty, 0) + #{stockQty},
            </if>
            <if test='billType != null and billType == "0" and stockQty != null'>
                stockQty = ifnull(stockQty, 0) - #{stockQty},
            </if>
            <if test='billType != null and billType == "1" and availQty != null'>
                availQty = ifnull(availQty, 0) + #{availQty},
            </if>
            <if test='billType != null and billType == "0" and availQty != null'>
                availQty = ifnull(availQty, 0) - #{availQty},
            </if>
            <if test="updateUser != null and updateUser != ''  ">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where stockId = #{stockId}
            <if test='billType != null and billType == "0" and stockQty != null'>
                and stockQty - #{stockQty} >= 0
            </if>
            <if test='billType != null and billType == "0" and availQty != null'>
                and availQty - #{availQty} >= 0
            </if>
    </update>

    <update id="updateProdStockQtyByUnionId" parameterType="ProdStockVo">
        update t_prod_stock
        <trim prefix="SET" suffixOverrides=",">
            <if test='billType != null and billType == "1"  and stockQty != null'>
                stockQty = ifnull(stockQty, 0) + #{stockQty},
            </if>
            <if test='billType != null and billType == "0" and stockQty != null'>
                stockQty = ifnull(stockQty, 0) - #{stockQty},
            </if>
            <if test='billType != null and billType == "1" and availQty != null'>
                availQty = ifnull(availQty, 0) + #{availQty},
            </if>
            <if test='billType != null and billType == "0" and availQty != null'>
                availQty = ifnull(availQty, 0) - #{availQty},
            </if>
            <if test="updateUser != null and updateUser != ''  ">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where prodId = #{prodId} and pprId = #{pprId} and vendorId = #{vendorId}
            <if test='billType != null and billType == "0" and stockQty != null'>
                and stockQty - #{stockQty} >= 0
            </if>
            <if test='billType != null and billType == "0" and availQty != null'>
                and availQty - #{availQty} >= 0
            </if>
    </update>

</mapper>