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

    <resultMap type="AmsDisposalOrder" id="AmsDisposalOrderResult">
        <result property="orderId"    column="order_id"    />
        <result property="disposalNo"    column="disposal_no"    />
        <result property="applicantId"    column="applicant_id"    />
        <result property="applicantName"    column="applicant_name"    />
        <result property="applyDeptId"    column="apply_dept_id"    />
        <result property="applyDeptName"    column="apply_dept_name"    />
        <result property="confirmUserId"    column="confirm_user_id"    />
        <result property="confirmUserName"    column="confirm_user_name"    />
        <result property="confirmTime"    column="confirm_time"    />
        <result property="rejectReason"    column="reject_reason"    />
        <result property="disposalMethod"    column="disposal_method"    />
        <result property="disposalTime"    column="disposal_time"    />
        <result property="orderStatus"    column="order_status"    />
        <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>

    <resultMap id="AmsDisposalOrderAmsDisposalOrderItemResult" type="AmsDisposalOrder" extends="AmsDisposalOrderResult">
        <collection property="amsDisposalOrderItemList" ofType="AmsDisposalOrderItem" column="order_id" select="selectAmsDisposalOrderItemList" />
    </resultMap>

    <resultMap type="AmsAsset" id="AvailableDisposalAssetResult">
        <result property="assetId"        column="asset_id"       />
        <result property="assetCode"      column="asset_code"     />
        <result property="assetName"      column="asset_name"     />
        <result property="categoryId"     column="category_id"    />
        <result property="categoryCode"   column="category_code"  />
        <result property="categoryName"   column="category_name"  />
        <result property="specModel"      column="spec_model"     />
        <result property="brand"          column="brand"          />
        <result property="assetStatus"    column="asset_status"   />
        <result property="warehouseId"    column="warehouse_id"   />
        <result property="warehouseCode"  column="warehouse_code" />
        <result property="warehouseName"  column="warehouse_name" />
        <result property="locationId"     column="location_id"    />
        <result property="locationCode"   column="location_code"  />
        <result property="locationName"   column="location_name"  />
        <result property="tagCode"        column="tag_code"       />
    </resultMap>

    <resultMap type="AmsDisposalOrderItem" id="AmsDisposalOrderItemResult">
        <result property="itemId"    column="item_id"    />
        <result property="orderId"    column="order_id"    />
        <result property="disposalNo"    column="disposal_no"    />
        <result property="assetId"    column="asset_id"    />
        <result property="assetCode"    column="asset_code"    />
        <result property="assetName"    column="asset_name"    />
        <result property="categoryId"    column="category_id"    />
        <result property="categoryCode"    column="category_code"    />
        <result property="categoryName"    column="category_name"    />
        <result property="specModel"    column="spec_model"    />
        <result property="brand"    column="brand"    />
        <result property="disposalReason"    column="disposal_reason"    />
        <result property="disposalRemark"    column="disposal_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"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectAmsDisposalOrderVo">
        select order_id, disposal_no, applicant_id, applicant_name, apply_dept_id, apply_dept_name, confirm_user_id, confirm_user_name, confirm_time, reject_reason, disposal_method, disposal_time, order_status, create_by, create_time, update_by, update_time, remark, del_flag from ams_disposal_order
    </sql>

    <select id="selectAmsDisposalOrderList" parameterType="AmsDisposalOrder" resultMap="AmsDisposalOrderResult">
        <include refid="selectAmsDisposalOrderVo"/>
        <where>
            del_flag = '0'
            <if test="disposalNo != null  and disposalNo != ''"> and disposal_no like concat(#{disposalNo}, '%')</if>
            <if test="applicantId != null "> and applicant_id = #{applicantId}</if>
            <if test="applicantName != null  and applicantName != ''"> and applicant_name like concat('%', #{applicantName}, '%')</if>
            <if test="applyDeptId != null "> and apply_dept_id = #{applyDeptId}</if>
            <if test="applyDeptName != null  and applyDeptName != ''"> and apply_dept_name like concat('%', #{applyDeptName}, '%')</if>
            <if test="confirmUserId != null "> and confirm_user_id = #{confirmUserId}</if>
            <if test="confirmUserName != null  and confirmUserName != ''"> and confirm_user_name like concat('%', #{confirmUserName}, '%')</if>
            <if test="confirmTime != null "> and confirm_time = #{confirmTime}</if>
            <if test="rejectReason != null  and rejectReason != ''"> and reject_reason = #{rejectReason}</if>
            <if test="disposalMethod != null  and disposalMethod != ''"> and disposal_method = #{disposalMethod}</if>
            <if test="disposalTime != null "> and disposal_time = #{disposalTime}</if>
            <if test="params.beginDisposalTime != null and params.beginDisposalTime != ''">
                and disposal_time &gt;= #{params.beginDisposalTime}
            </if>
            <if test="params.endDisposalTime != null and params.endDisposalTime != ''">
                and disposal_time &lt; date_add(#{params.endDisposalTime}, interval 1 day)
            </if>
            <if test="orderStatus != null  and orderStatus != ''"> and order_status = #{orderStatus}</if>
            <if test="assetCode != null and assetCode != ''">
                and exists (
                    select 1
                    from ams_disposal_order_item item
                    where item.order_id = ams_disposal_order.order_id
                      and item.del_flag = '0'
                      and item.asset_code like concat('%', #{assetCode}, '%')
                )
            </if>
        </where>
        order by create_time desc, order_id desc
    </select>

    <select id="selectAmsDisposalOrderByOrderId" parameterType="Long" resultMap="AmsDisposalOrderAmsDisposalOrderItemResult">
        select order_id, disposal_no, applicant_id, applicant_name, apply_dept_id, apply_dept_name, confirm_user_id, confirm_user_name, confirm_time, reject_reason, disposal_method, disposal_time, order_status, create_by, create_time, update_by, update_time, remark, del_flag
        from ams_disposal_order
        where order_id = #{orderId} and del_flag = '0'
    </select>

    <select id="selectAmsDisposalOrderByOrderIdForUpdate" parameterType="Long" resultMap="AmsDisposalOrderAmsDisposalOrderItemResult">
        select order_id, disposal_no, applicant_id, applicant_name, apply_dept_id, apply_dept_name, confirm_user_id, confirm_user_name, confirm_time, reject_reason, disposal_method, disposal_time, order_status, create_by, create_time, update_by, update_time, remark, del_flag
        from ams_disposal_order
        where order_id = #{orderId} and del_flag = '0'
        for update
    </select>

    <select id="selectAmsDisposalOrderItemList" resultMap="AmsDisposalOrderItemResult">
        select item_id, order_id, disposal_no, asset_id, asset_code, asset_name, category_id, category_code, category_name, spec_model, brand, disposal_reason, disposal_remark, create_by, create_time, update_by, update_time, remark, del_flag
        from ams_disposal_order_item
        where order_id = #{order_id} and del_flag = '0'
    </select>

    <insert id="insertAmsDisposalOrder" parameterType="AmsDisposalOrder" useGeneratedKeys="true" keyProperty="orderId">
        insert into ams_disposal_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="disposalNo != null and disposalNo != ''">disposal_no,</if>
            <if test="applicantId != null">applicant_id,</if>
            <if test="applicantName != null">applicant_name,</if>
            <if test="applyDeptId != null">apply_dept_id,</if>
            <if test="applyDeptName != null">apply_dept_name,</if>
            <if test="confirmUserId != null">confirm_user_id,</if>
            <if test="confirmUserName != null">confirm_user_name,</if>
            <if test="confirmTime != null">confirm_time,</if>
            <if test="rejectReason != null">reject_reason,</if>
            <if test="disposalMethod != null">disposal_method,</if>
            <if test="disposalTime != null">disposal_time,</if>
            <if test="orderStatus != null and orderStatus != ''">order_status,</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 and delFlag != ''">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="disposalNo != null and disposalNo != ''">#{disposalNo},</if>
            <if test="applicantId != null">#{applicantId},</if>
            <if test="applicantName != null">#{applicantName},</if>
            <if test="applyDeptId != null">#{applyDeptId},</if>
            <if test="applyDeptName != null">#{applyDeptName},</if>
            <if test="confirmUserId != null">#{confirmUserId},</if>
            <if test="confirmUserName != null">#{confirmUserName},</if>
            <if test="confirmTime != null">#{confirmTime},</if>
            <if test="rejectReason != null">#{rejectReason},</if>
            <if test="disposalMethod != null">#{disposalMethod},</if>
            <if test="disposalTime != null">#{disposalTime},</if>
            <if test="orderStatus != null and orderStatus != ''">#{orderStatus},</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 and delFlag != ''">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateAmsDisposalOrder" parameterType="AmsDisposalOrder">
        update ams_disposal_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="disposalNo != null and disposalNo != ''">disposal_no = #{disposalNo},</if>
            <if test="applicantId != null">applicant_id = #{applicantId},</if>
            <if test="applicantName != null">applicant_name = #{applicantName},</if>
            <if test="applyDeptId != null">apply_dept_id = #{applyDeptId},</if>
            <if test="applyDeptName != null">apply_dept_name = #{applyDeptName},</if>
            <if test="confirmUserId != null">confirm_user_id = #{confirmUserId},</if>
            <if test="confirmUserName != null">confirm_user_name = #{confirmUserName},</if>
            <if test="confirmTime != null">confirm_time = #{confirmTime},</if>
            <if test="rejectReason != null">reject_reason = #{rejectReason},</if>
            <if test="disposalMethod != null">disposal_method = #{disposalMethod},</if>
            <if test="disposalTime != null">disposal_time = #{disposalTime},</if>
            <if test="orderStatus != null and orderStatus != ''">order_status = #{orderStatus},</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 and delFlag != ''">del_flag = #{delFlag},</if>
        </trim>
        where order_id = #{orderId} and del_flag = '0'
        <if test="params.expectedOrderStatus != null and params.expectedOrderStatus != ''">
            and order_status = #{params.expectedOrderStatus}
        </if>
    </update>

    <update id="deleteAmsDisposalOrderByOrderId" parameterType="Long">
        update ams_disposal_order set del_flag = '1' where order_id = #{orderId} and del_flag = '0' and order_status = 'DRAFT'
    </update>

    <update id="deleteAmsDisposalOrderByOrderIds" parameterType="String">
        update ams_disposal_order set del_flag = '1' where order_id in
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        and del_flag = '0' and order_status = 'DRAFT'
    </update>

    <update id="deleteAmsDisposalOrderItemByOrderIds" parameterType="String">
        update ams_disposal_order_item set del_flag = '1' where order_id in
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        and del_flag = '0'
    </update>

    <update id="deleteAmsDisposalOrderItemByOrderId" parameterType="Long">
        update ams_disposal_order_item set del_flag = '1' where order_id = #{orderId} and del_flag = '0'
    </update>

    <insert id="batchAmsDisposalOrderItem">
        insert into ams_disposal_order_item( item_id, order_id, disposal_no, asset_id, asset_code, asset_name, category_id, category_code, category_name, spec_model, brand, disposal_reason, disposal_remark, create_by, create_time, update_by, update_time, remark, del_flag) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.itemId}, #{item.orderId}, #{item.disposalNo}, #{item.assetId}, #{item.assetCode}, #{item.assetName}, #{item.categoryId}, #{item.categoryCode}, #{item.categoryName}, #{item.specModel}, #{item.brand}, #{item.disposalReason}, #{item.disposalRemark}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime}, #{item.remark}, '0')
        </foreach>
    </insert>

    <select id="selectAvailableDisposalAssetList" resultMap="AvailableDisposalAssetResult">
        select asset.asset_id, asset.asset_code, asset.asset_name, asset.category_id,
               asset.category_code, asset.category_name, asset.spec_model, asset.brand,
               asset.asset_status, asset.warehouse_id, asset.warehouse_code, asset.warehouse_name,
               asset.location_id, asset.location_code, asset.location_name, asset.tag_code
        from ams_asset asset
        where asset.del_flag = '0'
          and asset.asset_status in ('IN_STOCK', 'IN_USE')
          and not exists (
              select 1
              from ams_disposal_order_item item
              inner join ams_disposal_order o on item.order_id = o.order_id
              where item.asset_id = asset.asset_id
                and item.del_flag = '0'
                and o.del_flag = '0'
                and o.order_status in ('DRAFT', 'PENDING_CONFIRM')
                <if test="currentOrderId != null">
                    and o.order_id != #{currentOrderId}
                </if>
          )
          <if test="asset.assetCode != null and asset.assetCode != ''">
              and asset.asset_code like concat('%', #{asset.assetCode}, '%')
          </if>
          <if test="asset.assetName != null and asset.assetName != ''">
              and asset.asset_name like concat('%', #{asset.assetName}, '%')
          </if>
          <if test="asset.categoryName != null and asset.categoryName != ''">
              and asset.category_name like concat('%', #{asset.categoryName}, '%')
          </if>
          <if test="asset.assetStatus != null and asset.assetStatus != ''">
              and asset.asset_status = #{asset.assetStatus}
          </if>
        order by asset.asset_id
    </select>

    <select id="countOtherActiveDisposalOrderByAssetId" resultType="int">
        select count(1)
        from ams_disposal_order_item item
        inner join ams_disposal_order o on item.order_id = o.order_id
        where item.asset_id = #{assetId}
          and item.del_flag = '0'
          and o.del_flag = '0'
          and o.order_status in ('DRAFT', 'PENDING_CONFIRM')
          <if test="currentOrderId != null">
              and o.order_id != #{currentOrderId}
          </if>
    </select>

</mapper>
