<?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.FaceGroupsMapper">

    <resultMap type="com.bonus.face.domain.FaceGroupsEntity" id="FaceGroupsResult">
        <result property="groupId"    column="group_id"    />
        <result property="groupCode"    column="group_code"    />
        <result property="groupName"    column="group_name"    />
        <result property="description"    column="description"    />
        <result property="createdAt"    column="created_at"    />
    </resultMap>

    <sql id="selectFaceGroupsVo">
        select group_id, group_code, group_name, description, created_at from face_groups
    </sql>

    <select id="selectFaceGroupsList" parameterType="com.bonus.face.domain.FaceGroupsEntity" resultMap="FaceGroupsResult">
        <include refid="selectFaceGroupsVo"/>
        <where>
            <if test="groupCode != null  and groupCode != ''">and group_code like concat('%', #{groupCode}, '%')</if>
            <if test="groupName != null  and groupName != ''">and group_name like concat('%', #{groupName}, '%')</if>
            <if test="description != null  and description != ''">and description = #{description}</if>
            <if test="createdAt != null ">and created_at = #{createdAt}</if>
        </where>
    </select>

    <select id="selectFaceGroupsByGroupId" parameterType="Long" resultMap="FaceGroupsResult">
        <include refid="selectFaceGroupsVo"/>
        where group_id = #{groupId}
    </select>
    <select id="isGroupNameUnique" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM face_groups
        WHERE group_name = #{groupName}
        <if test="groupId != null and groupId != ''">
            AND group_id != #{groupId}
        </if>
    </select>

    <insert id="insertFaceGroups" parameterType="com.bonus.face.domain.FaceGroupsEntity" useGeneratedKeys="true" keyProperty="groupId">
        insert into face_groups
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="groupCode != null and groupCode != ''">group_code,</if>
            <if test="groupName != null and groupName != ''">group_name,</if>
            <if test="description != null">description,</if>
            <if test="createdAt != null">created_at,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="groupCode != null and groupCode != ''">#{groupCode},</if>
            <if test="groupName != null and groupName != ''">#{groupName},</if>
            <if test="description != null">#{description},</if>
            <if test="createdAt != null">#{createdAt},</if>
        </trim>
    </insert>

    <update id="updateFaceGroups" parameterType="com.bonus.face.domain.FaceGroupsEntity">
        update face_groups
        <trim prefix="SET" suffixOverrides=",">
            <if test="groupCode != null and groupCode != ''">group_code = #{groupCode},</if>
            <if test="groupName != null and groupName != ''">group_name = #{groupName},</if>
            <if test="description != null">description = #{description},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
        </trim>
        where group_id = #{groupId}
    </update>

    <delete id="deleteFaceGroupsByGroupIds" parameterType="String">
        delete from face_groups where group_id in
        <foreach item="groupId" collection="array" open="(" separator="," close=")">
            #{groupId}
        </foreach>
    </delete>

    <delete id="deleteFaceDataByGroupCode" parameterType="String">
        delete from face_data where group_code = #{groupCode}
    </delete>
</mapper>