package shop.alien.store.controller;

import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.store.ElasticSearchLabel;
import shop.alien.store.config.ElasticSearchComponent;
import shop.alien.mapper.ElasticSearchMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * ElasticSearchController
 *
 * @author ssk
 * @version 1.0
 * @date 2025/4/22 9:19
 */
@Slf4j
@Api(tags = {"ElasticSearch"})
@CrossOrigin
@RestController
@RequestMapping("/elasticSearch")
@RequiredArgsConstructor
public class ElasticSearchController {

    private final ElasticSearchComponent elasticSearch;

    public final ElasticSearchMapper elasticSearchMapper;

// 索引json文件
//    {
//        "mappings":{        //mapping表示：定义mappings属性，替换创建索引时对应的mappings属性
//        "properties":{
//            "id":{       // 设置索引中包含id属性（相当于数据库表中创建一个id字段）
//                "type":"text"    //设置当前属性为关键字，可以被直接搜索
//            },
//            "businessId":{       // 设置索引中包含id属性（相当于数据库表中创建一个id字段）
//                "type":"text"    //设置当前属性为关键字，可以被直接搜索
//            },
//            "businessType":{
//                "type":"text"
//            },
//            "businessMainType":{
//                "type":"text"
//            },
//            "businessMainTypeStr":{
//                "type":"text"
//            },
//            "businessName":{
//                "type":"text",
//                "analyzer":"ik_max_word",
//                "copy_to":"all"
//            },
//            "businessAddress":{
//                "type":"text",
//                "analyzer":"ik_max_word",
//                "copy_to":"all"
//            },
//            "all":{  //all是一个定义属性（虚拟的属性，数据库中不存在的属性），用来描述多个字段的分词结果集合，当前属性可以参与查询
//                "type":"text",
//                "analyzer":"ik_max_word"
//            }
//        }
//    }
//   }

    @ApiOperation("查询店铺, 菜单, 套餐, 位置")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "内容", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping(value = "/search")
    public JSONArray search(@RequestParam("content") String content) {
        String indexName = "alien_databases";
        String fieldName = "all";
        return elasticSearch.searchDoc(indexName, fieldName, content);
    }

    @ApiOperation("新增数据")
    @ApiOperationSupport(order = 2)
    @PostMapping(value = "/insert")
    public boolean insert(@RequestBody List<ElasticSearchLabel> list) {
        return elasticSearch.createIndexDocMore("alien_databases", list);
    }

    @ApiOperation("初始新增数据")
    @ApiOperationSupport(order = 3)
    @GetMapping(value = "/initInsert")
    public boolean initInsert() {
        List<ElasticSearchLabel> list = new ArrayList<>();
        List<ElasticSearchLabel> storeInfoList = elasticSearchMapper.getStoreInfoList();
        List<ElasticSearchLabel> storeMenuList = elasticSearchMapper.getStoreMenuList();
        List<ElasticSearchLabel> storePackageList = elasticSearchMapper.getStorePackageList();
        list.addAll(storeInfoList);
        list.addAll(storeMenuList);
        list.addAll(storePackageList);
        return elasticSearch.createIndexDocMore("alien_databases", list);
    }

}
