package com.dayou.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dayou.common.BaseEntity;
import com.dayou.entity.Team;
import com.dayou.entity.TeamMember;
import com.dayou.exception.ErrorCode;
import com.dayou.mapper.TeamMapper;
import com.dayou.service.ITeamMemberService;
import com.dayou.service.ITeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.service.IUserService;
import com.dayou.vo.TeamVO;
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.List;
import java.util.ArrayList;
import java.util.Set;

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

/**
 * <p>
 * 团队 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-04-07
 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements ITeamService {

    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private IUserService userService;

    @Autowired
    private ITeamMemberService teamMemberService;

    @Override
    @SuppressWarnings("unchecked")
    public Page<TeamVO> selectPage(Page page, TeamVO teamVO){
        Page<TeamVO> xPage = teamMapper.getPage(page,teamVO);
        return xPage;
    }


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

    @Transactional
    @Override
    public Boolean add(Team team){
        Long supervisorId = team.getSupervisorId();
        Set<Long> departmentIdByUserId = userService.getDepartmentIdByUserId(supervisorId);
        if (CollectionUtils.isEmpty(departmentIdByUserId) || departmentIdByUserId.size()!=1){
            ErrorCode.throwBusinessException(ErrorCode.CUSTOM_ERROR,"该员工存在多个岗位分属不同部门，暂不支持创建团队");
        }
        List<TeamMember> list = teamMemberService.list(new LambdaQueryWrapper<TeamMember>().eq(TeamMember::getMemberId, supervisorId).eq(BaseEntity::getDeleted, Boolean.FALSE));

        if (CollectionUtils.isNotEmpty(list)){
            ErrorCode.throwBusinessException(ErrorCode.CUSTOM_ERROR,"该员工目前存在于其他团队中，若将其设置为主管请先从其他团队中移除。");
        }
        team.setDepartmentId(departmentIdByUserId.stream().findFirst().get());
        this.save(team);
        //创建团队的时候，营销主管默认加入该团队
        TeamMember tm = new TeamMember();
        tm.setTeamId(team.getId());
        tm.setMemberId(supervisorId);
        return teamMemberService.save(tm);
    }

    @Override
    public Boolean update(Team team){
        return  this.updateById(team);
    }

    @Transactional
    @Override
    public Boolean delete(Long id){
        teamMemberService.remove(new LambdaQueryWrapper<TeamMember>().eq(TeamMember::getTeamId,id));
        //逻辑删除
        return this.removeById(id);
    }
}
