<?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.dgtly.sync.mapper.SyncFailDetailMapper">
    
    <resultMap type="SyncFailDetail" id="SyncFailDetailResult">
        <result property="packageId"    column="package_id"    />
        <result property="failReason"    column="fail_reason"    />
        <result property="dataJson"    column="data_json"    />
        <result property="failLevel"    column="fail_level"    />
        <result property="exceptionType"    column="exception_type"    />
    </resultMap>

    <sql id="selectSyncFailDetailVo">
        select package_id, fail_reason, data_json,fail_level,exception_type from sync_fail_detail
    </sql>

    <select id="selectSyncFailDetailList" parameterType="SyncFailDetail" resultMap="SyncFailDetailResult">
        <include refid="selectSyncFailDetailVo"/>
        <where>  
            <if test="packageId != null  and packageId != ''"> and package_id = #{packageId}</if>
            <if test="failReason != null  and failReason != ''"> and fail_reason like concat('%',  #{failReason},'%')</if>
            <if test="dataJson != null  and dataJson != ''"> and data_json like concat('%', #{dataJson}, '%') </if>
            <if test="failLevel != null  and failLevel != ''"> and fail_level = #{failLevel}</if>
            <if test="exceptionType != null  and exceptionType != ''"> and exception_type = #{exceptionType}</if>
        </where>
    </select>
    
    <select id="selectSyncFailDetailById" parameterType="String" resultMap="SyncFailDetailResult">
        <include refid="selectSyncFailDetailVo"/>
        where package_id = #{packageId}
    </select>
        
    <insert id="insertSyncFailDetail" parameterType="SyncFailDetail">
        insert into sync_fail_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="packageId != null  and packageId != ''">package_id,</if>
            <if test="failReason != null  and failReason != ''">fail_reason,</if>
            <if test="dataJson != null  and dataJson != ''">data_json,</if>
            <if test="failLevel != null  and failLevel != ''">fail_level ,</if>
            <if test="exceptionType != null  and exceptionType != ''">exception_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="packageId != null  and packageId != ''">#{packageId},</if>
            <if test="failReason != null  and failReason != ''">#{failReason},</if>
            <if test="dataJson != null  and dataJson != ''">#{dataJson},</if>
            <if test="failLevel != null  and failLevel != ''">#{failLevel},</if>
            <if test="exceptionType != null  and exceptionType != ''">#{exceptionType},</if>
         </trim>
    </insert>

    <update id="updateSyncFailDetail" parameterType="SyncFailDetail">
        update sync_fail_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="failReason != null  and failReason != ''">fail_reason = #{failReason},</if>
            <if test="dataJson != null  and dataJson != ''">data_json = #{dataJson},</if>
            <if test="failLevel != null  and failLevel != ''">fail_level = #{failLevel},</if>
            <if test="exceptionType != null  and exceptionType != ''">exception_type = #{exceptionType},</if>
        </trim>
        where package_id = #{packageId}
    </update>

    <delete id="deleteSyncFailDetailById" parameterType="String">
        delete from sync_fail_detail where package_id = #{packageId}
    </delete>

    <delete id="deleteSyncFailDetailByIds" parameterType="String">
        delete from sync_fail_detail where package_id in 
        <foreach item="packageId" collection="array" open="(" separator="," close=")">
            #{packageId}
        </foreach>
    </delete>

    <insert id="batchInsertSyncFailDetail" parameterType="SyncFailDetail">
        insert into sync_fail_detail (package_id,fail_reason,data_json,fail_level,exception_type)
        values
        <foreach item="obj" collection="failDetails"  separator=",">
            (
                #{obj.packageId},
                #{obj.failReason},
                #{obj.dataJson},
                #{obj.failLevel},
                #{obj.exceptionType}
            )
        </foreach>
    </insert>

    <select id="selectSyncExceptionTypeList" parameterType="String" resultType="String">
        select exception_type
        from sync_fail_detail
        where package_id = #{packageId}
        group by exception_type
    </select>


</mapper>