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

import com.red.circle.console.app.convertor.activity.UserActivityAppConvertor;
import com.red.circle.console.app.dto.clienobject.user.UserActivityRewardReceiveRecordCO;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.inner.endpoint.activity.PropsActivityClient;
import com.red.circle.other.inner.model.dto.activity.props.ActivityPropsRuleDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.inner.model.cmd.user.UserActivityRewardReceiveRecordQryCmd;
import com.red.circle.other.inner.model.dto.user.UserActivityRewardReceiveRecordDTO;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.other.inner.endpoint.user.user.UserActivityClient;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户活动月度领取记录
 * </p>
 *
 * @author pengshigang
 * @since 2021-06-19
 */
@Service
@RequiredArgsConstructor
public class UserActivityRewardReceiveRecordServiceImpl implements
    UserActivityRewardReceiveRecordService {

  private final UserProfileClient userProfileClient;
  private final UserActivityClient userActivityClient;
  private final PropsActivityClient propsActivityClient;
  private final UserActivityAppConvertor userActivityAppConvertor;

  @Override
  public PageResult<UserActivityRewardReceiveRecordCO> pageRewardReceiveRecord(
      UserActivityRewardReceiveRecordQryCmd query) {

    PageResult<UserActivityRewardReceiveRecordDTO> pageResult = ResponseAssert.requiredSuccess(
        userActivityClient.pageUserActivityRewardReceiveRecord(query));

    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      return pageResult.convert(userActivityAppConvertor::toUserActivityRewardReceiveRecordCO);
    }

    Map<Long, UserProfileDTO> userMap = ResponseAssert.requiredSuccess(
        userProfileClient.mapByUserIds(
            pageResult.getRecords().stream().map(UserActivityRewardReceiveRecordDTO::getUserId)
                .collect(
                    Collectors.toSet())));

    Map<Long, ActivityPropsRuleDTO> ruleConfigMap = ResponseAssert.requiredSuccess(
        propsActivityClient.mapRuleConfigByIds(
            pageResult.getRecords().stream().map(UserActivityRewardReceiveRecordDTO::getActivityId)
                .collect(Collectors.toSet())));

    return pageResult.convert(receiveRecord -> {
      UserActivityRewardReceiveRecordCO co = userActivityAppConvertor.toUserActivityRewardReceiveRecordCO(
          receiveRecord);
      co.setUserId(receiveRecord.getUserId());
      UserProfileDTO user = userMap.get(receiveRecord.getUserId());
      if (Objects.nonNull(user)) {
        co.setUserProfile(user);
      }
      ActivityPropsRuleDTO ruleConfig = ruleConfigMap.get(receiveRecord.getActivityId());
      co.setRuleDescription(ruleConfig.getRuleDescription());
      co.setSysOrigin(ruleConfig.getSysOrigin());
      co.setActivityType(ruleConfig.getActivityType());
      return co;
    });
  }
}
