<?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.cpop.core.mapper.CoreMapper">

    <!--插入操作日志-->
    <insert id="insertOperationLog">
        INSERT INTO cp_sys_operation_log
        (id, operation_user_id, operation_user_name, operation_user_type, code, info, dev_ip, result, level, action_type, fail_reason, description,
         create_time, update_time, create_user_id, update_user_id)
        VALUES
        (#{id}, #{operationUserId}, #{operationUserName}, #{operationUserType}, #{code}, #{info}, #{devIp}, #{result}, #{level}, #{actionType}, #{failReason}, #{description},
         now(), now(), #{createUserId}, #{updateUserId})
    </insert>

    <!--更新系统用户登录地址-->
    <update id="updateSysUserLoginIp">
        UPDATE
            cp_sys_user
        SET
            login_ip = #{ipAddr}
        WHERE
            user_name = #{username}
        <if test="username != 'Cpop'">
            AND user_type = #{userType}
        </if>
    </update>

    <!--根据用户名获取用户信息-->
    <select id="getSysUserByUsername" resultType="com.cpop.core.base.table.SysUser">
        SELECT
            id,
            user_name AS userName,
            password,
            nick_name AS nickName,
            email,
            phone_number AS phoneNumber,
            sex,
            avatar,
            salt,
            status,
            login_ip AS loginIp,
            user_type AS userType,
            create_time AS createTime,
            create_user_id AS createUserId,
            update_time AS updateTime,
            update_user_id AS updateUserId
        FROM
            cp_sys_user
        WHERE
            is_delete = 0
        AND
            user_name = #{username}
        <if test="username != 'Cpop'">
            AND user_type = #{userType}
        </if>
    </select>

    <!--根据用户名获取用户信息-->
    <select id="getSysUserByPhone" resultType="com.cpop.core.base.table.SysUser">
        SELECT
        id,
        user_name AS userName,
        password,
        nick_name AS nickName,
        email,
        phone_number AS phoneNumber,
        sex,
        avatar,
        salt,
        status,
        login_ip AS loginIp,
        user_type AS userType,
        create_time AS createTime,
        create_user_id AS createUserId,
        update_time AS updateTime,
        update_user_id AS updateUserId
        FROM
        cp_sys_user
        WHERE
        is_delete = 0
        AND phone_number = #{phoneNumber}
        AND user_type = #{userType}
    </select>

    <!--加载参数缓存数据-->
    <select id="loadingConfigCache" resultType="com.cpop.core.base.table.SysConfig">
        SELECT
            config_key AS configKey,
            config_name AS configName,
            config_value AS configValue,
            config_type AS configType,
            remarks,
            create_time AS createTime,
            create_user_id AS createUserId,
            update_time AS updateTime,
            update_user_id AS updateUserId
        FROM
            cp_sys_config
    </select>

    <!--根据键名查询参数配置信息-->
    <select id="selectConfigByKey" resultType="com.cpop.core.base.table.SysConfig">
        SELECT
            config_key AS configKey,
            config_name AS configName,
            config_value AS configValue,
            config_type AS configType,
            remarks,
            create_time AS createTime,
            create_user_id AS createUserId,
            update_time AS updateTime,
            update_user_id AS updateUserId
        FROM
            cp_sys_config
        WHERE
            config_key = #{configKey}
    </select>

    <!--新增参数配置-->
    <insert id="insertConfig">
        INSERT INTO
            cp_sys_config
        (config_key, config_name, config_value, config_type, remarks, create_time, create_user_id, update_time, update_user_id)
        VALUES
            (#{configKey}, #{configName}, #{configValue}, #{configType}, #{remarks}, now(), #{createUserId}, now(), #{updateUserId})
    </insert>

    <!--修改参数配置-->
    <update id="updateConfig">
        UPDATE cp_sys_config
        SET
        <if test="configName != null and configName != ''">
            config_name = #{configName},
        </if>
        <if test="configValue != null and configValue != ''">
            config_value = #{configValue},
        </if>
        <if test="configType != null and configType != ''">
            config_type = #{configType},
        </if>
        <if test="remarks != null and remarks != ''">
            remarks = #{remarks},
        </if>
        <if test="updateUserId != null and updateUserId != ''">
            update_user_id = #{updateUserId},
        </if>
        update_time = now()
        WHERE
        config_key = #{configKey}
    </update>

    <!--根据键查询配置-->
    <select id="selectConfigByKeys" resultType="com.cpop.core.base.table.SysConfig">
        SELECT
        config_key AS configKey,
        config_name AS configName,
        config_value AS configValue,
        config_type AS configType,
        remarks,
        create_time AS createTime,
        create_user_id AS createUserId,
        update_time AS updateTime,
        update_user_id AS updateUserId
        FROM
        cp_sys_config
        WHERE
        config_key IN
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <!--需要删除的参数ID-->
    <delete id="deleteConfigByKeys">
        DELETE FROM cp_sys_config
        WHERE
        config_key IN
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!--新增系统用户-->
    <insert id="insertSysUser">
        INSERT INTO cp_sys_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null and id != ''">
                id,
            </if>
            <if test="userName != null and userName != ''">
                user_name,
            </if>
            <if test="password != null and password != ''">
                password,
            </if>
            <if test="rsaPassword != null and rsaPassword != ''">
                rsa_password,
            </if>
            <if test="nickName != null and nickName != ''">
                nick_name,
            </if>
            <if test="email != null and email != ''">
                email,
            </if>
            <if test="phoneNumber != null and phoneNumber != ''">
                phone_number,
            </if>
            <if test="sex != null">
                sex,
            </if>
            <if test="avatar != null and avatar != ''">
                avatar,
            </if>
            <if test="salt != null and salt != ''">
                salt,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="loginIp != null and loginIp != ''">
                dev_ip,
            </if>
            <if test="userType != null">
                user_type,
            </if>
            <if test="createUserId != null and createUserId != ''">
                create_user_id,
            </if>
            <if test="updateUserId != null and updateUserId != ''">
                update_user_id,
            </if>
            create_time,
            update_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null and id != ''">
                #{id},
            </if>
            <if test="userName != null and userName != ''">
                #{userName},
            </if>
            <if test="password != null and password != ''">
                #{password},
            </if>
            <if test="rsaPassword != null and rsaPassword != ''">
                #{rsaPassword},
            </if>
            <if test="nickName != null and nickName != ''">
                #{nickName},
            </if>
            <if test="email != null and email != ''">
                #{email},
            </if>
            <if test="phoneNumber != null and phoneNumber != ''">
                #{phoneNumber},
            </if>
            <if test="sex != null">
                #{sex},
            </if>
            <if test="avatar != null and avatar != ''">
                #{avatar},
            </if>
            <if test="salt != null and salt != ''">
                #{salt},
            </if>
            <if test="status != null">
                #{status},
            </if>
            <if test="loginIp != null and loginIp != ''">
                #{loginIp},
            </if>
            <if test="userType != null">
                #{userType},
            </if>
            <if test="createUserId != null and createUserId != ''">
                #{createUserId},
            </if>
            <if test="updateUserId != null and updateUserId != ''">
                #{updateUserId},
            </if>
            now(),
            now(),
        </trim>
    </insert>

    <!--修改系统用户-->
    <update id="updateSysUser">
        UPDATE cp_sys_user
        <set>
            <if test="userName != null and userName != ''">
                user_name = #{userName},
            </if>
            <if test="password != null and password != ''">
                password = #{password},
            </if>
            <if test="rsaPassword != null and rsaPassword != ''">
                rsa_password = #{rsaPassword},
            </if>
            <if test="nickName != null and nickName != ''">
                nick_name = #{nickName},
            </if>
            <if test="email != null and email != ''">
                email = #{email},
            </if>
            <if test="phoneNumber != null and phoneNumber != ''">
                phone_number = #{phoneNumber},
            </if>
            <if test="sex != null">
                sex = #{sex},
            </if>
            <if test="avatar != null and avatar != ''">
                avatar = #{avatar},
            </if>
            <if test="salt != null and salt != ''">
                salt = #{salt},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="loginIp != null and loginIp != ''">
                dev_ip = #{loginIp},
            </if>
            <if test="updateUserId != null and updateUserId != ''">
                update_user_id = #{updateUserId},
            </if>
            update_time = now(),
        </set>
        WHERE
        is_delete = 0
        AND id = #{id}
    </update>

    <!--根据主键删除用户(逻辑删除)-->
    <update id="removeSysUserById">
        UPDATE cp_sys_user
        SET
            is_delete = 1
        WHERE
            id = #{id}
    </update>
</mapper>
