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

    <select id="pageInfo" resultType="com.czg.market.vo.MkDistributionFlowVO">
        select distinct a.*, b.phone, c.phone as sourcePhone, e.shop_name from mk_distribution_flow as a
        left join mk_distribution_user as d on d.id=a.distribution_user_id
        left join tb_shop_info as e on e.id=a.shop_id
        left join tb_shop_user as b on a.shop_user_id=b.id
        left join tb_shop_user as c on c.id=a.source_shop_user_id
        <where>
            <if test="shopId != null">
                and a.shop_id=#{shopId}
            </if>

            <if test="startTime != null">
                and a.create_time>=#{startTime}
            </if>
            <if test="endTime != null">
                and a.create_time&lt;=#{endTime}
            </if>
            <if test="status != null and status != ''">
                and a.status=#{status}
            </if>
            <if test="key != null and key != ''">
                and (
                b.nick_name like concat('%',#{key},'%')
                or b.id like concat('%',#{key},'%')
                or c.id like concat('%',#{key},'%')
                or c.nick_name like concat('%',#{key},'%')
                )
            </if>
            <if test="id != null">
                and d.id=#{id}
            </if>
            <if test="userId != null">
                and d.user_id=#{userId}
            </if>
            <if test="type != null and type != ''">
                and a.type=#{type}
            </if>
        </where>
        order by a.create_time desc
    </select>
    <select id="totalAmount" resultType="java.math.BigDecimal">
        select sum(a.amount) from mk_distribution_flow as a
        left join mk_distribution_user as d on d.id=a.distribution_user_id
        left join tb_shop_user as b on a.shop_user_id=b.id
        left join tb_shop_user as c on c.id=a.shop_user_id
        where a.shop_id=#{shopId}
        <if test="startTime != null">
            and a.create_time>=#{startTime}
        </if>
        <if test="endTime != null">
            and a.create_time&lt;=#{endTime}
        </if>
        <if test="status != null and status != ''">
            and a.status=#{status}
        </if>
        <if test="key != null and key != ''">
            and (
            b.nick_name like concat('%',#{key},'%')
            or b.id like concat('%',#{key},'%')
            or c.id like concat('%',#{key},'%')
            or c.nick_name like concat('%',#{key},'%')
            )
        </if>
        <if test="id != null">
            and d.id=#{id}
        </if>
        <if test="type != null and type != ''">
            and a.type=#{type}
        </if>
        <if test="userId != null">
            and d.user_id=#{userId}
        </if>
    </select>
</mapper>
