<?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="cn.iocoder.yudao.module.base.mapper.RepairRecordsMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <resultMap id="baseResultMap" type="cn.iocoder.yudao.module.base.vo.RepairRecordsRespVO">
        <id column="id" property="id"/>
        <result column="ticket_id" property="ticketId"/>
        <result column="repair_item_id" property="repairItemId"/>
        <result column="type" property="type"/>
        <result column="remark" property="remark"/>
        <result column="deal_user_name" property="dealUserName"/>
        <result column="deal_user_id" property="dealUserId"/>
        <result column="creator" property="creator"/>
        <result column="create_time" property="createTime"/>
        <collection property="itemList" ofType="cn.iocoder.yudao.module.base.entity.RepairRecordsItem" columnPrefix="item_">
            <id column="id" property="id"/>
            <result column="record_id" property="recordId"/>
            <result column="ticket_id" property="ticketId"/>
            <result column="repair_item_id" property="repairItemId"/>
            <result column="remark" property="remark"/>
            <result column="image" property="image"/>
            <result column="is_open" property="isOpen"/>
            <result column="create_time" property="createTime"/>
        </collection>
    </resultMap>
    <select id="queryRepairRecords" resultMap="baseResultMap">
        SELECT main.*,
               drt.item_name AS projectName,
                drri.id AS item_id,
                drri.record_id AS item_record_id,
                drri.ticket_id AS item_ticket_id,
                drri.repair_item_id AS item_repair_item_id,
                drri.remark AS item_remark,
                drri.image AS item_image,
                drri.is_open AS item_is_open,
                drri.create_time AS item_create_time
        FROM dl_repair_records main
        LEFT JOIN dl_repair_titem drt ON main.repair_item_id = drt.id AND drt.deleted = 0
        LEFT JOIN dl_repair_records_item drri ON main.id = drri.record_id
        <where>
            main.deleted = '0'
            <if test="entity.ticketId != null and entity.ticketId != ''">
                AND main.ticket_id = #{entity.ticketId}
            </if>
            <if test="entity.repairItemId != null and entity.repairItemId != ''">
                AND main.repair_item_id = #{entity.repairItemId}
            </if>
            <if test="entity.dealUserId != null and entity.dealUserId != ''">
                AND main.deal_user_id = #{entity.dealUserId}
            </if>
            <if test="entity.type != null and entity.type != ''">
                AND main.type = #{entity.type}
            </if>
            <if test="entity.isOpen != null and entity.isOpen != ''">
                AND drri.is_open = #{entity.type}
            </if>
        </where>
        order by main.create_time desc,drri.create_time
    </select>
    <select id="selectNowRepairByTicketId" resultType="java.util.Map">
        SELECT
            drr.repair_item_id,
            drt.item_name
        FROM
            dl_repair_records drr
                LEFT JOIN dl_repair_titem drt ON drr.repair_item_id = drt.id
        WHERE
            drr.ticket_id = #{ticketId}
          AND drr.repair_item_id IS NOT NULL
          AND drr.type = 'kssg'
          AND drr.deleted = '0'
        ORDER BY
            drr.create_time DESC
            LIMIT 1
    </select>

</mapper>
