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

    <update id="updateExpiryTime">
        update groups_relation
        set start_time = null,
        expiry_time = null,
        status = #{entity.status},
        renew_status = #{entity.renewStatus},
        is_handle = #{entity.isHandle},
        user_id = #{entity.userId},
        account = null,
        customer_service = null,
        password = null,
        recharge_status = null,
        send_time = null,
        submit_time = null,
        operator = null,
        yhs_id = 0
        where id = #{entity.id}
        <if test="expireTime != null">
            and expiry_time = #{expireTime}
        </if>
    </update>

    <select id="getAccountIdsByUserIdOrNickNameOrSubmitAccount" resultType="java.lang.Long">
        select gr.groups_id
        from groups_relation gr,
             `user` u
                where  gr.user_id = u.id
        <if test="nickName != null and nickName != ''">
            and u.nickname like '%${nickName}%'
        </if>
        <if test="userId != null">
            and u.id = #{userId,jdbcType=BIGINT}
        </if>
        <if test="submitAccount != null and submitAccount != ''">
            and gr.account = #{submitAccount,jdbcType=VARCHAR}
        </if>
        group by gr.groups_id
    </select>

    <select id="getGroupTripsSkuIdByRelationId" resultType="java.lang.Long">
        select gt.sku_id from `groups_trips` gt left join `groups_relation` gr on gr.groups_id = gt.id where gr.id = #{relationId} limit 1
    </select>

    <select id="selectSimilarExpiredRelation" resultType="java.lang.Long">
        select s.id
        from (select gt.id, min(gr.expiry_time) minExpiryTime
        from `groups_trips` gt
        left join `groups_relation` gr on gr.groups_id = gt.id
        where gt.sku_id = #{skuId}
        and (gt.available_parking_num = 0 or (select count(*)
        from groups_relation gr
        where gr.groups_id = gt.id and gr.user_id != 0 and gr.num &lt;=
        #{maxNum}) &lt; gt.available_parking_num) and gt.available_num > 0
        and gt.status = 'validity'
        and gr.status = 'validity'
        and gr.user_id != 0
        and gr.expiry_time is not null
        group by gr.groups_id) s
        where s.minExpiryTime >= #{minExpiryTime}
        <if test="ip != null">
            and not exists (select 1 from (select id, user_id from groups_relation gnr where gnr.groups_id = s.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>
        order by minExpiryTime desc limit 1
    </select>

    <select id="selectRelationByGoodsId" resultType="com.cyksj.model.manage.views.GroupsRelationView">
        select gr.*
        from groups_relation gr
                 left join groups_trips gt on gr.groups_id = gt.id
                 left join account a on a.id = gt.account_id
                 left join user u on gr.user_id = u.id
                 left join goods_don g on a.goods_id = g.id
        where g.id = #{goodsId}
          and gr.user_id = #{userId}
          and gt.status = 'validity'
          and gr.status = 'validity'
          and gr.yhs_id = #{yhsId}
    </select>

    <select id="getGroupRelationViewByUserIdAndRelationId"
            resultType="com.cyksj.model.manage.views.GroupsRelationView">
        select gr.*,
               a.account as tripsAccount,
               g.title
        from groups_relation gr
                 left join groups_trips gt on gt.id = gr.groups_id
                 left join account a on a.id = gt.account_id
                 left join goods_don g on g.id = a.goods_id
        where gr.id = #{relationId} and gr.user_id = #{userId}
    </select>

    <select id="getBindPhoneByUserId" resultType="java.lang.String">
        select if(login_phone != '', login_phone,
                  (select phone from user_bind_detail where user_id = #{userId} or email_user_id = #{userId} limit 1))
        from user
        where id = #{userId} limit 1
    </select>

    <select id="selectViewPwdCount" resultType="java.lang.Integer">
        select count(distinct relation_id)
        from (select id, user_id from `groups_relation` where groups_id = #{groupsId} and `status` = 'validity') s
                 inner join ticket_pwd_view_record cr on cr.relation_id = s.id and s.user_id = cr.user_id
    </select>

    <select id="selectRandomOneRelationByGroupsId" resultType="com.cyksj.model.entity.GroupsRelation">
        select *
        from groups_relation
        where groups_id = #{groupsId}
          and user_id = 0
          and status = 'none'
        order by rand() limit 1
    </select>

    <select id="selectExpiredRelationByCondition" resultType="com.cyksj.model.entity.GroupsRelation">
        select gr.*
        from groups_relation gr
        inner join groups_trips gt on gt.id = gr.groups_id
        where gt.sku_id not in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and gr.expiry_time &lt;= #{now}
    </select>

    <select id="selectOutsideRelationByGroupsId" resultType="com.cyksj.model.entity.GroupsRelation">
        select *
        from groups_relation
        where groups_id = #{groupsId}
          and user_id = 0
          and num > #{relationNum}
          and status = 'outside'
        order by rand() limit 1
    </select>

    <select id="selectExpiredChatGPTRelation" resultType="com.cyksj.model.entity.GroupsRelation">
        select gr.*
        from groups_relation gr
        inner join groups_trips gt on gt.id = gr.groups_id
        where gt.sku_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and gr.expiry_time &lt;= #{nextBeginDay}
    </select>

    <select id="getUserRelationUserViews" resultType="com.cyksj.model.views.GroupsRelationUserView">
        select gr.*,
        u.nickname,
        u.headimgurl
        from (select * from (select id as relation_id,
        user_id,
        1 as isSelf from groups_relation where id = #{relationId}
        and status in
        <foreach collection="status" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        limit 1)s1
        union all
        select * from (select id as realtion_id,
        user_id,
        if(user_id = #{userId},true,false) as isSelf from groups_relation where id != #{relationId}
        and groups_id = #{groupsId}
        and status in
        <foreach collection="status" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        limit 4)s2)gr inner join user u on u.id = gr.user_id
    </select>

    <select id="getTicketGoodsId" resultType="java.lang.Long">
        select distinct goods_id from (select sku.goods_id from (select * from `groups_relation` where status in
        <foreach collection="status" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and user_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>)gr left join groups_trips gt on gt.id = gr.groups_id
        left join goods_don_sku sku on sku.id = gt.sku_id
        union all
        select goods_id from order_don o where user_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and relation_id = 0 and status not in ('close','noPayment','refund'))s
    </select>

    <select id="getRecommendGoodsViews" resultType="com.cyksj.model.views.UserRecommendGoodsFrontView">
        select * from (select
        id as goodsId,
        title,
        logo,
        img,
        tags,
        sort_by,
        `type`,
        min_months as months,
        min_days as days,
        min_price as price,
        (
        case when type = 2 and ${isPayNf} then 999
        when category = 1 then 888
        when category = 2 then 777
        when category = 3 then 755
        else 666 end
        ) as sorted
        from `goods_don` where id not in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="isPayNf">
            and type in (1,2)
        </if>
        <if test="!isPayNf">
            and type = 1
        </if>
        and deleted is true and status is true) s order by s.sorted desc,sort_by asc
    </select>

    <select id="getOutSideGroupsTripsRelationByGoodsId" resultType="java.lang.Long">
        select gt.id
        from `groups_trips` gt
                 inner join account a on a.id = gt.account_id
        where gt.sku_id = #{skuId}
          and gt.status = 'validity'
          and a.expiry_time >= #{beginOfDay}
          and (select count(*)
               from groups_relation gr
               where gr.num > #{skuNum}
                 and gr.num &lt;= #{maxNum}
                 and gr.groups_id = gt.id
                 and gr.user_id
            != 0)
         &lt; #{extraNum}
          and
            (
        select count(*)
        from groups_relation gr
        where gr.groups_id = gt.id
          and gr.user_id
            = #{userId}) = 0
          and (gt.available_parking_num = 0 or (select count(*) from groups_relation gr where gr.groups_id = gt.id and gr.user_id != 0 and gr.num &lt;= #{maxNum}) &lt; gt.available_parking_num)
        order by RAND() limit 1
    </select>

    <select id="selectAvailableRelation" resultType="java.lang.Integer">
        select count(gr.id)
        from account a
                 inner join `groups_trips` gt on gt.account_id = a.id
            and gt.`status` = 'validity'
            and gt.sku_id = #{skuId}
                 inner join groups_relation gr on gr.groups_id = gt.id
            and gr.status in ('none', 'locking')
            and gt.available_parking_num = 0
            and gr.num &lt;= #{skuNum}
    </select>

    <select id="selectSpecialGroupsRelationAvailable" resultType="java.lang.Integer">
        select ifnull(sum(a1 + (#{extractNum} - t1)), 0)
        from (select gt.id,
                     (select count(*)
                      from groups_relation gr2
                      where gr2.groups_id = gt.id
                        and num > #{skuNum}
                        and num &lt;= #{maxNum})                         t1,
                     (select count(*)
                      from groups_relation gr2
                      where gr2.groups_id = gt.id
                        and num > #{skuNum}
                        and num &lt;= #{maxNum}
                        and (gr2.user_id = 0 or gr2.status = 'locking')) a1
              from account a
                       inner join `groups_trips` gt on gt.account_id = a.id
                  and gt.`status` = 'validity'
                  and gt.available_parking_num = 0
                  and gt.sku_id = #{skuId}
            and a.expiry_time >= #{tenExpiry}) s
        where (t1 = #{extractNum} and a1 = 0) = 0
    </select>

    <select id="selectNumWaitingGroups" resultType="java.lang.Integer">
        select count(gr.id)
        from `groups_trips` gt
                 inner join groups_relation gr on gr.groups_id = gt.id
            and gt.`status` = 'waiting'
            and gt.sku_id = #{skuId}
            and gr.user_id != 0
            and gr.status in ('validity','outside')
    </select>

    <select id="selectLimitAvailableNum" resultType="java.lang.Integer">
        select sum(s1 - s2)
        from (select available_parking_num          as s1,
                     (select count(*)
                      from groups_relation gr
                      where gr.groups_id = gt.id
                        and gr.status in ('validity', 'outside')
                        and gr.num &lt;= #{maxNum}) as s2
              from groups_trips gt
              where gt.sku_id = #{skuId}
                and gt.status = 'validity'
                and gt.available_parking_num != 0) t
        where t.s1 >= s2
    </select>

    <select id="getGroupRelationAccountView" resultType="com.cyksj.model.manage.views.GroupsRelationBackView">
        select s.account, a.account as tripsAccount
        from (select groups_id, account from groups_relation where id = #{relationId} limit 1) s
                 inner join groups_trips gt on gt.id = s.groups_id
                 left join account a on a.id = gt.account_id
    </select>

    <select id="getAreaPhoneByUserId" resultType="com.cyksj.model.entity.PhoneCode">
        select s.phone, pc.cid
        from (select if(login_phone != '', login_phone,
                        (select phone
                         from user_bind_detail
                         where user_id = u.id
                            or email_user_id = u.id limit 1)) as phone
              from user u
              where id = #{userId} limit 1) s
                 left join phone_code pc on pc.phone = s.phone
    </select>

    <select id="getUserHasPayGoods" resultType="com.cyksj.model.views.RenewalView">
        select g.id as goods_id,g.title,g.logo from (select distinct groups_id from groups_relation where status in
        ('validity', 'outside') and (expiry_time is null
        or
        expiry_time > #{now}) and user_id != 0 and user_id in <foreach
            collection="list" item="item" open="(" separator="," close=")">
        #{item}
    </foreach> ) gr inner join groups_trips gt on gt.id = gr.groups_id
        inner join goods_don_sku sku on sku.id = gt.sku_id inner join goods_don g on g.id = sku.goods_id
        group by g.id,g.title,g.logo
        order by goods_id
    </select>
</mapper>