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

    <resultMap type="AmsRepairOrder" id="AmsRepairOrderResult">
        <result property="repairId"    column="repair_id"    />
        <result property="repairNo"    column="repair_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="reportUserId"    column="report_user_id"    />
        <result property="reportUserName"    column="report_user_name"    />
        <result property="faultDesc"    column="fault_desc"    />
        <result property="reportTime"    column="report_time"    />
        <result property="expectedFinishTime"    column="expected_finish_time"    />
        <result property="repairerType"    column="repairer_type"    />
        <result property="repairUserId"    column="repair_user_id"    />
        <result property="repairUserName"    column="repair_user_name"    />
        <result property="repairOrgName"    column="repair_org_name"    />
        <result property="repairContactPhone"    column="repair_contact_phone"    />
        <result property="repairStartTime"    column="repair_start_time"    />
        <result property="repairFinishTime"    column="repair_finish_time"    />
        <result property="repairResult"    column="repair_result"    />
        <result property="repairCost"    column="repair_cost"    />
        <result property="beforeAssetStatus"    column="before_asset_status"    />
        <result property="rejectReason"    column="reject_reason"    />
        <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 type="AmsAsset" id="AvailableRepairAssetResult">
        <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>

    <sql id="selectAmsRepairOrderVo">
        select repair_id, repair_no, asset_id, asset_code, asset_name, category_id, category_code, category_name, spec_model, brand, report_user_id, report_user_name, fault_desc, report_time, expected_finish_time, repairer_type, repair_user_id, repair_user_name, repair_org_name, repair_contact_phone, repair_start_time, repair_finish_time, repair_result, repair_cost, before_asset_status, reject_reason, order_status, create_by, create_time, update_by, update_time, remark, del_flag from ams_repair_order
    </sql>

    <select id="selectAmsRepairOrderList" parameterType="AmsRepairOrder" resultMap="AmsRepairOrderResult">
        <include refid="selectAmsRepairOrderVo"/>
        <where>
            del_flag = '0'
            <if test="repairNo != null  and repairNo != ''"> and repair_no like concat(#{repairNo}, '%')</if>
            <if test="assetId != null "> and asset_id = #{assetId}</if>
            <if test="assetCode != null  and assetCode != ''"> and asset_code like concat(#{assetCode}, '%')</if>
            <if test="assetName != null  and assetName != ''"> and asset_name like concat('%', #{assetName}, '%')</if>
            <if test="categoryId != null "> and category_id = #{categoryId}</if>
            <if test="categoryCode != null  and categoryCode != ''"> and category_code = #{categoryCode}</if>
            <if test="categoryName != null  and categoryName != ''"> and category_name like concat('%', #{categoryName}, '%')</if>
            <if test="specModel != null  and specModel != ''"> and spec_model = #{specModel}</if>
            <if test="brand != null  and brand != ''"> and brand = #{brand}</if>
            <if test="reportUserId != null "> and report_user_id = #{reportUserId}</if>
            <if test="reportUserName != null  and reportUserName != ''"> and report_user_name like concat('%', #{reportUserName}, '%')</if>
            <if test="faultDesc != null  and faultDesc != ''"> and fault_desc = #{faultDesc}</if>
            <if test="reportTime != null "> and report_time = #{reportTime}</if>
            <if test="params.beginReportTime != null and params.beginReportTime != ''">
                and report_time &gt;= #{params.beginReportTime}
            </if>
            <if test="params.endReportTime != null and params.endReportTime != ''">
                and report_time &lt; date_add(#{params.endReportTime}, interval 1 day)
            </if>
            <if test="expectedFinishTime != null "> and expected_finish_time = #{expectedFinishTime}</if>
            <if test="repairerType != null  and repairerType != ''"> and repairer_type = #{repairerType}</if>
            <if test="repairUserId != null "> and repair_user_id = #{repairUserId}</if>
            <if test="repairUserName != null  and repairUserName != ''"> and repair_user_name like concat('%', #{repairUserName}, '%')</if>
            <if test="repairOrgName != null  and repairOrgName != ''"> and repair_org_name like concat('%', #{repairOrgName}, '%')</if>
            <if test="repairContactPhone != null  and repairContactPhone != ''"> and repair_contact_phone = #{repairContactPhone}</if>
            <if test="repairStartTime != null "> and repair_start_time = #{repairStartTime}</if>
            <if test="repairFinishTime != null "> and repair_finish_time = #{repairFinishTime}</if>
            <if test="repairResult != null  and repairResult != ''"> and repair_result = #{repairResult}</if>
            <if test="repairCost != null "> and repair_cost = #{repairCost}</if>
            <if test="beforeAssetStatus != null  and beforeAssetStatus != ''"> and before_asset_status = #{beforeAssetStatus}</if>
            <if test="rejectReason != null  and rejectReason != ''"> and reject_reason = #{rejectReason}</if>
            <if test="orderStatus != null  and orderStatus != ''"> and order_status = #{orderStatus}</if>
            <if test='params.overdueFlag != null and params.overdueFlag == "Y"'>
                and expected_finish_time is not null
                and expected_finish_time &lt; now()
                and order_status != 'REPAIR_DONE'
            </if>
            <if test='params.overdueFlag != null and params.overdueFlag == "N"'>
                and (
                    expected_finish_time is null
                    or expected_finish_time &gt;= now()
                    or order_status = 'REPAIR_DONE'
                )
            </if>
        </where>
        order by create_time desc, repair_id desc
    </select>

    <select id="selectAmsRepairOrderByRepairId" parameterType="Long" resultMap="AmsRepairOrderResult">
        <include refid="selectAmsRepairOrderVo"/>
        where repair_id = #{repairId} and del_flag = '0'
    </select>

    <select id="selectAmsRepairOrderByRepairIdForUpdate" parameterType="Long" resultMap="AmsRepairOrderResult">
        <include refid="selectAmsRepairOrderVo"/>
        where repair_id = #{repairId} and del_flag = '0'
        for update
    </select>

    <insert id="insertAmsRepairOrder" parameterType="AmsRepairOrder" useGeneratedKeys="true" keyProperty="repairId">
        insert into ams_repair_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="repairNo != null and repairNo != ''">repair_no,</if>
            <if test="assetId != null">asset_id,</if>
            <if test="assetCode != null and assetCode != ''">asset_code,</if>
            <if test="assetName != null and assetName != ''">asset_name,</if>
            <if test="categoryId != null">category_id,</if>
            <if test="categoryCode != null">category_code,</if>
            <if test="categoryName != null">category_name,</if>
            <if test="specModel != null">spec_model,</if>
            <if test="brand != null">brand,</if>
            <if test="reportUserId != null">report_user_id,</if>
            <if test="reportUserName != null">report_user_name,</if>
            <if test="faultDesc != null">fault_desc,</if>
            <if test="reportTime != null">report_time,</if>
            <if test="expectedFinishTime != null">expected_finish_time,</if>
            <if test="repairerType != null and repairerType != ''">repairer_type,</if>
            <if test="repairUserId != null">repair_user_id,</if>
            <if test="repairUserName != null">repair_user_name,</if>
            <if test="repairOrgName != null">repair_org_name,</if>
            <if test="repairContactPhone != null">repair_contact_phone,</if>
            <if test="repairStartTime != null">repair_start_time,</if>
            <if test="repairFinishTime != null">repair_finish_time,</if>
            <if test="repairResult != null">repair_result,</if>
            <if test="repairCost != null">repair_cost,</if>
            <if test="beforeAssetStatus != null">before_asset_status,</if>
            <if test="rejectReason != null">reject_reason,</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="repairNo != null and repairNo != ''">#{repairNo},</if>
            <if test="assetId != null">#{assetId},</if>
            <if test="assetCode != null and assetCode != ''">#{assetCode},</if>
            <if test="assetName != null and assetName != ''">#{assetName},</if>
            <if test="categoryId != null">#{categoryId},</if>
            <if test="categoryCode != null">#{categoryCode},</if>
            <if test="categoryName != null">#{categoryName},</if>
            <if test="specModel != null">#{specModel},</if>
            <if test="brand != null">#{brand},</if>
            <if test="reportUserId != null">#{reportUserId},</if>
            <if test="reportUserName != null">#{reportUserName},</if>
            <if test="faultDesc != null">#{faultDesc},</if>
            <if test="reportTime != null">#{reportTime},</if>
            <if test="expectedFinishTime != null">#{expectedFinishTime},</if>
            <if test="repairerType != null and repairerType != ''">#{repairerType},</if>
            <if test="repairUserId != null">#{repairUserId},</if>
            <if test="repairUserName != null">#{repairUserName},</if>
            <if test="repairOrgName != null">#{repairOrgName},</if>
            <if test="repairContactPhone != null">#{repairContactPhone},</if>
            <if test="repairStartTime != null">#{repairStartTime},</if>
            <if test="repairFinishTime != null">#{repairFinishTime},</if>
            <if test="repairResult != null">#{repairResult},</if>
            <if test="repairCost != null">#{repairCost},</if>
            <if test="beforeAssetStatus != null">#{beforeAssetStatus},</if>
            <if test="rejectReason != null">#{rejectReason},</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="updateAmsRepairOrder" parameterType="AmsRepairOrder">
        update ams_repair_order
        set repair_no = #{repairNo},
            asset_id = #{assetId},
            asset_code = #{assetCode},
            asset_name = #{assetName},
            category_id = #{categoryId},
            category_code = #{categoryCode},
            category_name = #{categoryName},
            spec_model = #{specModel},
            brand = #{brand},
            report_user_id = #{reportUserId},
            report_user_name = #{reportUserName},
            fault_desc = #{faultDesc},
            report_time = #{reportTime},
            expected_finish_time = #{expectedFinishTime},
            repairer_type = #{repairerType},
            repair_user_id = #{repairUserId},
            repair_user_name = #{repairUserName},
            repair_org_name = #{repairOrgName},
            repair_contact_phone = #{repairContactPhone},
            repair_start_time = #{repairStartTime},
            repair_finish_time = #{repairFinishTime},
            repair_result = #{repairResult},
            repair_cost = #{repairCost},
            before_asset_status = #{beforeAssetStatus},
            reject_reason = #{rejectReason},
            order_status = #{orderStatus},
            create_by = #{createBy},
            create_time = #{createTime},
            update_by = #{updateBy},
            update_time = #{updateTime},
            remark = #{remark},
            del_flag = #{delFlag}
        where repair_id = #{repairId} and del_flag = '0'
        <if test="params.expectedOrderStatus != null and params.expectedOrderStatus != ''">
            and order_status = #{params.expectedOrderStatus}
        </if>
    </update>

    <!-- 逻辑删除：仅草稿单允许删除 -->
    <update id="deleteAmsRepairOrderByRepairId" parameterType="Long">
        update ams_repair_order set del_flag = '1' where repair_id = #{repairId} and del_flag = '0' and order_status = 'DRAFT'
    </update>

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

    <select id="countOtherActiveRepairOrderByAssetId" resultType="int">
        select count(1)
        from ams_repair_order
        where asset_id = #{assetId}
          and del_flag = '0'
          and order_status in
          <foreach item="status" collection="statuses" open="(" separator="," close=")">
              #{status}
          </foreach>
          <if test="currentRepairId != null">
              and repair_id != #{currentRepairId}
          </if>
    </select>

    <select id="selectAvailableRepairAssetList" resultMap="AvailableRepairAssetResult">
        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
          <foreach item="status" collection="statuses" open="(" separator="," close=")">
              #{status}
          </foreach>
          and not exists (
              select 1
              from ams_repair_order r
              where r.asset_id = asset.asset_id
                and r.del_flag = '0'
                and r.order_status in
                <foreach item="rStatus" collection="activeRepairStatuses" open="(" separator="," close=")">
                    #{rStatus}
                </foreach>
                <if test="currentRepairId != null">
                    and r.repair_id != #{currentRepairId}
                </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>
          order by asset.asset_id
    </select>
</mapper>
