<?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.pay.dao.CashOutDao">

    <select id="selectCashOutLimit3" resultType="com.sqx.modules.pay.entity.CashOut">
        select * from cash_out where state=1 order by out_at desc limit 3
    </select>

    <select id="selectYesterday" resultType="com.sqx.modules.pay.entity.CashOut">
        SELECT * FROM cash_out
        WHERE
        state = 0 AND user_type = 1
        AND create_at &lt; DATE_FORMAT(NOW(), '%Y-%m-%d 00:00:00' )
        AND create_at &gt; DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 DAY), '%Y-%m-%d 00:00:00')
    </select>

    <select id="selectTemp" resultType="com.sqx.modules.pay.entity.CashOut">
        SELECT * FROM cash_out
        WHERE
            (state = 0 or state=4) and order_number is not null and order_number != '' and create_at &lt; #{time}
    </select>

    <select id="selectCashOutSum" resultType="Double">
        select sum(money) from cash_out where state in (0,1) and user_id=#{userId} and date_format(create_at,'%Y-%m-%d') between #{startTime} and #{endTime}
    </select>

    <select id="selectSysUserCashOutSum" resultType="Double">
        select sum(money) from cash_out where state = 1
          <if test="sysUserId != null">
          and user_id=#{sysUserId}
          </if>
          and user_type = 2
          and create_at &gt; #{time}
    </select>

    <select id="sumMoney" resultType="Double">
        select sum(money) from cash_out where state = 1 and  create_at between #{start} and #{end}
    </select>

    <select id="countMoney" resultType="Integer">
        select count(*) from cash_out where  create_at between #{start} and #{end}
    </select>

    <select id="stayMoney" resultType="Integer">
        select count(*) from cash_out where  create_at between #{start} and #{end}   and state=0
    </select>

    <select id="selectMayMoney" resultType="Double">
     select money from user_money  where  user_id=#{userId}
    </select>

    <select id="selectSumByUserIdList" resultType="com.sqx.modules.pay.entity.CashOut">
        SELECT
            user_id,
            ROUND( sum( money ), 2 ) AS total,
            count(*) AS count
        FROM
            cash_out
        WHERE
            state = #{state}
        and user_id in
        <foreach collection="userIdList" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        GROUP BY
            user_id
    </select>


    <select id="countByUserIdList" resultType="com.sqx.modules.pay.entity.CashOut">
        SELECT user_id as userId,
               ROUND(SUM(CASE WHEN state = 1 THEN money ELSE 0 END), 2) AS total,
               COUNT(CASE WHEN state = 1 THEN 1 END)                    AS count,
               ROUND(SUM(CASE WHEN state = 3 THEN money ELSE 0 END), 2) AS verifyTotal,
               COUNT(CASE WHEN state = 3 THEN 1 END)                    AS verifyCount
        FROM cash_out
        GROUP BY user_id;

    </select>


    <select id="selectCashOutPage" resultType="com.sqx.modules.pay.entity.CashOut">
        SELECT c.*,
        ifnull(u.user_name,"用户不存在") as userName
#         s.username as sysUserName
        <if test="isApp == false">
            ,ifnull(t.total,0.00) as total,
            ifnull(t.count,0) as count,
            ifnull(t1.total,0.00) as verifyTotal,
            ifnull(t1.count,0) as verifyCount
        </if>
        FROM cash_out c
        left join tb_user u on c.user_id = u.user_id
#         left join sys_user s on s.user_id= c.sys_user_id
        <if test="isApp == false">
            left join (select user_id,ROUND(sum(money),2) as total,count(*) as count from cash_out where state = 1 GROUP BY user_id) t on c.user_id = t.user_id
            left join (select user_id,ROUND(sum(money),2) as total,count(*) as count from cash_out where state = 3 GROUP BY user_id) t1 on c.user_id = t1.user_id
        </if>
        <where>
            <if test="cashOut.userName!=null and cashOut.userName!=''">
                and u.user_name like concat("%",#{cashOut.userName},"%")
            </if>
            <if test="cashOut.state!=null">
                and c.state = #{cashOut.state}
            </if>
            <if test="cashOut.userId!=null">
                and c.user_id = #{cashOut.userId}
            </if>
            <if test="cashOut.sysUserId!=null">
                and c.sys_user_id = #{cashOut.sysUserId}
            </if>
<!--            <if test="cashOut.sysUserName!=null and cashOut.sysUserName!=''">-->
<!--                and s.username = #{cashOut.sysUserName}-->
<!--            </if>-->
            <if test="cashOut.userType!=null">
                and c.user_type = #{cashOut.userType}
            </if>
            <if test="cashOut.status!=null and cashOut.status!=-2">
                and c.state = #{cashOut.status}
            </if>
            <if test="cashOut.zhifubao!=null and cashOut.zhifubao!=''">
                and c.zhifubao like concat("%",#{cashOut.zhifubao},"%")
            </if>
            <if test="cashOut.zhifubaoName!=null and cashOut.zhifubaoName!=''">
                and c.zhifubao_name like concat("%",#{cashOut.zhifubaoName},"%")
            </if>
            <if test="cashOut.startTime!=null and cashOut.startTime!='' and (cashOut.endTime!=null and cashOut.endTime!='')">
                and date_format(c.create_at,'%Y-%m-%d') BETWEEN #{cashOut.startTime} and #{cashOut.endTime}
            </if>
            <if test="cashOut.startTime!=null and cashOut.startTime!='' and  (cashOut.endTime==null or cashOut.endTime=='')">
                and date_format(c.create_at,'%Y-%m-%d') &gt;= #{cashOut.startTime}
            </if>
            <if test="cashOut.endTime!=null and cashOut.endTime!='' and  (cashOut.startTime==null or cashOut.startTime=='')">
                and date_format(c.create_at,'%Y-%m-%d') &lt;= #{cashOut.endTime}
            </if>

        </where>

        order by c.create_at desc
    </select>

    <select id="selectCashOutList" resultType="com.sqx.modules.pay.entity.CashOut">
        SELECT c.*,
        ifnull(u.user_name,"用户不存在") as userName,
        s.username as sysUserName
        FROM cash_out c
        left join tb_user u on c.user_id = u.user_id
        left join sys_user s on s.user_id= c.sys_user_id
        <where>
            <if test="cashOut.userName!=null and cashOut.userName!=''">
                and u.user_name like concat("%",#{cashOut.userName},"%")
            </if>
            <if test="cashOut.state!=null">
                and c.state = #{cashOut.state}
            </if>
            <if test="cashOut.userId!=null">
                and c.user_id = #{cashOut.userId}
            </if>
            <if test="cashOut.sysUserId!=null">
                and c.sys_user_id = #{cashOut.sysUserId}
            </if>
            <if test="cashOut.userType!=null">
                and c.user_type = #{cashOut.userType}
            </if>
            <if test="cashOut.status!=null and cashOut.status!=-2">
                and c.state = #{cashOut.status}
            </if>
            <if test="cashOut.zhifubao!=null and cashOut.zhifubao!=''">
                and c.zhifubao like concat("%",#{cashOut.zhifubao},"%")
            </if>
            <if test="cashOut.zhifubaoName!=null and cashOut.zhifubaoName!=''">
                and c.zhifubao_name like concat("%",#{cashOut.zhifubaoName},"%")
            </if>
            <if test="cashOut.startTime!=null and cashOut.startTime!='' and (cashOut.endTime!=null and cashOut.endTime!='')">
                and date_format(c.create_at,'%Y-%m-%d') BETWEEN #{cashOut.startTime} and #{cashOut.endTime}
            </if>
            <if test="cashOut.startTime!=null and cashOut.startTime!='' and  (cashOut.endTime==null or cashOut.endTime=='')">
                and date_format(c.create_at,'%Y-%m-%d') &gt;= #{cashOut.startTime}
            </if>
            <if test="cashOut.endTime!=null and cashOut.endTime!='' and  (cashOut.startTime==null or cashOut.startTime=='')">
                and date_format(c.create_at,'%Y-%m-%d') &lt;= #{cashOut.endTime}
            </if>

        </where>

        order by c.create_at desc
    </select>

    <select id="selectTodayCashCount" resultType="java.lang.Integer">
        select count(*) from cash_out where create_at > #{time} and user_id=#{userId}
        and state in
        <foreach collection="stateList" item="state" open="(" separator="," close=")">
                #{state}
        </foreach>
        and state=#{state}
        and withdraw_type=1;
    </select>


    <update id="updateMayMoney">
     update user_money  set
        <if test="type==1">
            money=money+#{money}
        </if>
        <if test="type==2">
            money=money-#{money}
        </if>
        where user_id=#{userId}
    </update>

    <select id="selectSumMoney" resultType="java.math.BigDecimal">
        select sum(money) from cash_out where state = #{state} and user_id = #{userId}
    </select>

    <select id="queryUserTodayCashOutSum" resultType="double">
        select sum(money) from cash_out where state in (1, 3) and user_id = #{userId} and create_at >= #{time}
    </select>
</mapper>
