<?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="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="sumMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0) from cash_out where 1 = 1
        <if test="dateType=='day'">
            and date_format(create_at,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(create_at,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(create_at,'%Y')=date_format(#{date},'%Y')
        </if>
        and type = 1
        <if test="state!=null">
            and state = #{state}
        </if>
    </select>

    <select id="countMoney" resultType="Integer">
        select count(*) from cash_out where 1 = 1
        <if test="dateType=='day'">
            and date_format(create_at,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(create_at,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(create_at,'%Y')=date_format(#{date},'%Y')
        </if>
        and type = 1
        <if test="state!=null">
            and state = #{state}
        </if>
    </select>

    <select id="stayMoney" resultType="Integer">
        select count(*) from cash_out where 1 = 1
        <if test="dateType=='day'">
            and date_format(create_at,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(create_at,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(create_at,'%Y')=date_format(#{date},'%Y')
        </if>
        and state=0 and type = 1
    </select>

    <select id="selectMayMoney" resultType="Double">
     select money from user_money  where  user_id=#{userId}
    </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>

    <insert id="addMoneyDetails">
        insert into user_money_details (user_id, title, type, money, content, create_time)
        values (#{userId}, #{title}, #{type}, #{money}, #{content}, #{createTime})
    </insert>

    <select id="applyMoneySum" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0) from cash_out where 1 = 1
        <if test="dateType=='day'">
            and date_format(create_at,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(create_at,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(create_at,'%Y')=date_format(#{date},'%Y')
        </if>
        and state = 0 and type = 1
    </select>

    <select id="accomplishMoneySum" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0) from cash_out where 1 = 1
        <if test="dateType=='day'">
            and date_format(create_at,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(create_at,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(create_at,'%Y')=date_format(#{date},'%Y')
        </if>
        and state = 1 and type = 1
    </select>

    <select id="refuseMoneySum" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0) from cash_out where 1 = 1
        <if test="dateType=='day'">
            and date_format(create_at,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(create_at,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(create_at,'%Y')=date_format(#{date},'%Y')
        </if>
        and state = -1 and type = 1
    </select>

    <select id="zhifubaoMoneySum" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0) from cash_out where 1 = 1
        <if test="dateType=='day'">
            and date_format(create_at,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(create_at,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(create_at,'%Y')=date_format(#{date},'%Y')
        </if>
        and classify = 1 and state = 1 and type = 1
    </select>

    <select id="weixinMoneySum" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0) from cash_out where 1 = 1
        <if test="dateType=='day'">
            and date_format(create_at,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(create_at,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(create_at,'%Y')=date_format(#{date},'%Y')
        </if>
        and classify = 2 and state = 1 and type = 1
    </select>

    <select id="cashDepositMoneySum" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0) from cash_out where 1 = 1
        <if test="dateType=='day'">
            and date_format(create_at,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(create_at,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(create_at,'%Y')=date_format(#{date},'%Y')
        </if>
        and state = 1 and type = 2
    </select>


    <select id="cashDepositMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0) from pay_details where 1 = 1
        <if test="dateType=='day'">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(create_time,'%Y')=date_format(#{date},'%Y')
        </if>
        and type = 3 and state = 1
    </select>
    <select id="selectAdminHelpProfit" resultType="com.sqx.modules.pay.entity.CashOut">
        select * from ( SELECT
        c.*,
        u.nick_name as userName,
        u.phone as phone
        FROM
        cash_out c LEFT JOIN
        tb_user u
        ON u.user_id = c.user_id
        LEFT JOIN sys_user s
        ON c.`user_id` = s.`user_id`) t1
        <where>
            <if test="cashOut.userName!=null and cashOut.userName!=''">
                and t1.userName like concat("%",#{cashOut.userName},"%")
            </if>
            <if test="cashOut.zhifubao!=null and cashOut.zhifubao!=''">
                and t1.zhifubao like concat("%",#{cashOut.zhifubao},"%")
            </if>
            <if test="cashOut.orderNumber!=null and cashOut.orderNumber!=''">
                and t1.order_number like concat("%",#{cashOut.orderNumber},"%")
            </if>
            <if test="cashOut.zhifubaoName!=null and cashOut.zhifubaoName!=''">
                and t1.zhifubao_name like concat("%",#{cashOut.zhifubaoName},"%")
            </if>
            <if test="cashOut.phone!=null and cashOut.phone!=''">
                and t1.phone like concat("%",#{cashOut.phone},"%")
            </if>
            <if test="cashOut.classify!=null">
                and t1.classify = #{cashOut.classify}
            </if>
            <if test="cashOut.state!=null">
                and t1.state = #{cashOut.state}
            </if>
            <if test="cashOut.userId!=null">
                and t1.user_id = #{cashOut.userId}
            </if>
            <if test="cashOut.userType!=null">
                and t1.user_type = #{cashOut.userType}
            </if>
            <if test="startTime!=null and startTime!='' and endTime!=null and endTime!=''">
                and date_format(t1.create_at,'%Y-%m-%d') BETWEEN #{startTime} and #{endTime}
            </if>
            <if test="startTime!=null and startTime!='' and  (endTime==null or endTime=='')">
                and date_format(t1.create_at,'%Y-%m-%d') &gt;= #{startTime}
            </if>
            <if test="endTime!=null and endTime!='' and  (startTime==null or startTime=='')">
                and date_format(t1.create_at,'%Y-%m-%d') &lt;= #{endTime}
            </if>

        </where>
        order by t1.create_at desc


    </select>


</mapper>