package com.cim.idm.controller;

import com.cim.idm.exception.BaseException;
import com.cim.idm.response.BaseResponse;
import com.cim.idm.response.RespGenerator;
import com.cim.idm.service.QueryService;
import com.cim.idm.utils.PageResult;
import com.cim.idm.utils.ParamUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 查询接口控制器
 */
@SuppressWarnings("all")
@Slf4j
@RestController
@RequestMapping("/api")
@Api(tags = "查询接口", description = "提供统一查询接口和分页查询接口")
public class QueryController {

    @Autowired
    private QueryService queryService;

    /**
     * 统一查询接口
     * @param param 查询参数
     * @return 查询结果
     */
    @ApiOperation(value = "统一查询接口", notes = "根据 queryId 和 version 查询数据")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public BaseResponse<Map<String, Object>> query(
            @ApiParam(name = "param", value = "查询参数", required = true) @RequestBody Map<String, Object> param) {
        log.info("统一查询接口传输数据 param ==========> " + param);

        List<Map<String, Object>> mapList = new ArrayList<>();

        String queryId =  ParamUtils.getStringParam(param, "queryId");
        String version = ParamUtils.getStringParam(param, "version");
        Map<String, Object> bindMap = (Map<String, Object>) param.get("params");
        try {
            mapList = queryService.getQueryResult(queryId, version, bindMap);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
        return RespGenerator.returnOK(mapList);
    }

    /**
     * 统一查询接口(分页)
     * @param param 查询参数
     * @return 分页查询结果
     */
    @ApiOperation(value = "统一查询接口(分页)", notes = "根据 queryId、version 和分页参数查询数据")
    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    public BaseResponse<PageResult<Map<String, Object>>> queryPage(
            @ApiParam(name = "param", value = "查询参数", required = true) @RequestBody Map<String, Object> param) {
        log.info("统一查询接口(分页)传输数据 param ==========> " + param);

        PageResult<Map<String, Object>> mapList = new PageResult<>();
        String queryId = param.get("queryId").toString();
        String version = param.get("version").toString();
        int pageNum = (int) param.get("pageNum");
        int pageSize = (int) param.get("pageSize");
        Map<String, Object> bindMap = (Map<String, Object>) param.get("params");
        try {
            mapList = queryService.getQueryResultPage(queryId, version, bindMap, pageNum, pageSize);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
        return RespGenerator.returnOK(mapList);
    }

    /**
     * PDA统一查询接口
     * @param param 查询参数
     * @return 查询结果
     */
    @ApiOperation(value = "PDA统一查询接口", notes = "根据 queryId 和 version 查询数据")
    @RequestMapping(value = "/ApiQuery", method = RequestMethod.POST)
    public BaseResponse<Map<String, Object>> pdaQuery(
            @ApiParam(name = "param", value = "查询参数", required = true) @RequestBody Map<String, Object> param) {
        log.info("PDA统一查询接口传输数据 param ==========> " + param);

        List<Map<String, Object>> mapList = new ArrayList<>();
        String queryId = param.get("queryId").toString();
        String version = param.get("version").toString();
        Map<String, Object> bindMap = (Map<String, Object>) param.get("params");
        try {
            mapList = queryService.getQueryResult(queryId, version, bindMap);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
        return RespGenerator.returnOK(mapList);
    }
}
