<?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.manage.distribute.UserDistributeMapper">

    <select id="count" resultType="java.lang.Integer">
        select count(*)
        from distribute_waiting_send_points dw
        where dw.shared_id = #{userId}
    </select>

    <select id="countUserNum" resultType="java.lang.Integer">
        select count(distinct us.user_id)
        from `user_distribute_shared` us
        where us.shared_id = #{userId}
    </select>

    <select id="getHasPayOrdersBySharedId" resultType="java.math.BigDecimal">
        select ifnull(sum(od.money),0) from `user_distribute_shared` us left join `order_don` od on od.user_id =
        us.user_id
        where us.shared_id = #{userId}
        and is_fixed_distribute is true
        and od.status not in
        <foreach collection="noOrderStatus" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and od.created_time >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and od.created_time &lt; #{endTime}
        </if>
    </select>

    <select id="countHasPayBySharedId" resultType="java.math.BigDecimal">
        select count(distinct od.user_id) from `user_distribute_shared` us left join `order_don` od on od.user_id =
        us.user_id
        where us.shared_id = #{userId}
        and is_fixed_distribute is true
        and od.status not in
        <foreach collection="noOrderStatus" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and od.created_time >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and od.created_time &lt; #{endTime}
        </if>
    </select>

    <select id="selectSoldMaxGoods" resultType="java.lang.String">
        select title
        from goods_don
        where id = (select goods_id
                    from (select user_id from user_distribute_shared where shared_id = #{userId}) s
                             inner join order_don od
                                        on od.user_id = s.user_id
                    where od.status not in ('close', 'noPayment', 'refund')
                      and od.is_fixed_distribute = 1
                      and od.relation_id != 0
                    group by goods_id
                    order by max(money) desc limit 1) limit 1
    </select>

    <select id="getApplyDetailByUserId" resultType="com.cyksj.model.views.UserDistributeViews">
        select ifnull(sum(da.money), 0) as gotMoney, cast(u.points / 10 as decimal(10, 2)) as availableMoney
        from (select id, points from user where id = #{userId} limit 1) u
                 left join distribute_money_apply da on da.user_id = u.id and verify_status = 'success'
        group by u.id, u.points
    </select>

    <select id="getUserDistributeDetail" resultType="com.cyksj.model.entity.User">
        select u.* from (select * from user
        <where>
            <if test="loginPhone!=null and loginPhone != ''">
                login_phone = #{loginPhone}
            </if>
            <if test="email != null and email != ''">
                and email = #{email}
            </if>
            limit 1
        </where>
        )u inner join user_distribute ud on ud.user_id = u.id and ud.deleted is true limit 1
    </select>

    <select id="getDistributeGoodsRateByUidAndGid" resultType="java.lang.Integer">
        select udr.rate
        from (select shared_id from user_distribute_shared where user_id = #{userId} limit 1) us
                 inner join user_distribute ud on ud.user_id = us.shared_id and ud.deleted is true
                 inner join user_plat_distribute_rate udr
                            on udr.distribute_id = ud.id
        where udr.goods_id = #{goodsId}
    </select>

    <select id="selectRelationBusiness" resultType="com.cyksj.model.entity.DistributeMoneyApply">
        select ud.business_id, cu.nickname as businessName
        from (select business_id from user_distribute where user_id = #{userId} and deleted is true limit 1) ud
                 inner join cms_user cu on cu.id = ud.business_id
    </select>

    <select id="selectOrderBrokenLineMoney" resultType="com.cyksj.model.views.CpsOrderDataStatisticsView">
        select min(curdate) as firstDate,
        max(curdate) as lastDate
        from (select date_format(ifnull(od.created_time,vod.created_time),'%Y-%m-%d') curdate,
        count(dw.order_id) curdateOrderCount
        from distribute_waiting_send_points dw
        left join order_don od on od.id = dw.order_id and dw.is_vip = false
        left join vip_order_don vod on vod.id = dw.order_id and dw.is_vip = true
        where dw.shared_id = #{cpsUserId}
        and dw.send_status not in ('not_exist', 'close')
        group by ifnull(od.goods_id,vod.goods_id), date_format(ifnull(od.created_time,vod.created_time), '%Y-%m-%d'))s
        where 1 = 1
        <if test="first != null">
            and curdate >= #{first}
        </if>
        <if test="end != null">
            and curdate &lt; #{end}
        </if>
    </select>
</mapper>