<?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.ruoyi.jarvis.mapper.SuperAdminMapper">

    <resultMap type="SuperAdmin" id="SuperAdminResult">
        <result property="id" column="id"/>
        <result property="wxid" column="wxid"/>
        <result property="name" column="name"/>
        <result property="unionId" column="union_id"/>
        <result property="appKey" column="app_key"/>
        <result property="secretKey" column="secret_key"/>
        <result property="isActive" column="is_active"/>
        <result property="isCount" column="is_count"/>
        <result property="touser" column="touser"/>
        <result property="createdAt" column="created_at"/>
        <result property="updatedAt" column="updated_at"/>
    </resultMap>

    <sql id="selectSuperAdminVo">
        select id, wxid, name, union_id, app_key, secret_key, is_active, is_count, touser, created_at, updated_at from super_admin
    </sql>

    <select id="selectSuperAdminList" parameterType="SuperAdmin" resultMap="SuperAdminResult">
        <include refid="selectSuperAdminVo"/>
        <where>
            <if test="wxid != null  and wxid != ''"> and wxid = #{wxid}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="unionId != null"> and union_id = #{unionId}</if>
            <if test="appKey != null  and appKey != ''"> and app_key = #{appKey}</if>
            <if test="secretKey != null  and secretKey != ''"> and secret_key = #{secretKey}</if>
            <if test="isActive != null "> and is_active = #{isActive}</if>
        </where>
        order by created_at desc
    </select>

    <select id="selectSuperAdminById" parameterType="Long" resultMap="SuperAdminResult">
        <include refid="selectSuperAdminVo"/>
        where id = #{id}
    </select>

    <select id="selectSuperAdminByUnionId" parameterType="Long" resultMap="SuperAdminResult">
        <include refid="selectSuperAdminVo"/>
        where union_id = #{unionId}
    </select>

    <!-- 企微 UserID：可写在 wxid，或出现在 touser（逗号分隔的接收人 UserID）中 -->
    <select id="selectSuperAdminByWecomUserId" parameterType="String" resultMap="SuperAdminResult">
        <include refid="selectSuperAdminVo"/>
        where (is_active is null or is_active = 1)
        and (
            wxid = #{wxid}
            or CONCAT(',', REPLACE(REPLACE(IFNULL(touser,''),' ',''),'，',','), ',') like CONCAT('%,', #{wxid}, ',%')
        )
        order by id asc
        limit 1
    </select>

    <insert id="insertSuperAdmin" parameterType="SuperAdmin" useGeneratedKeys="true" keyProperty="id">
        insert into super_admin
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="wxid != null and wxid != ''">wxid,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="unionId != null">union_id,</if>
            <if test="appKey != null and appKey != ''">app_key,</if>
            <if test="secretKey != null and secretKey != ''">secret_key,</if>
            <if test="isActive != null">is_active,</if>
            <if test="isCount != null">is_count,</if>
            <if test="touser != null and touser != ''">touser,</if>
            created_at,
            updated_at,
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="wxid != null and wxid != ''">#{wxid},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="unionId != null">#{unionId},</if>
            <if test="appKey != null and appKey != ''">#{appKey},</if>
            <if test="secretKey != null and secretKey != ''">#{secretKey},</if>
            <if test="isActive != null">#{isActive},</if>
            <if test="isCount != null">#{isCount},</if>
            <if test="touser != null and touser != ''">#{touser},</if>
            now(),
            now(),
         </trim>
    </insert>

    <update id="updateSuperAdmin" parameterType="SuperAdmin">
        update super_admin
        <trim prefix="SET" suffixOverrides=",">
            <if test="wxid != null and wxid != ''">wxid = #{wxid},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="unionId != null">union_id = #{unionId},</if>
            <if test="appKey != null and appKey != ''">app_key = #{appKey},</if>
            <if test="secretKey != null and secretKey != ''">secret_key = #{secretKey},</if>
            <if test="isActive != null">is_active = #{isActive},</if>
            <if test="isCount != null">is_count = #{isCount},</if>
            <if test="touser != null">touser = #{touser},</if>
            updated_at = now(),
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSuperAdminById" parameterType="Long">
        delete from super_admin where id = #{id}
    </delete>

    <delete id="deleteSuperAdminByIds" parameterType="Long">
        delete from super_admin where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
