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

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
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.SlideshowRedirectTypeEnum;
import cn.stylefeng.team.portal.modular.management.entity.PortalSlideshow;
import cn.stylefeng.team.portal.modular.management.enums.PortalSlideshowExceptionEnum;
import cn.stylefeng.team.portal.modular.management.factory.SlideshowFactory;
import cn.stylefeng.team.portal.modular.management.mapper.PortalSlideshowMapper;
import cn.stylefeng.team.portal.modular.management.pojo.request.PortalSlideshowRequest;
import cn.stylefeng.team.portal.modular.management.service.PortalNewsService;
import cn.stylefeng.team.portal.modular.management.service.PortalSlideshowService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class PortalSlideshowServiceImpl extends ServiceImpl<PortalSlideshowMapper, PortalSlideshow> implements PortalSlideshowService {

    @Resource
    private PortalNewsService portalNewsService;

    @Override
    public void add(PortalSlideshowRequest portalSlideshowRequest) {

        this.validateSlideshow(portalSlideshowRequest);

        PortalSlideshow portalSlideshow = new PortalSlideshow();
        SlideshowFactory.commonFillParam(portalSlideshowRequest, portalSlideshow);
        this.save(portalSlideshow);
    }

    @Override
    public void del(PortalSlideshowRequest portalSlideshowRequest) {
        PortalSlideshow portalSlideshow = this.queryPortalSlideshow(portalSlideshowRequest);
        this.removeById(portalSlideshow.getSlideshowId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(PortalSlideshowRequest portalSlideshowRequest) {
        this.removeByIds(portalSlideshowRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(PortalSlideshowRequest portalSlideshowRequest) {

        this.validateSlideshow(portalSlideshowRequest);

        PortalSlideshow oldSlideshow = this.queryPortalSlideshow(portalSlideshowRequest);
        SlideshowFactory.commonFillParam(portalSlideshowRequest, oldSlideshow);
        this.updateById(oldSlideshow);
    }

    @Override
    public PortalSlideshow detail(PortalSlideshowRequest portalSlideshowRequest) {
        return this.queryPortalSlideshow(portalSlideshowRequest);
    }

    @Override
    public PageResult<PortalSlideshow> findPage(PortalSlideshowRequest portalSlideshowRequest) {
        LambdaQueryWrapper<PortalSlideshow> wrapper = createWrapper(portalSlideshowRequest);

        wrapper.select(PortalSlideshow::getSlideshowId, PortalSlideshow::getSlideshowTitle,
                PortalSlideshow::getSlideshowCoverFileId, PortalSlideshow::getSlideshowRedirectType,
                PortalSlideshow::getSlideshowNewsId, PortalSlideshow::getSlideshowRedirectUrl,
                PortalSlideshow::getFldSort, BaseEntity::getCreateTime);

        Page<PortalSlideshow> pageList = this.page(PageFactory.defaultPage(), wrapper);
        List<PortalSlideshow> records = pageList.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return PageResultFactory.createPageResult(pageList);
        }

        Map<Long, String> newsNameMap = portalNewsService.getNewsNameMap(records.stream().map(PortalSlideshow::getSlideshowNewsId).toList());
        for (PortalSlideshow record : records) {
            Long newsId = record.getSlideshowNewsId();
            String newsName = newsNameMap.get(newsId);
            record.setSlideshowNewsTitle(newsName);
        }

        return PageResultFactory.createPageResult(pageList);
    }

    private PortalSlideshow queryPortalSlideshow(PortalSlideshowRequest portalSlideshowRequest) {
        PortalSlideshow portalSlideshow = this.getById(portalSlideshowRequest.getSlideshowId());
        if (ObjectUtil.isEmpty(portalSlideshow)) {
            throw new ServiceException(PortalSlideshowExceptionEnum.PORTAL_SLIDESHOW_NOT_EXISTED);
        }
        return portalSlideshow;
    }

    private LambdaQueryWrapper<PortalSlideshow> createWrapper(PortalSlideshowRequest portalSlideshowRequest) {
        LambdaQueryWrapper<PortalSlideshow> queryWrapper = new LambdaQueryWrapper<>();

        Long designId = portalSlideshowRequest.getDesignId();
        queryWrapper.eq(PortalSlideshow::getDesignId, designId);

        String slideshowTitle = portalSlideshowRequest.getSlideshowTitle();
        queryWrapper.like(ObjectUtil.isNotEmpty(slideshowTitle), PortalSlideshow::getSlideshowTitle, slideshowTitle);

        queryWrapper.orderByAsc(PortalSlideshow::getFldSort);
        queryWrapper.orderByDesc(BaseEntity::getCreateTime);

        return queryWrapper;
    }

    private void validateSlideshow(PortalSlideshowRequest portalSlideshowRequest) {

        Integer slideshowRedirectType = portalSlideshowRequest.getSlideshowRedirectType();
        if (SlideshowRedirectTypeEnum.ITEM_10.getCode().equals(slideshowRedirectType)) {
            if (ObjectUtil.isEmpty(portalSlideshowRequest.getSlideshowNewsId())) {
                throw new ServiceException(PortalSlideshowExceptionEnum.NEWS_CANT_NULL);
            }
        }

        if (SlideshowRedirectTypeEnum.ITEM_20.getCode().equals(slideshowRedirectType)) {
            if (ObjectUtil.isEmpty(portalSlideshowRequest.getSlideshowRedirectUrl())) {
                throw new ServiceException(PortalSlideshowExceptionEnum.URL_CANT_NULL);
            }
        }
    }

}
