package com.czg.service.market.service.impl;

import cn.hutool.core.util.StrUtil;
import com.czg.market.dto.MkCarouselDTO;
import com.czg.system.entity.MiniAppPages;
import com.czg.system.service.MiniAppPageService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkCarousel;
import com.czg.market.service.MkCarouselService;
import com.czg.service.market.mapper.MkCarouselMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 轮播图配置表 服务层实现。
 *
 * @author ww
 * @since 2026-01-27
 */
@Service
public class MkCarouselServiceImpl extends ServiceImpl<MkCarouselMapper, MkCarousel> implements MkCarouselService {
    @DubboReference
    private MiniAppPageService miniAppPageService;

    @Override
    public List<MkCarousel> getCarousels(MkCarouselDTO mkCarouselDTO) {
        QueryWrapper queryWrapper = query().eq(MkCarousel::getShopId, mkCarouselDTO.getShopId())
                .eq(MkCarousel::getIsEnabled, mkCarouselDTO.getIsEnabled())
                .eq(MkCarousel::getIsShareable, mkCarouselDTO.getIsShareable())
                .orderBy(MkCarousel::getSort, false);
        if (StrUtil.isNotBlank(mkCarouselDTO.getName())) {
            queryWrapper.like(MkCarousel::getName, mkCarouselDTO.getName());
        }
        List<MkCarousel> list = list(queryWrapper);
        list.forEach(mkCarousel -> {
            if (mkCarousel.getJumpPageId() != null) {
                MiniAppPages miniAppPages = miniAppPageService.getById(mkCarousel.getJumpPageId());
                if (miniAppPages != null) {
                    mkCarousel.setJumpPagePath(miniAppPages.getPath());
                }
            }
        });
        return list;
    }
}
