package com.red.circle.console.app.service.app.count;

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.console.app.convertor.count.DailyOperationActiveIndexAppConvertor;
import com.red.circle.console.app.dto.clienobject.count.PropsSaleCO;
import com.red.circle.console.app.dto.clienobject.count.PropsSaleSourceCO;
import com.red.circle.console.app.dto.clienobject.count.PropsSingeSaleCO;
import com.red.circle.console.app.dto.clienobject.count.StatisticsDailyOperationActiveIndexCO;
import com.red.circle.console.app.dto.clienobject.count.user.SysOriginUserCO;
import com.red.circle.console.app.dto.clienobject.count.user.SysOriginUserRegisterCountCO;
import com.red.circle.console.app.dto.cmd.app.count.PropsSingeSaleQryCmd;
import com.red.circle.console.app.service.app.user.UserBaseInfoService;
import com.red.circle.console.infra.database.mongo.count.service.StatisticsDailyOperationActiveIndexService;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.other.inner.endpoint.count.StatisticsPropsSaleQuotaClient;
import com.red.circle.other.inner.endpoint.count.StatisticsRoomActiveClient;
import com.red.circle.other.inner.endpoint.material.props.PropsSourceClient;
import com.red.circle.other.inner.model.cmd.count.PropsSaleQuotaQryCmd;
import com.red.circle.other.inner.model.cmd.count.SysOriginUserQryCmd;
import com.red.circle.other.inner.model.dto.count.StatisticsPropsSaleQuotaDTO;
import com.red.circle.other.inner.model.dto.count.StatisticsRoomActiveDTO;
import com.red.circle.other.inner.model.dto.material.PropsResourcesDTO;
import com.red.circle.other.inner.model.dto.user.SysUserBaseInfoDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 活跃指标.
 *
 * @author pengliang on 2023/11/16
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CountOperationActiveIndexServiceImpl implements CountOperationActiveIndexService {

  private final PropsSourceClient propsSourceClient;
  private final StatisticsRoomActiveClient statisticsRoomActiveClient;
  private final UserBaseInfoService userBaseInfoService;
  private final StatisticsPropsSaleQuotaClient statisticsPropsSaleQuotaClient;
  private final DailyOperationActiveIndexAppConvertor dailyOperationActiveIndexAppConvertor;
  private final StatisticsDailyOperationActiveIndexService statisticsDailyOperationActiveIndexService;

  @Override
  public List<StatisticsRoomActiveDTO> listRoomActive(Long roomId, Date startTime, Date endTime) {
    return statisticsRoomActiveClient.listByTimeRange(roomId, startTime, endTime).getBody();
  }

  @Override
  public List<StatisticsDailyOperationActiveIndexCO> listLatestDays(Integer days) {
    return dailyOperationActiveIndexAppConvertor.toListDailyOperationActiveIndexCO(
        statisticsDailyOperationActiveIndexService.listLatestDays(days)
    );
  }

  @Override
  public PropsSingeSaleCO getSingePropsSale(PropsSingeSaleQryCmd qryCmd) {
    List<StatisticsPropsSaleQuotaDTO> propsSaleQuotas = statisticsPropsSaleQuotaClient.listPropsSaleQuota(
        new PropsSaleQuotaQryCmd()
            .setSysOrigin(qryCmd.getSysOrigin())
            .setDateType("MONTH_DAY")
            .setDate(qryCmd.getDate())
            .setPropsSourceId(qryCmd.getPropsSourceId())
    ).getBody();

    if (CollectionUtils.isEmpty(propsSaleQuotas)) {
      return null;
    }

    return new PropsSingeSaleCO()
        .setYearTotal(statisticsPropsSaleQuotaClient.getYearPropsSaleQuota(
                qryCmd.getSysOrigin(),
                qryCmd.getPropsSourceId()
            ).getBody()
        )
        .setMonthTotal(
            propsSaleQuotas.stream().mapToLong(StatisticsPropsSaleQuotaDTO::getSaleQuantity).sum())
        .setPropsSaleQuotas(propsSaleQuotas);
  }

  @Override
  public PropsSaleCO getPropsSale(PropsSaleQuotaQryCmd qryCmd) {
    List<StatisticsPropsSaleQuotaDTO> propsSaleQuotas = statisticsPropsSaleQuotaClient.listPropsSaleQuota(
        qryCmd).getBody();
    if (CollectionUtils.isEmpty(propsSaleQuotas)) {
      return null;
    }

    Map<Long, PropsResourcesDTO> propsSourceRecordMap = propsSourceClient.mapByIds(
            propsSaleQuotas.stream().map(StatisticsPropsSaleQuotaDTO::getPropsSourceId)
                .collect(Collectors.toSet()))
        .getBody();

    return new PropsSaleCO()
        .setTotal(
            propsSaleQuotas.stream().mapToLong(StatisticsPropsSaleQuotaDTO::getSaleQuantity).sum())
        .setPropsSales(propsSaleQuotas.stream().map(propsSaleQuota ->
            new PropsSaleSourceCO()
                .setSaleQuantity(propsSaleQuota.getSaleQuantity())
                .setPropsSource(propsSourceRecordMap.get(propsSaleQuota.getPropsSourceId()))
        ).collect(Collectors.toList()));
  }

  @Override
  public SysOriginUserRegisterCountCO getSysOriginUserCount(SysOriginUserQryCmd qryCmd) {
    ResultResponse<List<SysUserBaseInfoDTO>> userBaseInfoDTOS = userBaseInfoService.userListQuery(qryCmd);
    //log.warn("userBaseInfoDTOS,{}", userBaseInfoDTOS.getBody());
    if (CollectionUtils.isEmpty(userBaseInfoDTOS.getBody())) {
      return null;
    }

    List<SysUserBaseInfoDTO> yoloUser = userBaseInfoDTOS.getBody().stream()
            .filter(item -> item.getOriginSys().equals(SysOriginPlatformEnum.YOLO.name()))
            .toList();
    SysOriginUserCO yolo = new SysOriginUserCO();
    yolo.setLogo("https://momooline-dev.oss-ap-southeast-1.aliyuncs.com/Yolo-logo.png");
    yolo.setSysOrigin(SysOriginPlatformEnum.YOLO.name());
    yolo.setSaleQuantity(yoloUser.size());

    List<SysUserBaseInfoDTO> otherUser = userBaseInfoDTOS.getBody().stream()
            .filter(item -> item.getOriginSys().equals(SysOriginPlatformEnum.TARAB.name()))
            .toList();
    SysOriginUserCO tarab = new SysOriginUserCO();
    tarab.setLogo("https://momooline-dev.oss-ap-southeast-1.aliyuncs.com/160fbc10-b7ea-4a98-9a5d-ac95a520e2.png");
    tarab.setSysOrigin(SysOriginPlatformEnum.TARAB.name());
    tarab.setSaleQuantity(otherUser.size());
    List<SysOriginUserCO> sysOriginList = new ArrayList<>();
    sysOriginList.add(yolo);
    sysOriginList.add(tarab);


    return new SysOriginUserRegisterCountCO()
            .setTotal(yoloUser.size() + otherUser.size())
            .setUserCount(sysOriginList);
  }

}
