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

import com.google.common.collect.Maps;
import com.red.circle.common.business.core.enums.ApprovalStatusEnum;
import com.red.circle.console.app.convertor.approval.ApprovalUserSettingInfoAppConvertor;
import com.red.circle.console.app.dto.clienobject.approval.ApprovalProfileDescCO;
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.dynamic.ApprovalUserSettingInfoClient;
import com.red.circle.other.inner.model.cmd.approval.ApprovalProfileDescQryCmd;
import com.red.circle.other.inner.model.cmd.approval.ApprovalUserProfileQryCmd;
import com.red.circle.other.inner.model.dto.approval.ApprovalProfileDescDTO;
import com.red.circle.other.inner.model.dto.approval.ApprovalUserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.inner.endpoint.user.user.UserExpandClient;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.dto.user.UserExpandDTO;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.util.Map;
import java.util.Objects;
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 ApprovalUserSettingInfoServiceImpl implements ApprovalUserSettingInfoService {

  private final UserService userService;
  private final UserExpandClient userExpandClient;
  private final UserProfileClient userProfileClient;
  private final ApprovalUserSettingInfoClient approvalUserSettingInfoClient;
  private final ApprovalUserSettingInfoAppConvertor approvalUserSettingInfoAppConvertor;

  @Override
  public PageResult<ApprovalProfileDescCO> pageProfileDescApproval(
      ApprovalProfileDescQryCmd query) {
    PageResult<ApprovalProfileDescDTO> pageResult = ResponseAssert.requiredSuccess(
        approvalUserSettingInfoClient.pageProfileDescApproval(
            query));
    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      pageResult.convert(approvalUserSettingInfoAppConvertor::toApprovalProfileDescCO);
    }

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

    Map<Long, UserExpandDTO> userExpandMap = ResponseAssert.requiredSuccess(
        userExpandClient.mapUserExpand(getUserIds(pageResult)));

    return pageResult.convert(approvalFamilyDTO -> {

      ApprovalProfileDescCO approvalProfileDescCO = approvalUserSettingInfoAppConvertor.toApprovalProfileDescCO(
          approvalFamilyDTO);
      approvalProfileDescCO.setUserBaseInfo(userMap.get(approvalFamilyDTO.getUserId()));
      approvalProfileDescCO.setProfileDesc(
          Objects.nonNull(userExpandMap.get(approvalFamilyDTO.getUserId())) ? userExpandMap.get(
              approvalFamilyDTO.getUserId()).getSignature() : null);
      return approvalProfileDescCO;
    });
  }

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

  @Override
  public PageResult<ApprovalUserProfileDTO> pageUserProfileApproval(
      ApprovalUserProfileQryCmd query) {

    PageResult<ApprovalUserProfileDTO> pageResult = ResponseAssert.requiredSuccess(
        approvalUserSettingInfoClient.pageUserProfileApproval(
            query));
    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      return pageResult;
    }

    Map<Long, User> userMap =
        Objects.equals(query.getApproveStatus(), ApprovalStatusEnum.PENDING.name())
            ? Maps.newHashMap()
            : userService.mapBackUser(pageResult.getRecords()
                .stream().map(ApprovalUserProfileDTO::getUpdateUser).collect(Collectors.toSet()));

    return pageResult.convert(approvalUserProfileDTO -> {
      approvalUserProfileDTO
          .setUserSexName(getSexName(approvalUserProfileDTO.getUserSex()));
      User user = userMap.get(approvalUserProfileDTO.getUpdateUser());
      if (Objects.nonNull(user)) {
        approvalUserProfileDTO.setApprovalUserName(user.getNickname());
      }
      return approvalUserProfileDTO;
    });
  }

  private String getSexName(Integer sex) {
    //用户性别:0 女，1 男，2 变性人
    if (Objects.equals(sex, 0)) {
      return "女";
    }
    if (Objects.equals(sex, 1)) {
      return "男";
    }
    return "未知";
  }
}
