package com.red.circle.other.app.inner.service.team.manager.impl;

import com.google.common.collect.Maps;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.team.TeamManagerInnerConvertor;
import com.red.circle.other.app.inner.service.team.manager.TeamManagerClientService;
import com.red.circle.other.infra.database.rds.entity.team.TeamManagerInfo;
import com.red.circle.other.infra.database.rds.service.team.TeamManagerInfoService;
import com.red.circle.other.inner.model.cmd.team.manager.TeamManagerAddCmd;
import com.red.circle.other.inner.model.cmd.team.manager.TeamManagerOpsPageQryCmd;
import com.red.circle.other.inner.model.dto.agency.manager.TeamManagerDTO;
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;

/**
 * 团队经理 inner 服务实现.
 */
@Service
@RequiredArgsConstructor
public class TeamManagerClientServiceImpl implements TeamManagerClientService {

  private final TeamManagerInfoService teamManagerInfoService;
  private final TeamManagerInnerConvertor teamManagerInnerConvertor;

  @Override
  public TeamManagerDTO getById(Long id) {
    return teamManagerInnerConvertor.toTeamManagerDTO(teamManagerInfoService.getById(id));
  }

  @Override
  public TeamManagerDTO getByUserId(Long userId) {
    return teamManagerInnerConvertor.toTeamManagerDTO(teamManagerInfoService.getByUserId(userId));
  }

  @Override
  public void add(TeamManagerAddCmd cmd) {
    teamManagerInfoService.save(teamManagerInnerConvertor.toTeamManagerInfo(cmd));
  }

  @Override
  public boolean check(Long userId) {
    return teamManagerInfoService.checkManager(userId);
  }

  @Override
  public Map<Long, Boolean> mapCheck(Set<Long> userIds) {
    if (CollectionUtils.isEmpty(userIds)) {
      return Maps.newHashMap();
    }

    return teamManagerInfoService.listByUserIds(userIds).stream()
        .collect(Collectors.toMap(TeamManagerInfo::getUserId, v -> Boolean.TRUE, (v1, v2) -> v2));
  }

  @Override
  public PageResult<TeamManagerDTO> pageOps(TeamManagerOpsPageQryCmd qryCmd) {
    return teamManagerInnerConvertor.toPageTeamManagerDTO(
        teamManagerInfoService.query()
            .eq(Objects.nonNull(qryCmd.getUserId()), TeamManagerInfo::getUserId, qryCmd.getUserId())
            .eq(TeamManagerInfo::getSysOrigin, qryCmd.getSysOrigin())
            .eq(Objects.nonNull(qryCmd.getCreateUser()), TeamManagerInfo::getCreateUser,
                qryCmd.getCreateUser())
            .orderByDesc(TeamManagerInfo::getCreateTime)
            .page(qryCmd.getPageQuery())
    );
  }

  @Override
  public void updateSelectiveById(TeamManagerDTO dto) {
    teamManagerInfoService.updateSelectiveById(teamManagerInnerConvertor.toTeamManagerInfo(dto));
  }

  @Override
  public void deleteById(Long id) {
    teamManagerInfoService.deleteById(id);
  }

}
