<?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.bonus.face.mapper.FaceDataMapper">

    <resultMap type="com.bonus.face.domain.FaceDataEntity" id="FaceDataResult">
        <result property="faceId" column="face_id"/>
        <result property="groupCode" column="group_code"/>
        <result property="name" column="name"/>
        <result property="idCard" column="id_card"/>
        <result property="otherInfo" column="other_info"/>
        <result property="embedding" column="embedding"/>
        <result property="imagePath" column="image_path"/>
        <result property="createdAt" column="created_at"/>
        <result property="updatedAt" column="updated_at"/>
    </resultMap>

    <sql id="selectFaceDataVo">
        select face_id,
               group_code,
               name,
               id_card,
               other_info,
               embedding,
               image_path,
               created_at,
               updated_at
        from face_data
    </sql>

    <select id="selectFaceDataList" parameterType="com.bonus.face.domain.FaceDataEntity" resultMap="FaceDataResult">
        <include refid="selectFaceDataVo"/>
        <where>
            <if test="groupCode != null  and groupCode != ''">and group_code = #{groupCode}</if>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="idCard != null  and idCard != ''">and id_card  like concat('%', #{idCard}, '%')</if>
            <if test="otherInfo != null  and otherInfo != ''">and other_info = #{otherInfo}</if>
            <if test="embedding != null  and embedding != ''">and embedding = #{embedding}</if>
            <if test="imagePath != null  and imagePath != ''">and image_path = #{imagePath}</if>
            <if test="createdAt != null ">and created_at = #{createdAt}</if>
            <if test="updatedAt != null ">and updated_at = #{updatedAt}</if>
        </where>
    </select>

    <select id="selectFaceDataByFaceId" parameterType="Long" resultMap="FaceDataResult">
        <include refid="selectFaceDataVo"/>
        where face_id = #{faceId}
    </select>
    <select id="isGroupAndIdCardUnique" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM face_data
        WHERE group_code = #{groupCode} AND id_card =#{idCard}
        <if test="faceId != null and faceId != ''">
            AND face_id != #{faceId}
        </if>
    </select>

    <insert id="insertFaceData" parameterType="com.bonus.face.domain.FaceDataEntity" useGeneratedKeys="true"
            keyProperty="faceId">
        insert into face_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="groupCode != null">group_code,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="idCard != null and idCard != ''">id_card,</if>
            <if test="otherInfo != null">other_info,</if>
            <if test="embedding != null and embedding != ''">embedding,</if>
            <if test="imagePath != null">image_path,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="groupCode != null">#{groupCode},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="idCard != null and idCard != ''">#{idCard},</if>
            <if test="otherInfo != null">#{otherInfo},</if>
            <if test="embedding != null and embedding != ''">#{embedding},</if>
            <if test="imagePath != null">#{imagePath},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
        </trim>
    </insert>

    <update id="updateFaceData" parameterType="com.bonus.face.domain.FaceDataEntity">
        update face_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="groupCode != null">group_code = #{groupCode},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="idCard != null and idCard != ''">id_card = #{idCard},</if>
            <if test="otherInfo != null">other_info = #{otherInfo},</if>
            <if test="embedding != null and embedding != ''">embedding = #{embedding},</if>
            <if test="imagePath != null">image_path = #{imagePath},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
        </trim>
        where face_id = #{faceId}
    </update>

    <delete id="deleteFaceDataByFaceIds" parameterType="String">
        delete from face_data where face_id in
        <foreach item="faceId" collection="array" open="(" separator="," close=")">
            #{faceId}
        </foreach>
    </delete>
</mapper>