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


    <!--查询集群列表-->
    <select id="getClusterList" parameterType="api.common.pojo.param.system.ClusterParam" resultType="api.common.pojo.vo.system.ClusterVO">
        select
        p.id,
        u.id as user_id,
        u.username,
        u.use_type,
        IFNULL(p.num_simulation_license,0) as num_simulation_license,
        IFNULL(p.num_dynamic_license,0) as num_dynamic_license,
        p.date_simulation_license,
        p.date_dynamic_license,
        p.modify_time
        from system_user u
        left join system_cluster p on u.id = p.user_id
        where
            u.is_deleted = '0' and u.visible = '1' and IFNULL(p.is_deleted,'0') = '0'
            and u.role_code = #{roleCode,jdbcType=VARCHAR}
            <if test="createUserId != null and createUserId != ''">
                and u.create_user_id = #{createUserId,jdbcType=VARCHAR}
            </if>
            <if test="userName != null and userName != ''">
                and u.username like CONCAT('%',#{userName,jdbcType=VARCHAR},'%')
            </if>
            <if test="modifyTimeStart != null and modifyTimeStart != ''">
                and p.modify_time &gt;= #{modifyTimeStart}
            </if>
            <if test="modifyTimeEnd != null and modifyTimeEnd != ''">
                and p.modify_time &lt;= #{modifyTimeEnd}
            </if>
            <if test="dueTimeStart != null and dueTimeStart != '' and dueTimeEnd != null and dueTimeEnd != ''">
            and ((p.date_simulation_license &gt;= #{dueTimeStart} and p.date_simulation_license &lt;= #{dueTimeEnd})
            or (p.date_dynamic_license &gt;= #{dueTimeStart} and p.date_dynamic_license &lt;= #{dueTimeEnd})  )
            </if>
        <if test="dueTimeStart != null and dueTimeStart != '' and (dueTimeEnd == null or dueTimeEnd == '')">
            and (p.date_simulation_license &gt;= #{dueTimeStart} or p.date_dynamic_license &gt;= #{dueTimeStart})
        </if>
            <if test="dueTimeEnd != null and dueTimeEnd != '' and ( dueTimeStart == null or dueTimeStart == '' )">
                and (p.date_simulation_license &lt;= #{dueTimeEnd} or p.date_dynamic_license &lt;= #{dueTimeEnd})
            </if>
        order by p.modify_time desc
    </select>


    <!--    新增集群配置信息-->
    <insert id="insert" parameterType="api.common.pojo.po.system.ClusterPO">
        INSERT INTO system_cluster(
            id,
            user_id,
            num_simulation_license,
            date_simulation_license,
            num_dynamic_license,
            date_dynamic_license,
            create_time,
            modify_time,
            create_user_id,
            modify_user_id,
            is_deleted
        )
        VALUES(
            #{id,jdbcType=VARCHAR},
            #{userId,jdbcType=VARCHAR},
            #{numSimulationLicense,jdbcType=INTEGER},
            #{dateSimulationLicense,jdbcType=TIMESTAMP},
            #{numDynamicLicense,jdbcType=INTEGER},
            #{dateDynamicLicense,jdbcType=TIMESTAMP},
            #{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.ClusterPO">
        update system_cluster set
            is_deleted = #{isDeleted,jdbcType=VARCHAR},
            modify_time = #{modifyTime,jdbcType=TIMESTAMP},
            modify_user_id = #{modifyUserId,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <!--查询集群修改历史记录-->
    <select id="getClusterHistory" parameterType="api.common.pojo.param.system.ClusterParam" resultType="api.common.pojo.vo.system.ClusterVO">
        select
        id,
        IFNULL(num_simulation_license,0) as num_simulation_license,
        IFNULL(num_dynamic_license,0) as num_dynamic_license,
        date_simulation_license,
        date_dynamic_license,
        modify_time
        from system_cluster
        <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="getClusterNum" parameterType="api.common.pojo.param.system.ClusterParam" resultType="java.util.HashMap">

        select
          IFNULL(SUM(IF(DATE(p.date_simulation_license) >= DATE(NOW()),p.num_simulation_license,0)),0) as totalSimulation,
          IFNULL(SUM(IF(DATE(p.date_dynamic_license) >= DATE(NOW()),p.num_dynamic_license,0)),0) as totalDynamic
        from system_user u
        left join system_cluster p on u.id = p.user_id
        where u.is_deleted = '0' and p.is_deleted = '0' and u.role_code = #{roleCode,jdbcType=VARCHAR}
        <if test="createUserId != null and createUserId != ''">
            and u.create_user_id = #{createUserId,jdbcType=VARCHAR}
        </if>
    </select>

    <!--查询根据 userId 未到期的仿真软件license数量-->
    <select id="getSimulationLicenseNum" parameterType="java.lang.String" resultType="java.lang.Integer">
        select num_simulation_license from system_cluster
        where is_deleted = '0' and DATE(date_simulation_license) >= DATE(NOW()) and user_id = #{userId,jdbcType=VARCHAR}
    </select>

    <!--查询根据userId未到期的动力学软件license数量-->
    <select id="getDynamicLicenseNum" resultType="java.lang.Integer">
        select num_dynamic_license from system_cluster
        where is_deleted = '0' and DATE(date_dynamic_license) >= DATE(NOW()) and user_id = #{userId,jdbcType=VARCHAR}
    </select>


    <select id="getClusterByUserId" parameterType="java.lang.String" resultType="api.common.pojo.vo.system.ClusterVO">
        select num_simulation_license, date_simulation_license, num_dynamic_license, date_dynamic_license from system_cluster
        where is_deleted = '0' and user_id = #{userId,jdbcType=VARCHAR}
    </select>

    <select id="getAssignedSimLicenseNum" parameterType="api.common.pojo.param.system.ClusterParam" resultType="java.lang.Integer">
        select IFNULL(SUM(num_simulation_license),0) as AssignedSimLicenseNum from system_cluster
        where is_deleted = '0' and DATE(date_simulation_license) >= DATE(NOW()) and create_user_id = #{createUserId,jdbcType=VARCHAR}
        <if test="userId != null and userId != ''">
            and  user_id != #{userId,jdbcType=VARCHAR}
        </if>
    </select>
    <select id="getAssignedDynLicenseNum" parameterType="api.common.pojo.param.system.ClusterParam" resultType="java.lang.Integer">
        select  IFNULL(SUM(num_dynamic_license),0) as AssignedDynLicenseNum from system_cluster
        where is_deleted = '0' and DATE(date_dynamic_license) >= DATE(NOW()) and create_user_id = #{createUserId,jdbcType=VARCHAR}
        <if test="userId != null and userId != ''">
            and  user_id != #{userId,jdbcType=VARCHAR}
        </if>
    </select>

</mapper>
