<?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.WmsFinishgoodsMapper">
    
    <resultMap type="WmsFinishgoods" id="WmsFinishgoodsResult">
        <result property="finishgoodsId"    column="finishgoodsId"    />
        <result property="sourceinfoId"    column="sourceinfoId"    />
        <result property="goodsId"    column="goodsId"    />
        <result property="finishgoodsType"    column="finishgoodsType"    />
        <result property="finishgoodscode"    column="finishgoodscode"    />
        <result property="finishgoodPickno"    column="finishgoodPickno"    />
        <result property="finishgoodNo"    column="finishgoodNo"    />
        <result property="finishgoodCreateddate"    column="finishgoodCreateddate"    />
    </resultMap>

    <sql id="selectWmsFinishgoodsVo">
        select finishgoodsId, sourceinfoId, goodsId, finishgoodsType, finishgoodscode, finishgoodPickno, finishgoodNo, finishgoodCreateddate from WmsFinishgoods
    </sql>

    <select id="selectWmsFinishgoodsList" parameterType="WmsFinishgoods" resultMap="WmsFinishgoodsResult">
        <include refid="selectWmsFinishgoodsVo"/>
        <where>  
            <if test="sourceinfoId != null  and sourceinfoId != ''"> and sourceinfoId = #{sourceinfoId}</if>
            <if test="goodsId != null  and goodsId != ''"> and goodsId = #{goodsId}</if>
            <if test="finishgoodsType != null  and finishgoodsType != ''"> and finishgoodsType = #{finishgoodsType}</if>
            <if test="finishgoodscode != null  and finishgoodscode != ''"> and finishgoodscode = #{finishgoodscode}</if>
            <if test="finishgoodPickno != null  and finishgoodPickno != ''"> and finishgoodPickno = #{finishgoodPickno}</if>
            <if test="finishgoodNo != null  and finishgoodNo != ''"> and finishgoodNo = #{finishgoodNo}</if>
            <if test="finishgoodCreateddate != null  and finishgoodCreateddate != ''"> and finishgoodCreateddate = #{finishgoodCreateddate}</if>
        </where>
    </select>

    <select id="selectWmsFinishGoodsStockSumInfo" parameterType="WmsFinishgoodsReqVo" resultType="com.yunfeiyun.agmp.fms.domain.resvo.WmsFinishGoodsStockSumInfoResVo">
        SELECT
        fg.finishgoodscode,
        sum(s.stockAmount) as stockAmountSum,
        w.warehouseName,
        w.warehouseId,
        g.goodsSpec,
        g.goodsUnit,
        max(fg.finishgoodCreateddate) as latestStorageTime
        FROM
        WmsFinishgoods fg
        LEFT JOIN WmsGoods g ON g.goodsId = fg.goodsId
        LEFT JOIN WmsStock s ON s.goodsId = g.goodsId
        LEFT JOIN WmsWarehouse w on w.warehouseId = s.warehouseId
        <where>
            <if test="sourceinfoId != null  and sourceinfoId != ''"> and fg.sourceinfoId = #{sourceinfoId}</if>
            <if test="goodsId != null  and goodsId != ''"> and fg.goodsId = #{goodsId}</if>
            <if test="finishgoodsType != null  and finishgoodsType != ''"> and fg.finishgoodsType = #{finishgoodsType}</if>
            <if test="finishgoodscode != null  and finishgoodscode != ''"> and fg.finishgoodscode = #{finishgoodscode}</if>
            <if test="goodsName != null  and goodsName != ''"> and g.goodsName like concat('%', #{goodsName}, '%')</if>
            <if test="finishgoodPickno != null  and finishgoodPickno != ''"> and fg.finishgoodPickno = #{finishgoodPickno}</if>
            <if test="finishgoodNo != null  and finishgoodNo != ''"> and fg.finishgoodNo = #{finishgoodNo}</if>
            <if test="warehouseId != null  and warehouseId != ''"> and w.warehouseId = #{warehouseId}</if>
        </where>

        GROUP BY fg.finishgoodscode,s.warehouseId,g.goodsSpec,g.goodsUnit
    </select>

    <select id="selectWmsFinishgoodsByFinishgoodsId" parameterType="String" resultType="com.yunfeiyun.agmp.fms.domain.resvo.WmsFinishgoodsInfoResVo">
        SELECT * from WmsFinishgoods fg
        LEFT JOIN WmsGoods g on fg.goodsId = g.goodsId
        LEFT JOIN TssSourceinfo si on fg.sourceinfoId = si.sourceinfoId
        LEFT JOIN WmsStock s on s.goodsId = g.goodsId
        LEFT JOIN WmsWarehouse h on h.warehouseId = s.warehouseId
        LEFT JOIN MppTask t on t.stockId = s.stockId
        where finishgoodsId = #{finishgoodsId}
    </select>
    <select id="selectWmsFinishgoodsStockResVoByStockId" parameterType="String" resultType="com.yunfeiyun.agmp.fms.domain.resvo.WmsFinishgoodsStockResVo">
        SELECT
        fg.*,
        g.goodsName,
        g.goodsSpec,
        g.goodsUnit,
        s.stockAmount,
        w.warehouseName
        FROM
        WmsFinishgoods fg
        LEFT JOIN WmsGoods g ON fg.goodsId = g.goodsId
        LEFT JOIN WmsStock s on s.goodsId = g.goodsId
        LEFT JOIN WmsWarehouse w on w.warehouseId = s.warehouseId
        where s.stockId = #{stockId}
    </select>
        
    <insert id="insertWmsFinishgoods" parameterType="WmsFinishgoods">
        insert into WmsFinishgoods
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="finishgoodsId != null">finishgoodsId,</if>
            <if test="sourceinfoId != null">sourceinfoId,</if>
            <if test="goodsId != null">goodsId,</if>
            <if test="finishgoodsType != null">finishgoodsType,</if>
            <if test="finishgoodscode != null">finishgoodscode,</if>
            <if test="finishgoodPickno != null">finishgoodPickno,</if>
            <if test="finishgoodNo != null">finishgoodNo,</if>
            <if test="finishgoodCreateddate != null">finishgoodCreateddate,</if>
            <if test="landId != null">landId,</if>
            <if test="landName != null">landName,</if>
            <if test="finishgoodsExpense != null">finishgoodsExpense,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="finishgoodsId != null">#{finishgoodsId},</if>
            <if test="sourceinfoId != null">#{sourceinfoId},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="finishgoodsType != null">#{finishgoodsType},</if>
            <if test="finishgoodscode != null">#{finishgoodscode},</if>
            <if test="finishgoodPickno != null">#{finishgoodPickno},</if>
            <if test="finishgoodNo != null">#{finishgoodNo},</if>
            <if test="finishgoodCreateddate != null">#{finishgoodCreateddate},</if>
            <if test="landId != null">#{landId},</if>
            <if test="landName != null">#{landName},</if>
            <if test="finishgoodsExpense != null">#{finishgoodsExpense},</if>
         </trim>
    </insert>

    <update id="updateWmsFinishgoods" parameterType="WmsFinishgoods">
        update WmsFinishgoods
        <trim prefix="SET" suffixOverrides=",">
            <if test="sourceinfoId != null">sourceinfoId = #{sourceinfoId},</if>
            <if test="goodsId != null">goodsId = #{goodsId},</if>
            <if test="finishgoodsType != null">finishgoodsType = #{finishgoodsType},</if>
            <if test="finishgoodscode != null">finishgoodscode = #{finishgoodscode},</if>
            <if test="finishgoodPickno != null">finishgoodPickno = #{finishgoodPickno},</if>
            <if test="finishgoodNo != null">finishgoodNo = #{finishgoodNo},</if>
            <if test="finishgoodCreateddate != null">finishgoodCreateddate = #{finishgoodCreateddate},</if>
        </trim>
        where finishgoodsId = #{finishgoodsId}
    </update>

    <delete id="deleteWmsFinishgoodsByFinishgoodsId" parameterType="String">
        delete from WmsFinishgoods where finishgoodsId = #{finishgoodsId}
    </delete>

    <delete id="deleteWmsFinishgoodsByFinishgoodsIds" parameterType="String">
        delete from WmsFinishgoods where finishgoodsId in 
        <foreach item="finishgoodsId" collection="array" open="(" separator="," close=")">
            #{finishgoodsId}
        </foreach>
    </delete>
    <select id="selectWmsFinishgoodsSourceInfo" parameterType="String" resultType="com.yunfeiyun.agmp.fms.domain.resvo.WmsFinishgoodsSourceInfo">
        SELECT
        ws.stockId,
        wfg.finishgoodNo,
        wfg.finishgoodscode,
        tsi.sourceinfoName,
        wfg.finishgoodCreateddate,
        wtp.pickDate

        FROM
        WmsFinishgoods wfg
        LEFT JOIN WmsStock ws on  ws.goodsId	= wfg.goodsId
        LEFT JOIN WmsGoods wg ON ws.goodsId = wg.goodsId
        LEFT JOIN TssSourceinfo tsi on tsi.sourceinfoId = wfg.sourceinfoId
        LEFT JOIN FmsTaskpick wtp on wtp.pickNo = wfg.finishgoodPickno
        where
        ws.stockId = #{stockId}

    </select>
    <select id="selectCount" parameterType="String" resultType="int">
        select count(1) from WmsFinishgoods where sourceinfoId = #{sourceinfoId}
    </select>
</mapper>