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

import com.red.circle.console.app.convertor.approval.ApprovalDynamicAppConvertor;
import com.red.circle.console.app.dto.clienobject.approval.ApprovalDynamicCO;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.inner.endpoint.dynamic.ApprovalDynamicClient;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.cmd.approval.ApprovalDynamicCmd;
import com.red.circle.other.inner.model.cmd.approval.ApprovalDynamicQryCmd;
import com.red.circle.other.inner.model.dto.approval.ApprovalDynamicDTO;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
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/12
 */
@Service
@RequiredArgsConstructor
public class ApprovalDynamicContentServiceImpl implements ApprovalDynamicContentService {


  private final UserProfileClient userProfileClient;

  private final ApprovalDynamicClient approvalDynamicClient;
  private final ApprovalDynamicAppConvertor approvalDynamicAppConvertor;

  @Override
  public PageResult<ApprovalDynamicCO> page(ApprovalDynamicQryCmd query) {

    PageResult<ApprovalDynamicDTO> pageResult = ResponseAssert.requiredSuccess(
        approvalDynamicClient.pageApprovalDynamic(query));
    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      return pageResult.convert(approvalDynamicAppConvertor::toApprovalDynamicCO);
    }

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

    return pageResult.convert(approvalDynamicDTO -> {
      UserProfileDTO user = userMap.get(approvalDynamicDTO.getUserId());

      ApprovalDynamicCO approvalDynamicCO = approvalDynamicAppConvertor.toApprovalDynamicCO(
          approvalDynamicDTO);

      if (Objects.nonNull(user)) {
        approvalDynamicCO.setUserBaseInfo(user);
        approvalDynamicCO.setUserSexName(getSexName(user.getUserSex()));
      }else{
        approvalDynamicCO.setUserBaseInfo(new UserProfileDTO());
        approvalDynamicCO.setUserSexName(getSexName(2));
      }

      return approvalDynamicCO;
    });
  }

  @Override
  public void approvalPass(Set<Long> ids) {
    approvalDynamicClient.approvalPass(ids);

  }

  @Override
  public void approvalNotPass(Set<Long> ids, Long reqUserId) {
    ApprovalDynamicCmd approvalDynamicCmd = new ApprovalDynamicCmd();
    approvalDynamicCmd.setIds(ids);
    approvalDynamicCmd.setUserId(reqUserId);
    approvalDynamicClient.approvalNotPass(approvalDynamicCmd);

  }

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

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


}
