<?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.yami.shop.distribution.comment.dao.DistributionUserIncomeMapper">
    <resultMap id="BaseResultMap" type="com.yami.shop.distribution.comment.model.DistributionUserIncome">
        <id column="income_id" jdbcType="BIGINT" property="incomeId"/>
        <result column="wallet_id" jdbcType="BIGINT" property="walletId"/>
        <result column="income_type" jdbcType="VARCHAR" property="incomeType"/>
        <result column="state" jdbcType="TINYINT" property="state"/>
        <result column="income_amount" jdbcType="DECIMAL" property="incomeAmount"/>
        <result column="order_item_id" jdbcType="BIGINT" property="orderItemId"/>
        <result column="merchant_order_id" jdbcType="BIGINT" property="merchantOrderId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>

    </resultMap>

    <!--推广效果-->
    <resultMap id="DistributionUserIncome_DistributionUser"
               type="com.yami.shop.distribution.comment.model.DistributionUserIncome">
        <id column="income_id" jdbcType="BIGINT" property="incomeId"/>
        <result column="wallet_id" jdbcType="BIGINT" property="walletId"/>
        <result column="income_type" jdbcType="VARCHAR" property="incomeType"/>
        <result column="state" jdbcType="TINYINT" property="state"/>
        <result column="income_amount" jdbcType="DECIMAL" property="incomeAmount"/>
        <result column="order_item_id" jdbcType="BIGINT" property="orderItemId"/>
        <result column="merchant_order_id" jdbcType="BIGINT" property="merchantOrderId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
        <association property="distributionUser" javaType="com.yami.shop.distribution.comment.vo.DistributionUserVO">
            <id column="distribution_user_id" jdbcType="BIGINT" property="distributionUserId"/>
            <result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
            <result column="user_mobile" jdbcType="VARCHAR" property="userMobile"/>
        </association>
        <association property="order" javaType="com.yami.shop.bean.model.Order">
            <result column="status" jdbcType="TINYINT" property="status"/>
        </association>
    </resultMap>
    <select id="incomeAndDistributionUserPage" resultMap="DistributionUserIncome_DistributionUser">
        select
        dui.*,
        du.nick_name,du.user_mobile,
        o.`status`
        from
        tz_distribution_user_income dui
        left join tz_distribution_user_wallet duw on duw.wallet_id = dui.wallet_id
        left join tz_distribution_user du on du.distribution_user_id=duw.distribution_user_id
        left join tz_order o on o.order_number = dui.order_number
        <where>
            <if test="shopId !=null">
                and du.shop_id=#{shopId}
            </if>
            <if test="rangeTimeParam.startTime !=null">
                and dui.create_time &gt;= #{rangeTimeParam.startTime}
            </if>
            <if test="state !=null">
                and dui.state = #{state}
            </if>
            <if test="rangeTimeParam.endTime !=null">
                and dui.create_time &lt;=#{rangeTimeParam.endTime}
            </if>
            <if test="userMobile!=null and userMobile!=''">
                and du.user_mobile like concat('%',#{userMobile},'%')
            </if>
            <if test="orderNumber != null and orderNumber != ''">
                and dui.order_number = #{orderNumber}
            </if>
        </where>
    </select>
    <!--分销推广订单 -->
    <resultMap id="DistributionOrderDtoMap" type="com.yami.shop.distribution.comment.dto.DistributionOrderDto">
        <result column="income_amount" property="incomeAmount"/>
        <result column="state" property="state"/>
        <result column="create_time" property="createTime"/>
        <result column="prod_id" property="prodId"/>
        <result column="prod_name" property="prodName"/>
        <result column="pic" property="pic"/>
    </resultMap>
    <select id="getDistributionOrderDtoByDistributionUserId" resultMap="DistributionOrderDtoMap">
            select
            dui.income_amount,
            dui.state,
            dui.create_time,
            o.pic,
            o.prod_id,
            o.prod_name
            from
            tz_distribution_user_income dui
            left join tz_order_item o on o.order_item_id=dui.order_item_id
            where dui.distribution_user_id=#{distributionUserId}
    </select>

    <update id="updateStateByOrderNumber">
        UPDATE tz_distribution_user_income
        SET state = #{state}
        where
        order_number = #{orderNumber}
    </update>


    <resultMap id="DistributionUserWallet" type="com.yami.shop.distribution.comment.model.DistributionUserWallet">
        <id column="wallet_id" jdbcType="BIGINT" property="walletId"/>
        <result column="distribution_user_id" jdbcType="BIGINT" property="distributionUserId"/>
        <result column="unsettled_amount" jdbcType="DECIMAL" property="unsettledAmount"/>
        <result column="settled_amount" jdbcType="DECIMAL" property="settledAmount"/>
        <result column="addup_amount" jdbcType="DECIMAL" property="addupAmount"/>
        <result column="version" jdbcType="INTEGER" property="version"/>
    </resultMap>

    <!--<select id="getWalletByOrderNumberAndIncomeType" resultMap="DistributionUserWallet">-->
    <!--select-->
    <!--wallet_id,-->
    <!--distribution_user_id,-->
    <!--unsettled_amount,-->
    <!--settled_amount,-->
    <!--addup_amount,-->
    <!--version-->
    <!--from-->
    <!--tz_distribution_user_wallet-->
    <!--where wallet_id in(-->
    <!--select-->
    <!--any_value(wallet_id)-->
    <!--from-->
    <!--tz_distribution_user_income-->
    <!--where order_number = #{orderNumber}-->
    <!--and-->
    <!--income_type= #{incomeType}-->
    <!--GROUP BY order_number-->
    <!--)-->
    <!--</select>-->

    <!--<select id="getIncomeAmountByOrderNumber" resultType="double">-->
    <!--select-->
    <!--sum(income_amount)-->
    <!--from-->
    <!--tz_distribution_user_income-->
    <!--where-->
    <!--order_number  = #{orderNumber}-->
    <!--</select>-->


    <resultMap id="DistributionAmountAndWalletVO" type="com.yami.shop.distribution.comment.vo.DistributionAmountAndWalletVO">
        <result column="wallet_id" jdbcType="BIGINT" property="distributionUserWalletId"/>
        <result column="amount" jdbcType="DECIMAL" property="amount"/>
        <result column="parent_wallet_id" jdbcType="DECIMAL" property="parentDistributionUserWalletId"/>
        <result column="parent_amount" jdbcType="DECIMAL" property="parentAmount"/>
    </resultMap>
    <select id="getDistributionAmountAndWalletVOByOrderNumber" resultMap="DistributionAmountAndWalletVO">
            SELECT
                son.wallet_id,
                son.amount,
                parent.parent_wallet_id,
                parent.parent_amount
            FROM
                (
            SELECT
                any_value ( wallet_id ) AS wallet_id,
                sum( income_amount ) AS amount,
                order_number
            FROM
                tz_distribution_user_income
            WHERE
                order_number = #{orderNumber}
                AND income_type = 1
            GROUP BY
                wallet_id
                )  son left join
                (
            SELECT
                any_value ( wallet_id ) AS parent_wallet_id,
                sum( income_amount ) AS parent_amount,
                order_number
            FROM
                tz_distribution_user_income
            WHERE
                order_number = #{orderNumber}
                AND income_type = 2
            GROUP BY
                wallet_id
                )  parent on son.amount = parent_amount
    </select>

    <update id="updateStateByDistributionUserId">
        update tz_distribution_user_income
        set state = #{state}
        where
        distribution_user_id = #{distributionUserId}
    </update>

    <resultMap id="DistributionSettlementDto" type="com.yami.shop.distribution.comment.dto.DistributionSettlementDto">
        <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
        <result column="status" jdbcType="TINYINT" property="status"/>
        <result column="sum_amount" jdbcType="DECIMAL" property="sumAmount"/>
    </resultMap>

    <update id="updateStateByOrderNumberAndOrderItem">
        UPDATE tz_distribution_user_income
        SET state = #{state}
        WHERE
        order_number = #{orderNumber}
        AND
        order_item_id = #{orderItemId}
    </update>

    <select id="statisticsDisUserIncome" resultType="double">
        SELECT COALESCE(SUM(income_amount),0)
        FROM tz_distribution_user_income dui
        WHERE dui.`distribution_user_id`= #{distributionUserId}  AND dui.`state` =2
        AND dui.`update_time` BETWEEN #{startTime} AND #{endTime}
    </select>

    <select id="getDistributionUserIncomePage" resultType="com.yami.shop.distribution.comment.dto.DistributionUserIncomeDto">
        SELECT dui.`income_amount`,dui.`income_type`,dui.`update_time`,dui.state
        FROM tz_distribution_user_income dui
        WHERE dui.`distribution_user_id` = #{distributionUserId}
        ORDER BY dui.`update_time` DESC
    </select>

    <resultMap id="distributionOrderMap" type="com.yami.shop.distribution.comment.dto.DistributionUserIncomeOrderDto">
        <result column="actual_total" jdbcType="DECIMAL" property="actualTotal"/>
        <result column="income_amount" jdbcType="DECIMAL" property="incomeAmount"/>
        <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
        <result column="prod_name" jdbcType="VARCHAR" property="prodName"/>
        <result column="product_nums" jdbcType="BIGINT" property="productNums"/>
        <collection property="orderItemDtoList" javaType="list"
                    ofType="com.yami.shop.distribution.comment.dto.DistributionOrderItemDto">
            <result column="prod_name" jdbcType="VARCHAR" property="prodName"/>
            <result column="pic" jdbcType="VARCHAR" property="pic"/>
            <result column="price" jdbcType="DECIMAL" property="price"/>
            <result column="prod_count" jdbcType="BIGINT" property="prodCount"/>
            <result column="sku_name" jdbcType="VARCHAR" property="skuName"/>
        </collection>
    </resultMap>
    <select id="getMyPromotionOrderByState" resultMap="distributionOrderMap">
        SELECT dui.`order_number`, dui.`income_amount`, o.`product_nums`, o.`prod_name`,o.`actual_total`,
        oi.`prod_name`, oi.`price`, oi.`pic`, oi.`prod_count`,oi.`sku_name`
        FROM
        tz_distribution_user_income dui
        LEFT JOIN tz_order o
        ON o.`order_number` = dui.`order_number`
        LEFT JOIN tz_order_item oi
        ON oi.`order_number` = o.`order_number`
        WHERE dui.`income_type` = 1
        AND dui.`distribution_user_id` = #{distributionUserId}
        <if test="state != null">
            AND dui.`state` = #{state}
        </if>
        ORDER BY dui.`update_time` DESC
    </select>

    <select id="listWaitCommissionSettlement" resultType="com.yami.shop.distribution.comment.model.DistributionUserIncome">
        SELECT dui.*,oi.*,o.*, tor.return_money_sts FROM tz_distribution_user_income dui
        JOIN tz_order_item oi ON dui.`order_item_id` = oi.`order_item_id`
        JOIN tz_order  o ON o.`order_number` = dui.`order_number`
            left join tz_order_refund tor on tor.order_item_id = oi.order_item_id
        WHERE dui.`state` = 1 -- 未结算收入
        AND o.`status` = 5  -- 已确认收货
        AND oi.`distribution_card_no` != '' and oi.`distribution_card_no` is not null-- 分销订单项
        AND o.finally_time &lt;= #{date} -- 在维权期后的订单
    </select>

     <update id="updateBatchState">
         UPDATE tz_distribution_user_income dui SET dui.`state` = #{state},dui.`update_time` = NOW()
         WHERE dui.`income_id` IN
         <foreach collection="distributionUserIncomeList" item="distributionUserIncome"  open="(" separator="," close=")">
             #{distributionUserIncome.incomeId}
         </foreach>
     </update>
</mapper>
