<?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.zg.project.wisdom.mapper.RkInfoMapper">
    
    <resultMap type="RkInfo" id="RkInfoResult">
        <result property="id"    column="id"    />
        <result property="rkType"    column="rk_type"    />
        <result property="wlType"    column="wl_type"    />
        <result property="cangku"    column="cangku"    />
        <result property="rkTime"    column="rk_time"    />
        <result property="lihuoY"    column="lihuo_y"    />
        <result property="isChuku"    column="is_chuku"    />
        <result property="billNo" column="bill_no"/>
        <result property="rkTypeName" column="rk_type_name"/>
        <result property="wlTypeName" column="wl_type_name"/>
        <result property="cangkuName" column="cangku_name"/>
        <result property="remark"    column="remark"    />
        <result property="xj"    column="xj"    />
        <result property="xmNo"    column="xm_no"    />
        <result property="xmMs"    column="xm_ms"    />
        <result property="wlNo"    column="wl_no"    />
        <result property="wlMs"    column="wl_ms"    />
        <result property="gysNo"    column="gys_no"    />
        <result property="gysMc"    column="gys_mc"    />
        <result property="jhAmt"    column="jh_amt"    />
        <result property="htDj"    column="ht_dj"    />
        <result property="sapNo"    column="sap_no"    />
        <result property="xh"    column="xh"    />
        <result property="jhQty"    column="jh_qty"    />
        <result property="htQty"    column="ht_qty"    />
        <result property="dw"    column="dw"    />
        <result property="realQty"    column="real_qty"    />
        <result property="pcode"    column="pcode"    />
        <result property="pcodeId" column="pcode_id"/>
        <result property="trayCode"    column="tray_code"    />
        <result property="entityId"    column="entity_id"    />
        <result property="ckLihuoY" column="ck_lihuo_y"/>
        <result property="ckType" column="ck_type"/>
        <result property="ckTypeName" column="ck_type_name"/>
        <result property="teamCode" column="team_code"/>
        <result property="teamName" column="team_name"/>
        <result property="lyTime" column="ly_time"/>
        <result property="ckRemark" column="ck_remark"/>
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDelete"    column="is_delete"    />
    </resultMap>

    <sql id="selectRkInfoVo">
        SELECT
            ri.id,
            ri.bill_no,
            ri.rk_type, st.type_name AS rk_type_name,
            ri.wl_type, mt.type_name AS wl_type_name,
            ri.cangku, wh.warehouse_name AS cangku_name,
            ri.rk_time, ri.lihuo_y, ri.is_chuku, ri.remark,
            ri.ck_lihuo_y, ri.ck_type, sot.ck_type_name,
            ri.team_code, ct.team_name,
            ri.ck_remark,
            ri.ly_time,
            ri.xj, ri.xm_no, ri.xm_ms, ri.wl_no, ri.wl_ms,
            ri.gys_no, ri.gys_mc, ri.jh_amt, ri.ht_dj, ri.sap_no, ri.xh,
            ri.jh_qty, ri.ht_qty, ri.dw, ri.real_qty,
            ri.pcode, ri.pcode_id, ri.tray_code, ri.entity_id,
            ri.create_by, ri.create_time, ri.update_by, ri.update_time, ri.is_delete
        FROM rk_info ri
                 LEFT JOIN stock_in_type st ON ri.rk_type = st.type_code
                 LEFT JOIN material_type mt ON ri.wl_type = mt.type_code
                 LEFT JOIN warehouse_info wh ON ri.cangku = wh.warehouse_code
                 LEFT JOIN stock_out_type sot ON ri.ck_type = sot.ck_type_code
                 LEFT JOIN construction_team ct ON ri.team_code = ct.team_code
    </sql>

    <insert id="batchInsertRkInfo" parameterType="java.util.List">
        insert into rk_info (
        bill_no,
        rk_type, wl_type, cangku, lihuo_y, rk_time,
        wl_no, wl_ms, xm_no, xm_ms, xj, sap_no, gys_no, gys_mc,
        jh_qty, ht_qty, jh_amt, ht_dj, dw,
        pcode, pcode_id, tray_code, real_qty, entity_id,
        remark, is_chuku, is_delete, create_by, create_time
        )
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.billNo},
            #{item.rkType}, #{item.wlType}, #{item.cangku}, #{item.lihuoY}, #{item.rkTime},
            #{item.wlNo}, #{item.wlMs}, #{item.xmNo}, #{item.xmMs}, #{item.xj}, #{item.sapNo}, #{item.gysNo}, #{item.gysMc},
            #{item.jhQty}, #{item.htQty}, #{item.jhAmt}, #{item.htDj}, #{item.dw},
            #{item.pcode}, #{item.pcodeId}, #{item.trayCode}, #{item.realQty}, #{item.entityId},
            #{item.remark}, #{item.isChuku}, #{item.isDelete}, #{item.createBy}, #{item.createTime}
            )
        </foreach>
    </insert>

    <delete id="deleteByBillNo">
        DELETE FROM rk_info WHERE bill_no = #{billNo}
    </delete>

    <select id="selectRkInfoList" parameterType="RkInfo" resultMap="RkInfoResult">
        <include refid="selectRkInfoVo"/>
        <where>

            <if test="isChuku != null and isChuku != ''">
                and is_chuku = #{isChuku}
            </if>

            <if test="keyword != null and keyword != ''">
                and (
                xm_no like concat('%', #{keyword}, '%')
                or xm_ms like concat('%', #{keyword}, '%')
                or wl_no like concat('%', #{keyword}, '%')
                or wl_ms like concat('%', #{keyword}, '%')
                or gys_no like concat('%', #{keyword}, '%')
                or gys_mc like concat('%', #{keyword}, '%')
                or sap_no like concat('%', #{keyword}, '%')
                or bill_no like concat('%', #{keyword}, '%')
                )
            </if>

            <if test="rkType != null  and rkType != ''"> and rk_type = #{rkType}</if>
            <if test="wlType != null  and wlType != ''"> and wl_type = #{wlType}</if>
            <if test="cangku != null  and cangku != ''"> and cangku = #{cangku}</if>
            <if test="rkTime != null "> and rk_time = #{rkTime}</if>
            <if test="lihuoY != null  and lihuoY != ''"> and lihuo_y = #{lihuoY}</if>

            <if test="xj != null  and xj != ''"> and xj = #{xj}</if>
            <if test="billNo != null and billNo != ''">
                and bill_no = #{billNo}
            </if>
            <if test="xmNo != null  and xmNo != ''"> and xm_no = #{xmNo}</if>
            <if test="xmMs != null  and xmMs != ''"> and xm_ms = #{xmMs}</if>
            <if test="wlNo != null  and wlNo != ''"> and wl_no = #{wlNo}</if>
            <if test="wlMs != null  and wlMs != ''"> and wl_ms = #{wlMs}</if>
            <if test="gysNo != null  and gysNo != ''"> and gys_no = #{gysNo}</if>
            <if test="gysMc != null  and gysMc != ''"> and gys_mc = #{gysMc}</if>
            <if test="jhAmt != null "> and jh_amt = #{jhAmt}</if>
            <if test="htDj != null "> and ht_dj = #{htDj}</if>
            <if test="sapNo != null  and sapNo != ''"> and sap_no = #{sapNo}</if>
            <if test="xh != null  and xh != ''"> and xh = #{xh}</if>
            <if test="jhQty != null "> and jh_qty = #{jhQty}</if>
            <if test="htQty != null "> and ht_qty = #{htQty}</if>
            <if test="dw != null  and dw != ''"> and dw = #{dw}</if>
            <if test="realQty != null "> and real_qty = #{realQty}</if>
            <if test="pcode != null  and pcode != ''"> and pcode = #{pcode}</if>
            <if test="lyTime != null"> and ri.ly_time = #{lyTime}</if>
            <if test="trayCode != null  and trayCode != ''"> and tray_code = #{trayCode}</if>
            <if test="entityId != null  and entityId != ''"> and entity_id = #{entityId}</if>
            <if test="isDelete != null and isDelete != ''">
                and ri.is_delete = #{isDelete}
            </if>
            <if test="isDelete == null or isDelete == ''">
                and ri.is_delete = 0
            </if>
        </where>
    </select>

    <select id="selectRkInfoById" parameterType="Long" resultMap="RkInfoResult">
        <include refid="selectRkInfoVo"/>
        where ri.id = #{id} and ri.is_delete = 0
    </select>

    <select id="countRkInfoByLocationCode" resultType="int">
        SELECT COUNT(1)
        FROM rk_info ri
        WHERE ri.pcode = #{locationCode} AND ri.is_delete = 0
    </select>

    <select id="selectUsedPcodes" resultType="java.lang.String">
        SELECT DISTINCT pcode
        FROM rk_info ri
        WHERE ri.is_delete = '0' AND ri.is_chuku != '1'
    </select>

    <select id="selectSapNoByBillNo" resultType="java.lang.String">
        SELECT DISTINCT sap_no FROM rk_info
        WHERE bill_no = #{billNo} AND sap_no IS NOT NULL
    </select>

    <select id="countOverdueStock" resultType="int">
        SELECT COUNT(*) FROM rk_info
        WHERE ri.is_delete = 0 AND ri.is_chuku = 0
    <![CDATA[
          AND DATE(rk_time) <= DATE_SUB(CURDATE(), INTERVAL 20 DAY)
        ]]>
    </select>

    <select id="selectTopOverdueStock" resultMap="RkInfoResult">
        SELECT * FROM rk_info
        WHERE ri.is_delete = 0 AND ri.is_chuku = 0
    <![CDATA[
          AND DATE(rk_time) <= DATE_SUB(CURDATE(), INTERVAL 20 DAY)
    ]]>
    ORDER BY rk_time DESC
            LIMIT #{limit}
    </select>

    <update id="updateRkInfo" parameterType="RkInfo">
        update rk_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="rkType != null">rk_type = #{rkType},</if>
            <if test="wlType != null">wl_type = #{wlType},</if>
            <if test="cangku != null">cangku = #{cangku},</if>
            <if test="rkTime != null">rk_time = #{rkTime},</if>
            <if test="lihuoY != null">lihuo_y = #{lihuoY},</if>
            <if test="isChuku != null">is_chuku = #{isChuku},</if>
            <if test="billNo != null">bill_no = #{billNo},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="xj != null">xj = #{xj},</if>
            <if test="xmNo != null">xm_no = #{xmNo},</if>
            <if test="xmMs != null">xm_ms = #{xmMs},</if>
            <if test="wlNo != null">wl_no = #{wlNo},</if>
            <if test="wlMs != null">wl_ms = #{wlMs},</if>
            <if test="gysNo != null">gys_no = #{gysNo},</if>
            <if test="gysMc != null">gys_mc = #{gysMc},</if>
            <if test="jhAmt != null">jh_amt = #{jhAmt},</if>
            <if test="htDj != null">ht_dj = #{htDj},</if>
            <if test="sapNo != null">sap_no = #{sapNo},</if>
            <if test="xh != null">xh = #{xh},</if>
            <if test="jhQty != null">jh_qty = #{jhQty},</if>
            <if test="htQty != null">ht_qty = #{htQty},</if>
            <if test="dw != null">dw = #{dw},</if>
            <if test="realQty != null">real_qty = #{realQty},</if>
            <if test="pcode != null">pcode = #{pcode},</if>
            <if test="trayCode != null">tray_code = #{trayCode},</if>
            <if test="entityId != null">entity_id = #{entityId},</if>
            <if test="ckLihuoY != null">ck_lihuo_y = #{ckLihuoY},</if>
            <if test="ckType != null">ck_type = #{ckType},</if>
            <if test="ckTypeName != null">ck_type_name = #{ckTypeName},</if>
            <if test="sgdCode != null">sgd_code = #{sgdCode},</if>
            <if test="sgdName != null">sgd_name = #{sgdName},</if>
            <if test="lyTime != null">ly_time = #{lyTime},</if>
            <if test="ckRemark != null">ck_remark = #{ckRemark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteRkInfoById" parameterType="Long">
        update rk_info
        set is_delete = 1
        where id = #{id}
    </update>

    <update id="deleteRkInfoByIds" parameterType="String">
        update rk_info
        set ri.is_delete = 1
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="batchUpdateOutStock" parameterType="java.util.List">
        <foreach collection="list" item="item" separator=";">
            UPDATE rk_info
            SET
            is_chuku = 1,
            ck_time = #{item.ckTime},
            ck_type = #{item.ckType},
            team_code = #{item.teamCode},
            ck_lihuo_y = #{item.ckLihuoY},
            update_by = #{item.updateBy},
            update_time = #{item.updateTime}
            WHERE id = #{item.id} AND is_delete = 0
        </foreach>
    </update>

</mapper>