<?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.WmsChangeMapper">

    <resultMap type="WmsChange" id="WmsChangeResult">
        <result property="changeId" column="changeId"/>
        <result property="changeapplyId" column="changeapplyId"/>
        <result property="stockId" column="stockId"/>
        <result property="changeBiztype" column="changeBiztype"/>
        <result property="changeWay" column="changeWay"/>
        <result property="changeNum" column="changeNum"/>
        <result property="changeOldnum" column="changeOldnum"/>
        <result property="changeNewnum" column="changeNewnum"/>
        <result property="changeAuditor" column="changeAuditor"/>
        <result property="changeAuditeddate" column="changeAuditeddate"/>
        <result property="changeRemark" column="changeRemark"/>
    </resultMap>

    <sql id="selectWmsChangeVo">
        select changeId, changeapplyId, stockId, changeBiztype, changeWay, changeNum, changeOldnum, changeNewnum,
        changeAuditor, changeAuditeddate, changeRemark from WmsChange
    </sql>

    <select id="selectWmsChangeList" parameterType="WmsChange" resultMap="WmsChangeResult">
        <include refid="selectWmsChangeVo"/>
        <where>
            <if test="changeapplyId != null  and changeapplyId != ''">and changeapplyId = #{changeapplyId}</if>
            <if test="stockId != null  and stockId != ''">and stockId = #{stockId}</if>
            <if test="changeBiztype != null  and changeBiztype != ''">and changeBiztype = #{changeBiztype}</if>
            <if test="changeWay != null  and changeWay != ''">and changeWay = #{changeWay}</if>
            <if test="changeNum != null ">and changeNum = #{changeNum}</if>
            <if test="changeOldnum != null ">and changeOldnum = #{changeOldnum}</if>
            <if test="changeNewnum != null ">and changeNewnum = #{changeNewnum}</if>
            <if test="changeAuditor != null  and changeAuditor != ''">and changeAuditor = #{changeAuditor}</if>
            <if test="changeAuditeddate != null  and changeAuditeddate != ''">and changeAuditeddate =
                #{changeAuditeddate}
            </if>
            <if test="changeRemark != null  and changeRemark != ''">and changeRemark = #{changeRemark}</if>
        </where>
    </select>

    <select id="selectWmsChangeListOfFinishGoods" parameterType="WmsChangeReqVo" resultType="WmsChangeResVo">
        select fg.finishgoodscode, fg.finishgoodsType, h.warehouseName, fg.finishgoodsType, c.changeBiztype, c.changeWay, c.changeNum,
                c.changeAuditor, c.changeAuditeddate, u.userName changeAuditorName
        FROM WmsChange c
        LEFT JOIN WmsStock s ON s.stockId = c.stockId
        LEFT JOIN WmsGoods g ON g.goodsId = s.goodsId
        LEFT JOIN WmsFinishgoods fg ON fg.goodsId = g.goodsId
        LEFT JOIN WmsWarehouse h ON h.warehouseId = s.warehouseId
        LEFT JOIN SysUser u ON u.userId = c.changeAuditor
        where fg.finishgoodsId is not null
        <if test="warehouseId != null  and warehouseId != ''">and h.warehouseId = #{warehouseId}</if>
        <if test="changeBiztype != null  and changeBiztype != ''">
            and c.changeBiztype = #{changeBiztype}
        </if>
        <if test="goodsName != null  and goodsName != ''">and g.goodsName like concat('%', #{goodsName}, '%')</if>
        <if test="finishgoodscode != null  and finishgoodscode != ''">and fg.finishgoodscode = #{finishgoodscode}</if>
        <if test="startTime != null  and startTime != ''">and c.changeAuditeddate >= #{startTime}</if>
        <if test="endTime != null  and endTime != ''">and c.changeAuditeddate <![CDATA[ <= ]]> #{endTime}</if>
    </select>

    <select id="selectWmsChangeByChangeId" parameterType="String" resultMap="WmsChangeResult">
        <include refid="selectWmsChangeVo"/>
        where changeId = #{changeId}
    </select>

    <insert id="insertWmsChange" parameterType="WmsChange">
        insert into WmsChange
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="changeId != null">changeId,</if>
            <if test="changeapplyId != null">changeapplyId,</if>
            <if test="stockId != null">stockId,</if>
            <if test="changeBiztype != null">changeBiztype,</if>
            <if test="changeWay != null">changeWay,</if>
            <if test="changeNum != null">changeNum,</if>
            <if test="changeOldnum != null">changeOldnum,</if>
            <if test="changeNewnum != null">changeNewnum,</if>
            <if test="changeAuditor != null">changeAuditor,</if>
            <if test="changeAuditeddate != null">changeAuditeddate,</if>
            <if test="changeRemark != null">changeRemark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="changeId != null">#{changeId},</if>
            <if test="changeapplyId != null">#{changeapplyId},</if>
            <if test="stockId != null">#{stockId},</if>
            <if test="changeBiztype != null">#{changeBiztype},</if>
            <if test="changeWay != null">#{changeWay},</if>
            <if test="changeNum != null">#{changeNum},</if>
            <if test="changeOldnum != null">#{changeOldnum},</if>
            <if test="changeNewnum != null">#{changeNewnum},</if>
            <if test="changeAuditor != null">#{changeAuditor},</if>
            <if test="changeAuditeddate != null">#{changeAuditeddate},</if>
            <if test="changeRemark != null">#{changeRemark},</if>
        </trim>
    </insert>
    <insert id="insertWmsChangeList" parameterType="WmsChange">
        <foreach collection="wmsChangeList" item="item" index="index">
            insert into WmsChange
            (
            changeId,
            changeapplyId,
            stockId,
            changeBiztype,
            changeWay,
            changeNum,
            changeOldnum,
            changeNewnum,
            changeAuditor,
            changeAuditeddate,
            changeRemark
            ) values
            (
            #{item.changeId},
            #{item.changeapplyId},
            #{item.stockId},
            #{item.changeBiztype},
            #{item.changeWay},
            #{item.changeNum},
            #{item.changeOldnum},
            #{item.changeNewnum},
            #{item.changeAuditor},
            #{item.changeAuditeddate},
            #{item.changeRemark}
            );
        </foreach>
    </insert>

    <update id="updateWmsChange" parameterType="WmsChange">
        update WmsChange
        <trim prefix="SET" suffixOverrides=",">
            <if test="changeapplyId != null">changeapplyId = #{changeapplyId},</if>
            <if test="stockId != null">stockId = #{stockId},</if>
            <if test="changeBiztype != null">changeBiztype = #{changeBiztype},</if>
            <if test="changeWay != null">changeWay = #{changeWay},</if>
            <if test="changeNum != null">changeNum = #{changeNum},</if>
            <if test="changeOldnum != null">changeOldnum = #{changeOldnum},</if>
            <if test="changeNewnum != null">changeNewnum = #{changeNewnum},</if>
            <if test="changeAuditor != null">changeAuditor = #{changeAuditor},</if>
            <if test="changeAuditeddate != null">changeAuditeddate = #{changeAuditeddate},</if>
            <if test="changeRemark != null">changeRemark = #{changeRemark},</if>
        </trim>
        where changeId = #{changeId}
    </update>

    <delete id="deleteWmsChangeByChangeId" parameterType="String">
        delete from WmsChange where changeId = #{changeId}
    </delete>

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