<?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.diagbot.mapper.OpenedProductsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.OpenedProducts">
        <id column="id" property="id"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="creator" property="creator"/>
        <result column="modifier" property="modifier"/>
        <result column="product_id" property="productId"/>
        <result column="user_id" property="userId"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="service_status" property="serviceStatus"/>
        <result column="order_id" property="orderId"/>
        <result column="access_type" property="accessType"/>
        <result column="charge_type" property="chargeType"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultWrapper" type="com.diagbot.dto.OpendProductDTO">
        <id column="id" property="id"/>
        <result column="product_id" property="productId"/>
        <result column="user_id" property="userId"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="service_status" property="serviceStatus"/>
        <result column="order_id" property="orderId"/>
        <result column="access_type" property="accessType"/>
        <result column="charge_type" property="chargeType"/>
        <result column="order_time" property="orderTime"/>
    </resultMap>
    <select id="selectOpendInfoByUserId" resultMap="BaseResultMap" parameterType="java.util.List">
        SELECT opend.* FROM diag_opened_products opend WHERE user_id = #{userId}
    </select>

    <select id="selectOpenedProducts" resultMap="BaseResultMap" parameterType="java.util.Map">
        SELECT t.* FROM diag_opened_products t WHERE t.is_deleted='N'
        AND t.user_id = #{userId}
        AND t.product_id=#{productId}

    </select>


    <select id="getByAppkeyAndSecret" resultMap="BaseResultMap" parameterType="java.util.Map">
        select os.* from diag_service_token st, diag_product_service ps, diag_service_info si, diag_opened_products os
        where st.product_service_id = ps.id and ps.product_id = os.product_id and ps.service_id = si.id and si.user_id = os.user_id
        and st.is_deleted = 'N' and ps.is_deleted = 'N' and si.is_deleted = 'N' AND os.is_deleted = 'N'
        and st.app_key_id = #{appkey} and st.app_key_secret = #{secret}
    </select>

    <select id="getConsoleOpened" parameterType="long"
            resultType="com.diagbot.dto.GetConsoleOpenedDTO">
    	SELECT
			a.id as openId,
			b.id as productId,
			b.name as name,
			DATE_FORMAT(a.start_time,'%Y-%m-%d') as startTime,
			DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%i:%s') as endTime,
			a.service_status as serviceStatus,
			b.url as url,
			b.access_type as accessType,
			d.app_key_id as appKeyId,
			d.app_key_secret as appKeySecret,
			case when c.id is null then 0 else 1 end as renewalsStutas,
			case when a.start_time>sysdate() then 0 else 1 end as isBegin
		from diag_opened_products a join diag_lantone_product b on a.product_id=b.id
		left join diag_user_renewals c on a.user_id=c.user_id and a.product_id=c.product_id and c.renewals_status=0 and c.cancel_renewals=0 and c.is_deleted='N'
		left join 
		(SELECT
		b.product_id,
		c.app_key_id,
		c.app_key_secret
		FROM
		diag_service_info a
		JOIN diag_product_service b ON a.id = b.service_id
		JOIN diag_service_token c ON b.id = c.product_service_id
		WHERE
		a.is_deleted = 'N'
		AND b.is_deleted = 'N'
		AND c.is_deleted = 'N'
		AND a.user_id = #{userId}
		AND a.type = 2
		AND c.type = 1) d on b.id=d.product_id
		where a.is_deleted='N' and b.is_deleted='N' and a.user_id=#{userId}
    </select>

    <select id="getConsoleOpenedCount" parameterType="long" resultType="int">
    	SELECT
			count(1)
		from diag_opened_products a join diag_lantone_product b on a.product_id=b.id
		where a.is_deleted='N' and b.is_deleted='N' and a.user_id=#{userId};
    </select>

    <select id="getConsoleOnTrial" parameterType="long"
		resultType="com.diagbot.dto.GetConsoleOnTrialDTO">
		SELECT
		a.id as id,
		a.name as name,
		a.service_status as serviceStatus,
		a.access_type as accessType,
		a.trial_url as trialUrl,
		b.app_key_id as appKeyId,
		b.app_key_secret as appKeySecret,
		b.`STATUS` as tokenStatus,
		DATE_FORMAT(b.starting_date,'%Y-%m-%d %H:%i:%s') as startingDate,
		DATE_FORMAT(b.expiring_date,'%Y-%m-%d %H:%i:%s') as expiringDate
		FROM
		diag_lantone_product a
		LEFT JOIN (
		SELECT
		b.product_id,
		c.app_key_id,
		c.app_key_secret,
		c.`status`,
		c.starting_date,
		c.expiring_date
		FROM
		diag_service_info a
		JOIN diag_product_service b ON a.id = b.service_id
		JOIN diag_service_token c ON b.id = c.product_service_id
		WHERE
		a.is_deleted = 'N'
		AND b.is_deleted = 'N'
		AND c.is_deleted = 'N'
		AND a.user_id = #{userId}
		AND a.type = 2
		AND c.type = 3
		) b ON a.id = b.product_id
		WHERE
		a.is_deleted = 'N' and a.trial_status=1 and a.service_status = 1
		AND a.id NOT IN (
		SELECT
		product_id
		FROM
		diag_opened_products
		WHERE
		user_id = #{userId} and is_deleted = 'N')
	</select>

    <select id="getConsoleOnTrialCount" parameterType="long" resultType="int">
    	SELECT
		count(1)
		FROM
		diag_lantone_product a
		WHERE
		a.is_deleted = 'N' and a.trial_status=1
		AND a.id NOT IN (
		SELECT
		product_id
		FROM
		diag_opened_products
		WHERE
		user_id = #{userId} and is_deleted = 'N' and service_status = 1)
    </select>
    <select id="getInformationAvailableByUserId" parameterType="com.diagbot.entity.OpenedProducts"
            resultType="com.diagbot.dto.UserAndProdutDTO">

        SELECT
        a.id as Id,
        a.product_id as productId,
        a.user_id as userId,
        a.start_time as startTime,
        a.end_time as endTime,
        a.service_status as serviceStatus,
        a.access_type as accessType,
        a.charge_type as chargeType,
        b.name as productName
        FROM diag_opened_products a JOIN diag_lantone_product b
        ON a.product_id=b.id
        WHERE
        a.is_deleted = "N"
        AND b.is_deleted = "N"
        <if test="userId !=null">
            AND a.user_id = #{userId}
        </if>
    </select>

    <update id="startAndendByuserId" parameterType="com.diagbot.entity.OpenedProducts">
        UPDATE diag_opened_products a
        <trim prefix="set" suffixOverrides=",">
            <if test="gmtModified !=null">a.gmt_modified=#{gmtModified},</if>
            <if test="modifier != null">a.modifier=#{modifier},</if>
            <if test="serviceStatus != null">a.service_status =#{serviceStatus}</if>
        </trim>
        WHERE
        a.user_id = #{userId}
        AND a.product_id = #{productId}
    </update>

    <update id="delInformationAvailable" parameterType="com.diagbot.entity.OpenedProducts">
        UPDATE diag_opened_products a
        <trim prefix="set" suffixOverrides=",">
            a.is_deleted = "Y",
            <if test="gmtModified !=null">a.gmt_modified=#{gmtModified},</if>
            <if test="modifier != null">a.modifier=#{modifier}</if>
        </trim>
        WHERE
        a.user_id = #{userId}
        AND a.product_id = #{productId}
    </update>

    <update id="modifyOpeningTime" parameterType="com.diagbot.entity.OpenedProducts">
        UPDATE diag_opened_products a
        <trim prefix="set" suffixOverrides=",">
            <if test="gmtModified !=null">a.gmt_modified=#{gmtModified},</if>
            <if test="modifier != null">a.modifier=#{modifier},</if>
            <if test="startTime != null">a.start_time = #{startTime},</if>
            <if test="endTime != null">a.end_time = #{endTime},</if>
            <if test="serviceStatus != null">a.service_status = #{serviceStatus}</if>
        </trim>
        WHERE
        a.user_id = #{userId}
        AND a.product_id = #{productId}
    </update>

    <select id="getByProductId" resultType="com.diagbot.dto.OpendProductDTO">
        SELECT a.*,b.time order_time from diag_opened_products a
        LEFT JOIN diag_product_order b ON a.order_id =b.id
        WHERE product_id =#{opend.productId} AND a.is_deleted='N'
        <if test="opend.userId != null and opend.userId.size > 0">
            <foreach item="opend.userId" collection="opend.userId" open="and(" separator="or" close=")">
                 a.user_id = #{opend.userId}
            </foreach>
        </if>
        <if test="opend.serviceStatus != null and opend.serviceStatus != ''">
                and a.service_status = #{opend.serviceStatus}
        </if>
        ORDER BY b.time desc
    </select>

    <select id="getOpendById" resultMap="BaseResultMap">
        SELECT * FROM `diag_opened_products` a where a.service_status ='1'
        <if test="productId != null and productId != ''">
            and a.product_id =#{productId}
        </if>
    </select>
    <select id="getInformationAvailableByUserIds" parameterType="java.util.List" resultType="com.diagbot.dto.UserAndProdutDTO">
        SELECT
        a.id as Id,
        a.product_id as productId,
        a.user_id as userId,
        a.start_time as startTime,
        a.end_time as endTime,
        a.service_status as serviceStatus,
        b.access_type as accessType,
        b.charge_type as chargeType,
        b.name as productName
        FROM diag_opened_products a JOIN diag_lantone_product b
        ON a.product_id=b.id
        WHERE
        a.is_deleted = "N"
        AND b.is_deleted = "N"
        AND a.user_id in
        <foreach collection="list" item="ids" open="(" close=")" separator=",">
            #{ids}
        </foreach>
    </select>

    <select id="getInformationAvailableByUserIdsB" parameterType="java.util.List" resultType="com.diagbot.dto.UserAndProdutDTO">
        SELECT
        a.id as Id,
        a.product_id as productId,
        a.user_id as userId,
        a.start_time as startTime,
        a.end_time as endTime,
        a.service_status as serviceStatus,
        b.access_type as accessType,
        b.charge_type as chargeType,
        b.name as productName
        FROM diag_opened_products a JOIN diag_lantone_product b
        ON a.product_id=b.id
        WHERE
        a.is_deleted = "N"
        AND a.user_id in
        <foreach collection="list" item="ids" open="(" close=")" separator=",">
            #{ids}
        </foreach>
    </select>

    <select id="getOpendCountByUserId" resultType="java.lang.Long">
        SELECT a.product_id FROM diag_opened_products a WHERE a.is_deleted ='N' and a.user_id = #{userId}
    </select>
    
    <select id="getOpendInfos" resultType="com.diagbot.entity.OpenedProducts">
        SELECT * FROM `diag_opened_products` WHERE is_deleted = "N"
    </select>
</mapper>
