package com.ebts.generator.controller;

import com.ebts.common.core.controller.BaseController;
import com.ebts.common.core.entity.Result;
import com.ebts.common.utils.MapExcelUtil;
import com.ebts.common.utils.StringUtils;
import com.ebts.generator.entity.UniColumnInfo;
import com.ebts.generator.utils.sql.GenSqlUtil;
import com.github.pagehelper.PageHelper;
import com.ebts.generator.entity.UniCon;
import com.ebts.generator.entity.UniQuery;
import com.ebts.generator.service.QueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Author 18209
 * Date 2021/1/30 18:25
 * Version 1.0
 */
@RestController
@RequestMapping("/query")
public class QueryController extends BaseController {

    @Autowired
    private QueryService queryService;

    /**
     * todo 获取编辑sql的基本信息
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('query:list')")
    @GetMapping("/{id}")
    public Result<Map<String, Object>> Info(@PathVariable("id") Long id) {
        Result<UniQuery> result = queryService.selectQueryById(id);
        if (result.isSuccess()) {
            UniQuery uniQuery = result.getData();
            Map<String, Object> modeMap = new HashMap<>();
            List<UniCon> uniCons = uniQuery.getUniCons();
            List<UniColumnInfo> infoList = uniQuery.getInfoList();
            uniQuery.setInfoList(null);
            uniQuery.setUniCons(null);
            modeMap.put("info", uniQuery);
            modeMap.put("list", uniCons);
            modeMap.put("infoList", infoList);
            return Result.ok(modeMap);
        } else {
            return Result.error(result.getMsg());
        }
    }

    /**
     * 更新万能查询编辑信息 info 和查询信息
     *
     * @param uniQuery
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('query:edit')")
    @PutMapping
    public Result<Integer> edit(@Validated @RequestBody UniQuery uniQuery) {
        if (uniQuery.getInfoList().size() == 0){
            return Result.error("字段信息为空,请提取字段信息!");
        }
        return queryService.updateQueryInfo(uniQuery);
    }

    /**
     * 导出预览数据
     *
     * @param uniQuery
     * @return
     */
    @PreAuthorize("@ebts.hasAnyPermi('query:export')")
    @PutMapping("export")
    public Result<Map<String, Object>> export(@Validated @RequestBody UniQuery uniQuery) {
        Result<List<Map<String, Object>>> result = queryService.previewQuery(uniQuery);
        if (result.isSuccess()) {
            return new MapExcelUtil().exportExcel(result.getData(), uniQuery.getUqName());
        } else {
            return Result.error(result.getMsg());
        }
    }

    /**
     * 返回预览信息
     *
     * @param uniQuery
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('query:preview')")
    @PutMapping("preview")
    public Serializable Preview(@Validated @RequestBody UniQuery uniQuery) {
        startPage(uniQuery);
        Result<List<Map<String, Object>>> result = queryService.previewQuery(uniQuery);
        if (result.isSuccess()) {
            return getDataTable(result.getData());
        } else {
            return result;
        }
    }

    /**
     * 发布上线
     *
     * @param uniQuery
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('query:release')")
    @PutMapping("/release")
    public Result<Integer> Release(@RequestBody UniQuery uniQuery) {
        if (uniQuery.getId() == null) {
            return Result.error("id不能为空!");
        }
        if (uniQuery.getIsRelease() == 1 || uniQuery.getIsRelease() == 2) {
            return queryService.Release(uniQuery);
        } else {
            return Result.error("状态输入错误!");
        }
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage(UniQuery uniQuery) {
        Integer pageNum = uniQuery.getPageNum();
        Integer pageSize = uniQuery.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = GenSqlUtil.escapeOrderBySql("");
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
}
