<?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.WarehouseAccountingGuideMapper">
    <resultMap id="GuideResult" type="com.ruoyi.web.stock.domain.WarehouseAccountingGuide">
        <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"/>
    </resultMap>
    <resultMap id="GuideGoodsResult" type="com.ruoyi.web.stock.domain.WarehouseAccountingGuideGoods">
        <id property="id" column="id"/>
        <result property="guideId" column="guide_id"/>
        <result property="sheetNo" column="sheet_no"/>
        <result property="lineNo" column="line_no"/>
        <result property="productionWorkshop" column="production_workshop"/>
        <result property="goodsNum" column="goods_num"/>
        <result property="goodsName" column="goods_name"/>
        <result property="goodsSpec" column="goods_spec"/>
        <result property="guidePrice" column="guide_price"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
    </resultMap>

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

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

    <select id="selectList" parameterType="com.ruoyi.web.stock.domain.WarehouseAccountingGuide" resultMap="GuideResult">
        <include refid="guideSelect"/>
        <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.WarehouseAccountingGuide" useGeneratedKeys="true" keyProperty="id">
        insert into warehouse_accounting_guide (
            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_accounting_guide
        set del_flag = '2', update_by = #{updateBy}, update_time = now()
        where id = #{id} and del_flag = '0'
    </update>

    <select id="selectGoodsByGuideId" parameterType="Long" resultMap="GuideGoodsResult">
        select id, guide_id, sheet_no, line_no, production_workshop, goods_num, goods_name, goods_spec, guide_price, remark, del_flag, org_id
        from warehouse_accounting_guide_goods
        where guide_id = #{guideId} and del_flag = '0'
        order by line_no asc, id asc
    </select>

    <insert id="insertGoodsBatch">
        insert into warehouse_accounting_guide_goods (
            guide_id, sheet_no, line_no, production_workshop, goods_num, goods_name, goods_spec, guide_price, remark,
            del_flag, org_id, create_by, create_time, update_by, update_time
        ) values
        <foreach collection="list" item="item" separator=",">
            (
                #{item.guideId}, #{item.sheetNo}, #{item.lineNo}, #{item.productionWorkshop}, #{item.goodsNum}, #{item.goodsName}, #{item.goodsSpec}, #{item.guidePrice}, #{item.remark},
                #{item.delFlag}, #{item.orgId}, #{item.createBy}, now(), #{item.updateBy}, now()
            )
        </foreach>
    </insert>

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

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

