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

    <!-- 新增单据主表 -->
    <insert id="insertRkBill" parameterType="com.zg.project.wisdom.domain.RkBill">
        INSERT INTO rk_bill (
            rk_type, wl_type, cangku, rk_time, lihuo_y,
            bill_no, is_chuku, is_delete,
            created_by, created_at
        ) VALUES (
                     #{rkType}, #{wlType}, #{cangku}, #{rkTime}, #{lihuoY},
                     #{billNo}, #{isChuku}, #{isDelete},
                     #{createBy}, #{createTime}
                 )
    </insert>

    <!-- 查询列表（可用于分页或展示） -->
    <select id="selectRkBillList"
            parameterType="com.zg.project.wisdom.domain.RkBill"
            resultType="com.zg.project.wisdom.domain.RkBill">
        SELECT
        rb.id,
        rb.rk_type         AS rkType,
        st.type_name       AS rkTypeName,
        rb.wl_type         AS wlType,
        mt.type_name       AS wlTypeName,
        rb.cangku          AS cangku,
        wh.warehouse_name  AS cangkuName,
        rb.lihuo_y         AS lihuoY,
        rb.rk_time         AS rkTime,
        rb.bill_no         AS billNo,
        rb.is_chuku        AS isChuku,
        rb.is_delete       AS isDelete,
        rb.created_by      AS createBy,
        rb.created_at      AS createTime,
        rb.updated_by      AS updateBy,
        rb.updated_at      AS updateTime
        FROM rk_bill rb
        LEFT JOIN stock_in_type st   ON rb.rk_type = st.type_code
        LEFT JOIN material_type mt   ON rb.wl_type = mt.type_code
        LEFT JOIN warehouse_info wh  ON rb.cangku = wh.warehouse_code
        WHERE rb.is_delete = '0'
        <if test="billNo != null and billNo != ''">
            AND rb.bill_no LIKE CONCAT('%', #{billNo}, '%')
        </if>
        ORDER BY rb.rk_time DESC
    </select>

    <!-- 根据单据号查询单条 -->
    <select id="selectRkBillByBillNo" resultType="com.zg.project.wisdom.domain.RkBill">
        SELECT
            id, rk_type, wl_type, cangku, rk_time, lihuo_y,
            bill_no, is_chuku, is_delete,
            created_by AS createBy, created_at AS createTime,
            updated_by AS updateBy, updated_at AS updateTime,
            remark
        FROM rk_bill
        WHERE bill_no = #{billNo}
            LIMIT 1
    </select>

    <!-- 查询详情 -->
    <select id="selectRkBillById" resultType="com.zg.project.wisdom.domain.RkBill">
        SELECT * FROM rk_bill WHERE id = #{id} AND is_delete = '0'
    </select>

    <!-- 修改 -->
    <update id="updateRkBill" parameterType="com.zg.project.wisdom.domain.RkBill">
        UPDATE rk_bill
        SET rk_type = #{rkType},
            wl_type = #{wlType},
            cangku = #{cangku},
            rk_time = #{rkTime},
            lihuo_y = #{lihuoY},
            bill_no = #{billNo},
            is_chuku = #{isChuku},
            remark = #{remark},
            updated_by = #{updateBy},
            updated_at = #{updateTime}
        WHERE id = #{id}
    </update>

    <!-- 批量逻辑删除 -->
    <update id="logicDeleteRkBillByIds">
        UPDATE rk_bill
        SET is_delete = '1'
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateOutStock" parameterType="list">
        UPDATE rk_bill
        SET is_chuku = '1'
        WHERE id IN
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
