<?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.mirrorshop.UserMirrorShopMapper">


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

    <select id="selectUserMIrrorShopDetail" resultType="java.util.Map">
        select count(distinct ys.user_id) as numOfYhShop,
        count(distinct us.user_id) as numOfRegisterYhShopUser
        from (select user_id from `user_mirror_shop` ys
        where 1 = 1
        <if test="startDate != null">
            and created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and created_time &lt; #{endDate}
        </if>) ys left join user_mirror_shop_user_distribute_shared us on us.shop_user_id = ys.user_id
    </select>

    <select id="selectUserMirrorShopTakeMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(money),0) from `user_mirror_shop_distribute_waiting_send_points` where send_status in
        ('waiting','success')
        <if test="startDate != null">
            and created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and created_time &lt; #{endDate}
        </if>
    </select>

    <select id="selectUserMirrorShopApplyMoney" resultType="java.util.Map">
        select ifnull(sum(if(verify_status = 'success',money,0)),0)s,
        ifnull(sum(if(verify_status = 'verifying',money,0)),0)vf
        from user_mirror_shop_distribute_money_apply where verify_status in ('verifying','success')
        <if test="startDate != null">
            and created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and created_time &lt; #{endDate}
        </if>
    </select>

    <select id="selectOrderBrokenLineMoney" resultType="com.cyksj.model.views.UserMirrorShopOrderDataStatisticsView">
        select min(curdate) as firstDate,max(curdate) as lastDate from (select date_format(od.created_time,'%Y-%m-%d')
        as curdate
        from (select o.id,o.goods_id,o.created_time from order_don o
        inner join user_mirror_shop_order_relate uor on uor.order_id = o.id and uor.mp_id = #{mpId} where
        o.status not in ('close','noPayment')
        <if test="first != null">
            and o.created_time >= #{first}
        </if>
        <if test="end != null">
            and o.created_time &lt; #{end}
        </if>)od
        left join goods_don g on g.id = od.goods_id
        inner join user_mirror_shop_distribute_waiting_send_points dp on dp.order_id = od.id
        group by date_format(od.created_time,'%Y-%m-%d'))s
    </select>
</mapper>