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

    <resultMap id="WarehouseBaseResult" type="com.ruoyi.web.stock.domain.WarehouseBase">
        <id property="id" column="id"/>
        <result property="warehouseNo" column="warehouse_no"/>
        <result property="warehouseName" column="warehouse_name"/>
        <result property="engName" column="eng_name"/>
        <result property="warehouseAddress" column="warehouse_address"/>
        <result property="isCentral" column="is_central"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
        <result property="remark" column="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"/>
    </resultMap>

    <resultMap id="StockOptionResult" type="com.ruoyi.web.stock.domain.vo.StockOptionVo">
        <result property="code" column="code"/>
        <result property="name" column="name"/>
    </resultMap>

    <sql id="baseSelect">
        select id, warehouse_no, warehouse_name, eng_name, warehouse_address, is_central,
               del_flag, create_by, create_time, update_by, update_time, remark, org_id
        from warehouse_base
    </sql>

    <select id="selectWarehouseBaseById" parameterType="Long" resultMap="WarehouseBaseResult">
        <include refid="baseSelect"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectWarehouseBaseList" parameterType="com.ruoyi.web.stock.domain.WarehouseBase" resultMap="WarehouseBaseResult">
        <include refid="baseSelect"/>
        <where>
            del_flag = '0'
            <if test="warehouseNo != null and warehouseNo != ''">
                and warehouse_no like concat('%', #{warehouseNo}, '%')
            </if>
            <if test="warehouseName != null and warehouseName != ''">
                and warehouse_name like concat('%', #{warehouseName}, '%')
            </if>
            <if test="isCentral != null">
                and is_central = #{isCentral}
            </if>
            <if test="orgId != null and orgId != ''">
                and org_id = #{orgId}
            </if>
        </where>
        order by id desc
    </select>

    <select id="selectWarehouseBaseByNo" parameterType="String" resultMap="WarehouseBaseResult">
        <include refid="baseSelect"/>
        where warehouse_no = #{warehouseNo}
          and org_id = #{orgId}
          and del_flag = '0'
        limit 1
    </select>

    <insert id="insertWarehouseBase" parameterType="com.ruoyi.web.stock.domain.WarehouseBase" useGeneratedKeys="true" keyProperty="id">
        insert into warehouse_base (
            warehouse_no, warehouse_name, eng_name, warehouse_address, is_central,
            del_flag, create_by, create_time, update_by, update_time, remark, org_id
        ) values (
            #{warehouseNo}, #{warehouseName}, #{engName}, #{warehouseAddress}, #{isCentral},
            #{delFlag}, #{createBy}, now(), #{updateBy}, now(), #{remark}, #{orgId}
        )
    </insert>

    <update id="updateWarehouseBase" parameterType="com.ruoyi.web.stock.domain.WarehouseBase">
        update warehouse_base
        <set>
            warehouse_no = #{warehouseNo},
            warehouse_name = #{warehouseName},
            eng_name = #{engName},
            warehouse_address = #{warehouseAddress},
            is_central = #{isCentral},
            update_by = #{updateBy},
            update_time = now(),
            remark = #{remark},
            org_id = #{orgId}
        </set>
        where id = #{id}
          and del_flag = '0'
    </update>

    <update id="deleteWarehouseBaseById" parameterType="Long">
        update warehouse_base
        set del_flag = '2',
            update_time = now()
        where id = #{id}
          and del_flag = '0'
    </update>

    <select id="selectWarehouseOptions" resultMap="StockOptionResult">
        select warehouse_no as code, warehouse_name as name
        from warehouse_base
        where del_flag = '0'
          and org_id = #{orgId}
        order by warehouse_no asc
    </select>
</mapper>
