<?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.asset.mapper.AmsWarehouseMapper">
    
    <resultMap type="AmsWarehouse" id="AmsWarehouseResult">
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseCode"    column="warehouse_code"    />
        <result property="warehouseName"    column="warehouse_name"    />
        <result property="manageDeptId"    column="manage_dept_id"    />
        <result property="manageDeptName"    column="manage_dept_name"    />
        <result property="warehouseAddress"    column="warehouse_address"    />
        <result property="managerUserId"    column="manager_user_id"    />
        <result property="managerName"    column="manager_name"    />
        <result property="contactPhone"    column="contact_phone"    />
        <result property="enabled"    column="enabled"    />
        <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="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectAmsWarehouseVo">
        select warehouse_id, warehouse_code, warehouse_name, manage_dept_id, manage_dept_name, warehouse_address, manager_user_id, manager_name, contact_phone, enabled, create_by, create_time, update_by, update_time, remark, del_flag from ams_warehouse
    </sql>

    <select id="selectAmsWarehouseList" parameterType="AmsWarehouse" resultMap="AmsWarehouseResult">
        <include refid="selectAmsWarehouseVo"/>
        <where>
            del_flag = '0'
            <if test="warehouseCode != null  and warehouseCode != ''"> and warehouse_code = #{warehouseCode}</if>
            <if test="warehouseName != null  and warehouseName != ''"> and warehouse_name like concat('%', #{warehouseName}, '%')</if>
            <if test="manageDeptId != null"> and manage_dept_id = #{manageDeptId}</if>
            <if test="warehouseAddress != null  and warehouseAddress != ''"> and warehouse_address = #{warehouseAddress}</if>
            <if test="managerUserId != null"> and manager_user_id = #{managerUserId}</if>
            <if test="managerName != null  and managerName != ''"> and manager_name like concat('%', #{managerName}, '%')</if>
            <if test="contactPhone != null  and contactPhone != ''"> and contact_phone = #{contactPhone}</if>
            <if test="enabled != null  and enabled != ''"> and enabled = #{enabled}</if>
        </where>
    </select>
    
    <select id="selectAmsWarehouseByWarehouseId" parameterType="Long" resultMap="AmsWarehouseResult">
        <include refid="selectAmsWarehouseVo"/>
        where warehouse_id = #{warehouseId} and del_flag = '0'
    </select>

    <select id="checkWarehouseCodeUnique" parameterType="String" resultMap="AmsWarehouseResult">
        <include refid="selectAmsWarehouseVo"/>
        where warehouse_code = #{warehouseCode} and del_flag = '0' limit 1
    </select>

    <select id="countWarehouseReferences" parameterType="Long" resultType="int">
        select
            (select count(1) from ams_asset where warehouse_id = #{warehouseId} and del_flag = '0')
            + (select count(1) from ams_asset_lifecycle_log where del_flag = '0' and (before_warehouse_id = #{warehouseId} or after_warehouse_id = #{warehouseId}))
            + (select count(1) from ams_asset_location where warehouse_id = #{warehouseId} and del_flag = '0')
            + (select count(1) from ams_borrow_order_item where del_flag = '0' and (before_warehouse_id = #{warehouseId} or return_warehouse_id = #{warehouseId}))
            + (select count(1) from ams_inbound_order where warehouse_id = #{warehouseId} and del_flag = '0')
            + (select count(1) from ams_inventory_task where warehouse_id = #{warehouseId} and del_flag = '0')
            + (select count(1) from ams_inventory_task_item where del_flag = '0' and (book_warehouse_id = #{warehouseId} or inventory_warehouse_id = #{warehouseId}))
            + (select count(1) from ams_inventory_task_surplus where del_flag = '0' and (book_warehouse_id = #{warehouseId} or inventory_warehouse_id = #{warehouseId}))
            + (select count(1) from ams_receive_order_item where before_warehouse_id = #{warehouseId} and del_flag = '0')
            + (select count(1) from ams_return_order where receive_warehouse_id = #{warehouseId} and del_flag = '0')
            + (select count(1) from ams_return_order_item where after_warehouse_id = #{warehouseId} and del_flag = '0')
            + (select count(1) from ams_transfer_order_item where del_flag = '0' and (old_warehouse_id = #{warehouseId} or new_warehouse_id = #{warehouseId}))
    </select>

    <insert id="insertAmsWarehouse" parameterType="AmsWarehouse" useGeneratedKeys="true" keyProperty="warehouseId">
        insert into ams_warehouse
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="warehouseCode != null and warehouseCode != ''">warehouse_code,</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name,</if>
            <if test="manageDeptId != null">manage_dept_id,</if>
            <if test="manageDeptName != null">manage_dept_name,</if>
            <if test="warehouseAddress != null">warehouse_address,</if>
            <if test="managerUserId != null">manager_user_id,</if>
            <if test="managerName != null">manager_name,</if>
            <if test="contactPhone != null">contact_phone,</if>
            <if test="enabled != null and enabled != ''">enabled,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="warehouseCode != null and warehouseCode != ''">#{warehouseCode},</if>
            <if test="warehouseName != null and warehouseName != ''">#{warehouseName},</if>
            <if test="manageDeptId != null">#{manageDeptId},</if>
            <if test="manageDeptName != null">#{manageDeptName},</if>
            <if test="warehouseAddress != null">#{warehouseAddress},</if>
            <if test="managerUserId != null">#{managerUserId},</if>
            <if test="managerName != null">#{managerName},</if>
            <if test="contactPhone != null">#{contactPhone},</if>
            <if test="enabled != null and enabled != ''">#{enabled},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateAmsWarehouse" parameterType="AmsWarehouse">
        update ams_warehouse
        <trim prefix="SET" suffixOverrides=",">
            <if test="warehouseCode != null and warehouseCode != ''">warehouse_code = #{warehouseCode},</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name = #{warehouseName},</if>
            manage_dept_id = #{manageDeptId},
            manage_dept_name = #{manageDeptName},
            <if test="warehouseAddress != null">warehouse_address = #{warehouseAddress},</if>
            manager_user_id = #{managerUserId},
            manager_name = #{managerName},
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="enabled != null and enabled != ''">enabled = #{enabled},</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="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where warehouse_id = #{warehouseId}
    </update>

    <update id="deleteAmsWarehouseByWarehouseId" parameterType="Long">
        update ams_warehouse set del_flag = '1' where warehouse_id = #{warehouseId}
    </update>

    <update id="deleteAmsWarehouseByWarehouseIds" parameterType="String">
        update ams_warehouse set del_flag = '1' where warehouse_id in
        <foreach item="warehouseId" collection="array" open="(" separator="," close=")">
            #{warehouseId}
        </foreach>
    </update>

</mapper>
