package com.boman.system.service.impl;

import com.boman.common.core.exception.CustomException;
import com.boman.common.core.utils.StringUtils;
import com.boman.common.core.utils.array.ArrayUtils;
import com.boman.common.core.utils.obj.ObjectUtils;
import com.boman.common.datascope.annotation.DataScope;
import com.boman.domain.*;
import com.boman.domain.constant.UserConstants;
import com.boman.domain.dto.AjaxResult;
import com.boman.system.domain.vo.TreeSelect;
import com.boman.system.mapper.*;
import com.boman.system.service.IBomanGroupService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义组 服务实现
 *
 * @author ruoyi
 */
@Service
public class BomanGroupServiceImpl implements IBomanGroupService {
    @Autowired
    private BomanGroupMapper groupMapper;

    @Autowired
    private BomanGroupUserMapper groupUserMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询自定义组数据
     *
     * @param bomanGroup 自定义组
     * @return 自定义组集合
     */
    @Override
    @DataScope(deptAlias = "g")
    public List<BomanGroup> selectGroupList(BomanGroup bomanGroup) {
        return groupMapper.selectGroupList(bomanGroup);
    }

    /**
     * 根据组织id查询信息
     *
     * @param id
     * @return
     */
    @Override
    public BomanGroup selectGroupById(Long id) {
        return groupMapper.selectGroupById(id);
    }

    /**
     * 查询组织是否存在用户
     *
     * @param ids 组织ID
     * @return 结果 true 存在 false 不存在
     */
    @Override
    public boolean checkGroupExistUser(Long[] ids) {
        int result = groupMapper.checkGroupExistUser(ids);
        return result > 0 ? true : false;
    }

    /**
     * 校验组织名称是否唯一
     *
     * @param bomanGroup 组织信息
     * @return 结果
     */
    @Override
    public String checkGroupNameUnique(BomanGroup bomanGroup) {
        Long id = StringUtils.isNull(bomanGroup.getId()) ? -1L : bomanGroup.getId();
        SysDept sysDept = groupMapper.checkGroupNameUnique(bomanGroup.getGroupName());
        if (StringUtils.isNotNull(sysDept) && sysDept.getId().longValue() != id.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增保存组织信息
     *
     * @param bomanGroup 部门信息
     * @return
     */
    @Override
    public int insertGroup(BomanGroup bomanGroup) {
        return groupMapper.insertGroup(bomanGroup);
    }

    /**
     * 修改保存组织信息
     *
     * @param bomanGroup 组织信息
     * @return
     */
    @Override
    public int updateGroup(BomanGroup bomanGroup) {
        return groupMapper.updateGroup(bomanGroup);
    }

    /**
     * 删除
     *
     * @param ids 组织ID
     * @return
     */
    @Override
    public int deleteGroupId(Long[] ids) {
        return groupMapper.deleteGroupById(ids);
    }


    /**给组添加用户
     * @param groupId
     * @param userIds
     * @return
     */
    @Override
    public AjaxResult insertUserGroup(Long groupId, Long[] userIds) {
        //查询改组所有用户
        List<Long> list = groupUserMapper.selectGroupUserList(groupId);
        List<Long> longs = Arrays.asList(userIds);
        list.removeIf(longs::contains
        );
        if (list.size() > 0){
            for (Long userId : list) {
                BomanGroupUser bomanGroupUser = new BomanGroupUser();
                bomanGroupUser.setUserId(userId);
                bomanGroupUser.setGroupId(groupId);
                SysUser user = sysUserMapper.selectUserById(userId);
                if (user != null){
                    bomanGroupUser.setUserName(user.getUserName());
                }else {
                    return AjaxResult.error("该用户不存在");
                }
            }
        }
        return AjaxResult.success();
    }


    /**
     * 给组删除用户
     * @param groupId
     * @param userIds
     * @return
     */
    @Override
    public AjaxResult deleteGroupUser(Long groupId, Long[] userIds) {
        return null;
    }

}
