package com.infoepoch.pms.agent.domain.baisc.user;

import com.infoepoch.pms.agent.domain.baisc.organization.Organization;
import com.infoepoch.pms.agent.domain.baisc.organization.OrganizationRoot;
import com.infoepoch.pms.agent.platform.shared.exception.ValidationException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户业务处理（新）
 *
 * @Date 2024-06-27
 */
@Service
public class UserQueryService {

    @Autowired
    private IUserRepository userRepository;
    @Autowired
    private IUserQueryRepository userQueryRepository;

    /**
     * @param criteria
     * @return
     */
    public Map<String, Object> roleUserList(UserCriteria criteria) {
        if (StringUtils.isBlank(criteria.getRoleId())) {
            throw new ValidationException("角色ID不能为空");
        }
        // 只查询未禁用的账号
        criteria.setDisabled(false);
        List<User> userList;
        int totalCount;
        if (criteria.byPage()) {
            userList = userRepository.selectByCriteriaPage(criteria, criteria.getPageIndex(), criteria.getPageSize());
            totalCount = userRepository.selectByCriteriaCount(criteria);
        } else {
            userList = userRepository.selectByCriteria(criteria);
            totalCount = userList.size();
        }
        fillOrganizationName(userList);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", userList);
        resultMap.put("totalCount", totalCount);
        return resultMap;
    }

    /**
     * 用户弹窗列表
     *
     * @param userCriteria
     * @return
     */
    public List<User> popupList(UserCriteria userCriteria) {
        List<User> userList;
        if (userCriteria.byPage()) {
            userList = userQueryRepository.selectByCriteriaByPage(userCriteria, userCriteria.getPageIndex(), userCriteria.getPageSize());
        } else {
            userList = userQueryRepository.selectByCriteria(userCriteria);
        }
        return userList;
    }

    public List<User> popupListNotRela(UserCriteria userCriteria){
        List<User> userList;
        if (userCriteria.byPage()) {
            userList = userQueryRepository.selectByCriteriaByPageNotRela(userCriteria, userCriteria.getPageIndex(), userCriteria.getPageSize());
        } else {
            userList = userQueryRepository.selectByCriteriaNotRela(userCriteria);
        }
        return userList;
    }

    /**
     * 提案职工代表弹窗列表
     *
     * @param userCriteria
     * @return
     */
    public List<User> popupListProp(UserCriteria userCriteria) {
        List<User> userList;
        if (userCriteria.byPage()) {
            userList = userQueryRepository.selectByCriteriaByPageProp(userCriteria, userCriteria.getPageIndex(), userCriteria.getPageSize());
        } else {
            userList = userQueryRepository.selectByCriteriaProp(userCriteria);
        }
        return userList;
    }

    /**
     * 用户弹窗列表数量
     *
     * @param userCriteria
     * @return
     */
    public int popupListCount(UserCriteria userCriteria) {
        return userQueryRepository.selectCountByCriteria(userCriteria);
    }
    public int popupListCountProp(UserCriteria userCriteria) {
        return userQueryRepository.selectCountByCriteriaProp(userCriteria);
    }
    public int popupListNotRelaCount(UserCriteria userCriteria) {
        return userQueryRepository.selectCountByCriteriaByPageNotRela(userCriteria);
    }
    /**
     * @param userCriteria
     * @return
     */
    public List<User> list(UserCriteria userCriteria) {
        List<User> userList;
        if (userCriteria.byPage()) {
            userList = userRepository.selectByCriteriaPage(userCriteria, userCriteria.getPageIndex(), userCriteria.getPageSize());
        } else {
            userList = userRepository.selectByCriteria(userCriteria);
        }
        return userList;
    }

    // region 私有方法

    private void fillOrganizationName(List<User> userList) {
        if (CollectionUtils.isEmpty(userList))
            return;
        Map<String, Organization> organizationMap = OrganizationRoot.getOrganizationMap();
        for (User user : userList) {
            Organization organization = organizationMap.get(user.getOrganizationId());
            user.setOrganizationName(organization == null ? null : organization.getName());
        }
    }

    // endregion 私有方法

}
