package com.ruoyi.web.modules.home.service.impl;

import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.home.constant.SearchAppConstants;
import com.ruoyi.web.modules.home.dto.SearchQuery;
import com.ruoyi.web.modules.home.mapper.SearchAppMapper;
import com.ruoyi.web.modules.home.service.ISearchAppService;
import com.ruoyi.web.modules.home.vo.HomeHotGoodsVO;
import com.ruoyi.web.modules.home.vo.SearchGoodsPageVO;
import com.ruoyi.web.modules.home.vo.SearchResultAppVO;
import com.ruoyi.web.modules.home.vo.SearchShopAppVO;
import com.ruoyi.web.modules.home.vo.SearchShopPageVO;

/**
 * C 端全站搜索 Service 实现
 */
@Service
public class SearchAppServiceImpl implements ISearchAppService
{
    @Autowired
    private SearchAppMapper searchAppMapper;

    @Override
    public SearchResultAppVO search(SearchQuery query)
    {
        String keyword = resolveKeyword(query);
        String sortBy = resolveSortBy(query);

        SearchResultAppVO result = new SearchResultAppVO();
        result.setGoods(searchGoods(keyword, sortBy, query));
        result.setShops(searchShops(keyword, query));
        return result;
    }

    private String resolveKeyword(SearchQuery query)
    {
        if (query == null || StringUtils.isEmpty(query.getKeyword()))
        {
            throw new ServiceException(SearchAppConstants.MSG_KEYWORD_REQUIRED);
        }
        String keyword = query.getKeyword().trim();
        if (StringUtils.isEmpty(keyword))
        {
            throw new ServiceException(SearchAppConstants.MSG_KEYWORD_REQUIRED);
        }
        return keyword;
    }

    private String resolveSortBy(SearchQuery query)
    {
        String sortBy = StringUtils.isEmpty(query.getSortBy())
                ? SearchAppConstants.DEFAULT_SORT
                : query.getSortBy();
        if (!SearchAppConstants.isValidSortBy(sortBy))
        {
            throw new ServiceException(SearchAppConstants.MSG_SORT_INVALID);
        }
        return sortBy;
    }

    private SearchGoodsPageVO searchGoods(String keyword, String sortBy, SearchQuery query)
    {
        int pageNum = query.getGoodsPageNum() != null ? query.getGoodsPageNum() : SearchAppConstants.DEFAULT_PAGE_NUM;
        int pageSize = query.getGoodsPageSize() != null ? query.getGoodsPageSize() : SearchAppConstants.DEFAULT_PAGE_SIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<HomeHotGoodsVO> list = searchAppMapper.selectGoodsByKeyword(keyword, sortBy);
        if (list == null)
        {
            list = Collections.emptyList();
        }
        PageInfo<HomeHotGoodsVO> pageInfo = new PageInfo<>(list);
        SearchGoodsPageVO goods = new SearchGoodsPageVO();
        goods.setTotal(pageInfo.getTotal());
        goods.setRows(list);
        return goods;
    }

    private SearchShopPageVO searchShops(String keyword, SearchQuery query)
    {
        int pageNum = query.getShopPageNum() != null ? query.getShopPageNum() : SearchAppConstants.DEFAULT_PAGE_NUM;
        int pageSize = query.getShopPageSize() != null ? query.getShopPageSize() : SearchAppConstants.DEFAULT_PAGE_SIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<SearchShopAppVO> list = searchAppMapper.selectShopsByKeyword(keyword);
        if (list == null)
        {
            list = Collections.emptyList();
        }
        PageInfo<SearchShopAppVO> pageInfo = new PageInfo<>(list);
        SearchShopPageVO shops = new SearchShopPageVO();
        shops.setTotal(pageInfo.getTotal());
        shops.setRows(list);
        return shops;
    }
}
