package com.tofly.gpsboot.service.comm.impl;

import cn.hutool.core.collection.CollUtil;
import com.tofly.api.DepartmentClient;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.service.comm.IDisposeDateService;
import com.tofly.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DisposeDateServiceImpl implements IDisposeDateService {

    @Autowired
    private DepartmentClient departmentClient;
    @Override
    public Boolean isDisposePermissions(Long userId) {
        // 判断当前用户操作是否拥有操作该对象权限
        Long userid = SecurityUtils.getUser().getUserid();
        return userid.equals(userId);
    }
    @Override
    public String getDeptUsers(Long userId,Long deptId){
        Set<Long> userIdList = new HashSet<>();
        // 默认当前部门
        if (null == userId && null == deptId) {
            List<User> result = departmentClient.getDeptUserList(SecurityUtils.getUserDeptId()).getResult();
            if(CollUtil.isNotEmpty(result)) {
                Set<Long> collect = result.stream().map(User::getId).collect(Collectors.toSet());
                userIdList.addAll(collect);
            }
        }
        if(null == userId && null != deptId){
            List<User> result = departmentClient.getDeptUserList(deptId).getResult();
            if(CollUtil.isNotEmpty(result)) {
                Set<Long> collect = result.stream().map(User::getId).collect(Collectors.toSet());
                userIdList.addAll(collect);
            }
        }
        if(null != userId){
            userIdList.add(userId);
        }
        StringBuilder stringBuilder = new StringBuilder();
        userIdList.forEach(info -> stringBuilder.append(info).append(Constant.CSV));

        return stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString();
    }
}
