package com.ruoyi.web.controller.dragForm;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.tenantdatassource.TenantDataSource;
import com.ruoyi.system.entity.CommonEntity;
import com.ruoyi.system.entity.TableSql;
import com.ruoyi.system.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

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

    @Resource
    private ICommonService commonService;

    @Resource  // 动态sql
    private ITableSqlService iTableSqlService;

    @Resource // 动态表格
    private IDragTableService dragTableService;

    @Resource
    private IDragTableGroupService dragTableGroupService;

    @Resource
    private IDragFormService dragFormService;

    /**
     * 共通查询
     */
    @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("/batchEdit")
    public AjaxResult edit(@RequestBody CommonEntity commonEntity) {
        return toAjax(commonService.edit(commonEntity));
    }

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

    /**
     * 共通获取详情
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(CommonEntity commonEntity) {
        return success(commonService.getInfoById(commonEntity));
    }

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

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

    /**
     * 动态表单预览接口
     */
    @GetMapping("/dragTablePreview")
    public TableDataInfo DragTablePreview(CommonEntity commonEntity) {
        startPage();
        return getDataTable(commonService.dragTablePreview(commonEntity));
    }

    /**
     * 通用批量查询下拉框数据接口
     */
    @PostMapping("/queryDropDownBoxData")
    public AjaxResult queryDropDownBoxData(@RequestBody List<CommonEntity> commonEntityList) {
        return AjaxResult.success(commonService.queryDropDownBoxData(commonEntityList));
    }


}
