<?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.qmth.themis.business.dao.TOeFaceVerifyHistoryMapper">

    <select id="faceCountError" resultType="java.util.Map">
        select
            sum(t.tmpCount) as tmpCount,
            group_concat(t.photoUrl) as photoUrls,
            min(t.createTime) as createTime
        from
            (
            select
                count(1) as tmpCount,
                group_concat(tofvh.photo_url) as photoUrl,
                min(tofvh.create_time) as createTime
            from
                t_oe_face_verify_history tofvh
            <where>
                <if test="recordId != null and recordId != ''">
                    and tofvh.exam_record_id = #{recordId}
                </if>
                <if test="exception != null and exception != ''">
                    and tofvh.`exception` = #{exception}
                </if>
                <if test="type != null and type != ''">
                    and tofvh.`type` = #{type}
                </if>
                <if test="batchNo != null and batchNo != ''">
                    and tofvh.batch_no = #{batchNo}
                </if>
                <choose>
                    <when test="multipleFace == true">
                        and tofvh.face_count <![CDATA[ > ]]> 1
                    </when>
                    <otherwise>
                        and tofvh.face_count = 0
                    </otherwise>
                </choose>
            </where>
            group by tofvh.`exception`
        union all
            select
                count(1) as tmpCount,
                group_concat(t.photoUrl),
                min(t.create_time) as createTime
            from
                (
                select
                    CONVERT((tolvh.actions->>'$.faceCount')
                        USING utf8) as faceCount, tolvh.`exception`,
                    CONVERT((tolvh.actions->>'$.photoUrl')
                        USING utf8) as photoUrl,tolvh.create_time
                from
                (
                select
                tolvh.actions->>'$[0]' as actions,
                tolvh.`exception`,
                tolvh.create_time
                from
                t_oe_liveness_verify_history tolvh
                <where>
                    <if test="recordId != null and recordId != ''">
                        and tolvh.exam_record_id = #{recordId}
                    </if>
                    <if test="exception != null and exception != ''">
                        and tolvh.`exception` = #{exception}
                    </if>
                    <if test="type != null and type != ''">
                        and tolvh.`type` = #{type}
                    </if>
                    <if test="batchNo != null and batchNo != ''">
                        and tolvh.batch_no = #{batchNo}
                    </if>
                </where>
                ) tolvh
                 ) t
            <where>
                <choose>
                    <when test="multipleFace == true">
                        and t.faceCount <![CDATA[ > ]]> 1
                    </when>
                    <otherwise>
                        and t.faceCount = 0
                    </otherwise>
                </choose>
            </where>
        group by t.`exception` ) t
    </select>

    <select id="faceCompareError" resultType="java.util.Map">
        select
            sum(t.tmpCount) as tmpCount,
            group_concat(t.photoUrl) as photoUrls,
            min(t.createTime) as createTime
        from
            (
            select
                count(1) as tmpCount,
                group_concat(tofvh.photo_url) as photoUrl,
                min(tofvh.create_time) as createTime
            from
                t_oe_face_verify_history tofvh
            <where>
                <if test="recordId != null and recordId != ''">
                    and tofvh.exam_record_id = #{recordId}
                </if>
                <if test="exception != null and exception != ''">
                    and tofvh.`exception` = #{exception}
                </if>
                <if test="type != null and type != ''">
                    and tofvh.`type` = #{type}
                </if>
                <if test="batchNo != null and batchNo != ''">
                    and tofvh.batch_no = #{batchNo}
                </if>
            </where>
            group by tofvh.`exception`
        union all
                select
                count(1) as tmpCount,
                group_concat(t.photoUrl),
                min(t.create_time) as createTime
                from
                (
                select
                CONVERT((tolvh.actions->>'$.faceCount')
                USING utf8) as faceCount, tolvh.`exception`,
                CONVERT((tolvh.actions->>'$.photoUrl')
                USING utf8) as photoUrl,tolvh.create_time
                from
                (
                select
                tolvh.actions->>'$[0]' as actions,
                tolvh.`exception`,
                tolvh.create_time
                from
                t_oe_liveness_verify_history tolvh

                <where>
                    <if test="recordId != null and recordId != ''">
                        and tolvh.exam_record_id = #{recordId}
                    </if>
                    <if test="exception != null and exception != ''">
                        and tolvh.`exception` = #{exception}
                    </if>
                    <if test="type != null and type != ''">
                        and tolvh.`type` = #{type}
                    </if>
                    <if test="batchNo != null and batchNo != ''">
                        and tolvh.batch_no = #{batchNo}
                    </if>
                </where>
            ) tolvh
        ) t
            group by t.`exception`) t
    </select>

    <select id="eyeCloseError" resultType="java.util.Map">
        select
        sum(t.tmpCount) as tmpCount,
        group_concat(t.photoUrl) as photoUrls,
        min(t.createTime) as createTime
        from
        (
        select
        count(1) as tmpCount,
        group_concat(tofvh.photo_url) as photoUrl,
        min(tofvh.create_time) as createTime
        from
        t_oe_face_verify_history tofvh
        <where>
            <if test="recordId != null and recordId != ''">
                and tofvh.exam_record_id = #{recordId}
            </if>
            <if test="exception != null and exception != ''">
                and tofvh.`exception` = #{exception}
            </if>
            <if test="type != null and type != ''">
                and tofvh.`type` = #{type}
            </if>
            <if test="batchNo != null and batchNo != ''">
                and tofvh.batch_no = #{batchNo}
            </if>
        </where>
        group by tofvh.`exception`) t
    </select>

    <select id="realnessError" resultType="java.util.Map">
        select
        sum(t.tmpCount) as tmpCount,
        group_concat(t.photoUrl) as photoUrls,
        min(t.createTime) as createTime
        from
        (
        select
        count(1) as tmpCount,
        group_concat(tofvh.photo_url) as photoUrl,
        min(tofvh.create_time) as createTime
        from
        t_oe_face_verify_history tofvh
        <where>
            <if test="recordId != null and recordId != ''">
                and tofvh.exam_record_id = #{recordId}
            </if>
            <if test="exception != null and exception != ''">
                and tofvh.`exception` = #{exception}
            </if>
            <if test="type != null and type != ''">
                and tofvh.`type` = #{type}
            </if>
            <if test="batchNo != null and batchNo != ''">
                and tofvh.batch_no = #{batchNo}
            </if>
        </where>
        group by tofvh.`exception`) t
    </select>
</mapper>
