package com.ylx.companion.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.companion.domain.dto.CompanionAllMerchantsDTO;
import com.ylx.companion.domain.dto.CompanionRecommendDTO;
import com.ylx.companion.domain.vo.CompanionAllMerchantsVo;
import com.ylx.companion.domain.vo.ServiceCategoryVO;
import com.ylx.companion.service.ICompanionRecommendService;
import com.ylx.companion.domain.vo.CompanionRecommendVO;
import com.ylx.companion.domain.vo.HotCompanionProjectVO;
import com.ylx.massage.mapper.MaProjectMapper;
import com.ylx.massage.mapper.MaTechnicianMapper;
import com.ylx.servicecategory.domain.ServiceCategory;
import com.ylx.servicecategory.mapper.ServiceCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类描述：首页陪玩推荐
 *
 * @author Administrator
 * @version 1.0
 * @date 2026/6/10 11:01
 */
@Service
public class CompanionRecommendServiceImpl implements ICompanionRecommendService {
    @Resource
    private  MaTechnicianMapper technicianMapper;

    @Resource
    private MaProjectMapper maProjectMapper;
    @Autowired
    private ServiceCategoryMapper serviceCategoryMapper;
    @Override
    public CompanionRecommendVO getRecommend(CompanionRecommendDTO dto) {
        CompanionRecommendVO vo = new CompanionRecommendVO();

        // 1. 陪玩项目列表（type=2 且 上架状态）
        vo.setServiceCategoryVoList(getCompanionProjects());

        // 2. 热门陪玩商家（销量前五）
        vo.setHotCompanionMerchantVoList(technicianMapper.getHotCompanionMerchantList(dto));

        // 3. 热门陪玩项目（销量前五）
        Page<HotCompanionProjectVO> page = new Page<>(dto.getPageNo(),dto.getPageSize());
        vo.setHotCompanionProjectVoPage(maProjectMapper.getHotCompainonProjectList(page,dto.getCityCode(),dto.getCategoryId()));

        return vo;
    }

    @Override
    public Page<CompanionAllMerchantsVo> getCompanionAllMerchants(Page<CompanionAllMerchantsVo> page, CompanionAllMerchantsDTO dto) {
        return maProjectMapper.getCompanionAllMerchants(page,dto);
    }

    /**
     * 获取所有陪玩服务类目（转为 ServiceCategoryVO）
     */
    private List<ServiceCategoryVO> getCompanionProjects() {
        LambdaQueryWrapper<ServiceCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ServiceCategory::getServiceTag, 2)
                .eq(ServiceCategory::getIsOnline, 1)
                .eq(ServiceCategory::getIsDelete, 0);
        List<ServiceCategory> categories = serviceCategoryMapper.selectList(wrapper);
        if (CollectionUtil.isEmpty(categories)) {
            return new ArrayList<>();
        }
        return categories.stream().map(this::convertToServiceCategoryVO).collect(Collectors.toList());
    }

    /**
     * 将 ServiceCategory 实体转换为 ServiceCategoryVO
     */
    private ServiceCategoryVO convertToServiceCategoryVO(ServiceCategory category) {
        ServiceCategoryVO vo = new ServiceCategoryVO();
        vo.setCategoryId(category.getId());
        vo.setTitle(category.getName());
        // 如果字段名不一致，可手动映射
        return vo;
    }
}

