<?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.fjhx.applet.mapper.MaterialReceiveMapper">

    <select id="selectWaterDetail" resultType="java.util.Map">
        select swd.ID              id,
               swd.StockChangeType stockChangeType,
               aa.AccountNo        account,
               swd.WaterId         waterId,
               aa.ID               userId
        from stock_waterdetial swd
                 left join acc_account aa on swd.OperUserId = aa.ID
        where swd.InOutDevice = 1
          and swd.ApproveBillState = 0
          and swd.StockChangeType in (29, 30)
    </select>

    <update id="updateReceive">
        update stock_waterdetial
        <set>
            <if test="ApproveBillState neq null">
                ApproveBillState = #{ApproveBillState}
            </if>
            <if test="StockChangeType neq null">
                ,StockChangeType = #{StockChangeType}
            </if>
            <if test="PickingStatus neq null">
                ,PickingStatus = #{PickingStatus}
            </if>
            <if test="FlowLinkId neq null">
                ,FlowLinkId = #{FlowLinkId}
            </if>
        </set>
        where id in
        <foreach item="item" collection="idList" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <update id="updatePickingStatus">
        update stock_waterdetial
        <set>
            ApproveBillState = #{checkState}
            <if test="ApproveBillState eq '2' ">
                ,PickingStatus = '2'
            </if>
        </set>
        where flowLinkId = #{flowLinkId}
    </update>

    <select id="getList" resultType="java.util.Map">
        select swd.ID          id,
               swd.ChangeNum   changeName,
               swd.CreatedTime createTime,
               m.Name          materialName,
               m.Code          materialCode,
               uu.RealName     realName
        from stock_waterdetial swd
                 left join material m on swd.MaterialCode = m.code
                 left join u_user uu on swd.OperUserId = uu.ID
                 left join acc_account aa on swd.OperUserId = aa.ID
        where swd.PickingStatus = 2
          and aa.AccountNo = #{account}
    </select>

    <select id="countNum" resultType="java.lang.Integer">
        select count(0)
        from stock_waterdetial swd
                 inner join acc_account aa on swd.OperUserId = aa.ID
        where swd.PickingStatus = 2
          and aa.AccountNo = #{account}
    </select>

    <select id="flowDetails" resultType="java.util.Map">
        select m.Name          materialName,
               m.Code          materialCode,
               swd.CreatedTime createTime,
               swd.ChangeNum   changeNum,
               sw.Remark       remark,
               uu.RealName     realName
        from stock_waterdetial_copy1 swd
                 left join material m on swd.MaterialCode = m.code
                 left join stock_water sw on swd.WaterId = sw.ID
                 left join u_user uu on swd.OperUserId = uu.ID
        where swd.FlowLinkId = #{linkId}
    </select>

    <insert id="insertFlowApplyCheck">
        insert into flow_applycheck(id, IsDelete, CreatedTime, UpdatedTime, FlowId, NodeId, PreNodeId, NextNodeId,
                                    CheckUserId, CheckState, Suggestions, LinkId, ApprovalItem)
        values (#{id},
                0,
                now(),
                now(),
                #{flowId},
                #{nodeId},
                #{preNodeId},
                #{nextNodeId},
                #{checkUserId},
                #{checkState},
                #{suggestions},
                #{linkId},
                #{approvalItem})
    </insert>

</mapper>
