<?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.project.ticket.mapper.TPermitRelationMapper">

    <resultMap type="PermitRelation" id="PermitRelationResult">
        <result property="id" column="id"/>
        <result property="aId" column="aId"/>
        <result property="cId" column="cId"/>
        <result property="bpId" column="bpId"/>
        <result property="bWorkContent" column="bWorkContent"/>
        <result property="cbWorkContent" column="cbWorkContent"/>
        <result property="hVNoOne" column="hVNoOne"/>
        <result property="chVNoOne" column="chVNoOne"/>
        <result property="permitId" column="permit_id"/>
        <result property="permitType" column="permit_type"/>
        <result property="permitStatus" column="permit_status"/>
    </resultMap>

    <sql id="selectPermitRelationVo">
        SELECT t.id,
               a.a_id           AS aId,
               a.word_url           AS wordUrl,
               a.pdf_url           AS pdfUrl,
               c.c_id           AS cId,
               bp.BP_ID         AS bpId,
               a.B_WORK_CONTENT AS bWorkContent,
               c.B_WORK_CONTENT AS cbWorkContent,
               a.H_V_NO_ONE     AS hVNoOne,
               t.PERMIT_TYPE,
               t.PERMIT_STATUS
        FROM T_PERMIT_RELATION t
                 LEFT JOIN T_HAZARD_WORK_PERMIT a ON t.PERMIT_ID = a.A_ID
            AND t.PERMIT_TYPE = 'A'
                 LEFT JOIN T_RESTRICTED_SPACE_PERMIT c ON t.PERMIT_ID = c.C_ID
            AND t.PERMIT_TYPE = 'C'
            LEFT JOIN T_PERMIT_BLIND_PLATE bp ON t.PERMIT_ID = bp.BP_ID
            AND t.PERMIT_TYPE = 'BP'
    </sql>

    <select id="selectPermitRelations" parameterType="PermitRelation" resultMap="PermitRelationResult">
        <include refid="selectPermitRelationVo"/>
    </select>

    <insert id="insertPermitRelation" parameterType="PermitRelation">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_permit_relation.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into T_PERMIT_RELATION
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="permitId != null">permit_id,</if>
            <if test="permitType != null">permit_type,</if>
            <if test="permitStatus != null">permit_status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="permitId != null">#{permitId},</if>
            <if test="permitType != null">#{permitType},</if>
            <if test="permitStatus != null">#{permitStatus},</if>
        </trim>
    </insert>

</mapper>
