<?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.cm.central.control.dao.client.IClientDao">

    <resultMap id="clientDTO" type="com.cm.central.control.pojo.dtos.client.ClientDTO">
        <id column="client_id" property="clientId"/>
        <result column="client_name" property="clientName"/>
        <result column="client_summary" property="clientSummary"/>
        <result column="client_version" property="clientVersion"/>
        <result column="database_type" property="databaseType"/>
        <result column="database_type_dictionary_name" property="databaseTypeDictionaryName"/>
        <result column="database_version" property="databaseVersion"/>
        <result column="database_version_dictionary_name" property="databaseVersionDictionaryName"/>
        <result column="database_name" property="databaseName"/>
        <result column="customer_id" property="customerId"/>
        <result column="customer_customer_name" property="customerCustomerName"/>
        <result column="server_ip" property="serverIp"/>
        <result column="is_usercenter" property="isUsercenter"/>
        <result column="is_only_authorization" property="isOnlyAuthorization"/>
        <result column="mac_address" property="macAddress"/>
        <result column="license_start_time" property="licenseStartTime"/>
        <result column="license_effective_days" property="licenseEffectiveDays"/>
        <result column="license_expiration_date" property="licenseExpirationDate"/>
        <result column="authorization_count" property="authorizationCount"/>
        <result column="project_leader_id" property="projectLeaderId"/>
        <result column="project_leader_leader_name" property="projectLeaderLeaderName"/>
    </resultMap>

    <resultMap id="clientBO" type="com.cm.central.control.pojo.bos.ClientBO">
        <id column="client_id" property="clientId"/>
        <result column="client_secret" property="clientSecret"/>
        <result column="client_name" property="clientName"/>
        <result column="client_summary" property="clientSummary"/>
        <result column="client_version" property="clientVersion"/>
        <result column="database_type" property="databaseType"/>
        <result column="database_type_dictionary_name" property="databaseTypeDictionaryName"/>
        <result column="database_version" property="databaseVersion"/>
        <result column="database_version_dictionary_name" property="databaseVersionDictionaryName"/>
        <result column="database_name" property="databaseName"/>
        <result column="customer_id" property="customerId"/>
        <result column="customer_customer_name" property="customerCustomerName"/>
        <result column="server_ip" property="serverIp"/>
        <result column="is_usercenter" property="isUsercenter"/>
        <result column="is_only_authorization" property="isOnlyAuthorization"/>
        <result column="mac_address" property="macAddress"/>
        <result column="license_start_time" property="licenseStartTime"/>
        <result column="license_effective_days" property="licenseEffectiveDays"/>
        <result column="license_expiration_date" property="licenseExpirationDate"/>
        <result column="authorization_count" property="authorizationCount"/>
        <result column="project_leader_id" property="projectLeaderId"/>
        <result column="project_leader_leader_name" property="projectLeaderLeaderName"/>
    </resultMap>

    <resultMap id="clientLicenseAuthorizationDTO" type="com.cm.central.control.pojo.dtos.client.ClientLicenseAuthorizationDTO">
        <result column="is_only_authorization" property="isOnlyAuthorization"/>
        <result column="mac_address" property="macAddress"/>
        <result column="license_start_time" property="licenseStartTime"/>
        <result column="license_effective_days" property="licenseEffectiveDays"/>
        <result column="license_expiration_date" property="licenseExpirationDate"/>
    </resultMap>

    <!-- 新增客户端 -->
    <insert id="saveClient" parameterType="map" flushCache="true">
        INSERT INTO central_client(
            client_id,
            client_secret,
            client_name,
            client_summary,
            client_version,
            database_type,
            database_version,
            database_name,
            customer_id,
            server_ip,
            is_usercenter,
            is_only_authorization,
            mac_address,
            license_start_time,
            license_effective_days,
            license_expiration_date,
            authorization_count,
            project_leader_id,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{clientId},
            #{clientSecret},
            #{clientName},
            #{clientSummary},
            #{clientVersion},
            #{databaseType},
            #{databaseVersion},
            #{databaseName},
            #{customerId},
            #{serverIp},
            #{isUsercenter},
            #{isOnlyAuthorization},
            #{macAddress},
            #{licenseStartTime},
            #{licenseEffectiveDays},
            #{licenseExpirationDate},
            #{authorizationCount},
            #{projectLeaderId},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除客户端 -->
    <update id="removeClient" parameterType="map" flushCache="true">
        UPDATE
            central_client
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            client_id IN
        <foreach collection="clientIds" index="index" open="(" separator="," close=")">
            #{clientIds[${index}]}
        </foreach>
    </update>

    <!-- 删除客户端（物理） -->
    <update id="deleteClient" parameterType="map" flushCache="true">
        DELETE FROM
            central_client
        WHERE
            client_id IN
        <foreach collection="clientIds" index="index" open="(" separator="," close=")">
            #{clientIds[${index}]}
        </foreach>
    </update>

    <!-- 修改客户端 -->
    <update id="updateClient" parameterType="map" flushCache="true">
        UPDATE
            central_client
        SET
        <if test="clientName != null and clientName != ''">
            client_name = #{clientName},
        </if>
        <if test="clientSummary != null">
            client_summary = #{clientSummary},
        </if>
        <if test="clientVersion != null and clientVersion != ''">
            client_version = #{clientVersion},
        </if>
        <if test="databaseType != null and databaseType != ''">
            database_type = #{databaseType},
        </if>
        <if test="databaseVersion != null and databaseVersion != ''">
            database_version = #{databaseVersion},
        </if>
        <if test="databaseName != null and databaseName != ''">
            database_name = #{databaseName},
        </if>
        <if test="customerId != null and customerId != ''">
            customer_id = #{customerId},
        </if>
        <if test="serverIp != null and serverIp != ''">
            server_ip = #{serverIp},
        </if>
        <if test="isUsercenter != null">
            is_usercenter = #{isUsercenter},
        </if>
        <if test="projectLeaderId != null and projectLeaderId != ''">
            project_leader_id = #{projectLeaderId},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            client_id = #{clientId}
    </update>

    <!-- 修改客户端 -->
    <update id="updateClientLicense" parameterType="map" flushCache="true">
        UPDATE
            central_client
        SET
            is_only_authorization = #{isOnlyAuthorization},
            mac_address = #{macAddress},
            license_start_time = #{licenseStartTime},
            license_effective_days = #{licenseEffectiveDays},
            license_expiration_date = #{licenseExpirationDate},
            authorization_count = authorization_count + 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            client_id = #{clientId}
    </update>

    <!-- 客户端详情 -->
    <select id="getClient" parameterType="map" resultMap="clientDTO" useCache="true">
        SELECT
            t1.client_name,
            t1.client_secret,
            t1.client_summary,
            t1.client_version,
            t1.database_type,
            t1.database_version,
            t1.database_name,
            t1.customer_id,
            t1.server_ip,
            t1.is_usercenter,
            t1.is_only_authorization,
            t1.mac_address,
            t1.license_start_time,
            t1.license_effective_days,
            t1.license_expiration_date,
            t1.project_leader_id,
            t1.client_id
        FROM
            central_client t1
        WHERE
            t1.is_delete = 0
        <if test="clientId != null and clientId != ''">
        AND
            t1.client_id = #{clientId}
        </if>
    </select>

    <!-- 客户端列表 -->
    <select id="listClient" parameterType="map" resultMap="clientDTO">
        SELECT
            t1.client_name,
            t1.client_secret,
            t1.client_summary,
            t1.client_version,
            t1.database_type,
            dt1.dictionary_name database_type_dictionary_name,
            t1.database_version,
            dt2.dictionary_name database_version_dictionary_name,
            t1.database_name,
            t1.customer_id,
            jt1.customer_name customer_customer_name,
            t1.server_ip,
            t1.is_usercenter,
            t1.is_only_authorization,
            t1.mac_address,
            t1.license_start_time,
            t1.license_effective_days,
            t1.license_expiration_date,
            t1.authorization_count,
            jt2.leader_name project_leader_leader_name,
            t1.client_id
        FROM
            central_client t1
        LEFT JOIN
            data_dictionary dt1
        ON
            dt1.dictionary_id = t1.database_type
        AND
            dt1.is_delete = 0
        LEFT JOIN
            data_dictionary dt2
        ON
            dt2.dictionary_id = t1.database_version
        AND
            dt2.is_delete = 0
        LEFT JOIN
            central_customer jt1
        ON
            t1.customer_id = jt1.customer_id
        AND
            jt1.is_delete = 0
        LEFT JOIN
            central_project_leader jt2
        ON
            t1.project_leader_id = jt2.project_leader_id
        AND
            jt2.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.client_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="projectLeaderId != null and projectLeaderId != ''">
        AND
            t1.project_leader_id = #{projectLeaderId}
        </if>
        <if test="clientIds != null and clientIds.size > 0">
        AND
            t1.client_id IN
            <foreach collection="clientIds" index="index" open="(" separator="," close=")">
            #{clientIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 客户端统计  -->
    <select id="countClient" parameterType="map" resultType="Integer" useCache="true">
        SELECT
            COUNT(*)
        FROM
            central_client t1
        WHERE
            t1.is_delete = 0
    </select>

    <!-- 获取客户端密码 -->
    <select id="getClientSecretByClientId" parameterType="java.lang.String" resultType="java.lang.String">
        SELECT
            client_secret
        FROM
            central_client
        WHERE
            is_delete = 0
        AND
            client_id = #{_parameter}
    </select>

    <!-- 客户端license授权(通过ID) -->
    <select id="getClientLicenseAuthorizationByClientId" parameterType="java.lang.String" resultMap="clientLicenseAuthorizationDTO">
        SELECT
            is_only_authorization,
            mac_address,
            license_start_time,
            license_effective_days,
            license_expiration_date,
            client_id
        FROM
            central_client
        WHERE
            is_delete = 0
        AND
            client_id = #{_parameter}
    </select>

    <!-- 获取client详情 -->
    <select id="getClientBO" parameterType="map" resultMap="clientBO" useCache="true">
        SELECT
            t1.client_name,
            t1.client_secret,
            t1.client_summary,
            t1.client_version,
            t1.database_type,
            t1.database_version,
            t1.database_name,
            t1.customer_id,
            t1.server_ip,
            t1.is_usercenter,
            t1.is_only_authorization,
            t1.mac_address,
            t1.license_start_time,
            t1.license_effective_days,
            t1.license_expiration_date,
            t1.project_leader_id,
            t1.client_id
        FROM
            central_client t1
        WHERE
            t1.is_delete = 0
        <if test="clientId != null and clientId != ''">
        AND
            t1.client_id = #{clientId}
        </if>
    </select>

</mapper>