package com.infynova.udi.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.infynova.udi.service.UdiService;
import com.infynova.udi.service.helper.EsSearchHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@Slf4j
@Api(tags = "udi-es")
@ApiSupport(order = 500)
@RestController
@RequestMapping("/es")
public class UdiEsController {

    @Resource
    private EsSearchHelper esSearchHelper;

    @ApiOperationSupport(order = 100)
    @ApiOperation(value = "createIndex", notes = "createIndex",hidden = true)
    @PostMapping("createIndex")
    public CreateIndexResponse createIndex(){
        return esSearchHelper.createIndex();
    }

    @ApiOperationSupport(order = 200)
    @ApiOperation(value = "deleteIndex", notes = "deleteIndex",hidden = true)
    @PostMapping("deleteIndex")
    public AcknowledgedResponse deleteIndex(){
        return esSearchHelper.deleteIndex();
    }

    @ApiOperationSupport(order = 300)
    @ApiOperation(value = "initData", notes = "initData",hidden = true)
    @PostMapping("initData")
    public void initData(){
        esSearchHelper.initData();
    }

    @ApiOperationSupport(order = 400)
    @ApiOperation(value = "searchThree", notes = "searchThree")
    @PostMapping("searchThree")
    public SearchResponse searchThree(@RequestParam(value = "companyName", required = false) String companyName,
                                      @RequestParam(value = "productFactoryCode", required = false) String productFactoryCode){
        return esSearchHelper.searchThree(companyName,productFactoryCode);
    }

    @ApiOperationSupport(order = 500)
    @ApiOperation(value = "searchFour", notes = "searchFour")
    @PostMapping("searchFour")
    public SearchResponse searchFour(@RequestParam(value = "brandName", required = false) String brandName,
                              @RequestParam(value = "productName", required = false) String productName,
                              @RequestParam(value = "specification", required = false) String specification,
                              @RequestParam(value = "model", required = false) String model){
        return esSearchHelper.searchFour(brandName,productName,specification,model);
    }
}