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

    <select id="getExpiredAccountView" resultType="com.cyksj.model.views.ExpiredAccountDataView">
        select gr.id as relationId,
               gd.title as platform,
               a.account,
               a.password,
               a.us_account,
               a.cn_account,
               concat(gdk.spec_val,'¥',cast(gdk.price/100 as decimal(10,2))) as 'specVal',
               gr.user_id,
               gr.account as userAccount,
               u.headimgurl as img,
               u.nickname,
               gr.start_time as payTime,
               gr.start_time,
               gr.expiry_time,
               gr.renew_status,
               gr.is_handle,
               gr.customer_service,
               if(cu.id is null,false,true) as isCorpWx
        from `account` a left join `groups_trips` gt on gt.account_id = a.id
        left join `groups_relation` gr on gr.groups_id = gt.id
        left join `goods_don_sku` gdk on gdk.id = gt.sku_id
        left join `goods_don` gd on gd.id = a.goods_id
        left join `user` u on gr.user_id = u.id
        left join (select id,unionid from `corp_user` where unionid != '') cu on cu.unionid = u.unionid
        <where>
            <if test="isCorpWx != null and isCorpWx">
                cu.id is not null
            </if>
            <if test="isCorpWx != null and !isCorpWx">
                cu.id is null
            </if>
            <if test="customerService != null and customerService != '' and customerService != 'waiting'">
                and gr.customer_service = #{customerService}
            </if>
            <if test="customerService == 'waiting'">
                and gr.customer_service is null
            </if>
            <if test="goodsId != null">
                and a.goods_id = #{goodsId}
            </if>
            <if test="skuId != null">
                and gdk.id = #{skuId}
            </if>
            <if test="account != null and account != ''">
                and a.account like '%${account}%'
            </if>
            <if test="renewStatus != null">
                and gr.renew_status is ${renewStatus}
            </if>
            <if test="handleStatus != null">
                and gr.is_handle is ${handleStatus}
            </if>
            <if test="cnAccount != null and cnAccount != ''">
                and a.cn_account = #{cnAccount}
            </if>
            <if test="usAccount != null and usAccount != ''">
                and a.us_account = #{usAccount}
            </if>
            <if test="userId != null">
                and gr.user_id = #{userId}
            </if>
            <if test="startTime != null">
                and gr.expiry_time >= #{startTime}
            </if>
            <if test="endTime != null">
                and gr.expiry_time &lt; #{endTime}
            </if>
                and gr.expiry_time > #{now}
                order by expiry_time
        </where>
    </select>

    <select id="getNoAppleOneOrderDon" resultType="com.cyksj.model.entity.OrderDon">
        select distinct od.* from `order_don` od
        where od.sku_id in
        <foreach collection="skuIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and od.status not in
        <foreach collection="noOrderStatus" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and od.created_time between #{yesZeroDate} and #{thisZeroDate}
    </select>

    <select id="getExpiredAccountViewNoPage" resultType="com.cyksj.model.excel.ExcelAccountData">
        select gr.id as relationId,
        gd.title as platform,
        a.account,
        a.password,
        a.us_account,
        a.cn_account,
        concat(gdk.spec_val,'¥',cast(gdk.price/100 as decimal(10,2))) as 'type',
        gr.user_id,
        gr.account as userAccount,
        u.headimgurl as img,
        u.nickname,
        gr.start_time as payTime,
        gr.expiry_time,
        gr.renew_status,
        gr.is_handle,
        gr.customer_service,
        if(cu.id is null,'未加入','已加入') as isCorpWx
        from `account` a left join `groups_trips` gt on gt.account_id = a.id
        left join `groups_relation` gr on gr.groups_id = gt.id
        left join `goods_don_sku` gdk on gdk.id = gt.sku_id
        left join `goods_don` gd on gd.id = a.goods_id
        left join `user` u on gr.user_id = u.id
        left join (select id,unionid from `corp_user` where unionid != '') cu on cu.unionid = u.unionid
        <where>
            <if test="isCorpWx != null and isCorpWx">
                cu.id is not null
            </if>
            <if test="isCorpWx != null and !isCorpWx">
                cu.id is null
            </if>
            <if test="customerService != null and customerService != '' and customerService != 'waiting'">
                and gr.customer_service = #{customerService}
            </if>
            <if test="customerService == 'waiting'">
                and gr.customer_service is null
            </if>
            <if test="goodsId != null">
                and a.goods_id = #{goodsId}
            </if>
            <if test="skuId != null">
                and gdk.id = #{skuId}
            </if>
            <if test="account != null and account != ''">
                and a.account like '%${account}%'
            </if>
            <if test="renewStatus != null">
                and gr.renew_status is ${renewStatus}
            </if>
            <if test="handleStatus != null">
                and gr.is_handle is ${handleStatus}
            </if>
            <if test="cnAccount != null and cnAccount != ''">
                and a.cn_account = #{cnAccount}
            </if>
            <if test="usAccount != null and usAccount != ''">
                and a.us_account = #{usAccount}
            </if>
            <if test="userId != null">
                and gr.user_id = #{userId}
            </if>
            <if test="startTime != null">
                and gr.expiry_time >= #{startTime}
            </if>
            <if test="endTime != null">
                and gr.expiry_time &lt; #{endTime}
            </if>
            and gr.expiry_time > #{now}
            order by expiry_time
        </where>
    </select>

    <select id="getExpiredAllAccount" resultType="com.cyksj.model.manage.views.AccountView">
        select a.id,
               a.account,
               a.password,
               a.goods_id,
               a.bank_card,
               a.start_time,
               a.expiry_time,
               a.amount,
               a.remark,
               a.cn_account,
               a.us_account,
               us_account,
               a.created_time,
               a.update_time,
               g.title
        from (select s.account_id, count(*) num
              from (select ur.account_id, ur.relation_id
                    from user_ticket_cleared_record ur
                    where ur.deleted is true
                    group by ur.account_id, ur.relation_id) s
              group by s.account_id) s
                 inner join account a on a.id = s.account_id
                 left join groups_trips gt on gt.account_id = s.account_id
                 left join goods_don g on g.id = a.goods_id
        where s.num >= gt.num
    </select>

    <select id="selectNetflixTempAccount" resultType="com.cyksj.model.entity.Account">
        select a.*
        from account a
        where goods_id = 1
          and type = 3
          and (select count(1)
               from netflix_temp_account_user ntu
                        inner join groups_relation gr on gr.id = ntu.relation_id and ntu.user_id = gr.user_id
               where ntu.account_id = a.id) &lt; 20
            limit 1
    </select>
</mapper>