package com.ruoyi.web.controller.dragForm;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.entity.CommonEntity;
import com.ruoyi.system.entity.TableSql;
import com.ruoyi.system.service.ICommonService;
import com.ruoyi.system.service.IDragTableService;
import com.ruoyi.system.service.ITableSqlService;
import dm.jdbc.filter.stat.util.JSONUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Anonymous
@RestController
@RequestMapping("/dragform/common")
public class CommonController extends BaseController {

    @Resource
    private ICommonService commonService;

    @Resource
    private ITableSqlService iTableSqlService;

    @Resource
    private IDragTableService dragTableService;

    /**
     * 共通查询
     */
    @GetMapping("/selectList")
    public TableDataInfo selectList(CommonEntity commonEntity) {
        startPage();
        return getDataTable(commonService.selectList(commonEntity));
    }

    /**
     * 共通批量新增
     */
    @PostMapping("/batchInsert")
    public AjaxResult batchInsert(@RequestBody CommonEntity commonEntity) {
        return toAjax(commonService.batchInsert(commonEntity));
    }

    /**
     * 共通修改sql
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody CommonEntity commonEntity) {
        return toAjax(commonService.edit(commonEntity));
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody CommonEntity commonEntity) {
        return toAjax(commonService.batchDelete(commonEntity));
    }

    /**
     * 联合查询通用sql
     *
     * @param commonEntity
     * @return
     */
    @GetMapping("/getTableList")
    public TableDataInfo queryTableList(CommonEntity commonEntity) {
        System.out.println(commonEntity.getQueryMap());
        TableSql tableSql = iTableSqlService.selectTableSqlByTSqlKey(commonEntity.getQueryMap().get("sqlkey").toString());
        startPage();  // 校验是否sqlserver 否执行 是执行另一种方式的分页
        return getDataTable(commonService.queryTableList(commonEntity, tableSql));
    }

    /**
     * 通用导出excel
     */
//    @GetMapping("/export")
//    public void export(HttpServletResponse response, @RequestParam("tableName") String tableName, @RequestParam("sqlKey") String sqlKey) throws Exception {
//        commonService.export(response, tableName, sqlKey);
//    }
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody CommonEntity commonEntity) throws Exception {
        commonService.export(response, commonEntity);
    }

    /**
     * 通用导出excel模版
     *
     * @param response
     * @param tableName
     * @param sqlKey
     * @throws Exception
     */
    @GetMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response, @RequestParam("tableName") String tableName, @RequestParam("sqlKey") String sqlKey) throws Exception {
        commonService.exportTemplate(response, tableName, sqlKey);
    }

    /**
     * 通用动态表单详情
     *
     * @param sqlKey
     * @return
     */
    @GetMapping("/dragTableInfo")
    public AjaxResult dragTableInfo(CommonEntity commonEntity) {
        return AjaxResult.success(dragTableService.dragTableInfo(commonEntity.getQueryMap().get("sqlkey").toString()));
    }

}
