package com.bohuikeji.delivery.backstage.mapper;

import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.frame.module.admin.base.RanchBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;


import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 派送员信息表 Mapper 接口
 * </p>
 *
 * @author 10552
 * @since 2019-08-22
 */
@Mapper
public interface TCourierMapper extends RanchBaseMapper<TCourier> {
    @Select("SELECT \n" +
            "  c.* ,a.ordercount AS weekcoun\n" +
            "FROM\n" +
            "  t_courier c \n" +
            " INNER JOIN (SELECT \n" +
            "  COUNT(order_id) AS ordercount,courier_id \n" +
            "FROM\n" +
            "  t_order \n" +
            "WHERE TO_DAYS(NOW()) - TO_DAYS(acceptance_time) <= 7 \n" +
            "GROUP BY courier_id ) a ON c.courier_id = a.courier_id \n" +
            "ORDER BY weekcoun DESC limit 10")
    List<TCourier> selectWeekRanking();
    @Select("SELECT \n" +
            "  c.* ,a.ordercount AS monthcoun\n" +
            "FROM\n" +
            "  t_courier c \n" +
            " INNER JOIN (SELECT \n" +
            "  COUNT(order_id) AS ordercount,courier_id \n" +
            "FROM\n" +
            "  t_order \n" +
            "WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= DATE(acceptance_time)\n" +
            "GROUP BY courier_id ) a ON c.courier_id = a.courier_id \n" +
            "ORDER BY monthcoun DESC limit 10;")
    List<TCourier> selectMonthRanking();

    /**
     * 今日金额
     * @param id
     * @param value
     * @return
     */
    @Select("select sum(order_courier_money) from t_order where order_end_time >= date(now()) and order_end_time < DATE_ADD(date(now()),INTERVAL 1 DAY) and courier_id= #{id} and order_status=\'已完成\'")
    BigDecimal selectTodayIncome(@Param("id") Integer id);

    /**
     * 今日接单量
     * @param id
     * @return
     */
    @Select("select count(order_id) from t_order where acceptance_time >= date(now()) and acceptance_time < DATE_ADD(date(now()),INTERVAL 1 DAY) and courier_id= #{id} and order_status=\'已完成\'")
    Integer selectTodayCount(@Param("id") Integer id);

    /**
     * 派送员接单总数
     * @param id
     * @return
     */
    @Select("select count(order_id) from t_order where courier_id=#{id}")
    Integer selectTotalCount(@Param("id") Integer id);

    /**
     * 派送员未完成订单量
     * @param id
     * @return
     */
    @Select("select count(order_id) from t_order where courier_id= #{id} and order_status !=\"已完成\"")
    Integer selectNotCompleteCount(@Param("id") Integer id);

    /**
     * 派送员完成的订单数量
     * @param id
     * @return
     */
    @Select("select count(order_id) from t_order where courier_id= #{id} and order_status =\"已完成\"")
    Integer selectCompleteCount(@Param("id") Integer id);

    /**
     * 派送员总收入金额
     * @param id
     * @return
     */
    @Select("select sum(order_courier_money) from t_order where courier_id= #{id} and order_status=\"已完成\"")
    BigDecimal selectIncome(@Param("id") Integer id);
}
