<?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.ruoyi.web.stock.mapper.WarehouseRelatedFeeMapper">

    <resultMap id="FeeResult" type="com.ruoyi.web.stock.domain.WarehouseRelatedFee">
        <id property="id" column="id"/>
        <result property="bizDate" column="biz_date"/>
        <result property="sheetNo" column="sheet_no"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap id="FeeItemResult" type="com.ruoyi.web.stock.domain.WarehouseRelatedFeeItem">
        <id property="id" column="id"/>
        <result property="feeId" column="fee_id"/>
        <result property="sheetNo" column="sheet_no"/>
        <result property="lineNo" column="line_no"/>
        <result property="subjectLevel1" column="subject_level1"/>
        <result property="subjectLevel2" column="subject_level2"/>
        <result property="amount" column="amount"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="feeSelect">
        select id, biz_date, sheet_no, del_flag, org_id, create_by, create_time, update_by, update_time
        from warehouse_related_fee
    </sql>

    <select id="selectById" parameterType="Long" resultMap="FeeResult">
        <include refid="feeSelect"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectList" parameterType="com.ruoyi.web.stock.domain.WarehouseRelatedFee" resultMap="FeeResult">
        <include refid="feeSelect"/>
        <where>
            del_flag = '0'
            <if test="orgId != null and orgId != ''">and org_id = #{orgId}</if>
            <if test="sheetNo != null and sheetNo != ''">and sheet_no like concat('%', #{sheetNo}, '%')</if>
            <if test="bizDate != null">and biz_date = #{bizDate}</if>
        </where>
        order by id desc
    </select>

    <insert id="insert" parameterType="com.ruoyi.web.stock.domain.WarehouseRelatedFee" useGeneratedKeys="true" keyProperty="id">
        insert into warehouse_related_fee (
            biz_date, sheet_no, del_flag, org_id, create_by, create_time, update_by, update_time
        ) values (
            #{bizDate}, #{sheetNo}, #{delFlag}, #{orgId}, #{createBy}, now(), #{updateBy}, now()
        )
    </insert>

    <update id="deleteById">
        update warehouse_related_fee
        set del_flag = '2', update_by = #{updateBy}, update_time = now()
        where id = #{id} and del_flag = '0'
    </update>

    <select id="selectItemsByFeeId" parameterType="Long" resultMap="FeeItemResult">
        select id, fee_id, sheet_no, line_no, subject_level1, subject_level2, amount, remark,
               del_flag, org_id, create_by, create_time, update_by, update_time
        from warehouse_related_fee_item
        where fee_id = #{feeId} and del_flag = '0'
        order by line_no asc, id asc
    </select>

    <insert id="insertItemsBatch">
        insert into warehouse_related_fee_item (
            fee_id, sheet_no, line_no, subject_level1, subject_level2, amount, remark,
            del_flag, org_id, create_by, create_time, update_by, update_time
        ) values
        <foreach collection="list" item="item" separator=",">
            (
                #{item.feeId}, #{item.sheetNo}, #{item.lineNo}, #{item.subjectLevel1}, #{item.subjectLevel2}, #{item.amount}, #{item.remark},
                #{item.delFlag}, #{item.orgId}, #{item.createBy}, now(), #{item.updateBy}, now()
            )
        </foreach>
    </insert>

    <update id="deleteItemsByFeeId">
        update warehouse_related_fee_item
        set del_flag = '2', update_by = #{updateBy}, update_time = now()
        where fee_id = #{feeId} and del_flag = '0'
    </update>

    <select id="selectTodayMaxSeq" resultType="java.lang.Integer">
        select max(cast(right(sheet_no, 4) as unsigned))
        from warehouse_related_fee
        where org_id = #{orgId}
          and date_format(biz_date, '%Y%m%d') = #{dateText}
          and del_flag = '0'
    </select>
</mapper>

