<?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.yunfeiyun.agmp.fms.mapper.WmsStockMapper">
    
    <resultMap type="WmsStock" id="WmsStockResult">
        <result property="stockId"    column="stockId"    />
        <result property="warehouseId"    column="warehouseId"    />
        <result property="goodsId"    column="goodsId"    />
        <result property="stockAmount"    column="stockAmount"    />
        <result property="stockRemark"    column="stockRemark"    />
    </resultMap>
    <resultMap id="WmsStockListResVo" type="WmsStockListResVo">
        <result property="goodsId"    column="goodsId"    />
        <result property="goodsName"    column="goodsName"    />
        <result property="goodsType"    column="goodsType"    />
        <result property="warehouseName"    column="warehouseName"    />
        <result property="stockAmount"    column="stockAmount"    />
        <result property="stockId"     column="stockId"/>
        <result property="goodsUnit"    column="goodsUnit"    />
        <result property="goodsSpec"    column="goodsSpec"    />
        <result property="supplierName" column="supplierName"/>
        <result property="goodsManufactor" column="goodsManufactor"/>
    </resultMap>

    <sql id="selectWmsStockVo">
        select stockId, warehouseId, goodsId,  stockAmount, stockRemark from WmsStock
    </sql>

    <select id="selectWmsStockList" parameterType="com.yunfeiyun.agmp.fms.domain.reqvo.WmsStockListReqVo" resultType="com.yunfeiyun.agmp.fms.domain.resvo.WmsStockListResVo">
        SELECT
        g.goodsId,
        g.goodsName,
        ag.assetgoodsType AS goodsType,
        w.warehouseName,
        w.warehouseId,
        s.stockAmount,
        s.stockId,
        g.goodsUnit,
        g.goodsSpec,
        ag.assetgoodsManufactor AS goodsManufactor,
        u.supplierName
        FROM
        WmsStock s
        LEFT JOIN WmsGoods g ON g.goodsId = s.goodsId
        LEFT JOIN WmsAssetgoods ag ON ag.goodsId = g.goodsId
        LEFT JOIN WmsWarehouse w ON w.warehouseId = s.warehouseId
        LEFT JOIN WmsSupplier u ON u.supplierId = ag.supplierId
        <where>
            <if test="goodsType != null and goodsType != ''">and ag.assetgoodsType =#{goodsType}</if>
            <if test="warehouseId != null  and warehouseId != ''"> and s.warehouseId = #{warehouseId}</if>
            <if test="goodsName != null and goodsName != ''">and g.goodsName like concat('%', #{goodsName}, '%')</if>
            <if test="warehouseType != null and warehouseType !=''">and w.warehouseType = #{warehouseType}</if>
            <if test="minStockAmount != null ">and stockAmount > #{minStockAmount}</if>
        </where>
    </select>


    <select id="selectWmsStockByStockId" parameterType="String" resultMap="WmsStockResult">
        <include refid="selectWmsStockVo"/>
        where stockId = #{stockId}
    </select>
    <select id="selectWmsStock" parameterType="com.yunfeiyun.agmp.fms.domain.WmsStock" resultMap="WmsStockResult">
        <include refid="selectWmsStockVo"/>
        <where>
            <if test="stockId !=null and stockId != ''">and stockId = #{stockId}</if>
            <if test="warehouseId !=null and warehouseId != ''">and warehouseId = #{warehouseId}</if>
            <if test="goodsId !=null and goodsId != ''">and goodsId = #{goodsId}</if>
        </where>
    </select>
    <select id="selectWmsStockByChangeApplyItemList" parameterType="WmsChangeapplyitem" resultMap="WmsStockResult">
        <include refid="selectWmsStockVo"/>
        where (goodsId,warehouseId) in
         <foreach collection="wmsChangeapplyitemList" item="item" open="(" separator="," close=")">
             (#{item.goodsId},#{item.warehouseId})
         </foreach>
    </select>
    <select id="slectWmsStockListByStockIds" parameterType="String" resultMap="WmsStockResult">
        <include refid="selectWmsStockVo"/>
        where stockId in
        <foreach item="stockId" collection="stockIds" open="(" separator="," close=")">
            #{stockId}
        </foreach>
    </select>
        
    <insert id="insertWmsStock" parameterType="WmsStock">
        insert into WmsStock
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stockId != null">stockId,</if>
            <if test="warehouseId != null">warehouseId,</if>
            <if test="goodsId != null">goodsId,</if>
            <if test="stockAmount != null">stockAmount,</if>
            <if test="stockRemark != null">stockRemark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stockId != null">#{stockId},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="stockAmount != null">#{stockAmount},</if>
            <if test="stockRemark != null">#{stockRemark},</if>
         </trim>
    </insert>

    <update id="updateWmsStock" parameterType="WmsStock">
        update WmsStock
        <trim prefix="SET" suffixOverrides=",">
            <if test="warehouseId != null">warehouseId = #{warehouseId},</if>
            <if test="goodsId != null">goodsId = #{goodsId},</if>
            <if test="stockAmount != null">stockAmount = #{stockAmount},</if>
            <if test="stockRemark != null">stockRemark = #{stockRemark},</if>
        </trim>
        where stockId = #{stockId}
    </update>
    <update id="updateWmsStockByBatch" parameterType="WmsStock">
        <foreach collection="wmsStockList" item="item" index="index">
        update WmsStock
        <trim prefix="SET" suffixOverrides=",">
            <if test="item.warehouseId != null">warehouseId = #{item.warehouseId},</if>
            <if test="item.goodsId != null">goodsId = #{item.goodsId},</if>
            <if test="item.stockAmount != null">stockAmount = #{item.stockAmount},</if>
            <if test="item.stockRemark != null">stockRemark = #{item.stockRemark},</if>
        </trim>
        where stockId = #{item.stockId};
        </foreach>
    </update>

    <delete id="deleteWmsStockByStockId" parameterType="String">
        delete from WmsStock where stockId = #{stockId}
    </delete>

    <delete id="deleteWmsStockByStockIds" parameterType="String">
        delete from WmsStock where stockId in 
        <foreach item="stockId" collection="array" open="(" separator="," close=")">
            #{stockId}
        </foreach>
    </delete>
</mapper>