package ink.wgink.service.role.service.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.interfaces.user.IUserBaseService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.role.RoleUserDTO;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.pojo.pos.RolePO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.service.role.dao.IRoleUserDao;
import ink.wgink.service.role.service.IRoleService;
import ink.wgink.service.role.service.IRoleUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: RoleUserServiceImpl
 * @Description: 角色用户
 * @Author: WangGeng
 * @Date: 2021/2/18 11:24
 * @Version: 1.0
 **/
@Service
public class RoleUserServiceImpl extends DefaultBaseService implements IRoleUserService {

    @Autowired
    private IRoleUserDao roleUserDao;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IUserBaseService userBaseService;

    @Override
    public void update(String roleId, List<String> userIds) {
        delete(roleId, userIds);
        if (!userIds.isEmpty()) {
            Map<String, Object> params = getHashMap(4);
            params.put("roleId", roleId);
            for (String userId : userIds) {
                params.put("userId", userId);
                roleUserDao.save(params);
            }
        }
    }

    @Override
    public void delete(String roleId, List<String> userIds) {
        Map<String, Object> params = getHashMap(4);
        params.put("roleId", roleId);
        params.put("userIds", userIds);
        roleUserDao.delete(params);
    }

    @Override
    public List<String> listUserId(String roleId) {
        Map<String, Object> params = getHashMap(2);
        params.put("roleId", roleId);
        return roleUserDao.listUserId(params);
    }

    @Override
    public List<String> listUserId(List<String> roleIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("roleIds", roleIds);
        return roleUserDao.listUserId(params);
    }

    @Override
    public SuccessResultList<List<RoleUserDTO>> listPage(String roleId, ListPage page) {
        SuccessResultList<List<UserDTO>> successResultList;
        if (StringUtils.equals(roleId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            successResultList = listPageUserDTOByExcludeRole(page);
        } else {
            successResultList = listPageUser(roleId, page);
        }
        List<UserDTO> userDTOs = successResultList.getRows();
        List<RoleUserDTO> roleUserDTOs = new ArrayList<>();
        for (UserDTO userDTO : userDTOs) {
            RoleUserDTO roleUserDTO = new RoleUserDTO(userDTO);
            roleUserDTOs.add(roleUserDTO);
        }
        return new SuccessResultList<>(roleUserDTOs, successResultList.getPage(), successResultList.getTotal());
    }

    @Override
    public List<RolePO> listRolePOByUserId(String userId) {
        Map<String, Object> params = getHashMap(2);
        params.put("userId", userId);
        List<String> roleIds = roleUserDao.listRoleId(params);
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }
        return roleService.listPO(roleIds);
    }


    private List<String> listGroupUserId(Map<String, Object> params) {
        return roleUserDao.listGroupUserId(params);
    }

    /**
     * 没有用户组的用户分页列表
     *
     * @param page
     * @return
     */
    private SuccessResultList<List<UserDTO>> listPageUserDTOByExcludeRole(ListPage page) {
        List<String> hasRoleUserIds = listGroupUserId(new HashMap<>(0));
        return userBaseService.listPageByExcludeIds(hasRoleUserIds, page);
    }

    /**
     * 用户组的用户分页列表
     *
     * @param roleId 用户组ID
     * @param page
     * @return
     */
    private SuccessResultList<List<UserDTO>> listPageUser(String roleId, ListPage page) {
        List<String> userIds = listUserId(roleId);
        if (userIds.isEmpty()) {
            return new SuccessResultList<>(new ArrayList<>(), 1, 0L);
        }
        return userBaseService.listPageByIds(userIds, page);
    }
    
}
