<?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.czg.service.market.mapper.MkDistributionUserMapper">
    <update id="updateIncome">
        update mk_distribution_user
        set
            <if test="pendingIncome != null">
                total_income = total_income + #{pendingIncome},
            </if>
            <if test="receivedIncome != null">
                total_income = total_income + #{receivedIncome},
            </if>
            pending_income = pending_income + #{pendingIncome},
            received_income = received_income + #{receivedIncome},
            withdrawn_income = withdrawn_income + #{withdrawIncome}
        where id = #{id} and shop_id = #{shopId}
    </update>

    <select id="selectMyDistributionShops" resultType="com.czg.market.vo.DistributionCenterShopVO">
        select shop.id           as shopId,
               shop.cover_img    as coverImg,
               shop.shop_name    as shopName,
               shop.address      as shopAddress,
               user.total_income as income,
               config.is_enable as isEnable
        from mk_distribution_user user
                 left join tb_shop_info shop on user.shop_id = shop.id
                 left JOIN mk_distribution_config config ON user.shop_id = config.shop_id
        where user.user_id = #{userId}
          and user.status > 0
          and user.is_del = 0
    </select>
    <select id="selectUnDistributionShops" resultType="com.czg.market.vo.DistributionCenterShopVO">
        SELECT shop.id                                       AS shopId,
               shop.cover_img                                AS coverImg,
               shop.shop_name                                AS shopName,
               shop.address                                  AS shopAddress,
               config.open_type                              AS openType,
               config.invite_count                           AS shopInviteCount,
               IFNULL(du.invite_count, 0)                    AS userInviteCount,
               COUNT(DISTINCT ord.id)                        AS orderCount,
               CASE WHEN COUNT(shu.id) > 0 THEN 1 ELSE 0 END AS shopUser
        FROM tb_shop_info shop
                 inner JOIN mk_distribution_config config ON shop.id = config.shop_id and config.is_enable = 1
                 LEFT JOIN mk_distribution_user du ON shop.id = du.shop_id AND du.user_id = #{userId} AND du.is_del = 0
                 LEFT JOIN tb_shop_user shu
                           ON shop.id IN (shu.main_shop_id, shu.source_shop_id) AND shu.user_id = #{userId}
                 LEFT JOIN tb_order_info ord
                           ON shop.id = ord.shop_id AND ord.user_id = #{userId} AND ord.STATUS = 'done'
        where du.id IS NULL
           OR du.status = 0
        GROUP BY shop.id

    </select>

    <select id="getOrderConsumeAmountByList" resultType="java.math.BigDecimal">
        SELECT IFNULL(SUM(ord.pay_amount), 0) AS totalAmount
        FROM tb_shop_user_invite invite
                 INNER JOIN tb_shop_user su on invite.shop_user_id = su.id
                 INNER JOIN tb_order_info ord ON su.user_id = ord.user_id
            and invite.shop_id = #{shopId}
            AND ord.shop_id = #{shopId}
            AND ord.STATUS = 'done'
            AND ord.pay_type NOT IN ('vip_pay', 'credit_pay')
            AND ord.paid_time >= invite.invite_time
        WHERE invite.distribution_user_id = #{inviterId}
    </select>

    <select id="getOrderConsumeAmountById" resultType="java.math.BigDecimal">
        select IFNULL(SUM(pay_amount), 0) AS totalAmount
        from tb_order_info
        where shop_id = #{shopId}
          AND pay_type NOT IN ('vip_pay', 'credit_pay')
          and status = 'done'
          and user_id = #{userId}
    </select>
</mapper>
