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.BusinessDevelopmentTeam;
import com.red.circle.other.infra.database.rds.service.team.BusinessDevelopmentTeamService;
import com.red.circle.other.app.inner.convertor.team.BdInnerConvertor;
import com.red.circle.other.app.inner.service.team.BdTeamMemberClientService;
import com.red.circle.other.inner.model.cmd.team.bd.BdTeamMemberOpsPageQryCmd;
import com.red.circle.other.inner.model.dto.agency.bd.BdTeamMemberDTO;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * bd 团队成员 实现.
 *
 * @author pengliang on 2023/11/21
 */
@Service
@RequiredArgsConstructor
public class BdTeamMemberClientServiceImpl implements BdTeamMemberClientService {

  private final BdInnerConvertor bdInnerConvertor;
  private final BusinessDevelopmentTeamService businessDevelopmentTeamService;

  @Override
  public PageResult<BdTeamMemberDTO> pageOps(BdTeamMemberOpsPageQryCmd qryCmd) {
    return bdInnerConvertor.toPageBdTeamMemberDTO(
        businessDevelopmentTeamService.query()
            .eq(BusinessDevelopmentTeam::getUserId, qryCmd.getUserId())
            .eq(Objects.nonNull(qryCmd.getAgentId()), BusinessDevelopmentTeam::getAgentId,
                qryCmd.getAgentId())
            .orderByDesc(BusinessDevelopmentTeam::getCreateTime)
            .page(qryCmd.getPageQuery())
    );

  }

  @Override
  public boolean exist(Long agentUserId) {
    return businessDevelopmentTeamService.existTeam(agentUserId);
  }

  @Override
  public void add(String sysOrigin, Long agentUserId, Long bdUserId, Long teamId,
      Long updateUserId) {
    businessDevelopmentTeamService.addTeam(sysOrigin, agentUserId, bdUserId, teamId, updateUserId);
  }

  @Override
  public BdTeamMemberDTO getById(Long id) {
    return bdInnerConvertor.toBdTeamMemberDTO(
        businessDevelopmentTeamService.getById(id)
    );
  }

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

  @Override
  public void deleteAllByUserId(Long userId) {
    businessDevelopmentTeamService.deleteAllByUserId(userId);
  }

  @Override
  public List<BdTeamMemberDTO> listByUserId(Long userId) {
    return bdInnerConvertor.toListBdTeamMemberDTO(
        businessDevelopmentTeamService.listByUserId(userId)
    );

  }

}
