package com.cyksj.web.controller.search;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.SearchHistoryMapper;
import com.cyksj.model.entity.SearchHistory;
import com.cyksj.model.views.GoodsFrontListView;
import com.cyksj.model.views.InformationListView;
import com.cyksj.model.views.SearchView;
import com.cyksj.service.information.InformationService;
import com.cyksj.web.util.StpUserUtil;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.operator.Contain;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zwhui
 * @date 2023/10/12 17:13
 */
@Slf4j
@RestController
@RequestMapping("/applets/search")
@RequiredArgsConstructor
public class SearchController {

    private final BeanSearcher beanSearcher;

    private final InformationService informationService;

    private final SearchHistoryMapper searchHistoryMapper;

    private final HttpServletRequest request;

    /**
     * 查询搜索结果
     */
    @GetMapping("/get")
    public Result<SearchView> search(String name){
        Long userId = StpUserUtil.getUserIdAfterLogin();
        if (userId != null){
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.setName(name);
            searchHistory.setUserId(userId);
            searchHistoryMapper.insert(searchHistory);
        }
        List<GoodsFrontListView> goodsList = beanSearcher.searchAll(GoodsFrontListView.class, MapUtils.flatBuilder(request.getParameterMap()).field(GoodsFrontListView::getTitle,name).op(Operator.Contain).build());
        List<InformationListView> informationList = informationService.getInformationByTitle(name);
        SearchView view = new SearchView();
        view.setGoodsList(goodsList);
        view.setInformationList(informationList);
        return GatewayResponse.SUCCESS.newBuilder().toResult(view);
    }


    /**
     * 查询搜索历史
     */
    @GetMapping("/get/searchHistory")
    public Result<List<SearchHistory>> getSearchHistory(){
        Long userId = StpUserUtil.getUserIdAfterLogin();
        if (userId != null){
            return GatewayResponse.SUCCESS.newBuilder().toResult(searchHistoryMapper.selectList(Wrappers.lambdaQuery(SearchHistory.class).eq(SearchHistory::getUserId,userId).orderByDesc(SearchHistory::getCreatedTime).last("limit 10")));
        }
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }

    /**
     * 删除搜索历史
     */
    @DeleteMapping("/delete/history")
    private Result<Void> delHistory(Long id){
        long userId = StpUserUtil.getLoginIdAsLong();
        searchHistoryMapper.delete(Wrappers.lambdaQuery(SearchHistory.class).eq(id != null,SearchHistory::getId,id).eq(SearchHistory::getUserId,userId));
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }
}
