<?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.interfaces.mapper.PtServiceApplyDao">
    <resultMap type="com.ruoyi.interfaces.domain.PtServiceApply" id="ptServiceApplyResultMap">
        <result property="id" column="ID" jdbcType="VARCHAR"/>
        <result property="unitName" column="UNIT_NAME" jdbcType="VARCHAR"/>
        <result property="appId" column="APP_ID" jdbcType="VARCHAR"/>
        <result property="srvId" column="SRV_ID" jdbcType="VARCHAR"/>
        <result property="applyTime" column="APPLY_TIME"/>
        <result property="applyExpire" column="APPLY_EXPIRE"/>
        <result property="applyNote" column="APPLY_NOTE" jdbcType="VARCHAR"/>
        <result property="applyState" column="APPLY_STATE" jdbcType="VARCHAR"/>
        <result property="approveTime" column="APPROVE_TIME"/>
        <result property="approveUser" column="APPROVE_USER" jdbcType="VARCHAR"/>
        <result property="approveNote" column="APPROVE_NOTE" jdbcType="VARCHAR"/>
        <result property="name" column="NAME" jdbcType="VARCHAR"/>
        <result property="userName" column="USERNAME" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="table_columns">
        ID
        ,
		UNIT_NAME,
		APP_ID,
		SRV_ID,
		APPLY_TIME,
		APPLY_EXPIRE,
		APPLY_NOTE,
		APPLY_STATE,
		APPROVE_TIME,
		APPROVE_USER,
		APPROVE_NOTE
    </sql>
    <sql id="entity_properties">
        #{id,jdbcType=VARCHAR}
        ,
        #{unitName,jdbcType=VARCHAR},
        #{appId,jdbcType=VARCHAR},
        #{srvId,jdbcType=VARCHAR},
        #{applyTime},
        #{applyExpire},
        #{applyNote,jdbcType=VARCHAR},
        #{applyState,jdbcType=VARCHAR},
        #{approveTime},
        #{approveUser,jdbcType=VARCHAR},
        #{approveNote,jdbcType=VARCHAR}
    </sql>


    <select id="getByAuth" resultMap="ptServiceApplyResultMap">
        SELECT
        <include refid="table_columns"/>
        FROM PT_SERVICE_APPLY
        WHERE APPLY_STATE = '1'
        AND now() &lt;= APPLY_EXPIRE + 1
        AND now() &gt;= APPLY_TIME
        AND SRV_ID = #{srvId}
        AND APP_ID LIKE CONCAT('%', CONCAT(#{appId}, '%'))
    </select>

    <insert id="insert" parameterType="com.ruoyi.interfaces.domain.PtServiceApply">
        insert into PT_SERVICE_APPLY(
        <include refid="table_columns"/>
        )
        values (
        <include refid="entity_properties"/>
        )
    </insert>

    <select id="selectByPrimaryKey" parameterType="com.ruoyi.interfaces.domain.PtServiceApply"
            resultMap="ptServiceApplyResultMap">
        SELECT A.*,B.NAME,D.USER_NAME AS USERNAME
        FROM PT_SERVICE_APPLY A
        JOIN PT_SERVICE B ON A.SRV_ID=B.SRV_ID
        LEFT JOIN PT_APP_USER D ON A.APPROVE_USER=D.USER_ID
        <where>
            <if test="approveUser != null and approveUser !=''">
                and A.APPROVE_USER like CONCAT('%', CONCAT(#{approveUser}, '%'))
            </if>
            <if test="applyState != null and applyState !=''">
                and A.APPLY_STATE = #{applyState,jdbcType=VARCHAR}
            </if>
            <if test="unitName != null and unitName !=''">
                and A.UNIT_NAME like CONCAT('%', CONCAT(#{unitName}, '%'))
            </if>
            <if test="userName != null and userName !=''">
                and D.USER_NAME like CONCAT('%', CONCAT(#{userName,jdbcType=VARCHAR}, '%'))
            </if>
            <if test="name != null and name !=''">
                and B.NAME like CONCAT('%', CONCAT(#{name}, '%'))
            </if>
        </where>
        order by A.APPLY_TIME desc
    </select>
    <select id="get" resultMap="ptServiceApplyResultMap">
        SELECT A.*, B.NAME, D.USER_NAME AS USERNAME
        FROM PT_SERVICE_APPLY A
                 JOIN PT_SERVICE B ON A.SRV_ID = B.SRV_ID
                 LEFT JOIN PT_APP_USER D ON A.APPROVE_USER = D.USER_ID
        WHERE A.ID = #{id}
    </select>

    <update id="updateByPrimaryKey" parameterType="com.ruoyi.interfaces.domain.PtServiceApply">
        update PT_SERVICE_APPLY
        <set>
            <if test="unitName != null and unitName !=''">
                UNIT_NAME = #{unitName,jdbcType=VARCHAR},
            </if>
            <if test="appId != null and appId !=''">
                APP_ID = #{appId,jdbcType=VARCHAR},
            </if>
            <if test="srvId != null and srvId !=''">
                SRV_ID = #{srvId,jdbcType=VARCHAR},
            </if>
            <if test="applyTime != null">
                APPLY_TIME = #{applyTime},
            </if>
            <if test="applyExpire != null">
                APPLY_EXPIRE = #{applyExpire},
            </if>
            <if test="applyNote != null and applyNote !=''">
                APPLY_NOTE = #{applyNote,jdbcType=VARCHAR},
            </if>
            <if test="applyState != null and applyState !=''">
                APPLY_STATE = #{applyState,jdbcType=VARCHAR},
            </if>
            <if test="approveTime != null">
                APPROVE_TIME = #{approveTime},
            </if>
            <if test="approveUser != null and approveUser !=''">
                APPROVE_USER = #{approveUser,jdbcType=VARCHAR},
            </if>
            <if test="approveNote != null and approveNote !=''">
                APPROVE_NOTE = #{approveNote,jdbcType=VARCHAR},
            </if>

        </set>
        where ID = #{id,jdbcType=VARCHAR}
    </update>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
        delete
        from PT_SERVICE_APPLY
        where ID = #{id,jdbcType=VARCHAR}
    </delete>

</mapper>
