package cn.pluss.platform.mapper;

import cn.pluss.platform.entity.MerchantOrder;
import cn.pluss.platform.entity.UserProfit;
import cn.pluss.platform.vo.*;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * @see cn.pluss.platform.mapper.MerchantOrderVOMapper
 */
@Mapper
public interface MerchantOrderMapper extends BaseMapper<MerchantOrder> {

	/**
	 * 统计一些数值的合计值
	 * @return .
	 */
	<E extends MerchantOrder> MerchantOrder sumData(@Param(Constants.WRAPPER) Wrapper<E> wrapper);

    MerchantOrder queryMerchantOrder(MerchantOrder merchantOrder);

    List<MerchantOrder> queryMerchantOrderList(MerchantOrder merchantOrder);

    List<MerchantOrder> queryMerchantOrderPage(Map map);

    Integer queryMerchantOrderPageCount(Map map);

    void saveMerchantOrderBatch(List<MerchantOrder> merchantOrderList);
    
    /**
     * 可提现总金额（提现规则  超过一个月的时间）推广
     * @param 
     * @return
     */
	Double sumMerchantAllowCashMoney(@Param(value = "memberCode") String memberCode);
    
    /**
     * 不可提现总金额（提现规则 不超过一个月）推广
     * @param 
     * @return
     */
	Double sumMerchantNotAllowCashMoney(@Param(value = "memberCode") String memberCode);
    
    /**
     * 今日分润 推广
     * @param merchantCode
     * @return
     */
	Double sumNowDayShareMoney(@Param(value = "memberCode") String merchantCode);
    
    /**
     * 累计总分润 推广
     * @return
     */
	Double sumAllShareMoney(@Param(value = "memberCode") String memberCode);

    
    
    /**
     * 供 商户推广- 交易明细 接口使用 
     * @param map
     * @return
     */
	List<MerchantOrder> queryMerchantOrderPageApp(Map map);
    
    /**
     * 供 商户推广- 交易明细 接口使用
     * @param map
     * @return
     */
	Integer queryMerchantOrderPageCountApp(Map map);

    Integer queryMerchantOrderPageCountByTime(Map map);
    
    Double queryMerchantOrdeFeeByTime(Map map);

	List<MerchantOrder> queryMerchantOrderPageByTime(Map map);
	
	/**
	 * 汇总流水（manage）
	 */
	SummaryOfWaterVO querySummaryOfWater(Map map);
	
	/**
	 * 订单退款信息
	 * @param map
	 * @return
	 */
	List<MerchantOrder> queryMerchantOrderRefundPage(Map map);
	
	/**
	 * 订单退款统计
	 * @param map
	 * @return
	 */
	Integer queryMerchantOrderRefundPageCount(Map map);

	Double queryMerchantOrderShareMoney(Map map);
	
	/**
	 * 
	 * queryOrderTask:(给wap端的定时任务使用的 暂定查3天内状态为5待支付的订单). <br/> 
	 * 
	 * @author Administrator 
	 * @param merchantOrder
	 * @return 
	 * @since JDK 1.8
	 */
	List<MerchantOrder> queryOrderTask(MerchantOrder merchantOrder);
	
	/**
	 * 7日交易数统计
	 * @param s 
	 */
	List<OrderStatisticsVO> queryOrderStatistics(MerchantOrder s);
	
	/**
	 * 支付通道统计
	 * @param s 
	 */
	List<PayTypeStatisticsVO> queryPayTypeStatistics(MerchantOrder s);
	
	
	Double queryMerchantOrderEnterFeeByTime(Map map);
	
	Double queryMerchantOrderShareMoneyByTime(Map map);
	
	/**
	 * 
	 * queryMerchantOrderPageShuju:(最新统计订单方法). <br/> 
	 * 
	 * @author Administrator 
	 * @param map
	 * @return 
	 * @since JDK 1.8
	 */
	List<MerchantMangeOrderVO> queryMerchantOrderPageShuju(Map<String, Object> map);

//	List<MerchantMangeOrderVO> queryMerchantOrderNoPageShuju(Map<String, Object> map);
	
	/**
	 * 
	 * queryMerchantOrderPageCountShuju:(最新统计订单方法). <br/> 
	 * 
	 * @author Administrator 
	 * @param map
	 * @return 
	 * @since JDK 1.8
	 */
	Integer queryMerchantOrderPageCountShuju(Map<String, Object> map);

//	Integer queryMerchantOrderNoPageCountShuju(Map<String, Object> map);
	/**
	 * 
	 * querySumEarnings:(统计总的收益 根据时间). <br/> 
	 * 
	 * @author Administrator 
	 * @param map
	 * @return 
	 * @since JDK 1.8
	 */
	List<MerchantMangeOrderVO> querySumEarnings(Map<String, Object> map);
	
	/**
	 * 
	 * promoterSumCash:(推广员退关分润余额信息统计). <br/> 
	 * 
	 * @author Administrator 
	 * @param userId
	 * @return 
	 * @since JDK 1.8
	 */
	PromoterSumVO promoterSumCash(@Param(value = "userId") Long userId);
	
	
	
	/**
	 * 
	 * PlatformSum:(统计平台大屏的门店数量 商户数量 交易总额 交易笔数 会员数量 推广员数量 推广分润总额 粉丝奖励总额 收益总计). <br/> 
	 * 
	 * @author Administrator 
	 * @return 
	 * @since JDK 1.8
	 */
	PlatformSumVO PlatformSum(@Param(value = "timeStatus") String timeStatus);
	
	/**
	 * 
	 * sumConsumeFeebystoreId:(查询门店的今日流水 跟总流水). <br/> 
	 * 
	 * @author Administrator 
	 * @param storeId
	 * @return 
	 * @since JDK 1.8
	 */
	StoreVO sumConsumeFeebystoreId(String storeId);
	
	/**
	 * 
	 * sumConsumeFeebystoreId:(查询商户的今日流水 跟总流水). <br/> 
	 * 
	 * @author Administrator
	 * @return 
	 * @since JDK 1.8
	 */
	StoreVO sumConsumeFeebymerchantCode(String merchantCode);

	/**
	 * 修改总金额
	 * @param orderMap
	 * @return
	 */
	Double getOrderFeeSum1(Map<String, Object> orderMap);
	
	Double getToDayTransSum1(Map<String, Object> orderMap);

	@Select("SELECT SUM(consumeFee) fee, status FROM tb_pluss_merchant_order ${ew.customSqlSegment}")
	<E extends MerchantOrder> List<Map<String, Object>> getSumConsumeFee(@Param(Constants.WRAPPER) Wrapper<E> wrapper);

	@Select("SELECT IFNULL(SUM(IF(`status` = 1, enterFee, -enterFee)), 0) fee FROM tb_pluss_merchant_order ${ew.customSqlSegment}")
	<E extends MerchantOrder> Map<String, Object> getSumEnterFee(@Param(Constants.WRAPPER) Wrapper<E> wrapper);

	@Select("SELECT IFNULL(SUM(IF(`status` = 1, consumeFee, -consumeFee)), 0) fee, count(1) count FROM tb_pluss_merchant_order " +
			"WHERE merchantCode = #{merchantCode} AND createDate = #{date} AND `status` IN (1, 2)")
	Map<String, Object> getConsumeFee(@Param("merchantCode") String merchantCode, @Param("date") String date);

    Map<String, Object> querySumCount(Map<String, Object> params);

	/**
	 * 获取分组总营业额
	 * @param merchantCodeList
	 * @return
	 */
	List<Map<String, Object>> selectGroupMerchantTotalConsumeMoney(@Param("merchantCodeList") List<String> merchantCodeList);

	/**
	 * 查询自己和子属商户交易成功总额
	 * @param merchantNoList
	 * @return
	 */
    Double queryNextMercTransAmtCount(List<String> merchantNoList);

	/**
	 * 查询直推商户当月的总交易流水
	 * @param parentId
	 * @return
	 */
	Double querySubMerchantTransMonthAmount(@Param("parentId") String parentId);

	/**
	 * 查询直推商户上月的总交易流水
	 * @param parentId
	 * @return
	 */
	Double querySubMerchantTransLastMonthAmount(@Param("parentId") String parentId);

	/**
	 * 批量更新
	 * @param lists
	 */
    void updateBatch(List<MerchantOrder> lists);

	/**
	 * 获取超级代理商下级商户当月交易额
	 * @param params
	 * @return
	 */
	List<Map<String, Object>> getSuperMerchantToDayTradeMoneyCount(Map<String, Object> params);

	/**
	 * 获取指定时间段内的订单记录
	 * @param merchantCode 店铺code
	 * @param start 开始时间
	 * @param end   结束时间
	 * @return  每天的总流水
	 */
	List<Map<String, Object>> queryDailyCount(@Param("merchantCode") String merchantCode, @Param("start") Date start, @Param("end") Date end);

	/**
	 *订单推广分润明细
	 * @param map
	 *
	 * @return
	 */
	List<MerchantOrderProfitVO> selectProfitPage(Map<String,Object> map);

	Integer selectProfitPageCount(Map<String, Object> map);


	Double toDayTransZfbSum(Map<String, Object> orderMap);

	Double toDayTransWxSum(Map<String, Object> orderMap);

    Double toDayTransYsfSum(Map<String, Object> orderMap);

    Integer toDayTransZfbCount(Map<String, Object> orderMap);

    Integer toDayTransWxCount(Map<String, Object> orderMap);

    Integer toDayTransYsfCount(Map<String, Object> orderMap);

	/**
	 * 统计营销活动奖励金
	 * @return
	 */
//	Map<String, BigDecimal> getCountMaketAmt(@Param("userId") Long userId,@Param("date") String date,@Param("auditDate") String auditDate);

	/**
	 * 获取商户当前已经积累的交易额度
	 * @param merchantCode
	 * @return
	 */
	Map<String, Double> getCountTradeAmt(@Param("merchantCode")String merchantCode,
										 @Param("date") String date,
										 @Param("startDate") String startDate,
										 @Param("endDate") String endDate);

	BigDecimal getCountMaketAmtAndDate(@Param("merchantCode") String merchantCode, @Param("date") String date);


	Page<MerchantOrder> pageWapInfo(@Param("page") Page<MerchantOrder> page, @Param("order")MerchantOrder order);

	/**
	 * 计算累计省钱金额和今日省钱金额
	 * @param merchantCode
	 * @param nowDate
	 * @return
	 */
    Map<String, BigDecimal> getSaveMoneyCount(@Param("merchantCode") String merchantCode,
											  @Param("date") String nowDate,
											  @Param("startDate")String startDate,
											  @Param("endDate")String endDate);

	/**
	 * 分页连表查询
	 * @param page
	 * @param query
	 * @return
	 */
	Page<MerchantOrder> pageInfo(@Param("page") Page<MerchantOrder> page, @Param("order")MerchantOrder query);

	/**
	 * 获取商户当天交易的结算金额
	 * @return
	 */
    BigDecimal getCountNowDateSettleAmt(String merchantCode);

    /**
     * @description:订单列表根据查询条件统计数据
     * @date: 2021/8/5 16:59
     * @param map:
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    Map<String, Object> getOrderCountData(Map<String, Object> map);

	@Select("SELECT * FROM tb_pluss_merchant_order WHERE orderNumber = #{orderNumber} FOR UPDATE")
    MerchantOrder getLockByOrderNumber(String orderNumber);
	/**
	 * @description:获取订单首页数据
	 * @date: 2021/10/19 14:56
	 * @return java.util.Map<java.lang.String,java.lang.Object>
	 */
	Map<String, Object> getPlatformOrderData();

	/**
	 * @description:统计累计金额数据
	 * @date: 2021/10/19 17:53
	 * @return java.util.Map<java.lang.String,java.lang.Object>
	 */
	Map<String, Object> getPlatformAmtData();

	/**
	 * 获取每个用户的订单流水
	 * @date: 2022/1/15 11:34
	 * @param date:
	 * @return
	 */
	List<UserProfit> getOrderFlow(@Param("date")String date, @Param("sort")Integer sort);


	Integer getOrderMaxSort(String date);

	/**
	 * 查询交易数据
	 * @return
	 */
	Map<String, String> getPlatformTransData();

	/**
	 * 根据商户号和时间获取流水
	 * @date: 2022/3/1 17:01
	 * @param mercList:
	 * @param date:
	 * @param flag:
	 * @return java.math.BigDecimal
	 */
    BigDecimal getAreaOrderCount(@Param("mercList") List<String> mercList, @Param("date") String date, @Param("flag") Integer flag);

	/**
	 * 获取交易金额
	 * @Param date
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Select("SELECT IFNULL(SUM(consumeFee - refundAmt),0) FROM tb_pluss_merchant_order  WHERE createDate = #{date} AND `status` = 1 AND createDt >= #{startDate} AND createDt <= #{endDate}")
	BigDecimal getPlatformAmtYestNowData(@Param("date") String date,@Param("startDate") String startDate, @Param("endDate") String endDate);

	/**
	 * 获取有效交易笔数
	 * @param merchantCode
	 * @return
	 */
	@Select("SELECT count(1) FROM (SELECT mercUserId FROM tb_pluss_merchant_order WHERE merchantCode = #{merchantCode} GROUP BY mercUserId) a")
	Integer getValidNumByMerchantCode(String merchantCode);

	@Select("SELECT * FROM tb_pluss_merchant_order WHERE mercOrderNo = #{mercOrderNo} limit 1")
    MerchantOrder getByMercOrderNo(String mercOrderNo);
}

