<?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.sqx.modules.app.dao.UserMoneyDetailsDao">

    <select id="selectUserMoneyDetails" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        select *
        from user_money_details
        where user_id = #{userId}
          and classify = #{classify}
        order by create_time desc
    </select>

    <select id="selectShopCashDeposit" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        select *
        from user_money_details
        where shop_id = #{shopId}
          and classify = #{classify}
        order by create_time desc
    </select>

    <select id="selectShopCashDepositList" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        select *
        from user_money_details
        where shop_id = #{shopId}
          and classify = 2
        order by create_time desc
    </select>

    <sql id="walletQueryCondition">
        <where>
            <if test="query.shopName != null and query.shopName != ''">
                and gs.shop_name like concat("%", #{query.shopName}, "%")
            </if>

            <if test="query.type != null and query.type != ''">
                and umd.type = #{query.type}
            </if>

            <if test="query.classify != null and query.classify != ''">
                and umd.classify = #{query.classify}
            </if>

            <if test="query.startTime != null and query.startTime != ''">
                and umd.create_time >= #{query.startTime}
            </if>

            <if test="query.endTime != null and query.endTime != ''">
                and umd.create_time <![CDATA[<=]]> #{query.endTime}</if>
        </where>
    </sql>

    <select id="walletDetailList" resultType="com.sqx.modules.pay.vo.WalletDetailVO">
        select
        umd.id,
        gs.shop_name,
        umd.title,
        umd.content,
        umd.type,
        umd.classify,
        umd.money,
        umd.create_time
        from
        user_money_details umd
        left join goods_shop gs on gs.shop_id = umd.shop_id
        <include refid="walletQueryCondition"></include>
        order by umd.create_time desc
    </select>

    <select id="excelWalletDetails" resultType="com.sqx.modules.pay.vo.WalletDetailVO">
        select
        umd.id,
        gs.shop_name,
        umd.title,
        umd.content,
        umd.type,
        umd.classify,
        umd.money,
        umd.create_time
        from
        user_money_details umd
        left join goods_shop gs on gs.shop_id = umd.shop_id
        <include refid="walletQueryCondition"></include>
        order by umd.create_time desc
    </select>

    <select id="walletDetailStatistics" resultType="java.math.BigDecimal">
        select
        sum(o.amount)
        from
        (
        select
        case
        when umd.type = 1 then umd.money
        else -umd.money
        end as amount
        from
        user_money_details umd
        left join goods_shop gs on gs.shop_id = umd.shop_id
        <include refid="walletQueryCondition"></include>
        order by umd.create_time desc
        ) o
    </select>

    <select id="accountEntryManagementDetailsList" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        SELECT
        umd2.id,
        umd2.orderNumber,
        umd2.user_id as userId,
        umd2.shop_id as shopId,
        umd2.title,
        umd2.content,
        umd2.type,
        umd2.classify,
        umd2.money,
        umd2.create_time as createTime,
        umd2.state,
        tbu.user_name as userName,
        tbu.phone as userPhone,
        gs.shop_name as shopName,
        gs.phone as shopPhone,
        tcu.coupon_name as couponName,
        tcu.money as couponMoney,
        apr.discount_amount as activityDiscountAmount,
        tbo.errand_money as errandMoney,
        tbo.pay_money as payMoney,
        tbo.order_id as orderId,
        tbo.order_type_extra as orderTypeExtra
        FROM
        (
        SELECT
        RIGHT( umd.title, 18 ) AS orderNumber,
        umd.id,
        umd.user_id,
        umd.shop_id,
        umd.title,
        umd.content,
        umd.type,
        umd.classify,
        umd.money,
        umd.create_time,
        umd.state,
        umd.children_id
        FROM
        `user_money_details` umd
        WHERE
        umd.type = 1
        AND umd.classify = 3
        AND umd.shop_id IS NOT NULL
        AND umd.state=2
        AND umd.title LIKE '商户订单收入' '%'
        ) umd2
        LEFT JOIN tb_order tbo on tbo.order_number=umd2.orderNumber
        LEFT JOIN tb_user tbu on tbu.user_id=tbo.user_id
        LEFT JOIN goods_shop gs on gs.shop_id=tbo.shop_id
        LEFT JOIN tb_coupon_user tcu on tbo.coupon_id = tcu.id
        LEFT JOIN activity_part_record apr on apr.order_id = tbo.order_id
        where 1 = 1
        <if test="shopName != null and shopName != ''">
            and gs.shop_name like  concat("%", #{shopName}, "%")
        </if>

        <if test="shopPhone != null and shopPhone != ''">
            and gs.phone= #{shopPhone}
        </if>

        <if test="userPhone != null and userPhone != ''">
            and tbu.phone=#{userPhone}
        </if>

        <if test="couponName != null and couponName != ''">
            and tcu.coupon_name like  concat("%", #{couponName}, "%")
        </if>

        <if test="orderNumber != null and orderNumber != ''">
            and tbo.order_number = #{orderNumber}
        </if>

        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and umd2.create_time >= #{startTime} and #{endTime} >= umd2.create_time
        </if>
        ORDER BY umd2.id desc
        LIMIT #{limit} OFFSET #{row}
    </select>

    <select id="accountEntryManagementDetailsCount" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        (
        SELECT
        RIGHT( umd.title, 18 ) AS orderNumber,
        umd.id,
        umd.user_id,
        umd.shop_id,
        umd.title,
        umd.content,
        umd.type,
        umd.classify,
        umd.money,
        umd.create_time,
        umd.state,
        umd.children_id
        FROM
        `user_money_details` umd
        WHERE
        umd.type = 1
        AND umd.classify = 3
        AND umd.shop_id IS NOT NULL
        AND umd.state=2
        AND umd.title LIKE '商户订单收入' '%'
        ) umd2
        LEFT JOIN tb_order tbo on tbo.order_number=umd2.orderNumber
        LEFT JOIN tb_user tbu on tbu.user_id=tbo.user_id
        LEFT JOIN goods_shop gs on gs.shop_id=tbo.shop_id
        LEFT JOIN tb_coupon_user tcu on tbo.coupon_id = tcu.id
        LEFT JOIN activity_part_record apr on apr.order_id = tbo.order_id
        where 1 = 1
        <if test="shopName != null and shopName != ''">
            and gs.shop_name like concat("%", #{shopName}, "%")
        </if>

        <if test="shopPhone != null and shopPhone != ''">
            and gs.phone= #{shopPhone}
        </if>

        <if test="userPhone != null and userPhone != ''">
            and tbu.phone=#{userPhone}
        </if>

        <if test="couponName != null and couponName != ''">
            and tcu.coupon_name like concat("%", #{couponName}, "%")
        </if>

        <if test="orderNumber != null and orderNumber != ''">
            and tbo.order_number = #{orderNumber}
        </if>

        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and umd2.create_time >= #{startTime} and #{endTime} >= umd2.create_time
        </if>
    </select>
    <select id="excelAccountEntry" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        SELECT
        umd2.id,
        umd2.orderNumber,
        umd2.user_id as userId,
        umd2.shop_id as shopId,
        umd2.title,
        umd2.content,
        umd2.type,
        umd2.classify,
        umd2.money,
        umd2.create_time as createTime,
        umd2.state,
        tbu.user_name as userName,
        tbu.phone as userPhone,
        gs.shop_name as shopName,
        gs.phone as shopPhone,
        tcu.coupon_name as couponName,
        tcu.money as couponMoney,
        apr.discount_amount as activityDiscountAmount,
        tbo.errand_money as errandMoney
        FROM
        (
        SELECT
        RIGHT( umd.title, 18 ) AS orderNumber,
        umd.id,
        umd.user_id,
        umd.shop_id,
        umd.title,
        umd.content,
        umd.type,
        umd.classify,
        umd.money,
        umd.create_time,
        umd.state,
        umd.children_id
        FROM
        `user_money_details` umd
        WHERE
        umd.type = 1
        AND umd.classify = 3
        AND umd.shop_id IS NOT NULL
        AND umd.state=2
        AND umd.title LIKE '商户订单收入' '%'
        ) umd2
        LEFT JOIN tb_order tbo on tbo.order_number=umd2.orderNumber
        LEFT JOIN tb_user tbu on tbu.user_id=tbo.user_id
        LEFT JOIN goods_shop gs on gs.shop_id=tbo.shop_id
        LEFT JOIN tb_coupon_user tcu on tbo.coupon_id = tcu.id
        LEFT JOIN activity_part_record apr on apr.order_id = tbo.order_id
        where 1 = 1
        <if test="query.shopName != null and query.shopName != ''">
            and gs.shop_name like  concat("%", #{query.shopName}, "%")
        </if>

        <if test="query.shopPhone != null and query.shopPhone != ''">
            and gs.phone= #{query.shopPhone}
        </if>

        <if test="query.userPhone != null and query.userPhone != ''">
            and tbu.phone=#{query.userPhone}
        </if>

        <if test="query.couponName != null and query.couponName != ''">
            and tcu.coupon_name like  concat("%", #{query.couponName}, "%")
        </if>

        <if test="query.orderNumber != null and query.orderNumber != ''">
            and tbo.order_number = #{query.orderNumber}
        </if>

        <if test="query.startTime != null and query.startTime != '' and query.endTime != null and query.endTime != ''">
            and umd2.create_time >= #{query.startTime} and #{query.endTime} >= umd2.create_time
        </if>
        ORDER BY umd2.id desc
    </select>

    <select id="accountEntryAbnormal" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        SELECT
        umd2.id,
        umd2.orderNumber,
        umd2.user_id as userId,
        umd2.shop_id as shopId,
        umd2.title,
        umd2.content,
        umd2.type,
        umd2.classify,
        umd2.money,
        umd2.create_time as createTime,
        umd2.state,
        tbu.user_name as userName,
        tbu.phone as userPhone,
        gs.shop_name as shopName,
        gs.phone as shopPhone,
        tcu.coupon_name as couponName,
        tcu.money as couponMoney,
        apr.discount_amount as activityDiscountAmount,
        tbo.errand_money as errandMoney
        FROM
        (
        SELECT
        RIGHT( umd.title, 18 ) AS orderNumber,
        umd.id,
        umd.user_id,
        umd.shop_id,
        umd.title,
        umd.content,
        umd.type,
        umd.classify,
        umd.money,
        umd.create_time,
        umd.state,
        umd.children_id
        FROM
        `user_money_details` umd
        WHERE
        umd.type = 1
        AND umd.classify = 3
        AND umd.shop_id IS NOT NULL
        AND umd.state=2
        AND EXISTS
        (select
        umd3.id
        from
        `user_money_details` umd3
        where
        umd3.title like '商户订单收入%'
        and umd.title =umd3.title
        group by umd3.title
        HAVING COUNT(*) > 1)
        ) umd2
        LEFT JOIN tb_order tbo on tbo.order_number=umd2.orderNumber
        LEFT JOIN tb_user tbu on tbu.user_id=tbo.user_id
        LEFT JOIN goods_shop gs on gs.shop_id=tbo.shop_id
        LEFT JOIN tb_coupon_user tcu on tbo.coupon_id = tcu.id
        LEFT JOIN activity_part_record apr on apr.order_id = tbo.order_id
        where 1 = 1
        <if test="query.shopName != null and query.shopName != ''">
            and gs.shop_name like  concat("%", #{query.shopName}, "%")
        </if>

        <if test="query.shopPhone != null and query.shopPhone != ''">
            and gs.phone= #{query.shopPhone}
        </if>

        <if test="query.userPhone != null and query.userPhone != ''">
            and tbu.phone=#{query.userPhone}
        </if>

        <if test="query.couponName != null and query.couponName != ''">
            and tcu.coupon_name like  concat("%", #{query.couponName}, "%")
        </if>

        <if test="query.orderNumber != null and query.orderNumber != ''">
            and tbo.order_number = #{query.orderNumber}
        </if>

        <if test="query.startTime != null and query.startTime != '' and query.endTime != null and query.endTime != ''">
            and umd2.create_time >= #{query.startTime} and #{query.endTime} >= umd2.create_time
        </if>
        ORDER BY umd2.id desc
    </select>

    <select id="excelAccountEntryAbnormal" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        SELECT
        umd2.id,
        umd2.orderNumber,
        umd2.user_id as userId,
        umd2.shop_id as shopId,
        umd2.title,
        umd2.content,
        umd2.type,
        umd2.classify,
        umd2.money,
        umd2.create_time as createTime,
        umd2.state,
        tbu.user_name as userName,
        tbu.phone as userPhone,
        gs.shop_name as shopName,
        gs.phone as shopPhone,
        tcu.coupon_name as couponName,
        tcu.money as couponMoney,
        apr.discount_amount as activityDiscountAmount,
        tbo.errand_money as errandMoney
        FROM
        (
        SELECT
        RIGHT( umd.title, 18 ) AS orderNumber,
        umd.id,
        umd.user_id,
        umd.shop_id,
        umd.title,
        umd.content,
        umd.type,
        umd.classify,
        umd.money,
        umd.create_time,
        umd.state,
        umd.children_id
        FROM
        `user_money_details` umd
        WHERE
        umd.type = 1
        AND umd.classify = 3
        AND umd.shop_id IS NOT NULL
        AND umd.state=2
        AND EXISTS
        (select
        umd3.id
        from
        `user_money_details` umd3
        where
        umd3.title like '商户订单收入%'
        and umd.title =umd3.title
        group by umd3.title
        HAVING COUNT(*) > 1)
        ) umd2
        LEFT JOIN tb_order tbo on tbo.order_number=umd2.orderNumber
        LEFT JOIN tb_user tbu on tbu.user_id=tbo.user_id
        LEFT JOIN goods_shop gs on gs.shop_id=tbo.shop_id
        LEFT JOIN tb_coupon_user tcu on tbo.coupon_id = tcu.id
        LEFT JOIN activity_part_record apr on apr.order_id = tbo.order_id
        where 1 = 1
        <if test="query.shopName != null and query.shopName != ''">
            and gs.shop_name like  concat("%", #{query.shopName}, "%")
        </if>

        <if test="query.shopPhone != null and query.shopPhone != ''">
            and gs.phone= #{query.shopPhone}
        </if>

        <if test="query.userPhone != null and query.userPhone != ''">
            and tbu.phone=#{query.userPhone}
        </if>

        <if test="query.couponName != null and query.couponName != ''">
            and tcu.coupon_name like  concat("%", #{query.couponName}, "%")
        </if>

        <if test="query.orderNumber != null and query.orderNumber != ''">
            and tbo.order_number = #{query.orderNumber}
        </if>

        <if test="query.startTime != null and query.startTime != '' and query.endTime != null and query.endTime != ''">
            and umd2.create_time >= #{query.startTime} and #{query.endTime} >= umd2.create_time
        </if>
        ORDER BY umd2.id desc
    </select>
</mapper>