package com.qs.mp.quartz.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.mp.admin.domain.TicketBox;
import com.qs.mp.admin.service.ITicketBoxService;
import com.qs.mp.channel.domain.*;
import com.qs.mp.channel.service.*;
import com.qs.mp.common.enums.*;
import com.qs.mp.common.utils.DateUtils;
import com.qs.mp.common.utils.LogUtil;
import com.qs.mp.common.utils.StringUtils;
import com.qs.mp.common.utils.WebhookService;
import com.qs.mp.system.domain.SysUser;
import com.qs.mp.system.service.ISysUserService;
import com.qs.mp.user.domain.UserCoinLog;
import com.qs.mp.user.domain.UserCoupon;
import com.qs.mp.user.domain.UserExchangeOrder;
import com.qs.mp.user.domain.UserTicketOrder;
import com.qs.mp.user.service.IUserCoinLogService;
import com.qs.mp.user.service.IUserCouponService;
import com.qs.mp.user.service.IUserExchangeOrderService;
import com.qs.mp.user.service.IUserTicketOrderService;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 日统计任务
 *
 * @author zhongcp
 * @Date 2022/3/17
 */
@Component("dayStatTask")
public class DayStatTask {

  protected final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

  @Autowired
  private ISysUserService sysUserService;

  @Autowired
  private IChannelService channelService;

  @Autowired
  private ITicketBoxService ticketBoxService;

  @Autowired
  private IUserTicketOrderService userTicketOrderService;

  @Autowired
  private IChannelCommissionService channelCommissionService;

  @Autowired
  private IChannelWithdrawService channelWithdrawService;

  @Autowired
  private IUserExchangeOrderService userExchangeOrderService;

  @Autowired
  private IUserCoinLogService userCoinLogService;

  @Autowired
  private IChannelCouponVerifyLogService channelCouponVerifyLogService;

  @Autowired
  private IChannelOrderService channelOrderService;

  @Autowired
  private IUserCouponService userCouponService;

  @Value("${export.page-size}")
  private int pageSize;

  /**
   * 日统计任务 2022.04.02 代理：xxx 门店：xxx 用户：xxx =============== 票组名字|线上（面值），销售：¥0.00 票组名字|线上（面值），销售：¥0.00
   * 总销售额：¥0.00 累计分佣：¥0.00 累计提现：¥0.00 兑换订单：5689 派发盲豆：8880 兑换盲豆：6666
   */
  public void stat(String executeDay) {
    LogUtil.info(logger, "...日统计定时任务开始...");
    Date bizDay;
    if (StringUtils.isNotBlank(executeDay)) {
      bizDay = DateUtils.parseStrToDate(executeDay, DateUtils.YYYYMMDD);
    } else {
      bizDay = DateUtils.addDays(DateUtils.getToday(), -1);
    }
    StringBuilder sb = new StringBuilder();
    sb.append(DateUtils.dateTime(bizDay)).append("\n");
    int allChannelCnt = channelService.count(new LambdaQueryWrapper<Channel>().gt(Channel::getLevel, 0));
    int channelCnt = channelService.count(new LambdaQueryWrapper<Channel>().gt(Channel::getLevel, 0)
        .ge(Channel::getCreatedTime, bizDay)
        .lt(Channel::getCreatedTime, DateUtils.addDays(bizDay, 1)));
    sb.append("累计代理：").append(allChannelCnt).append("，当日新增：").append(channelCnt).append("\n");
    int siteCnt = channelService.count(new LambdaQueryWrapper<Channel>().eq(Channel::getLevel, 0)
        .ge(Channel::getCreatedTime, bizDay)
        .lt(Channel::getCreatedTime, DateUtils.addDays(bizDay, 1)));
    int allSiteCnt = channelService.count(new LambdaQueryWrapper<Channel>().eq(Channel::getLevel, 0));
    sb.append("累计门店：").append(allSiteCnt).append("，当日新增：").append(siteCnt).append("\n");
    int userCnt = sysUserService.count(new LambdaQueryWrapper<SysUser>()
        .ge(SysUser::getCreateTime, bizDay)
        .lt(SysUser::getCreateTime, DateUtils.addDays(bizDay, 1)));
    int allUserCnt = sysUserService.count(new LambdaQueryWrapper<SysUser>());
    sb.append("累计用户：").append(allUserCnt - allSiteCnt - allChannelCnt).append("，当日新增：").append(userCnt - siteCnt - channelCnt).append("\n");
    sb.append("===================\n");

    StringBuffer sbDetail = new StringBuffer("");
    LambdaQueryWrapper<TicketBox> queryWrapper = new LambdaQueryWrapper<TicketBox>()
//        .nested(wrapper -> {
//          wrapper.eq(TicketBox::getType, TicketTypeEnum.ONLINE).eq(TicketBox::getStatus,
//              TicketBoxStatusEnum.PUT_ON).or().eq(TicketBox::getType, TicketTypeEnum.OFFLINE);
//        })
//        .notLike(TicketBox::getTitle, "测试票")
        .eq(TicketBox::getIsDeleted, 0);

    int totalSize = ticketBoxService.count(queryWrapper);
    int totalPage = 1;
    if (totalSize > pageSize) {
      totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1;
    }
    BigDecimal sumSaleAmt = BigDecimal.ZERO;
    BigDecimal sumAllSaleAmt = BigDecimal.ZERO;


    // 线上销售额和汇总
    BigDecimal onlineSumSaleAmt = BigDecimal.ZERO;
    BigDecimal onlineSumAllSaleAmt = BigDecimal.ZERO;
    // 线下销售额和汇总
    BigDecimal offlineSumSaleAmt = BigDecimal.ZERO;
    BigDecimal offlineSumAllSaleAmt = BigDecimal.ZERO;


    // 预付的金额统计
    BigDecimal sumPreSaleAmt = BigDecimal.ZERO;
    BigDecimal sumAllPreSaleAmt = BigDecimal.ZERO;
    for (int i = 0; i < totalPage; i++) {
      queryWrapper.orderByAsc(TicketBox::getBoxNo);
      queryWrapper.last("limit " + (i * pageSize) + ", " + pageSize);
      List<TicketBox> ticketBoxList = ticketBoxService.list(queryWrapper);
      // 区分线上和线下标识
      boolean isOnline = true;
      for (TicketBox ticketBox : ticketBoxList) {
        if (TicketTypeEnum.OFFLINE.equals(ticketBox.getType())) {
          isOnline = false;
        }

        Map<String, Object> orderMap = userTicketOrderService.getMap(
            new QueryWrapper<UserTicketOrder>()
                .select("IFNULL(sum(pay_amt) ,0) as orderAmt")
                .lambda().eq(UserTicketOrder::getBoxId, ticketBox.getBoxId())
                .eq(UserTicketOrder::getStatus, UserTicketOrderStatusEnum.FINISHED)
                .ge(UserTicketOrder::getPayTime, bizDay)
                .lt(UserTicketOrder::getPayTime, DateUtils.addDays(bizDay, 1)));

        // 预付的金额统计
        QueryWrapper<ChannelOrder> channelOrderQueryWrapper = new QueryWrapper<>();
        channelOrderQueryWrapper.eq("t2.box_id", ticketBox.getBoxId());
        channelOrderQueryWrapper.gt("t1.type" , 1);
        channelOrderQueryWrapper.gt("t1.status" , 0);
        channelOrderQueryWrapper.ge("t1.pay_time" , bizDay);
        channelOrderQueryWrapper.lt("t1.pay_time" , DateUtils.addDays(bizDay, 1));
        int preSaleAmt = channelOrderService.selectPreSaleAmtByQueryWrapper(channelOrderQueryWrapper);


        if (null != orderMap || preSaleAmt > 0) {
          BigDecimal orderAmt = BigDecimal.ZERO;
          if (Objects.nonNull(orderMap)) {
            orderAmt = new BigDecimal(
                    Integer.valueOf(String.valueOf(orderMap.get("orderAmt")))).divide(new BigDecimal(100),
                    2, RoundingMode.DOWN);
//            sumSaleAmt = sumSaleAmt.add(orderAmt);
            if (isOnline) {
              onlineSumSaleAmt = onlineSumSaleAmt.add(orderAmt);
            } else {
              offlineSumSaleAmt = offlineSumSaleAmt.add(orderAmt);
            }
          }


          // 设置预付票金额
          BigDecimal preOrderAmt = new BigDecimal(preSaleAmt).divide(new BigDecimal(100),2,RoundingMode.DOWN);
          sumPreSaleAmt = sumPreSaleAmt.add(preOrderAmt);

          Map<String, Object> allOrderMap = userTicketOrderService.getMap(
              new QueryWrapper<UserTicketOrder>()
                  .select("IFNULL(sum(pay_amt) ,0) as orderAmt")
                  .lambda().eq(UserTicketOrder::getBoxId, ticketBox.getBoxId())
                  .eq(UserTicketOrder::getStatus, UserTicketOrderStatusEnum.FINISHED)
                  .lt(UserTicketOrder::getPayTime, DateUtils.addDays(bizDay, 1)));
          BigDecimal allOrderAmt = BigDecimal.ZERO;
          if (Objects.nonNull(allOrderMap)) {
            allOrderAmt = new BigDecimal(
                Integer.valueOf(String.valueOf(allOrderMap.get("orderAmt")))).divide(new BigDecimal(100),
                2, RoundingMode.DOWN);
//            sumAllSaleAmt = sumAllSaleAmt.add(allOrderAmt);
            if (isOnline) {
              onlineSumAllSaleAmt = onlineSumAllSaleAmt.add(allOrderAmt);
            } else {
              offlineSumAllSaleAmt = offlineSumAllSaleAmt.add(allOrderAmt);
            }
          }

          // 预付累积金额
          QueryWrapper<ChannelOrder> channelOrderQueryWrapper1 = new QueryWrapper<>();
          channelOrderQueryWrapper1.eq("t2.box_id", ticketBox.getBoxId());
          channelOrderQueryWrapper1.gt("t1.type" , 1);
          channelOrderQueryWrapper1.gt("t1.status" , 0);
          channelOrderQueryWrapper1.lt("t1.pay_time" , DateUtils.addDays(bizDay, 1));
          int allPreSaleAmt = channelOrderService.selectPreSaleAmtByQueryWrapper(channelOrderQueryWrapper1);

          // 设置预付票累积金额
          BigDecimal allPreOrderAmt = new BigDecimal(allPreSaleAmt).divide(new BigDecimal(100),2,RoundingMode.DOWN);
          sumAllPreSaleAmt = sumAllPreSaleAmt.add(allPreOrderAmt);




          // 只输出当日销量大于0的票组
          if (orderAmt.compareTo(BigDecimal.ZERO) > 0 || preOrderAmt.compareTo(BigDecimal.ZERO) > 0) {
            sbDetail.append(ticketBox.getTitle()).append("|").append(ticketBox.getType().getDesc())
                .append("(").append(
                    new BigDecimal(ticketBox.getSalePrice()).divide(new BigDecimal(100), 2,
                        RoundingMode.DOWN)).append("元)，累计零售销售额：")
                .append(allOrderAmt).append("元，当日：")
                .append(orderAmt).append("元\n");

            sbDetail.append("累计预付销售额：")
                    .append(allPreOrderAmt).append("元，当日：")
                    .append(preOrderAmt).append("元\n");
            sbDetail.append("------------------------\n");
          }
        }
      }
    }
    sumSaleAmt = BigDecimal.ZERO.add(onlineSumSaleAmt).add(offlineSumSaleAmt);
    sumAllSaleAmt = BigDecimal.ZERO.add(onlineSumAllSaleAmt).add(offlineSumAllSaleAmt);
    sb.append("累计零售总销售金额：").append(sumAllSaleAmt).append("元，当日：").append(sumSaleAmt).append("元\n");
    sb.append("线上累计零售销售金额：").append(onlineSumAllSaleAmt).append("元，当日：").append(onlineSumSaleAmt).append("元\n");
    sb.append("线下累计零售销售金额：").append(offlineSumAllSaleAmt).append("元，当日：").append(offlineSumSaleAmt).append("元，").append("累计预付销售金额：").append(sumAllPreSaleAmt).append("元，当日：").append(sumPreSaleAmt).append("元\n");

//    sb.append("累计零售销售额：").append(sumAllSaleAmt).append("元，当日：").append(sumSaleAmt).append("元\n");
//    sb.append("累计预付销售额：").append(sumAllPreSaleAmt).append("元，当日：").append(sumPreSaleAmt).append("元\n");
    Map<String, Object> commMap = channelCommissionService.getMap(
        new QueryWrapper<ChannelCommission>()
            .select("IFNULL(sum(comm_amt) ,0) as commAmt")
            .lambda().ge(ChannelCommission::getCreatedTime, bizDay)
            .lt(ChannelCommission::getCreatedTime, DateUtils.addDays(bizDay, 1)));
    Map<String, Object> allCommMap = channelCommissionService.getMap(
        new QueryWrapper<ChannelCommission>()
            .select("IFNULL(sum(comm_amt) ,0) as commAmt")
            .lambda().lt(ChannelCommission::getCreatedTime, DateUtils.addDays(bizDay, 1)));
    if (null != commMap) {
      sb.append("累计分佣：").append(
              new BigDecimal(Integer.valueOf(String.valueOf(allCommMap.get("commAmt")))).divide(
                  new BigDecimal(100), 2, RoundingMode.DOWN))
          .append("元，当日：").append(
              new BigDecimal(Integer.valueOf(String.valueOf(commMap.get("commAmt")))).divide(
                  new BigDecimal(100), 2, RoundingMode.DOWN)).append("元\n");
    }

    if (null != commMap) {

      // 线上核销统计
      QueryWrapper<UserCoupon> onlineDisCountQueryWrapper = new QueryWrapper<>();
      onlineDisCountQueryWrapper.ge("t1.verify_time", bizDay);
      onlineDisCountQueryWrapper.lt("t1.verify_time", DateUtils.addDays(bizDay, 1));
      onlineDisCountQueryWrapper.eq("t1.status", UserCouponStatusEnum.USED);
      onlineDisCountQueryWrapper.eq("t2.discount_type", CouponDiscountTypeEnum.MONEY_OFF);
      onlineDisCountQueryWrapper.eq("t2.type", CouponTypeEnum.TICKET_ORDER);
      Integer onlineDisCount = userCouponService.sumDiscountByQueryWrapper(onlineDisCountQueryWrapper);
      QueryWrapper<UserCoupon> onlineAllDisCountQueryWrapper = new QueryWrapper<>();
      onlineAllDisCountQueryWrapper.lt("t1.verify_time", DateUtils.addDays(bizDay, 1));
      onlineAllDisCountQueryWrapper.eq("t1.status", UserCouponStatusEnum.USED);
      onlineAllDisCountQueryWrapper.eq("t2.discount_type", CouponDiscountTypeEnum.MONEY_OFF);
      onlineAllDisCountQueryWrapper.eq("t2.type", CouponTypeEnum.TICKET_ORDER);
      Integer onlineAllDisCount = userCouponService.sumDiscountByQueryWrapper(onlineAllDisCountQueryWrapper);

      // 线下核销统计
      QueryWrapper<UserCoupon> offlineDisCountQueryWrapper = new QueryWrapper<>();
      offlineDisCountQueryWrapper.ge("t1.verify_time", bizDay);
      offlineDisCountQueryWrapper.lt("t1.verify_time", DateUtils.addDays(bizDay, 1));
      offlineDisCountQueryWrapper.eq("t1.status", UserCouponStatusEnum.USED);
      offlineDisCountQueryWrapper.eq("t2.discount_type", CouponDiscountTypeEnum.MONEY_OFF);
      offlineDisCountQueryWrapper.eq("t2.type", CouponTypeEnum.SITE_CONSUME);
      Integer offlineDisCount = userCouponService.sumDiscountByQueryWrapper(offlineDisCountQueryWrapper);
      QueryWrapper<UserCoupon> offlineAllDisCountQueryWrapper = new QueryWrapper<>();
      offlineAllDisCountQueryWrapper.lt("t1.verify_time", DateUtils.addDays(bizDay, 1));
      offlineAllDisCountQueryWrapper.eq("t1.status", UserCouponStatusEnum.USED);
      offlineAllDisCountQueryWrapper.eq("t2.discount_type", CouponDiscountTypeEnum.MONEY_OFF);
      offlineAllDisCountQueryWrapper.eq("t2.type", CouponTypeEnum.SITE_CONSUME);
      Integer offlineAllDisCount = userCouponService.sumDiscountByQueryWrapper(offlineAllDisCountQueryWrapper);


      sb.append("线上券累计核销金额：").append(
                      new BigDecimal(Integer.valueOf(String.valueOf(onlineAllDisCount))).divide(
                              new BigDecimal(100), 2, RoundingMode.DOWN))
              .append("元，当日：").append(
                      new BigDecimal(Integer.valueOf(String.valueOf(onlineDisCount))).divide(
                              new BigDecimal(100), 2, RoundingMode.DOWN)).append("元\n");

      sb.append("线下券累计核销金额：").append(
                      new BigDecimal(Integer.valueOf(String.valueOf(offlineAllDisCount))).divide(
                              new BigDecimal(100), 2, RoundingMode.DOWN))
              .append("元，当日：").append(
                      new BigDecimal(Integer.valueOf(String.valueOf(offlineDisCount))).divide(
                              new BigDecimal(100), 2, RoundingMode.DOWN)).append("元\n");

    }

    Map<String, Object> withdrawMap = channelWithdrawService.getMap(
        new QueryWrapper<ChannelWithdraw>()
            .select("IFNULL(sum(money) ,0) as withdrawAmt")
            .lambda().ge(ChannelWithdraw::getCreateTime, bizDay)
            .lt(ChannelWithdraw::getCreateTime, DateUtils.addDays(bizDay, 1))
            .eq(ChannelWithdraw::getStatus, ChannelWithdrawStatusEnum.FINISHED));
    Map<String, Object> allWithdrawMap = channelWithdrawService.getMap(
        new QueryWrapper<ChannelWithdraw>()
            .select("IFNULL(sum(money) ,0) as withdrawAmt")
            .lambda().lt(ChannelWithdraw::getCreateTime, DateUtils.addDays(bizDay, 1))
            .eq(ChannelWithdraw::getStatus, ChannelWithdrawStatusEnum.FINISHED));
    Map<String, Object> allAcctMoneyMap = channelService.getMap(
        new QueryWrapper<Channel>()
            .select("IFNULL(sum(money) ,0) as acctAmt"));
    if (null != withdrawMap) {
      sb.append("累计提现：").append(
              new BigDecimal(Integer.valueOf(String.valueOf(allWithdrawMap.get("withdrawAmt")))).divide(
                  new BigDecimal(100), 2, RoundingMode.DOWN))
          .append("元，待提现：").append(
              new BigDecimal(Integer.valueOf(String.valueOf(allAcctMoneyMap.get("acctAmt")))).divide(
                  new BigDecimal(100), 2, RoundingMode.DOWN))
          .append("元，当日：").append(
              new BigDecimal(Integer.valueOf(String.valueOf(withdrawMap.get("withdrawAmt")))).divide(
                  new BigDecimal(100), 2, RoundingMode.DOWN)).append("元\n");
    }


    // 线上和线下派发券金额统计
    QueryWrapper<UserCoupon> onlineUserCouponQueryWrapper = new QueryWrapper<>();
    onlineUserCouponQueryWrapper.ge("t1.created_time", bizDay);
    onlineUserCouponQueryWrapper.lt("t1.created_time", DateUtils.addDays(bizDay, 1));
    onlineUserCouponQueryWrapper.eq("t2.discount_type", CouponDiscountTypeEnum.MONEY_OFF);
    onlineUserCouponQueryWrapper.eq("t2.type", CouponTypeEnum.TICKET_ORDER);
    Integer onlineUserCouponDisSum = userCouponService.sumDiscountByQueryWrapper(onlineUserCouponQueryWrapper);


    QueryWrapper<UserCoupon> onlineAllUserCouponQueryWrapper = new QueryWrapper<>();
    onlineAllUserCouponQueryWrapper.lt("t1.created_time", DateUtils.addDays(bizDay, 1));
    onlineAllUserCouponQueryWrapper.eq("t2.discount_type", CouponDiscountTypeEnum.MONEY_OFF);
    onlineAllUserCouponQueryWrapper.eq("t2.type", CouponTypeEnum.TICKET_ORDER);
    Integer onlineAllUserCouponDisSum = userCouponService.sumDiscountByQueryWrapper(onlineAllUserCouponQueryWrapper);


    QueryWrapper<UserCoupon> offlineUserCouponQueryWrapper = new QueryWrapper<>();
    offlineUserCouponQueryWrapper.ge("t1.created_time", bizDay);
    offlineUserCouponQueryWrapper.lt("t1.created_time", DateUtils.addDays(bizDay, 1));
    offlineUserCouponQueryWrapper.eq("t2.discount_type", CouponDiscountTypeEnum.MONEY_OFF);
    offlineUserCouponQueryWrapper.eq("t2.type", CouponTypeEnum.SITE_CONSUME);
    Integer offlineUserCouponDisSum = userCouponService.sumDiscountByQueryWrapper(offlineUserCouponQueryWrapper);


    QueryWrapper<UserCoupon> offlineAllUserCouponQueryWrapper = new QueryWrapper<>();
    offlineAllUserCouponQueryWrapper.lt("t1.created_time", DateUtils.addDays(bizDay, 1));
    offlineAllUserCouponQueryWrapper.eq("t2.discount_type", CouponDiscountTypeEnum.MONEY_OFF);
    offlineAllUserCouponQueryWrapper.eq("t2.type", CouponTypeEnum.SITE_CONSUME);
    Integer offlineAllUserCouponDisSum = userCouponService.sumDiscountByQueryWrapper(offlineAllUserCouponQueryWrapper);

    sb.append("线上累计派券金额：").append(
                    new BigDecimal(Integer.valueOf(String.valueOf(onlineAllUserCouponDisSum))).divide(
                            new BigDecimal(100), 2, RoundingMode.DOWN))
            .append("元，当日：").append(
                    new BigDecimal(Integer.valueOf(String.valueOf(onlineUserCouponDisSum))).divide(
                            new BigDecimal(100), 2, RoundingMode.DOWN)).append("元\n");

    sb.append("线下累计派券金额：").append(
                    new BigDecimal(Integer.valueOf(String.valueOf(offlineAllUserCouponDisSum))).divide(
                            new BigDecimal(100), 2, RoundingMode.DOWN))
            .append("元，当日：").append(
                    new BigDecimal(Integer.valueOf(String.valueOf(offlineUserCouponDisSum))).divide(
                            new BigDecimal(100), 2, RoundingMode.DOWN)).append("元\n");


    int exCnt = userExchangeOrderService.count(new LambdaQueryWrapper<UserExchangeOrder>()
        .ge(UserExchangeOrder::getCreatedTime, bizDay)
        .lt(UserExchangeOrder::getCreatedTime, DateUtils.addDays(bizDay, 1)));
    int allExCnt = userExchangeOrderService.count(new LambdaQueryWrapper<UserExchangeOrder>()
        .lt(UserExchangeOrder::getCreatedTime, DateUtils.addDays(bizDay, 1)));
    sb.append("累计兑换订单：").append(allExCnt).append("个，当日：").append(exCnt).append("个\n");

    Map<String, Object> deliverMap = userCoinLogService.getMap(new QueryWrapper<UserCoinLog>()
        .select("IFNULL(sum(log_money) ,0) as deliverAmt")
        .lambda().eq(UserCoinLog::getType, CoinLogTypeEnum.PRIZE)
        .ge(UserCoinLog::getCreatedTime, bizDay)
        .lt(UserCoinLog::getCreatedTime, DateUtils.addDays(bizDay, 1)));
    Map<String, Object> allDeliverMap = userCoinLogService.getMap(new QueryWrapper<UserCoinLog>()
        .select("IFNULL(sum(log_money) ,0) as deliverAmt")
        .lambda().eq(UserCoinLog::getType, CoinLogTypeEnum.PRIZE)
        .lt(UserCoinLog::getCreatedTime, DateUtils.addDays(bizDay, 1)));
    if (null != deliverMap) {
      sb.append("累计派发盲豆：").append(Integer.valueOf(String.valueOf(allDeliverMap.get("deliverAmt"))))
          .append("颗，当日：").append(Integer.valueOf(String.valueOf(deliverMap.get("deliverAmt"))))
          .append("颗\n");
    }

    Map<String, Object> exchangeMap = userCoinLogService.getMap(new QueryWrapper<UserCoinLog>()
        .select("IFNULL(sum(log_money) ,0) as exchangeAmt")
        .lambda().eq(UserCoinLog::getType, CoinLogTypeEnum.EXCHANGE)
        .ge(UserCoinLog::getCreatedTime, bizDay)
        .lt(UserCoinLog::getCreatedTime, DateUtils.addDays(bizDay, 1)));
    Map<String, Object> allExchangeMap = userCoinLogService.getMap(new QueryWrapper<UserCoinLog>()
        .select("IFNULL(sum(log_money) ,0) as exchangeAmt")
        .lambda().eq(UserCoinLog::getType, CoinLogTypeEnum.EXCHANGE)
        .lt(UserCoinLog::getCreatedTime, DateUtils.addDays(bizDay, 1)));
    if (null != withdrawMap) {
      sb.append("累计兑换盲豆：").append(0 - Integer.valueOf(String.valueOf(allExchangeMap.get("exchangeAmt"))))
          .append("颗，当日：").append(0 - Integer.valueOf(String.valueOf(exchangeMap.get("exchangeAmt"))))
          .append("颗\n");
    }
    sb.append("===================\n");
    sb.append(sbDetail);

    // 分割,超过15条则分批发送
    String data = sb.toString();
    String[] dataArray = data.split("------------------------");
    if (dataArray.length >= 15) {
      int count = 0;
      StringBuilder body = new StringBuilder();
      for (int i = 0; i < dataArray.length; i++) {
        count++;
        body.append(dataArray[i]).append("------------------------");
        if (count >= 15 || i >= dataArray.length - 1) {
          LogUtil.info(logger, "通知发送结果：" + WebhookService.sendBizData(body.toString()));
          body.delete(0, body.length());
          count = 0;
        }
      }
    } else {
      LogUtil.info(logger, "通知发送结果：" + WebhookService.sendBizData(data));
    }
    LogUtil.info(logger, sb.toString());
    LogUtil.info(logger, "...日统计任务结束..." );
  }
}
