<?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.roadm.mapper.LevelAuditMapper">
    <resultMap id="levelAuditConfig" type="cn.roadm.bo.levelAudit.LevelAuditConfigDO">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="key" column="config_key"/>
        <result property="flag" column="flag"/>
        <result property="level" column="level"/>
    </resultMap>

    <resultMap id="levelAuditMap" type="cn.roadm.bo.levelAudit.LevelAuditRecordDO">
        <id property="id" column="id"/>
        <result property="spId" column="sp_id"/>
        <result property="uid" column="uid"/>
        <result property="step" column="step"/>
        <result property="status" column="status"/>
        <result property="reason" column="reason"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="name"/>
        <result property="auditSource" column="audit_source"/>
        <result property="extendMessage" column="extend_message"/>
        <result property="extendObject" column="extend_object"/>
        <result property="subId" column="subId"/>
    </resultMap>

    <select id="selectSystemConfigMapByKey" resultType="java.lang.String">
        select value
        from sms_system_config
        where config_key = #{configKey}
    </select>

    <select id="selectLevelAuditConfigByKey" resultMap="levelAuditConfig">
        select id, name, config_key, flag, level
        from sp_service_audit_config
        where config_key = #{key}
    </select>

    <select id="listPageSelectLevelAuditInfo" resultMap="levelAuditMap">
        select au.id,
               au.sp_id,
               au.uid,
               au.step,
               au.status,
               au.reason,
               DATE_FORMAT(au.create_time, '%Y-%m-%d %H:%i:%s') as create_time,
               au.update_time,
               au.audit_source,
               au.extend_message,
               au.subId,
               au.extend_object
        from sp_service_audit au
        <where>
            au.status = 0
              and au.step = #{auditLevel}
            <if test="spId != null and spId != ''">
                and au.sp_id = #{spId}
            </if>
            <if test="auditSource != null">
                and au.audit_source = #{auditSource}
            </if>
        </where>
        order by au.create_time desc
    </select>

    <select id="getLevelAuditRecordById" resultMap="levelAuditMap">
        select au.id,
               au.sp_id,
               au.uid,
               au.step,
               au.status,
               au.reason,
               au.create_time,
               au.update_time,
               au.subId,
               au.extend_object
        from sp_service_audit au
        where id = #{id}
    </select>

    <update id="updateSpAuditStatus">
        update sp_service_audit
        set status      = #{status},
            uid         = #{uid},
            update_time = now(),
            reason      = #{reason}
        where id = #{id}
    </update>


    <update id="updateNextLevelAuditReady">
        update sp_service_audit
        set status = 0
        where sp_id = #{spId}
          and step = #{step}
          and status = -1
          and audit_source = #{auditSource}
    </update>

    <select id="listQuerySpAuditHistory" resultMap="levelAuditMap">
        select sa.id,
               sa.uid,
               sa.step,
               sa.`status`,
               sa.reason,
               DATE_FORMAT(sa.create_time, '%Y-%m-%d %H:%i:%s') as create_time,
               DATE_FORMAT(sa.update_time, '%Y-%m-%d %H:%i:%s') as update_time,
               us.user_name,
               us.name
        from sp_service_audit sa
                 left join sms_user_info us on sa.uid = us.id
        <where>
            sa.sp_id = #{spId}
          and audit_source = #{auditSourceType}
            <if test="subId != null">
                and find_in_set(#{subId}, sa.subId)
            </if>
        </where>
        order by  sa.create_time desc
        limit 100
    </select>

    <select id="selectThisLevelUsers" resultType="cn.roadm.bo.PageSelectOptionsVO">
        select id as value, user_name as label
        from sms_user_info
        where audit_level = #{level}
    </select>

    <update id="updateEveryLevelAuditUser">
        update sp_service_audit
        set uid = #{uid}
        where sp_id = #{spId}
          and step = #{step}
          and status = -1
          and audit_source = #{auditSource}
    </update>

    <update id="updateFirstLevelStatusReady">
        update sp_service_audit
        set status = 0
        where sp_id = #{spId}
          and step = #{firstKey}
          and status = -1
    </update>

    <update id="updateLevelAuditToReadyStatus">
        update sp_service_audit
        set status      = 0,
            reason      = null,
            update_time = null
        where id = #{id}
    </update>

    <select id="queryLevelAuditConfigCountByIdAndUserId" resultType="int">
		select count(1) from sp_service_audit where id=#{id} and uid=#{userId} limit 1
    </select>

    <insert id="addNextLevelAuditRecord">
        insert into sp_service_audit (sp_id, step, status, create_time, audit_source, extend_message, subId, extend_object)
        values (#{spId}, #{step}, #{status}, now(), #{auditSource}, #{extendMessage}, #{subId}, #{extendObject})
    </insert>

    <select id="queryDownloadRecordDetail" resultType="cn.roadm.bo.levelAudit.convert.DownloadCenterConvertDO">
        select id, operate_type as exportType, create_time as startTime, result_time as CompletionTime, `count` as dataCount, export_condition as exportCondition  from sms_excel_file where id = #{id}
    </select>
</mapper>