<?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.css.simulation.resource.server.infra.db.mysql.mapper.ParameterMapper" >


    <!--查询参数列表-->
    <select id="getParameterList" parameterType="api.common.pojo.param.system.ParameterParam" resultType="api.common.pojo.vo.system.ParameterVO">
        select
        p.id,
        u.id as user_id,
        u.username,
        IFNULL(p.num_create_user,0) as num_create_user,
        IFNULL(p.num_create_scene_package,0) as num_create_scene_package,
        IFNULL(p.num_scene_per_package,0) as  num_scene_per_package,
        IFNULL(p.create_time,u.modify_time) as create_time
        from system_user u
        left join system_parameter p on u.id = p.user_id
        where
            u.is_deleted = '0' and u.visible = '1' and u.role_code = '2'
            and IFNULL(p.is_deleted,'0') = '0'
            <if test="userName != null and userName != ''">
                and u.username like CONCAT('%',#{userName,jdbcType=VARCHAR},'%')
            </if>
        order by p.modify_time desc
    </select>


    <!--    新增参数配置信息-->
    <insert id="insert" parameterType="api.common.pojo.po.system.ParameterPO">
        INSERT INTO system_parameter(
            id,
            user_id,
            num_create_user,
            num_create_scene_package,
            num_scene_per_package,
            create_time,
            modify_time,
            create_user_id,
            modify_user_id,
            is_deleted
        )
        VALUES(
            #{id,jdbcType=VARCHAR},
            #{userId,jdbcType=VARCHAR},
            #{numCreateUser,jdbcType=INTEGER},
            #{numCreateScenePackage,jdbcType=INTEGER},
            #{numScenePerPackage,jdbcType=INTEGER},
            #{createTime,jdbcType=TIMESTAMP},
            #{modifyTime,jdbcType=TIMESTAMP},
            #{createUserId,jdbcType=VARCHAR},
            #{modifyUserId,jdbcType=VARCHAR},
            #{isDeleted,jdbcType=VARCHAR}
        )
    </insert>

    <!--    删除参数配置信息-->
    <update id="deleteById" parameterType="api.common.pojo.po.system.ParameterPO">
        update system_parameter set
          modify_time = #{modifyTime,jdbcType=TIMESTAMP},
          modify_user_id = #{modifyUserId,jdbcType=VARCHAR},
          is_deleted = #{isDeleted,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <!--查询参数修改历史记录-->
    <select id="getParamHistory" parameterType="api.common.pojo.param.system.ParameterParam" resultType="api.common.pojo.vo.system.ParameterVO">
        select
        id,
        IFNULL(num_create_user,0) as num_create_user,
        IFNULL(num_create_scene_package,0) as num_create_scene_package,
        IFNULL(num_scene_per_package,0) as num_scene_per_package,
        modify_time
        from system_parameter
        <where>
            is_deleted = '1'
        </where>
        <if test="userId != null and userId != ''">
            and user_id = #{userId,jdbcType=VARCHAR}
        </if>
        order by modify_time desc
    </select>

    <select id="getSceneParam" parameterType="java.lang.String" resultType="java.util.HashMap">
        select num_create_scene_package as numCreateScenePackage, num_scene_per_package as numScenePerPackage
        from system_parameter
        where is_deleted = '0' and user_id = #{userId,jdbcType=VARCHAR}
    </select>

    <select id="getParameterByUserId" parameterType="java.lang.String" resultType="api.common.pojo.vo.system.ParameterVO">
        select num_create_user, num_scene_per_package, num_create_scene_package
        from system_parameter
        where is_deleted = '0' and user_id = #{userId,jdbcType=VARCHAR}
    </select>


</mapper>
