package ink.wgink.service.group.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.group.GroupUserDTO;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.pojo.pos.GroupPO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.service.group.dao.IGroupUserDao;
import ink.wgink.service.group.service.IGroupService;
import ink.wgink.service.group.service.IGroupUserService;
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: GroupUserServiceImpl
 * @Description: 用户组用户
 * @Author: wanggeng
 * @Date: 2021/2/16 11:32 上午
 * @Version: 1.0
 */
@Service
public class GroupUserServiceImpl extends DefaultBaseService implements IGroupUserService {

    @Autowired
    private IGroupUserDao groupUserDao;
    @Autowired
    private IUserBaseService userBaseService;
    @Autowired
    private IGroupService groupService;

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

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

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

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

    @Override
    public SuccessResultList<List<GroupUserDTO>> listPage(String groupId, ListPage page) {
        SuccessResultList<List<UserDTO>> successResultList;
        if (StringUtils.equals(groupId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            successResultList = listPageUserDTOByExcludeGroup(page);
        } else {
            successResultList = listPageUser(groupId, page);
        }
        List<UserDTO> userDTOs = successResultList.getRows();
        List<GroupUserDTO> groupUserDTOs = new ArrayList<>();
        for (UserDTO userDTO : userDTOs) {
            GroupUserDTO groupUserDTO = new GroupUserDTO(userDTO);
            groupUserDTOs.add(groupUserDTO);
        }
        return new SuccessResultList<>(groupUserDTOs, successResultList.getPage(), successResultList.getTotal());
    }

    @Override
    public List<GroupPO> listGroupPOByUserId(String userId) {
        Map<String, Object> params = getHashMap(2);
        params.put("userId", userId);
        List<String> groupIds = groupUserDao.listGroupId(params);
        if (groupIds.isEmpty()) {
            return new ArrayList<>();
        }
        return groupService.listPO(groupIds);
    }


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

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

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

}
