package com.ebts.generator.controller;

import com.ebts.common.annotation.Log;
import com.ebts.common.core.controller.BaseController;
import com.ebts.common.core.entity.Result;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.utils.StringUtils;
import com.ebts.generator.dto.TopResult;
import com.ebts.generator.entity.UniQuery;
import com.ebts.generator.service.TopQueryService;
import com.ebts.generator.service.UniQueryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Map;


/**
 * @author: Clay
 * @date: 2021/7/23 21:36
 * @description：TopQueryController
 * @email: clay@hchyun.com
 */
@RestController
@RequestMapping("/top")
public class TopQueryController extends BaseController {

    @Resource
    private UniQueryService uniQueryService;

    @Resource
    private TopQueryService topQueryService;

    /**
     * 获取基本信息
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('top:query')")
    @GetMapping(value = "/{id}")
    public Result<UniQuery> getInfo(@PathVariable("id") Long id) {
        return uniQueryService.selectUniQueryById(id);
    }

    @GetMapping("/online/{id}")
    public Result<String> online(@PathVariable("id") Long id) {
        // ServerResult ServerResult = queryService.updateQueryInfo(uniQuery);
        // if (ServerResult.isStart()) {
        //     return AjaxResult.success(ServerResult.getData());
        // } else {
        //     return AjaxResult.info(ServerResult.getMsg());
        // }
        return Result.ok();
    }


    /**
     * 获取基本信息
     *
     * @param
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('top:table')")
    @GetMapping("/table")
    public Result<Map<String,Object>> table() {
        return Result.ok(topQueryService.selectTopSearchTables());
    }

    /**
     * 简单修改top万能能查询
     */
    @PreAuthorize("@ebts.hasPermi('top:edit')")
    @Log(title = "top万能查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Integer> edit(@RequestBody UniQuery uniQuery) {
        return uniQueryService.updateUniQuery(uniQuery);
    }

    /**
     * 简单修改top万能能查询
     */
    @PreAuthorize("@ebts.hasPermi('top:preview')")
    @Log(title = "top预览", businessType = BusinessType.OTHER)
    @PostMapping("/preview")
    public Serializable preview(@RequestBody Map<String, String> params) {
        if (StringUtils.isNull(params.get("jsonData"))||StringUtils.isNull(params.get("id"))) {
            return Result.error("缺少必要参数!");
        }
        Result<TopResult> result = topQueryService.preview(params.get("jsonData"), params.get("id"));
        if (result.isSuccess()){
            TopResult data = result.getData();
            return getDataTable(data.getDataMap(),data.getUniCons(),data.getInfoList());
        }else {
            return Result.error(result.getMsg());
        }
    }
}
