<?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.datacentre.dao.DataCentreDao">

    <!--总用户数-->
    <select id="findAllCount" resultType="int">
        select count(*)
        from tb_user
        where status = 1
    </select>
    <!--今日新增用户数-->
    <select id="findDayAddUser" resultType="int">
        select count(*)
        from tb_user
        where status = 1
          and date_format(create_time, '%Y-%m-%d') = date_format(#{date}, '%Y-%m-%d')
    </select>
    <!--本月新增用户数-->
    <select id="findMonthAddUser" resultType="int">
        select count(*)
        from tb_user
        where status = 1
          and date_format(create_time, '%Y-%m') = date_format(#{date}, '%Y-%m')
    </select>
    <!--本年新增用户数-->
    <select id="findYearAddUser" resultType="int">
        select count(*)
        from tb_user
        where status = 1
          and date_format(create_time, '%Y') = date_format(#{date}, '%Y')
    </select>
    <!--时间范围内收入-->
    <select id="findDayIncome" resultType="java.math.BigDecimal">
        select ifnull(sum(pay_money), 0)
        from tb_order
        where status in (3, 4)
          and pay_time >= #{starTime}
          and pay_time <![CDATA[<=]]> #{endTime}
    </select>

    <!--今日发单数量-->
    <select id="findIndentCount" resultType="int">
        select count(*)
        from tb_order
        where status in (3, 4)
          and pay_time >= #{starTime}
          and pay_time <![CDATA[<=]]> #{endTime}
    </select>

    <select id="selectPayOrderAnalyze" resultType="com.sqx.modules.order.entity.TbOrder">
        select tor.*, gs.shop_name as shopName from tb_order tor left join goods_shop gs on tor.shop_id = gs.shop_id
        where tor.is_pay = 1
        <if test="query.dateType=='day'">
            and date_format(tor.pay_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(tor.pay_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(tor.pay_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and tor.pay_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and tor.pay_time <![CDATA[<=]]> #{query.endTime}
        </if>
        order by tor.pay_time desc
    </select>

    <select id="sumOrderMonth" resultType="int">
        select count(*)
        from tb_order
        where user_id = #{userId}
          and status in (3, 4)
          and date_format(pay_time, '%Y-%m') = date_format(#{date}, '%Y-%m')
    </select>

    <select id="sumJoinOrderMonth" resultType="int">
        select count(*)
        from tb_indent
        where rider_user_id = #{userId}
          and indent_state in (2, 3, 4, 6, 11)
          and date_format(create_time, '%Y-%m') = date_format(#{date}, '%Y-%m')
    </select>

    <select id="sumRiderMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(rider_money), 0)
        from tb_indent
        where rider_user_id = #{userId}
          and indent_state = 6
    </select>

    <select id="sumCashMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0)
        from cash_out
        where user_id = #{userId}
          and state = 1
          and date_format(out_at, '%Y-%m') = date_format(#{date}, '%Y-%m')
    </select>

    <select id="sumTopUpMonth" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0)
        from pay_details
        where user_id = #{userId}
          and classify = 2
          and date_format(pay_time, '%Y-%m') = date_format(#{date}, '%Y-%m')
    </select>

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

    <select id="findAllCertification" resultType="com.sqx.modules.app.entity.UserEntity">
        select tu.*,rs.station_name AS stationName from tb_user tu
        left join rider_station rs on tu.rider_station_id=rs.id
        where 1 = 1
        <if test="phone!=null and phone!=''">
            and tu.phone=#{phone}
        </if>
        <if test="userName!=null and userName!=''">
            and tu.user_name = #{userName}
        </if>
        <if test="checkCertification!=null and checkCertification!='' and checkCertification!='-1' ">
            and tu.check_certification = #{checkCertification}
        </if>
        <if test="checkCertification!=null or checkCertification=='-1'">
            and tu.check_certification in (0,1,2)
        </if>
        <if test="riderStationId!=null and riderStationId!=''">
            and tu.rider_station_id = #{riderStationId}
        </if>
        order by tu.audit_time desc
    </select>

    <update id="checkCertification">
        update tb_user
        set check_certification         = #{checkCertification},
            check_certification_message = #{checkCertificationMessage}
        where user_id = #{userId}
    </update>

    <select id="findCertification" resultType="com.sqx.modules.app.entity.UserEntity">
        select *
        from tb_user
        where user_id = #{userId}
          and status = 1
    </select>

    <select id="rankingList" resultType="com.sqx.modules.errand.entity.TbIndent">
        SELECT
        *,
        @rank_num := @rank_num + 1 AS rankNum
        FROM
        ( SELECT @rank_num := 0 ) r,
        ( SELECT ifnull( sum( rider_money ), 0 ) AS moneyOrder, i.rider_user_id, u.nick_name as nickName, u.avatar as
        avatar,
        i.user_province as userProvince, i.user_city as userCity, i.user_district as userDistrict,rs.station_name as stationName,
        u.user_name as riderNickName,u.balance as riderBalance,u.phone as riderPhone
        FROM tb_indent i left join tb_user u on i.rider_user_id = u.user_id
        left join rider_station rs on u.rider_station_id=rs.id
        WHERE i.rider_user_id IS NOT NULL and i.indent_state = 6
        <if test="address!=null and address!=''">
                and (i.user_province like concat('%',#{address},'%') or i.user_city like concat('%',#{address},'%') or
                i.user_district like concat('%',#{address},'%'))
        </if>
        <if test="dateType=='day'">
            and date_format(i.create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(i.create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(i.create_time,'%Y')=date_format(#{date},'%Y')
        </if>
        <if test="riderStationId != null and riderStationId != ''">
            and u.rider_station_id=#{riderStationId}
        </if>
        GROUP BY i.rider_user_id ) a
        ORDER BY moneyOrder DESC
    </select>

    <select id="excelRankList" resultType="com.sqx.modules.errand.entity.TbIndent">
        SELECT
        *,
        @rank_num := @rank_num + 1 AS rankNum
        FROM
        ( SELECT @rank_num := 0 ) r,
        ( SELECT ifnull( sum( rider_money ), 0 ) AS moneyOrder, i.rider_user_id, u.nick_name as nickName, u.avatar as
        avatar,
        i.user_province as userProvince, i.user_city as userCity, i.user_district as userDistrict,rs.station_name as stationName,
        u.user_name as riderNickName,u.balance as riderBalance
        FROM tb_indent i left join tb_user u on i.rider_user_id = u.user_id
        left join rider_station rs on u.rider_station_id=rs.id
        WHERE i.rider_user_id IS NOT NULL
        <if test="query.address!=null and query.address!=''">
            and (i.user_province like concat('%',#{query.address},'%') or i.user_city like concat('%',#{query.address},'%') or
            i.user_district like concat('%',#{query.address},'%'))
        </if>
        <if test="query.dateType=='day'">
            and date_format(i.create_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(i.create_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(i.create_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        <if test="query.riderStationId != null and query.riderStationId !=''">
            and u.rider_station_id=#{query.riderStationId}
        </if>
        GROUP BY i.rider_user_id ) a
        ORDER BY moneyOrder DESC
    </select>

    <select id="selectNewShopCount" resultType="int">
        select count(*) from goods_shop where 1 = 1
        <if test="query.dateType=='day'">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(create_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(create_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        <if test="query.endTime != null and query.endTime != ''">
            and create_time <![CDATA[<=]]> #{query.endTime}
        </if>
        <if test="query.startTime != null and query.startTime != ''">
            and create_time >= #{query.startTime}
        </if>
    </select>

    <select id="selectRankingList" resultType="com.sqx.modules.order.entity.TbOrder">
        SELECT
        *,
        @rank_num := @rank_num + 1 AS rankNum
        FROM
        ( SELECT @rank_num := 0 ) r,
        ( SELECT ifnull( sum( pay_money ), 0 ) AS shopMoney,ifnull( sum( shop_income_money ), 0 ) AS shopIncomeMoney, tor.shop_id , u.shop_name,gp.id as
        shopType,gp.shop_type_name as shopTypeName
        FROM tb_order tor force index(tb_order_pay_time_IDX) left join goods_shop u on tor.shop_id = u.shop_id
        left join shop_type gp on gp.id=u.shop_type_id
        WHERE tor.shop_id IS NOT NULL and tor.status = 4
        <if test="query.startTime != null and query.startTime !=''">
            and tor.pay_time >= #{query.startTime}
        </if>
        <if test="query.endTime != null and query.endTime != ''">
            and tor.pay_time <![CDATA[<=]]> #{query.endTime}
        </if>
        <if test="query.shopType != null and query.shopType != '' ">
            and gp.id = #{query.shopType}
        </if>
        GROUP BY tor.shop_id ) a
        ORDER BY shopMoney DESC
    </select>

    <select id="selectRankingListAmount" resultType="com.sqx.modules.order.entity.TbOrder">
        SELECT ifnull( sum( pay_money ), 0 ) AS shopMoney,ifnull( sum( shop_income_money ), 0 ) AS shopIncomeMoney
        FROM tb_order tor force index(tb_order_pay_time_IDX) left join goods_shop u on tor.shop_id = u.shop_id
        left join shop_type gp on gp.id=u.shop_type_id
        WHERE tor.shop_id IS NOT NULL and tor.status = 4
        <if test="query.startTime != null and query.startTime !=''">
            and tor.pay_time >= #{query.startTime}
        </if>
        <if test="query.endTime != null and query.endTime != ''">
            and tor.pay_time <![CDATA[<=]]> #{query.endTime}
        </if>
        <if test="query.shopType != null and query.shopType != '' ">
            and gp.id = #{query.shopType}
        </if>
    </select>

    <select id="excelShopCenter" resultType="com.sqx.modules.order.entity.TbOrder">
        SELECT
        *,
        @rank_num := @rank_num + 1 AS Rank
        FROM
        ( SELECT @rank_num := 0 ) r,
        ( SELECT ifnull( sum( pay_money ), 0 ) AS shopMoney,ifnull( sum( shop_income_money ), 0 ) AS shopIncomeMoney, tor.shop_id , u.shop_name,gp.id as
        shopType,gp.shop_type_name as shopTypeName
        FROM tb_order tor left join goods_shop u on tor.shop_id = u.shop_id
        left join shop_type gp on gp.id=u.shop_type_id
        WHERE tor.shop_id IS NOT NULL and tor.status = 4
        <if test="query.startTime != null and query.startTime !=''">
            and tor.pay_time >= #{query.startTime}
        </if>
        <if test="query.endTime != null and query.endTime != ''">
            and tor.pay_time <![CDATA[<=]]> #{query.endTime}
        </if>
        <if test="query.shopType != null and query.shopType != '' ">
            and gp.id = #{query.shopType}
        </if>
        <if test="query.dateType=='day'">
            and date_format(tor.pay_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(tor.pay_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(tor.pay_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        GROUP BY tor.shop_id ) a
        ORDER BY shopMoney DESC
    </select>

    <select id="allUserCount" resultType="int">
        select count(*) from tb_user where status = 1
        <if test="query.dateType=='day'">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(create_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(create_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        <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>
    </select>

    <select id="phoneUserCount" resultType="int">
        select count(*) from tb_user where status = 1 and phone is not null
        <if test="query.dateType=='day'">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(create_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(create_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        <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>
    </select>

    <select id="selectUserFeedback" resultType="com.sqx.modules.errand.entity.Feedback">
        select * from sys_feedback where 1 = 1
        <if test="userEmail!=null and userEmail!=''">
            and user_email = #{userEmail}
        </if>
        order by feedback_time desc
    </select>

    <select id="findUserAddIndent" resultType="com.sqx.modules.errand.entity.TbIndent">
        select i.*,
               tu.nick_name as riderNickName,
               tu.phone     as riderPhone
        from tb_indent i
                 left join tb_indent ti on ti.order_id = i.order_id
                 left join tb_user tu on ti.rider_user_id = tu.user_id
        where i.user_id = #{userId}
          and i.indent_state in (2, 3, 4, 5, 6, 8, 9, 10)
        order by i.create_time desc
    </select>

    <select id="findUserReceivingIndent" resultType="com.sqx.modules.errand.entity.TbIndent">
        select *
        from tb_indent
        where rider_user_id = #{userId}
          and indent_state in (3, 4, 5, 6)
        order by create_time desc
    </select>

    <select id="findTopUpMoney" resultType="com.sqx.modules.pay.entity.PayDetails">
        select pd.*, tu.nick_name as nickName from pay_details pd left join tb_user tu on pd.user_id = tu.user_id where
        1 = 1
        <if test="userId!=null">
            and pd.user_id = #{userId}
        </if>
        <if test="startTime!=null and startTime!=''and endTime!=null and endTime!='' ">
            and str_to_date(pd.create_time, '%Y-%m-%d') between str_to_date(#{startTime}, '%Y-%m-%d') AND
            str_to_date(#{endTime}, '%Y-%m-%d')
        </if>
        order by pd.create_time desc
    </select>

    <select id="billMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(indent_money),0) from tb_indent where indent_state in (2, 3, 4, 6)
        <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>
    </select>

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

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

    <select id="selectCashDeposit" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        select umd.*, tu.phone as phone, tu.nick_name as nickName from user_money_details umd left join tb_user tu on
        umd.user_id = tu.user_id where 1 = 1
        <if test="phone!=null and phone!=''">
            and tu.phone = #{phone}
        </if>
        <if test="type!=null">
            and umd.type = #{type}
        </if>
        <if test="userId!=null">
            and umd.user_id = #{userId}
        </if>
        and classify = 1
        order by umd.create_time desc
    </select>

    <select id="tcwmplatformMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(pay_money), 0) from tb_order where status = 4
        <if test="dateType=='day'">
            and date_format(pay_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(pay_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(pay_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>

    <select id="tcwmShopMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(shop_income_money), 0) from tb_order where status = 4
        <if test="dateType=='day'">
            and date_format(pay_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(pay_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(pay_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>

    <select id="selectFeedbackList" resultType="com.sqx.modules.errand.entity.Feedback">
        select * from sys_feedback where 1 = 1
        <if test="type!=null">
            and feedback_type = #{type}
        </if>
        order by feedback_time desc
    </select>

    <select id="selectTakeCount" resultType="int">
        select count(*) from tb_order
        where order_type = #{orderType}
        and is_pay=1 and status in (0,3,4,7,6)
        <if test="query.shopId!=null">
            and shop_id = #{query.shopId}
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and pay_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and pay_time <![CDATA[<=]]> #{query.endTime}
        </if>
    </select>

    <select id="selectTakeMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(pay_money), 0) from tb_order
        where order_type = #{orderType}
        and is_pay=1 and status in (0,3,4,7,6)
        <if test="query.shopId!=null">
            and shop_id = #{query.shopId}
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and pay_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and pay_time <![CDATA[<=]]> #{query.endTime}
        </if>
    </select>

    <select id="cancelOrderCount" resultType="int">
        select count(*) from tb_order where status in (5,8)
        <if test="query.shopId!=null">
            and shop_id = #{query.shopId}
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and update_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and update_time <![CDATA[<=]]> #{query.endTime}
        </if>
    </select>

    <select id="cancelOrderMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(pay_money), 0) from tb_order where status in (5,8)
        <if test="query.shopId!=null">
            and shop_id = #{query.shopId}
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and update_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and update_time <![CDATA[<=]]> #{query.endTime}
        </if>
    </select>

</mapper>