package com.easyproject.clientbus.service.guide.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyproject.clientbus.domain.dto.SelectGuidePageDto;
import com.easyproject.clientbus.domain.vo.ServiceGuideCategoryVo;
import com.easyproject.clientbus.domain.vo.ServiceGuideItemVo;
import com.easyproject.clientbus.service.guide.IServiceGuideService;
import com.easyproject.common.core.domain.AjaxClientResult;
import com.easyproject.service.domain.guide.ServiceGuideCategory;
import com.easyproject.service.domain.guide.ServiceGuideItem;
import com.easyproject.service.mapper.guide.ServiceGuideCategoryMapper;
import com.easyproject.service.mapper.guide.ServiceGuideItemMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 方法说明: 服务指南
 * @param
 * @return
 * @author java-101
 * @date 2025-03-23 17:12:32
 */
@Slf4j
@Service
public class ServiceGuideServiceImpl implements IServiceGuideService {

    @Resource
    private ServiceGuideCategoryMapper serviceGuideCategoryMapper;
    @Resource
    private ServiceGuideItemMapper serviceGuideItemMapper;

    /**
     * 方法说明: 服务指南分类
     * @param
     * @return
     * @author java-101
     * @date 2025-03-23 17:17:15
     */
    @Override
    public AjaxClientResult guideCategory() {
        List<ServiceGuideCategory> categoryList = serviceGuideCategoryMapper.selectList(Wrappers.<ServiceGuideCategory>lambdaQuery().eq(ServiceGuideCategory::getStatus, 1).orderByAsc(ServiceGuideCategory::getSort));
        List<ServiceGuideCategoryVo> categoryVoList = null;
        if(ObjectUtil.isNotEmpty(categoryList)){
            categoryVoList = BeanUtil.copyToList(categoryList, ServiceGuideCategoryVo.class);
        }
        return AjaxClientResult.success(categoryVoList);
    }

    /**
     * 方法说明: 服务指南栏目
     * @param
     * @return
     * @author java-101
     * @date 2025-03-23 17:17:24
     */
    @Override
    public AjaxClientResult guideItem(SelectGuidePageDto dto) {
        if(ObjectUtil.isEmpty(dto.getCategoryId())){
            return AjaxClientResult.error("指南分类错误");
        }
        ServiceGuideCategory serviceGuideCategory = serviceGuideCategoryMapper.selectById(dto.getCategoryId());
        if(ObjectUtil.isEmpty(serviceGuideCategory)){
            return AjaxClientResult.error("指南分类错误");
        }
        IPage<ServiceGuideItem> page = serviceGuideItemMapper.selectPage(new Page<>(dto.getPageNum(), dto.getPageSize()), Wrappers.<ServiceGuideItem>lambdaQuery().eq(ServiceGuideItem::getCategoryId, dto.getCategoryId()).orderByAsc(ServiceGuideItem::getSort));
        List<ServiceGuideItemVo> itemVoList = null;
        if(ObjectUtil.isNotEmpty(page.getRecords())){
            itemVoList = BeanUtil.copyToList(page.getRecords(), ServiceGuideItemVo.class);
        }
        //自定义分页信息
        IPage<ServiceGuideItemVo> voIPage = new Page<>();
        voIPage.setRecords(itemVoList);
        voIPage.setPages(page.getPages());
        voIPage.setSize(page.getSize());
        voIPage.setTotal(page.getTotal());
        voIPage.setCurrent(page.getCurrent());

        return AjaxClientResult.success(voIPage);
    }
}
