package com.ruoyi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.controller.req.*;
import com.ruoyi.enums.order.OrderStatusEnums;
import com.ruoyi.frequency.hosearch.entity.HotSearch;
import com.ruoyi.frequency.hosearch.service.HotSearchService;
import com.ruoyi.frequency.order.service.OrderService;
import com.ruoyi.frequency.works.service.WorksService;
import com.ruoyi.frequency.workstype.service.WorksTypeService;
import com.ruoyi.post.controller.req.PostPageReq;
import com.ruoyi.post.frequency.post.service.PostService;
import com.ruoyi.post.frequency.posttype.entity.PostType;
import com.ruoyi.response.ResponseData;
import com.ruoyi.utils.SnowIdUtils;
import com.ruoyi.vo.RyController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索
 *
 * @author liwenlong
 * @date 2023-10-12
 */
@CrossOrigin
@RestController
@RequestMapping("/api/search")
@Api(tags = "搜索")
public class ApiSearchController extends RyController
{

    @Autowired
    private WorksService worksService;

    @Autowired
    private WorksTypeService worksTypeService;

    @Autowired
    private PostService postService;

    @Autowired
    private OrderService orderService;


    @PostMapping("/searchPage")
    @ApiOperation(value = "搜索", notes = "搜索")
    public ResponseData searchPage(@RequestBody SearchPageReq req) {

        ResponseData responseData = new ResponseData();

        //类型 1作品库 2绘图员 3论坛 4订单
        switch (req.getType()){
            case 1:

                WorksPageReq worksPageReq = new WorksPageReq();
                worksPageReq.setKeyword(req.getKeyword());
                worksPageReq.setPageNo(req.getPageNo());
                worksPageReq.setPageSize(req.getPageSize());
                worksPageReq.setShowEnterprise(2);
                responseData = worksService.worksPage(worksPageReq,getUserVo());
                break;
            case 2:
                StorePageReq storePageReq  = new StorePageReq();
                storePageReq.setKeyword(req.getKeyword());
                storePageReq.setPageNo(req.getPageNo());
                storePageReq.setPageSize(req.getPageSize());
                responseData = worksTypeService.storePage(storePageReq, getUserVo());
                break;
            case 3:
                PostPageReq postPageReq  = new PostPageReq();
                postPageReq.setKeyword(req.getKeyword());
                postPageReq.setPageNo(req.getPageNo());
                postPageReq.setPageSize(req.getPageSize());
                responseData =  postService.postPage(postPageReq,getUserVo());
                break;
            case 4:
                OrderPageReq orderPageReq  = new OrderPageReq();
                orderPageReq.setKeyword(req.getKeyword());

                orderPageReq.setStatus(OrderStatusEnums.to_be_confirm.getCode());

                orderPageReq.setIsHall(true);

                orderPageReq.setPageNo(req.getPageNo());
                orderPageReq.setPageSize(req.getPageSize());

                responseData =  orderService.orderPage(orderPageReq,getUserVo());
                break;
            default:
                return ResponseData.success();
        }

        //记录搜索
        if (ObjectUtil.isNotEmpty(req.getKeyword())){
            HotSearch hotSearch = new HotSearch()
                    .setId(SnowIdUtils.uniqueLong())
                    .setSearchTerm(req.getKeyword());
            hotSearch.setCreateData();
            hotSearch.setUpdateData();

            //判断是否搜索出结果

            Boolean isSuccess = false;
            if(responseData.getData() != null){
                PageResult pageResult = (PageResult)responseData.getData();
                if (pageResult != null && pageResult.getRows().size() > 0){
                    isSuccess = true;
                }
            }

            hotSearchService.addHotSearch(hotSearch,isSuccess);
        }

        return responseData;

    }

    @Autowired
    private HotSearchService hotSearchService;

    @PostMapping("/hotSearchList")
    @ApiOperation(value = "热门搜索记录", notes = "热门搜索记录")
    public ResponseData hotSearchList(@RequestBody HotSearchListReq req) {
        List<HotSearch> list = hotSearchService.hotSearchList(req);
        return ResponseData.success(list);
    }
}
