<?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.inventory.mapper.TbAssetInventoryMapper">
    
    <resultMap type="TbAssetInventory" id="TbAssetInventoryResult">
        <result property="id"    column="id"    />
        <result property="orderNumber"    column="order_number"    />
        <result property="name"    column="name"    />
        <result property="inventoryDepartment"    column="inventory_department"    />
        <result property="inventoryBy"    column="inventory_by"    />
        <result property="inventoryDate"    column="inventory_date"    />
        <result property="inventoryLocation"    column="inventory_location"    />
        <result property="inventoryStatus"    column="inventory_status"    />
        <result property="inventoryResult"    column="inventory_result"    />
        <result property="recordStatus"    column="record_status"    />
        <result property="remark"    column="remark"    />
        <result property="createdTime"    column="created_time"    />
        <result property="updatedTime"    column="updated_time"    />
    </resultMap>

    <sql id="selectTbAssetInventoryVo">
        select id, order_number, name, inventory_department, inventory_by, inventory_date, inventory_location, inventory_status, inventory_result, record_status, remark, created_time, updated_time from tb_asset_inventory
    </sql>

    <select id="selectTbAssetInventoryList" parameterType="TbAssetInventory" resultMap="TbAssetInventoryResult">
        <include refid="selectTbAssetInventoryVo"/>
        <where>  
            <if test="orderNumber != null  and orderNumber != ''"> and order_number = #{orderNumber}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="inventoryDepartment != null  and inventoryDepartment != ''"> and inventory_department = #{inventoryDepartment}</if>
            <if test="inventoryBy != null  and inventoryBy != ''"> and inventory_by = #{inventoryBy}</if>
            <if test="inventoryDate != null "> and inventory_date = #{inventoryDate}</if>
            <if test="inventoryLocation != null  and inventoryLocation != ''"> and inventory_location = #{inventoryLocation}</if>
            <if test="inventoryStatus != null "> and inventory_status = #{inventoryStatus}</if>
            <if test="inventoryResult != null "> and inventory_result = #{inventoryResult}</if>
            <if test="recordStatus != null "> and record_status = #{recordStatus}</if>
        </where>
    </select>
    
    <select id="selectTbAssetInventoryById" parameterType="Long" resultMap="TbAssetInventoryResult">
        <include refid="selectTbAssetInventoryVo"/>
        where id = #{id}
    </select>

    <select id="selectOrderNumberByIds" parameterType="Long" resultType="String">
        select DISTINCT order_number from tb_asset_inventory
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectTbAssetInventoryByNumber" parameterType="String" resultMap="TbAssetInventoryResult">
        <include refid="selectTbAssetInventoryVo"/>
        where order_number = #{orderNumber}
    </select>
        
    <insert id="insertTbAssetInventory" parameterType="TbAssetInventory" useGeneratedKeys="true" keyProperty="id">
        insert into tb_asset_inventory
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNumber != null and orderNumber != ''">order_number,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="inventoryDepartment != null and inventoryDepartment != ''">inventory_department,</if>
            <if test="inventoryBy != null and inventoryBy != ''">inventory_by,</if>
            <if test="inventoryDate != null">inventory_date,</if>
            <if test="inventoryLocation != null and inventoryLocation != ''">inventory_location,</if>
            <if test="inventoryStatus != null">inventory_status,</if>
            <if test="inventoryResult != null">inventory_result,</if>
            <if test="recordStatus != null">record_status,</if>
            <if test="remark != null">remark,</if>
            <if test="createdTime != null">created_time,</if>
            <if test="updatedTime != null">updated_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNumber != null and orderNumber != ''">#{orderNumber},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="inventoryDepartment != null and inventoryDepartment != ''">#{inventoryDepartment},</if>
            <if test="inventoryBy != null and inventoryBy != ''">#{inventoryBy},</if>
            <if test="inventoryDate != null">#{inventoryDate},</if>
            <if test="inventoryLocation != null and inventoryLocation != ''">#{inventoryLocation},</if>
            <if test="inventoryStatus != null">#{inventoryStatus},</if>
            <if test="inventoryResult != null">#{inventoryResult},</if>
            <if test="recordStatus != null">#{recordStatus},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
         </trim>
    </insert>

    <update id="updateTbAssetInventory" parameterType="TbAssetInventory">
        update tb_asset_inventory
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderNumber != null and orderNumber != ''">order_number = #{orderNumber},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="inventoryDepartment != null and inventoryDepartment != ''">inventory_department = #{inventoryDepartment},</if>
            <if test="inventoryBy != null and inventoryBy != ''">inventory_by = #{inventoryBy},</if>
            <if test="inventoryDate != null">inventory_date = #{inventoryDate},</if>
            <if test="inventoryLocation != null and inventoryLocation != ''">inventory_location = #{inventoryLocation},</if>
            <if test="inventoryStatus != null">inventory_status = #{inventoryStatus},</if>
            <if test="inventoryResult != null">inventory_result = #{inventoryResult},</if>
            <if test="recordStatus != null">record_status = #{recordStatus},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
            <if test="updatedTime != null">updated_time = #{updatedTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTbAssetInventoryById" parameterType="Long">
        delete from tb_asset_inventory where id = #{id}
    </delete>

    <delete id="deleteTbAssetInventoryByIds" parameterType="String">
        delete from tb_asset_inventory where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="selectTbAssetInventoryDTOList" parameterType="com.ruoyi.inventory.domain.dto.TbAssetInventoryDTO" resultType="com.ruoyi.inventory.domain.dto.TbAssetInventoryDTO">
        select
        a.id,
        a.order_number orderNumber,
        a.name,
        b.dept_name deptName,
        a.inventory_department inventoryDepartment,
        a.inventory_by inventoryBy,
        a.inventory_date inventoryDate,
        a.inventory_location inventoryLocation,
        a.inventory_status inventoryStatus,
        a.inventory_result inventoryResult,
        a.record_status recordStatus,
        a.remark,
        a.created_time createdTime,
        a.updated_time updatedTime,
        c.name locationName
        from
        tb_asset_inventory a
        left join
        sys_dept b
        on
        a.inventory_department = b.dept_id
        left join
        tb_location c
        on
        a.inventory_location = c.number
        <where>
            <if test="orderNumber != null  and orderNumber != ''"> and a.order_number = #{orderNumber}</if>
            <if test="name != null  and name != ''"> and name like a.concat('%', #{name}, '%')</if>
            <if test="inventoryDepartment != null  and inventoryDepartment != ''"> and a.inventory_department = #{inventoryDepartment}</if>
            <if test="inventoryBy != null  and inventoryBy != ''"> and a.inventory_by = #{inventoryBy}</if>
            <if test="inventoryDate != null "> and a.inventory_date = #{inventoryDate}</if>
            <if test="inventoryLocation != null  and inventoryLocation != ''"> and a.inventory_location = #{inventoryLocation}</if>
            <if test="inventoryStatus != null "> and a.inventory_status = #{inventoryStatus}</if>
            <if test="inventoryResult != null "> and a.inventory_result = #{inventoryResult}</if>
            <if test="recordStatus != null "> and a.record_status = #{recordStatus}</if>
        </where>
    </select>
</mapper>