package ink.wgink.service.position.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.position.PositionUserDTO;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.pojo.pos.PositionPO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.service.position.dao.IPositionUserDao;
import ink.wgink.service.position.service.IPositionService;
import ink.wgink.service.position.service.IPositionUserService;
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: IPositionUserServiceImpl
 * @Description: 职位用户业务
 * @Author: WangGeng
 * @Date: 2021/2/16 17:49
 * @Version: 1.0
 **/
@Service
public class PositionUserServiceImpl extends DefaultBaseService implements IPositionUserService {

    @Autowired
    private IPositionUserDao positionUserDao;
    @Autowired
    private IUserBaseService userBaseService;
    @Autowired
    private IPositionService positionService;

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

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

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

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

    @Override
    public SuccessResultList<List<PositionUserDTO>> listPage(String positionId, ListPage page) {
        SuccessResultList<List<UserDTO>> successResultList;
        if (StringUtils.equals(positionId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            successResultList = listPageUserDTOByExcludePosition(page);
        } else {
            successResultList = listPageUser(positionId, page);
        }
        List<UserDTO> userDTOs = successResultList.getRows();
        List<PositionUserDTO> positionUserDTOs = new ArrayList<>();
        for (UserDTO userDTO : userDTOs) {
            PositionUserDTO positionUserDTO = new PositionUserDTO(userDTO);
            positionUserDTOs.add(positionUserDTO);
        }
        return new SuccessResultList<>(positionUserDTOs, successResultList.getPage(), successResultList.getTotal());
    }

    @Override
    public List<PositionPO> listPositionPOByUserId(String userId) {
        Map<String, Object> params = getHashMap(2);
        params.put("userId", userId);
        List<String> positionIds = positionUserDao.listPositionId(params);
        if (positionIds.isEmpty()) {
            return new ArrayList<>();
        }
        return positionService.listPO(positionIds);
    }

    @Override
    public List<PositionPO> listPositionPOByUserIds(List<String> userIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("userIds", userIds);
        List<String> positionIds = positionUserDao.listPositionId(params);
        if (positionIds.isEmpty()) {
            return new ArrayList<>();
        }
        return positionService.listPO(positionIds);
    }

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

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

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

}
