<?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="query.startTime!=null and query.startTime!=''">
            and create_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and create_time <![CDATA[<=]]> #{query.endTime}
        </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="query.startTime!=null and query.startTime!=''">
            and create_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and create_time <![CDATA[<=]]> #{query.endTime}
        </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="query.startTime!=null and query.startTime!=''">
            and create_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and create_time <![CDATA[<=]]> #{query.endTime}
        </if>
        and type = 3 and state = 1
    </select>

    <sql id="cashOutQueryCondition">
        <where>
            <if test="params.zhifubaoName != null and params.zhifubaoName != '' and params.zhifubaoName != 'null'">
                and co.zhifubao_name = #{params.zhifubaoName}
            </if>
            <if test="params.zhifubao != null and params.zhifubao != '' and params.zhifubao != 'null'">
                and co.zhifubao = #{params.zhifubao}
            </if>
            <if test="params.userId != null">
                and co.user_id = #{params.userId}
            </if>
            <if test="params.type != null and params.type != '' and params.type != 'null'">
                and co.type = #{params.type}
            </if>
            <if test="params.state != null">
                and co.state = #{params.state}
            </if>
            <if test="params.phone != null and params.phone != '' and params.phone != 'null'">
                and tu.phone like concat('%',#{params.phone},'%')
            </if>
            <if test="params.userName != null and params.userName != '' and params.userName != 'null'">
                and tu.user_name like concat('%',#{params.userName},'%')
            </if>
            <if test="params.classify == 1 || params.classify == 5 || params.classify == 6">
                and co.classify = #{params.classify}
            </if>
            <if test="params.classify == 2">
                and co.classify in (2, 3, 4)
            </if>
            <if test="params.startTime != null and params.startTime != '' and params.startTime != 'null'">
                and co.create_at <![CDATA[ >= ]]> #{params.startTime}
            </if>
            <if test="params.endTime != null and params.endTime != '' and params.endTime != 'null'">
                and co.create_at <![CDATA[ <= ]]> #{params.endTime}
            </if>
            <if test="params.outStartTime != null and params.outStartTime != '' and params.outStartTime != 'null'">
                and co.out_at <![CDATA[ >= ]]> #{params.outStartTime}
            </if>
            <if test="params.outEndTime != null and params.outEndTime != '' and params.outEndTime != 'null'">
                and co.out_at <![CDATA[ <= ]]> #{params.outEndTime}
            </if>
            <if test="params.shopName != null and params.shopName != '' and params.shopName != 'null'">
                and gh.shop_name like concat('%',#{params.shopName},'%')
            </if>
            <if test="params.bankCardNo != null and params.bankCardNo != '' and params.bankCardNo != 'null'">
                and co.bank_card_no = #{params.bankCardNo}
            </if>
            <if test="params.bankRealName != null and params.bankRealName != '' and params.bankRealName != 'null'">
                and co.bank_real_name = #{params.bankRealName}
            </if>
            <if test="params.openBank != null and params.openBank != '' and params.openBank != 'null'">
                and co.open_bank = #{params.openBank}
            </if>
            <if test="params.riderStationId != null and params.riderStationId != '' ">
                and tu.rider_station_id = #{params.riderStationId}
            </if>
        </where>
    </sql>

    <select id="selectCashOutList" resultType="com.sqx.modules.pay.vo.CashOutVO">
        select gh.shop_name, co.*,rs.station_name as stationName,tu.user_name as userName, gh.mch_id as mchId, tu.phone as phoneNumber
        from cash_out co
        left join goods_shop gh on gh.shop_id = co.shop_id
        left join tb_user tu on tu.user_id=co.user_id
        left join rider_station rs on tu.rider_station_id=rs.id
        <include refid="cashOutQueryCondition"></include>
        order by id desc
    </select>

    <select id="selectRiderCashOutList" resultType="com.sqx.modules.pay.vo.CashOutVO">
        select co.*, rs.station_name as stationName, tu.user_name as userName, rbci.mch_id as mchId
        from cash_out co
        left join tb_user tu on tu.user_id = co.user_id
        left join rider_bank_card_info rbci on rbci.user_id = co.user_id
        left join rider_station rs on tu.rider_station_id=rs.id
        <include refid="cashOutQueryCondition"></include>
        order by co.id desc
    </select>

    <select id="selectCashOutTotal" resultType="java.lang.Double">
        select ifnull(sum(co.money), 0.0) as totalMoney
        from cash_out co
        left join tb_user tu on tu.user_id = co.user_id
        left join goods_shop gh on gh.shop_id = co.shop_id
        <include refid="cashOutQueryCondition"></include>
    </select>

    <select id="selectCommissionTotal" resultType="java.lang.Double">
        select ifnull(sum(co.rate), 0.0) as totalMoney
        from cash_out co
        left join tb_user tu on tu.user_id=co.user_id
        left join goods_shop gh on gh.shop_id = co.shop_id
        <include refid="cashOutQueryCondition"></include>
    </select>

    <select id="excelPayDetails" resultType="com.sqx.modules.pay.vo.CashOutVO">
        select gh.shop_name, co.*,rs.station_name as stationName
        from cash_out co
        left join goods_shop gh on gh.shop_id = co.shop_id
        left join tb_user tu on tu.user_id=co.user_id
        left join rider_station rs on tu.rider_station_id=rs.id
        <include refid="cashOutQueryCondition"></include>
        order by id desc
    </select>

</mapper>