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

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.team.portal.modular.front.factory.FrontAppLinkFactory;
import cn.stylefeng.team.portal.modular.front.pojo.request.CommonComponentRequest;
import cn.stylefeng.team.portal.modular.front.pojo.request.CommonSummaryRequest;
import cn.stylefeng.team.portal.modular.front.pojo.response.FrontAppLinkItem;
import cn.stylefeng.team.portal.modular.front.service.ComponentAppLinkService;
import cn.stylefeng.team.portal.modular.management.entity.PortalAppLink;
import cn.stylefeng.team.portal.modular.management.entity.PortalAppLinkCategory;
import cn.stylefeng.team.portal.modular.management.service.PortalAppLinkCategoryService;
import cn.stylefeng.team.portal.modular.management.service.PortalAppLinkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ComponentAppLinkServiceImpl implements ComponentAppLinkService {

    @Resource
    private PortalAppLinkCategoryService portalAppLinkCategoryService;

    @Resource
    private PortalAppLinkService portalAppLinkService;

    @Override
    public List<PortalAppLinkCategory> getCategoryList(CommonComponentRequest slideshowSelectListRequest) {
        LambdaQueryWrapper<PortalAppLinkCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PortalAppLinkCategory::getDesignId, slideshowSelectListRequest.getDesignId());
        wrapper.select(PortalAppLinkCategory::getCategoryId, PortalAppLinkCategory::getCategoryName);
        return portalAppLinkCategoryService.list(wrapper);
    }

    @Override
    public List<FrontAppLinkItem> getAppLinkSummary(CommonSummaryRequest commonSummaryRequest) {

        LambdaQueryWrapper<PortalAppLinkCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PortalAppLinkCategory::getDesignId, commonSummaryRequest.getDesignId());
        wrapper.in(PortalAppLinkCategory::getCategoryId, commonSummaryRequest.getCategoryIdList());
        wrapper.orderByAsc(PortalAppLinkCategory::getFldSort);
        List<PortalAppLinkCategory> list = this.portalAppLinkCategoryService.list(wrapper);
        if (ObjectUtil.isEmpty(list)) {
            return new ArrayList<>();
        }

        LambdaQueryWrapper<PortalAppLink> appLinkWrapper = new LambdaQueryWrapper<>();
        appLinkWrapper.in(PortalAppLink::getLinkCategoryId, commonSummaryRequest.getCategoryIdList());

        appLinkWrapper.select(PortalAppLink::getAppLinkId, PortalAppLink::getLinkCategoryId, PortalAppLink::getLinkName,
                PortalAppLink::getLinkIconFileId, PortalAppLink::getRedirectType, PortalAppLink::getRedirectUrl,
                PortalAppLink::getOpenType);

        appLinkWrapper.orderByAsc(PortalAppLink::getFldSort);
        List<PortalAppLink> totalAppLink = portalAppLinkService.list(appLinkWrapper);

        return FrontAppLinkFactory.buildAppLink(list, totalAppLink);
    }

}
