package com.goafanti.customer.service.impl;

import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.AdminCallDayMapper;
import com.goafanti.common.dao.SalesTeamMapper;
import com.goafanti.common.model.SalesTeam;
import com.goafanti.common.utils.BeanUtilsExt;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.customer.bo.InputListCallDayBo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class AdminCallDayServiceImpl extends BaseMybatisDao<AdminCallDayMapper> implements AdminCallDayService{

    @Resource
    private SalesTeamMapper salesTeamMapper;

    @Override
    public Object listCallDay(InputListCallDayBo in) {
//        if (in.getEndTime()!=null)in.setEndTime(in.getEndTime()+" 23:59:59");
        Map<String, Object> map = BeanUtilsExt.pushMap(in);
        Set<Integer> set = new HashSet<>();
        if (!TokenManager.hasRole(AFTConstants.SUPERADMIN)&& !TokenManager.hasRole(AFTConstants.CALL_MANAGER)){
            String adminId = TokenManager.getAdminId();
            List<SalesTeam> salesTeams = salesTeamMapper.queryByAid(adminId);
            if (!salesTeams.isEmpty()){
                List<Integer> collect = salesTeams.stream().map(SalesTeam::getId).collect(Collectors.toList());
                set.addAll(collect);
                for (Integer i : collect) {
                    List<Integer> integers = salesTeamMapper.queryByAncestors(i);
                    set.addAll(integers);
                }
                //查看的权限
                map.put("teamIds",set);
            }
        }
        if (in.getTeamId()!=null){
            List<Integer> integers = salesTeamMapper.queryByAncestors(Integer.valueOf(in.getTeamId()));
            integers.add(Integer.valueOf(in.getTeamId()));
            map.put("teamIdList",integers);
        }
        return findPage("findAdminCallDayList", "findAdminCallDayCount", map, in.getPageNo(), in.getPageSize());
    }

    /**
     *
     * @param map
     * @param set
     * @param integers
     */
    private void pushTeams(Map<String, Object> map, Set<Integer> set, List<Integer> integers) {
        if (integers.isEmpty()){
            map.put("teamIds",set);
        }else {
            List<Integer> newList = integers.stream()
                    .filter(set::contains)
                    .collect(Collectors.toList());
            map.put("teamIds",newList);
        }
    }
}
