<?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="ink.wgink.module.wechat.dao.miniapp.IMiniAppUserDao">

    <cache flushInterval="3600000"/>

    <resultMap id="miniappUserPO" type="ink.wgink.module.wechat.pojo.pos.miniapp.MiniappUserPO">
        <result column="app_id" property="appId"/>
        <result column="open_id" property="openId"/>
        <result column="user_id" property="userId"/>
        <result column="user_code" property="userCode"/>
        <result column="nick_name" property="nickName"/>
        <result column="avatar_url" property="avatarUrl"/>
        <result column="is_init_account" property="isInitAccount"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `wechat_mini_app_user` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `app_id` varchar(255) DEFAULT NULL COMMENT 'appid',
            `open_id` varchar(255) DEFAULT NULL COMMENT 'openid',
            `user_id` varchar(255) DEFAULT NULL COMMENT '用户ID',
            `user_code` varchar(255) DEFAULT NULL COMMENT '用户码',
            `nick_name` varchar(255) DEFAULT NULL COMMENT '微信昵称',
            `avatar_url` varchar(500) DEFAULT NULL COMMENT '微信头像',
            `is_init_account` int(1) DEFAULT '0' COMMENT '是否初始化账户',
            `gmt_create` datetime DEFAULT NULL,
            PRIMARY KEY (`id`),
            KEY `app_id` (`app_id`) USING BTREE,
            KEY `open_id` (`open_id`) USING BTREE,
            KEY `user_id` (`user_id`) USING BTREE,
            KEY `user_code` (`user_code`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='小程序用户';
    </update>

    <!-- 保存 -->
    <insert id="save" parameterType="map" flushCache="true" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        INSERT INTO wechat_mini_app_user(
            app_id,
            open_id,
            user_id,
            user_code,
            is_init_account,
            gmt_create
        ) VALUES(
            #{appId},
            #{openId},
            #{userId},
            #{userCode},
            #{isInitAccount},
            #{gmtCreate}
        )
    </insert>

    <!-- 删除用户 -->
    <delete id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            wechat_mini_app_user
        WHERE
        <if test="userIds != null and userIds.size > 0">
            user_id IN
            <foreach collection="userIds" index="index" open="(" separator="," close=")">
                #{userIds[${index}]}
            </foreach>
        </if>
    </delete>

    <!-- 更新信息 -->
    <update id="updateInfo" parameterType="map" flushCache="true">
        UPDATE
            wechat_mini_app_user
        SET
            nick_name = #{nickName},
        avatar_url = #{avatarUrl}
        WHERE
        <if test="userId != null and userId != ''">
            user_id = #{userId}
        </if>
    </update>

    <!-- 更新是否初始化账号 -->
    <update id="updateIsInitAccount" parameterType="map" flushCache="true">
        UPDATE
            wechat_mini_app_user
        SET
            is_init_account = #{isInitAccount}
        WHERE
        <if test="userId != null and userId != ''">
            user_id = #{userId}
        </if>
    </update>

    <!-- 更新用户码 -->
    <update id="updateUserCode" parameterType="map" flushCache="true">
        UPDATE
            wechat_mini_app_user
        SET
            user_code = #{userCode}
        WHERE
            app_id = #{appId}
        AND
            open_id = #{openId}
    </update>

    <!-- 列表 -->
    <select id="listPO" parameterType="map" resultMap="miniappUserPO" useCache="true">
        SELECT
            app_id,
            open_id,
            user_id,
            user_code,
            nick_name,
            avatar_url,
            is_init_account,
            gmt_create
        FROM
            wechat_mini_app_user
        <where>
            <if test="userIds != null and userIds.size > 0">
            user_id IN
                <foreach collection="userIds" index="index" open="(" separator="," close=")">
                #{userIds[${index}]}
                </foreach>
            </if>
            <if test="userCodes != null and userCodes.size > 0">
        AND
            user_code IN
                <foreach collection="userCodes" index="index" open="(" separator="," close=")">
                #{userCodes[${index}]}
                </foreach>
            </if>
        </where>
    </select>

    <!-- 详情 -->
    <select id="getPO" parameterType="map" resultMap="miniappUserPO" useCache="true">
        SELECT
            app_id,
            open_id,
            user_id,
            user_code,
            nick_name,
            avatar_url,
            is_init_account,
            gmt_create
        FROM
            wechat_mini_app_user
        <where>
            <if test="appId != null and appId != ''">
            app_id = #{appId}
            </if>
            <if test="openId != null and openId != ''">
        AND
            open_id = #{openId}
            </if>
            <if test="userId != null and userId != ''">
        AND
            user_id = #{userId}
            </if>
            <if test="userCode != null and userCode != ''">
        AND
            user_code = #{userCode}
            </if>
        </where>
    </select>

</mapper>