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

import com.red.circle.common.business.core.enums.ApprovalStatusEnum;
import com.red.circle.console.app.convertor.approval.ApprovalUserViolationHistoryAppConvertor;
import com.red.circle.console.app.dto.clienobject.approval.ApprovalUserViolationHistoryCO;
import com.red.circle.console.infra.database.rds.entity.admin.User;
import com.red.circle.console.infra.database.rds.service.admin.UserService;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.inner.endpoint.approval.ApprovalUserViolationHistoryClient;
import com.red.circle.other.inner.model.cmd.approval.ApprovalUserViolationHistoryQryCmd;
import com.red.circle.other.inner.model.dto.approval.ApprovalUserViolationHistoryDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 审批违规历史记录
 *
 * @author zongpubin on 2024/1/22
 */

@Service
@RequiredArgsConstructor
public class ApprovalUserViolationHistoryServiceImpl implements
    ApprovalUserViolationHistoryService {

  private final UserService userService;
  private final UserProfileClient userProfileClient;
  private final ApprovalUserViolationHistoryClient approvalUserViolationHistoryClient;
  private final ApprovalUserViolationHistoryAppConvertor approvalUserViolationHistoryAppConvertor;


  @Override
  public PageResult<ApprovalUserViolationHistoryCO> pageHistory(
      ApprovalUserViolationHistoryQryCmd query) {
    PageResult<ApprovalUserViolationHistoryDTO> pageResult = ResponseAssert.requiredSuccess(
        approvalUserViolationHistoryClient.pageHistory(query));

    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      return pageResult.convert(
          approvalUserViolationHistoryAppConvertor::toApprovalUserViolationHistoryCO);
    }

    Map<Long, UserProfileDTO> userMap = ResponseAssert.requiredSuccess(
        userProfileClient.mapByUserIds(getUserIds(pageResult)));

    return pageResult.convert(history -> {
      ApprovalUserViolationHistoryCO historyResult = approvalUserViolationHistoryAppConvertor
          .toApprovalUserViolationHistoryCO(history);
      historyResult.setViolationTypeName(history.getViolationType().getDesc());
      historyResult
          .setApprovalResultName(
              ApprovalStatusEnum.getDescValue(historyResult.getApprovalResult()));
      //判断是App用户审核的还是后台
      handleApprovalInfo(historyResult);
      historyResult.setUserBaseInfo(
          Optional.ofNullable(userMap.get(historyResult.getUserId()))
              .orElse(new UserProfileDTO()));
      return historyResult;
    });
  }

  private Set<Long> getUserIds(PageResult<ApprovalUserViolationHistoryDTO> pageResult) {
    return pageResult.getRecords().stream().map(ApprovalUserViolationHistoryDTO::getUserId).collect(
        Collectors.toSet());
  }

  private void handleApprovalInfo(ApprovalUserViolationHistoryCO historyResult) {
    if (Objects.equals(historyResult.getOriginType(), 1)) {
      UserProfileDTO userInfo = Optional
          .ofNullable(ResponseAssert.requiredSuccess(
              userProfileClient.getByUserId(historyResult.getCreateUser())))
          .orElse(new UserProfileDTO());
      historyResult.setApprovalUserBaseInfo(userInfo);
      historyResult.setApprovalNickname(userInfo.getUserNickname());
      return;
    }
    historyResult.setApprovalNickname(
        Optional.ofNullable(userService.getById(historyResult.getCreateUser()))
            .map(User::getNickname)
            .orElse(""));
  }
}
