package com.ebts.generator.controller;

import com.ebts.common.annotation.Log;
import com.ebts.common.core.controller.BaseController;
import com.ebts.common.core.entity.AjaxResult;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.utils.SecurityUtils;
import com.ebts.common.utils.ServerResult;
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.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;


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

    @Autowired
    private UniQueryService uniQueryService;

    @Autowired
    private TopQueryService topQueryService;

    /**
     * 获取基本信息
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('top:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ServerResult<UniQuery> ServerResult = uniQueryService.selectUniQueryById(id);
        if (ServerResult.isStart()) {
            return AjaxResult.success(ServerResult.getData());
        } else {
            return AjaxResult.info(ServerResult.getMsg());
        }
    }

    /**
     * 获取基本信息
     *
     * @param
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('top:table')")
    @GetMapping("/table")
    public AjaxResult table() {
        return AjaxResult.success(topQueryService.selectTopSearchTables());
    }

    /**
     * 简单修改top万能能查询
     */
    @PreAuthorize("@ebts.hasPermi('top:edit')")
    @Log(title = "top万能查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UniQuery uniQuery) {
        if (uniQuery.getId()<0){
            return AjaxResult.error("缺少关键字段");
        }
        if (StringUtils.isNull(uniQuery.getTopJson())){
            return AjaxResult.error("缺少关键字段");
        }
        ServerResult<Integer> ServerResult = uniQueryService.updateUniQuery(uniQuery);
        if (ServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(ServerResult.getMsg());
        }
    }
    /**
     * 简单修改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"))){
            return AjaxResult.error("缺少必要参数!");
        }
        ServerResult<TopResult> serverResult = topQueryService.preview(params.get("jsonData"),params.get("id"));
        if (serverResult.isStart()){
            return getDataTable(serverResult.getData().getDataMap(),serverResult.getData().getUniCons());
        }else {
            return AjaxResult.error(serverResult.getMsg());
        }
    }
}
