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

import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.infra.database.rds.entity.team.RoomBdLead;
import com.red.circle.other.infra.database.rds.service.team.BusinessDevelopmentBaseInfoService;
import com.red.circle.other.infra.database.rds.service.team.BusinessDevelopmentTeamService;
import com.red.circle.other.infra.database.rds.service.team.RoomBdLeadService;
import com.red.circle.other.app.inner.convertor.team.BdInnerConvertor;
import com.red.circle.other.app.inner.service.team.BdTeamLeaderClientService;
import com.red.circle.other.inner.model.cmd.team.bd.BdTeamLeaderAddCmd;
import com.red.circle.other.inner.model.cmd.team.bd.BdTeamLeaderOpsPageQryCmd;
import com.red.circle.other.inner.model.dto.agency.bd.BdTeamInfoDTO;
import com.red.circle.other.inner.model.dto.agency.bd.BdTeamLeaderDTO;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * bd leader 团队服务 实现.
 *
 * @author pengliang on 2023/11/21
 */
@Service
@RequiredArgsConstructor
public class BdTeamLeaderClientServiceImpl implements BdTeamLeaderClientService {

  private final BdInnerConvertor bdInnerConvertor;
  private final RoomBdLeadService roomBdLeadService;
  private final BusinessDevelopmentTeamService businessDevelopmentTeamService;
  private final BusinessDevelopmentBaseInfoService businessDevelopmentBaseInfoService;

  @Override
  public BdTeamLeaderDTO getById(Long id) {
    return bdInnerConvertor.toBdTeamLeaderDTO(roomBdLeadService.getById(id));
  }

  @Override
  public BdTeamLeaderDTO getByUserId(Long userId) {
    return bdInnerConvertor.toBdTeamLeaderDTO(roomBdLeadService.getByUserId(userId));
  }

  @Override
  public Long countBdByLeaderUserId(Long leadUserId) {
    return businessDevelopmentBaseInfoService.countBdByLeaderUserId(leadUserId);
  }

  @Override
  public List<BdTeamInfoDTO> listByLeaderUserId(Long leadUserId, Long lastId) {
    return bdInnerConvertor.toListBdTeamInfoDTO(
        businessDevelopmentBaseInfoService.listBdByLeadUserId(leadUserId, lastId));
  }

  @Override
  public void add(BdTeamLeaderAddCmd cmd) {
    roomBdLeadService.save(bdInnerConvertor.toRoomBdLead(cmd));
  }

  @Override
  public boolean check(Long leadUserId) {
    return roomBdLeadService.checkBdLeader(leadUserId);
  }

  @Override
  public PageResult<BdTeamLeaderDTO> pageOps(BdTeamLeaderOpsPageQryCmd qryCmd) {
    return bdInnerConvertor.toPageBdTeamLeaderDTO(
        roomBdLeadService.query()
            .eq(Objects.nonNull(qryCmd.getUserId()), RoomBdLead::getUserId, qryCmd.getUserId())
            .eq(RoomBdLead::getSysOrigin, qryCmd.getSysOrigin())
            .eq(Objects.nonNull(qryCmd.getCreateUser()), RoomBdLead::getCreateUser,
                qryCmd.getCreateUser())
            .orderByDesc(RoomBdLead::getCreateTime)
            .page(qryCmd.getPageQuery())
    );
  }

  @Override
  public Map<Long, Long> mapCountByLeadUserIds(Set<Long> bdLeadUserIds) {
    return businessDevelopmentBaseInfoService.mapCountByLeaderUserIds(bdLeadUserIds);
  }

  @Override
  public void updateLeaderSelectiveById(BdTeamLeaderDTO dto) {
    roomBdLeadService.updateSelectiveById(bdInnerConvertor.toRoomBdLead(dto));
  }

  @Override
  public Map<Long, Long> mapTeamBdUserId(Set<Long> ids) {
    return businessDevelopmentTeamService.mapTeamBdUserId(ids);
  }

  @Override
  public void removeLeader(Long teamId, Long bdLeadUserId) {
    roomBdLeadService.deleteById(teamId);
    businessDevelopmentBaseInfoService.removeBdLeader(bdLeadUserId);
  }


}
