<?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.cyksj.mapper.GroupsMapper">

    <select id="selectGPTDoubleVerifyTrips" resultType="com.cyksj.model.entity.AccountDoubleVerifyRecord">
        select gt.*
        from (select a.account, t.account_id, t.id as groups_id, t.sku_id, t.num
              from account a
                       inner join groups_trips t on a.id = t.account_id
              where t.sku_id in (select id from goods_don_sku where goods_id = #{goodsId} and status is true)
                and t.num > 1
                and a.api_secret = '') gt
        where gt.num =
              (select count(*) from groups_relation gr where gr.groups_id = gt.groups_id and gr.`status` = 'validity')
          and not EXISTS(select account_id
                         from account_double_verify_record
                         where account_id = gt.account_id
                           and deleted is true)
          and DATE_SUB(now(), INTERVAL 3 day) >= (select max(update_time)
                                                  from groups_relation gr
                                                  where gr.groups_id = gt.groups_id
                                                    and gr.`status` = 'validity')
    </select>

    <select id="getExpiryAccountGroups" resultType="com.cyksj.model.entity.GroupsTrips">
        select gt.id,
               gt.status,
               a.expiry_time
        from `groups_trips` gt inner join account a on a.id = gt.account_id
        where gt.sku_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and gt.num != available_num
        and a.expiry_time &lt; #{nextBeginDay}
        and gt.num > 1
    </select>

    <select id="selectSameSpecItemGroupsByTime" resultType="java.lang.Long">
        select gt.id
        from `groups_trips` gt
        inner join account a on a.id = gt.account_id
        where gt.id != #{groupsId}
        and gt.sku_id = #{skuId}
        and gt.status = 'validity'
        and a.expiry_time >= #{expiryTime}
        and a.expiry_time &lt;= #{betDay}
        and (select count(*) from groups_relation gr where gr.status ='outside' and gr.groups_id = gt.id and
        gr.user_id != 0) &lt; #{extraNum}
        order by gt.available_num asc limit 1
    </select>

    <select id="selectSameSpecEmptyGroups" resultType="java.lang.Long">
        select id
        from `groups_trips`
        where status = 'waiting'
          and available_num > 0
          and sku_id = #{skuId}
        order by available_num asc limit 1
    </select>

    <select id="selectMidJourneySameSpecItemGroups" resultType="java.lang.Long">
        select gt.id
        from `groups_trips` gt
                 inner join account a on a.id = gt.account_id
        where gt.id != #{groupsId}
          and gt.sku_id = #{skuId}
          and gt.status = 'validity'
          and a.expiry_time >= #{expiryTime}
          and gt.available_num > 0
        order by gt.available_num asc limit 1
    </select>

    <select id="countExpiredNum" resultType="java.lang.Integer">
        select count(ur.id)
        from user_ticket_cleared_record ur
        where ur.account_id = #{accountId}
          and deleted is true
          and source = 'self'
    </select>

    <select id="selectTicketGroups" resultType="com.cyksj.model.entity.GroupsTrips">
        select *
        from groups_trips gt
        where gt.sku_id = #{skuId}
        and gt.status = #{status}
        and gt.available_num > 0
        <if test="ip != null">
            and not exists (select 1 from (select id, user_id from groups_relation gnr where gnr.groups_id = gt.id
            and gnr.user_id != 0) gr inner join
            order_don o on o.user_id = gr.user_id and o.relation_id = gr.id
            inner join order_don_location_relate ocr on ocr.order_id = o.id where ocr.ip = #{ip} limit 1)
        </if>
        and (available_parking_num = 0 or available_parking_num > (select count(*)
        from groups_relation gr
        where gr.groups_id = gt.id
        and gr.user_id != 0 and gr.num &lt;= #{maxNum}))
        order by available_num asc
        limit 1
    </select>
</mapper>