package com.dayou.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dayou.bo.LeaderRatioBO;
import com.dayou.common.BaseEntity;
import com.dayou.dto.TeamLeaderDTO;
import com.dayou.entity.Team;
import com.dayou.entity.TeamMember;
import com.dayou.mapper.TeamMemberMapper;
import com.dayou.service.ITeamMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.service.ITeamService;
import com.dayou.service.IUserService;
import com.dayou.vo.TeamMemberVO;
import com.dayou.vo.TeamVO;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 团队成员 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-04-07
 */

@Slf4j
@Service
public class TeamMemberServiceImpl extends ServiceImpl<TeamMemberMapper, TeamMember> implements ITeamMemberService {

    @Autowired
    private TeamMemberMapper teamMemberMapper;

    @Autowired
    private ITeamService teamService;

    @Autowired
    private IUserService userService;

    @Override
    @SuppressWarnings("unchecked")
    public Page<TeamMember> selectPage(Page page,TeamMember teamMember){
        return this.page(page, new QueryWrapper<TeamMember>(teamMember));
    }


    @Override
    public TeamMember detail(Long id){
        return this.getById(id);
    }

    @Override
    public Boolean add(TeamVO teamVO){
        List<Long> members = teamVO.getMemberIds();
        Long teamId = teamVO.getId();
        if (CollectionUtils.isNotEmpty(members) && teamId!=null){
            Set<TeamMember> teamMembers = members.stream().map(x -> {
                TeamMember teamMember = new TeamMember();
                teamMember.setMemberId(x);
                teamMember.setTeamId(teamId);
                return teamMember;
            }).collect(Collectors.toSet());
            return  this.saveBatch(teamMembers);
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean update(TeamMember teamMember){
        return  this.updateById(teamMember);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public Set<LeaderRatioBO> getLeader(Long userId,String departmentName) {
        Team leader = teamMemberMapper.getLeader(userId, departmentName);
        if (leader!=null){
            Long supervisorId = leader.getSupervisorId();
            Long cultivateId = leader.getCultivateId();
            Set<Long> leaderIds = Sets.newHashSet(supervisorId, cultivateId);
            Set<LeaderRatioBO> set = userService.getLeaderRatioBO(leaderIds);
            return set;
        }
        //如果没有加入团队，则团队领导为营销经理
        Set<LeaderRatioBO> set = userService.marketManagerRatioBO();
        if (CollectionUtils.isEmpty(set)){
            log.info("警告！市场部经理人员暂未设置");
        }
        return set;
    }

    @Override
    public Set<Long> getJuniorUserIds(Long userId) {
        Set<Long> juniorUserIds = new HashSet<>();
        Team team = teamService.getOne(new LambdaQueryWrapper<Team>().eq(Team::getSupervisorId, userId).eq(BaseEntity::getDeleted, Boolean.FALSE));
        //当team为空时，则userId代表营销经理
        if (team!=null){
            juniorUserIds = this.list(new LambdaQueryWrapper<TeamMember>()
                    .eq(TeamMember::getTeamId,team.getId()).eq(BaseEntity::getDeleted,Boolean.FALSE)).stream().map(TeamMember::getMemberId).collect(Collectors.toSet());
        }else {
            juniorUserIds = userService.getJuniorUserId(userId);
        }
        return juniorUserIds;
    }


}
