<?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="selectCashOutSumsByNowMonth" resultType="Double">
        select ifnull(sum(money),0.00) from cash_out where `state` in (0,1) and user_id=#{userId} and date_format(create_at,'%Y-%m')=date_format(now(),'%Y-%m')
    </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>

    <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>

        <if test="state!=null">
            and state = #{state}
        </if>
        <if test="type!=null">
            and type = #{type}
        </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>
        <if test="state!=null">
            and state = #{state}
        </if>
        <if test="type!=null">
            and type = #{type}
        </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
        <if test="type!=null">
            and type = #{type}
        </if>
    </select>

    <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
        <if test="type!=null">
            and type = #{type}
        </if>
    </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
        <if test="type!=null">
            and type = #{type}
        </if>
    </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
        <if test="type!=null">
            and type = #{type}
        </if>
    </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
        <if test="type!=null">
            and type = #{type}
        </if>
    </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
        <if test="type!=null">
            and type = #{type}
        </if>
    </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 = 3
    </select>

    <select id="cargoInsurance" resultType="java.math.BigDecimal">
        select ifnull(sum(cargo_insurance), 0) from tb_indent 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 indent_state =6
    </select>

    <select id="MoneySumByIndentType" resultType="java.math.BigDecimal">
        select ifnull(sum(platform_money), 0) from tb_indent 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 indent_state = 6 and indent_type = #{indentType}
    </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>


</mapper>