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

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.team.portal.api.enums.NewsStatusEnum;
import cn.stylefeng.team.portal.modular.front.pojo.request.CommonComponentRequest;
import cn.stylefeng.team.portal.modular.front.pojo.request.CommonPageRequest;
import cn.stylefeng.team.portal.modular.front.pojo.request.CommonSummaryRequest;
import cn.stylefeng.team.portal.modular.front.pojo.response.CommonSummaryItem;
import cn.stylefeng.team.portal.modular.front.service.ComponentNewsService;
import cn.stylefeng.team.portal.modular.management.entity.PortalNews;
import cn.stylefeng.team.portal.modular.management.entity.PortalNewsCategory;
import cn.stylefeng.team.portal.modular.management.enums.PortalNewsExceptionEnum;
import cn.stylefeng.team.portal.modular.management.pojo.request.PortalNewsRequest;
import cn.stylefeng.team.portal.modular.management.service.PortalNewsCategoryService;
import cn.stylefeng.team.portal.modular.management.service.PortalNewsService;
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 ComponentNewsServiceImpl implements ComponentNewsService {

    @Resource
    private PortalNewsCategoryService portalNewsCategoryService;

    @Resource
    private PortalNewsService portalNewsService;

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

    @Override
    public List<CommonSummaryItem> getNewsSummary(CommonSummaryRequest newsSummaryRequest) {
        LambdaQueryWrapper<PortalNewsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PortalNewsCategory::getDesignId, newsSummaryRequest.getDesignId());
        wrapper.in(PortalNewsCategory::getCategoryId, newsSummaryRequest.getCategoryIdList());
        wrapper.orderByAsc(PortalNewsCategory::getFldSort);
        List<PortalNewsCategory> list = this.portalNewsCategoryService.list(wrapper);
        if (ObjectUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<CommonSummaryItem> result = new ArrayList<>();
        for (PortalNewsCategory portalNewsCategory : list) {
            CommonSummaryItem commonSummaryItem = new CommonSummaryItem();
            commonSummaryItem.setCategoryId(portalNewsCategory.getCategoryId());
            commonSummaryItem.setCategoryName(portalNewsCategory.getCategoryName());
            result.add(commonSummaryItem);
        }
        return result;
    }

    @Override
    public PageResult<PortalNews> getNewsPageList(CommonPageRequest commonPageRequest) {
        PortalNewsRequest portalNewsRequest = new PortalNewsRequest();
        portalNewsRequest.setDesignId(commonPageRequest.getDesignId());
        portalNewsRequest.setNewsCategoryId(commonPageRequest.getCategoryId());

        portalNewsRequest.setNewsStatus(NewsStatusEnum.ITEM_2.getCode());
        return portalNewsService.findPage(portalNewsRequest);
    }

    @Override
    public PortalNews getNewsDetail(Long newsId) {

        PortalNewsRequest portalNewsRequest = new PortalNewsRequest();
        portalNewsRequest.setNewsId(newsId);
        PortalNews detail = portalNewsService.detail(portalNewsRequest);

        if (NewsStatusEnum.ITEM_1.getCode().equals(detail.getNewsStatus())) {
            throw new ServiceException(PortalNewsExceptionEnum.NEWS_IS_NOT_PUBLISH);
        }

        return detail;
    }

}
