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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.LantoneProduct">
        <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="name" property="name"/>
        <result column="decription" property="decription"/>
        <result column="charge_type" property="chargeType"/>
        <result column="url" property="url"/>
        <result column="service_status" property="serviceStatus"/>
        <result column="trial_status" property="trialStatus"/>
        <result column="trial_url" property="trialUrl"/>
        <result column="access_type" property="accessType"/>
    </resultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultDTOMap" type="com.diagbot.dto.LantoneProductOrgDTO">
        <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="name" property="name"/>
        <result column="decription" property="decription"/>
        <result column="charge_type" property="chargeType"/>
        <result column="url" property="url"/>
        <result column="service_status" property="serviceStatus"/>
        <result column="trial_status" property="trialStatus"/>
        <result column="trial_url" property="trialUrl"/>
        <result column="access_type" property="accessType"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="opened_service_status" property="openedServiceStatus"/>
        <result column="time" property="time"/>
        <result column="service_ids" property="serviceIds"/>
    </resultMap>

    <select id="productLines" resultType="com.diagbot.dto.ProductLineDTO">
        SELECT * FROM `diag_lantone_product` WHERE is_deleted = 'N'
    </select>


    <select id="productLinesB" resultType="com.diagbot.dto.ProductLineDTO">
        SELECT * FROM `diag_lantone_product`
    </select>

    <!--根据产品名称分页查询-->
    <select id="selectProduct" resultType="com.diagbot.dto.LantoneProductDTO">
        SELECT
        p.id,
        p.gmt_create,
        p.creator,
        p.`name`,
        p.decription,
        p.charge_type,
        p.url,
        p.service_status,
        p.trial_status,
        p.trial_url,
        p.access_type,
        (
        SELECT
        COUNT(1)
        FROM
        diag_opened_products o
        WHERE
        p.id = o.product_id
        AND
        o.is_deleted ='N'
        ) oppended_num
        FROM
        `diag_lantone_product` p WHERE p.is_deleted ='N'
        <if test="name != null and name != ''">
            AND p.name LIKE concat ('%',#{name},'%')
        </if>
        ORDER BY gmt_create DESC
    </select>

    <select id="opendedProduct" resultMap="BaseResultMap" parameterType="java.util.Map">
        SELECT op.id,op.start_time,op.end_time,op.service_status,pod.time,orn.`name`
        from `sys-diagbotman`.diag_opened_products op,`sys-diagbotman`.diag_product_order
        pod,`sys-user`.sys_user_organization uor,`sys-user`.sys_organization orn
        where pod.num = op.order_id AND uor.user_id =op.user_id AND orn.id =uor.organization_id AND
        op.product_id =#{map.id}
        <if test="map.servicdStatus !=null and map.servicdStatus !=''">
            op.service_status =#{map.serviceStatus}
        </if>
    </select>

    <!--根据用户查询已开通产品-->
    <select id="opendedProductByUserId" resultMap="BaseResultDTOMap" parameterType="java.lang.Long">
        select a.*,b.start_time,b.end_time,b.service_status as opened_service_status,c.time
        from diag_lantone_product a,diag_opened_products b,diag_product_order c,diag_order_details d
        where a.id=b.product_id and a.id=d.product_id
			  and c.num=d.order_num
              and d.audit_status=1
              and a.is_deleted='N' and b.is_deleted='N' and c.is_deleted='N' and d.is_deleted='N'
              -- and (find_in_set('2',a.access_type) or FIND_IN_SET('3',a.access_type))
              and b.user_id=#{userId}  and c.user_id=#{userId}  and d.user_id=#{userId}
    </select>
    
    <select id="selectProductByName" resultMap="BaseResultMap">
        SELECT * FROM `diag_lantone_product` WHERE is_deleted='N' and `name`=#{name}
    </select>

</mapper>
