package cn.stylefeng.team.portal.modular.front.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.rule.pidset.ParentIdParseUtil;
import cn.stylefeng.roses.kernel.sys.api.OrganizationServiceApi;
import cn.stylefeng.roses.kernel.sys.api.pojo.org.HrOrganizationDTO;
import cn.stylefeng.team.portal.modular.front.pojo.UserPortalRenderVo;
import cn.stylefeng.team.portal.modular.front.service.PortalRenderService;
import cn.stylefeng.team.portal.modular.management.mapper.PortalDesignMapper;
import cn.stylefeng.team.portal.modular.management.pojo.response.PortalDesignVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Service
public class PortalRenderServiceImpl implements PortalRenderService {

    @Resource
    private OrganizationServiceApi organizationServiceApi;

    @Resource
    private PortalDesignMapper portalDesignMapper;

    @Override
    public List<PortalDesignVo> getUserPortalList() {

        Long userId = LoginContext.me().getLoginUser().getUserId();
        Long currentOrgId = LoginContext.me().getLoginUser().getCurrentOrgId();

        UserPortalRenderVo userPortalRenderVo = new UserPortalRenderVo();
        userPortalRenderVo.setPointScopeBusinessIdList(currentOrgId == null ? CollectionUtil.set(false, userId) : CollectionUtil.set(false, userId, currentOrgId));

        if (currentOrgId != null) {
            HrOrganizationDTO orgInfo = this.organizationServiceApi.getOrgInfo(currentOrgId);
            List<Long> parentIdList = ParentIdParseUtil.parseToPidList(orgInfo.getOrgPids());
            parentIdList.add(currentOrgId);
            userPortalRenderVo.setPointAndSubBusinessIdList(new HashSet<>(parentIdList));
        }

        return portalDesignMapper.getUserPortalList(userPortalRenderVo);
    }

}
