package com.jeequan.jeepay.service.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeequan.jeepay.core.entity.PayOrderCount;
import com.jeequan.jeepay.db.entity.PayOrder;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 支付订单表 Mapper 接口
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2021-04-27
 */
public interface PayOrderMapper extends BaseMapper<PayOrder> {

    /**
     * 交易统计
     * @param param
     * @param allAgentNoList
     * @return
     */
    PayOrderCount payCount(@Param("param") Map param, @Param("allAgentNoList") List<String> allAgentNoList);

    /**
     * 支付订单统计
     * @param param
     * @param allAgentNoList
     * @return
     */
    PayOrderCount payOrderCount(@Param("param") Map param, @Param("allAgentNoList") List<String> allAgentNoList);

    /**
     * 支付方式统计
     * @param param
     * @return
     */
    List<PayOrderCount> payTypeCount(Map param);

    /**
     * 支付统计
     * @param param
     * @return
     */
    List<PayOrderCount> selectOrderCount(Map param);

    /** 更新订单退款金额和次数 **/
    int updateRefundAmountAndCount(@Param("payOrderId") String payOrderId, @Param("currentRefundAmount") Long currentRefundAmount);

    /**
     * 商户数据统计列表
     * @param param
     * @param allAgentNoList
     * @return
     */
    List<PayOrderCount> countListByMch(@Param("param") HashMap<String, Object> param, @Param("allAgentNoList") List<String> allAgentNoList);

    /** 商户数据统计总数 **/
    Long countByMch(@Param("param") HashMap<String, Object> param, @Param("allAgentNoList") List<String> allAgentNoList);

    /**
     * 商户门店数据统计列表
     * @param param
     * @return
     */
    List<PayOrderCount> countListByStore(@Param("param") HashMap<String, Object> param);

    /** 商户门店数据统计总数 **/
    Long countByStore(@Param("param") HashMap<String, Object> param);

    /**
     * 商户支付方式数据统计列表
     * @param param
     * @return
     */
    List<PayOrderCount> countListByWayCode(@Param("param") HashMap<String, Object> param);

    /** 商户支付方式数据统计总数 **/
    Long countByWayCode(@Param("param") HashMap<String, Object> param);

    /**
     * 商户支付类型数据统计列表
     * @param param
     * @return
     */
    List<PayOrderCount> countListByWayCodeType(@Param("param") HashMap<String, Object> param);

    /** 商户支付类型数据统计总数 **/
    Long countByWayCodeType(@Param("param") HashMap<String, Object> param);

    /**
     * 交易报表统计列表
     * @param param
     * @param allAgentNoList
     * @return
     */
    List<PayOrderCount> countListByTransaction(@Param("param") HashMap<String, Object> param, @Param("allAgentNoList") List<String> allAgentNoList);

    /**
     * 交易报表统计总数
     * @param param
     * @param allAgentNoList
     * @return
     */
    Long countByTransaction(@Param("param") HashMap<String, Object> param, @Param("allAgentNoList") List<String> allAgentNoList);

    /**
     * 服务商统计列表 TODO 异常
     * @param param
     * @param allAgentNoList
     * @return
     */
    List<PayOrderCount> countListByAgent(@Param("param") HashMap<String, Object> param, @Param("allAgentNoList") List<String> allAgentNoList);

    /**服务商统计总数 **/
    Long countByAgent(@Param("param") HashMap<String, Object> param, @Param("allAgentNoList") List<String> allAgentNoList);

    /**
     * 服务商统计列表
     * @param param
     * @return
     */
    List<PayOrderCount> countListByIsv(@Param("param") HashMap<String, Object> param);

    /**服务商统计总数 **/
    Long countByIsv(@Param("param") HashMap<String, Object> param);

    /**
     * 通道统计列表
     * @param param
     * @return
     */
    List<PayOrderCount> countListByChannel(@Param("param") HashMap<String, Object> param);

    /***
     * 通道统计总数
     * @param param
     * @return
     */
    Long countByChannel(@Param("param") HashMap<String, Object> param);

    /**
     * 设备统计总数
     * @param paramsMap
     * @param agentSubList
     * @return
     */
    Long countByDevice(@Param("param") HashMap<String, Object> paramsMap, @Param("allAgentNoList") List<String> agentSubList);

    /**
     * 通道统计总数
     * @param paramsMap
     * @param agentSubList
     * @return
     */
    List<PayOrderCount> countListByDevice(@Param("param") HashMap<String, Object> paramsMap, @Param("allAgentNoList") List<String> agentSubList);

    /**
     * 商户数据统计总计
     * @param param
     * @param allAgentNoList
     * @return
     */
    PayOrderCount totalByMch(@Param("param") HashMap<String, Object> param, @Param("allAgentNoList") List<String> allAgentNoList);

    /** **
     * 商户门店数据统计总计
     * @param param
     * @return
     */
    PayOrderCount totalByStore(@Param("param") HashMap<String, Object> param);

    /**
     * 商户支付方式数据统计总计
     * @param param
     * @return
     */
    PayOrderCount totalByWayCode(@Param("param") HashMap<String, Object> param);

    /**
     * 商户支付类型数据统计总计
     * @param param
     * @return
     */
    PayOrderCount totalByWayCodeType(@Param("param") HashMap<String, Object> param);

    /**
     * 交易报表统计总计
     * @param param
     * @param allAgentNoList
     * @return
     */
    PayOrderCount totalByTransaction(@Param("param") HashMap<String, Object> param, @Param("allAgentNoList") List<String> allAgentNoList);

    /**
     * 服务商统计总计
     * @param param
     * @param allAgentNoList
     * @return
     */
    PayOrderCount totalByAgent(@Param("param") HashMap<String, Object> param, @Param("allAgentNoList") List<String> allAgentNoList);

    /**
     * 服务商统计总计
     * @param param
     * @return
     */
    PayOrderCount totalByIsv(@Param("param") HashMap<String, Object> param);

    /**
     * 通道统计总计
     * @param param
     * @return
     */
    PayOrderCount totalByChannel(@Param("param") HashMap<String, Object> param);

    /**
     * 设备统计总计
     * @param param
     * @param allAgentNoList
     * @return
     */
    PayOrderCount totalByDevice(@Param("param") HashMap<String, Object> param, @Param("allAgentNoList") List<String> allAgentNoList);

    /** 查询出： 支付成功的订单 && 分润数据不存在的 订单号。 （问题订单待修复）  **/
    List<PayOrder> selectSuccessOrderWithProfitIsNull(@Param("endDate") Date endDate);

    /**
     * 统计商户的交易金额 用作结算数据
     * @param settleType
     * @param startDate
     * @param endDate
     * @return
     */
    List<PayOrder> listMchSuccTradeData(@Param("settleType") String settleType, @Param("startDate") Date startDate,@Param("endDate") Date endDate);

    @Update("UPDATE t_pay_order set ext_div_amt = #{extDivAmt} WHERE pay_order_id = #{payOrderId}")
    int updateExtDivAmt(@Param("payOrderId") String payOrderId,@Param("extDivAmt") long extDivAmt);

    @Update("UPDATE t_pay_order set division_state = #{divState} WHERE pay_order_id = #{payOrderId}")
    void updateDivState(@Param("payOrderId") String payOrderId, @Param("divState")byte divState);

    @Update("UPDATE t_pay_order set market_amt = market_amt + #{marketAmt} WHERE pay_order_id = #{payOrderId}")
    void updateMarketAmt(@Param("payOrderId") String payOrderId, @Param("marketAmt") Long marketAmt);

    @Select("SELECT * FROM t_pay_order WHERE platform_mch_order_no = #{cxMchOrderNo}")
    PayOrder getPlatformMchOrderNo(String cxMchOrderNo);

    IPage<PayOrder> getListByPage(IPage<PayOrder> iPage, @Param("payOrder")PayOrder payOrder);

    /**
     * 路由数据统计
     * @param start
     * @param endTime
     * @param storeId
     * @param mchNo
     * @return
     */
    PayOrderCount routeDateCount(@Param("start")Date start, @Param("end")Date endTime, @Param("storeId") String storeId, @Param("mchNo") String mchNo);

    /**
     *
     * @param page
     * @param entity
     * @param dateRange
     * @return
     */
    IPage<PayOrderCount> routePageList(@Param("page") Page<PayOrderCount> page, @Param("entity") PayOrderCount entity, @Param("dateRange")Date[] dateRange);

    /**
     * 路由数据统计
     * @param entity
     * @param dateRange
     * @return
     */
    PayOrderCount routeCount(@Param("entity") PayOrderCount entity,@Param("dateRange") Date[] dateRange);
}
