package com.boman.web.core.controller;

import com.boman.common.core.web.domain.AjaxResult;
import com.boman.domain.GenTable;
//import com.boman.gen.controller.MyController;
import com.boman.web.core.domain.FormDataDto;
import com.boman.web.core.service.TableServiceCmdService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author shiqian
 * @description
 * @date 2021年03月22日 09:19
 **/
@RestController
@RequestMapping("/p/cs")
public class ObjController {

    @Autowired
    private TableServiceCmdService tableServiceCmdService;

    /**
     * 功能描述: 通用保存接口
     *                {
     *                    "objId": 1,
     *                    "table": "sys_config",
     *                    "fixedData": {
     *                        "CONFIG_NAME": "测试config_name",
     *                        "CONFIG_KEY": "测试config_key",
     *                        "CONFIG_VALUE": "测试config_value",
     *                        "CONFIG_TYPE": "Y",
     *                        "REMARK": "测试remark"
     *                    }
     *                }
     *
     * @param formDataDto 前台传过来的dto
     * @return com.boman.common.core.web.domain.AjaxResult
     */
    @ApiOperation(value = "单对象保存")
    @PostMapping("/objectSave")
    public AjaxResult objectSave(@RequestBody FormDataDto formDataDto) {
        return tableServiceCmdService.objectSave(formDataDto);
    }

    /**
     * 功能描述: 通用删除接口 (真的删除)
     *               eg:  {
     *                         "table": "sys_config",
     *                         "idList": [
     *                             141,
     *                             142
     *                         ]
     *                     }
     *
     *
     *
     * @param formDataDto 前台传过来的dto
     * @return com.boman.common.core.web.domain.AjaxResult
     */
    @ApiOperation(value = "单对象删除")
    @PostMapping("/objectDelete")
    public AjaxResult objectDelete(@RequestBody FormDataDto formDataDto) {
        return tableServiceCmdService.objectDelete(formDataDto);
    }

    /**
     * 功能描述: 通用删除接口 (逻辑删除)
     *
     *                    eg:{
     *                           "table": "sys_config",
     *                           "logicDelName": "CONFIG_KEY",
     *                           "logicDelValue": "我被修改了吧",
     *                           "idList": [
     *                               141,
     *                               142
     *                           ]
     *                       }
     *
     * @param formDataDto 前台传过来的dto
     * @return com.boman.common.core.web.domain.AjaxResult
     */
    @ApiOperation(value = "单对象逻辑删除")
    @PostMapping("/objectLogicDelete")
    public AjaxResult objectLogicDelete(@RequestBody FormDataDto formDataDto) {
        return tableServiceCmdService.objectLogicDelete(formDataDto);
    }

    /**
     * 功能描述: 获取单表单数据 默认查所有字段
     *                    eg:{
     *                          "table": "sys_config",
     *                          "fixedData": {
     *                              "id": 20
     *                            }
     *                        }
     * @param condition condition
     * @return com.boman.common.core.web.domain.AjaxResult
     */
    @ApiOperation(value = "获取单表单数据")
    @PostMapping("/getObject")
    public AjaxResult getObject(@RequestBody FormDataDto condition) {
        return tableServiceCmdService.getObject(condition);
    }

    /**
     * 功能描述: 获取表单列表数据
     *                    eg:{
     *                          "table": "sys_config",
     *                          "limit": 0,
     *                          "offset": 10,
     *                          "orderBy": "create_time asc",
     *                          "fixedData": {
     *                              "condition": {
     *                                  "CONFIG_NAME": "主框架",
     *                                  "CONFIG_VALUE": "skin-blue",
     *                                  "CREATE_TIME": ["2017-01-01", "2019-02-02"]
     *                              },
     *                              "showData": [
     *                                  "CONFIG_ID", "CONFIG_NAME", "CONFIG_VALUE"
     *                              ]
     *                            }
     *                        }
     * @param condition condition
     * @return com.boman.common.core.web.domain.AjaxResult
     */
    @ApiOperation(value = "获取表单列表数据")
    @PostMapping("/queryList")
    public AjaxResult queryList(@RequestBody FormDataDto condition) {
        return tableServiceCmdService.queryList(condition);
    }

    /**
     * 功能描述: 获取表单查询字段、按钮、表头
     * 注意: 都是从redis中拿的，如果数据库和redis不一致，则需刷新一下redis
     * 刷新的入口为 {@link MyController#loadTable(GenTable)}
     *
     *                    eg:{
     *                          "table": "sys_config"
     *                        }
     *
     * @param condition condition
     * @return com.boman.common.core.web.domain.AjaxResult
     */
    @ApiOperation(value = "获取表单查询字段和按钮")
    @PostMapping("/getTableQuery")
    public AjaxResult getTableQuery(@RequestBody FormDataDto condition) {
        return tableServiceCmdService.getTableQuery(condition);
    }

    /**
     * 功能描述: 获取表单查询字段
     * 注意: 都是从redis中拿的，如果数据库和redis不一致，则需刷新一下redis
     * 刷新的入口为 {@link MyController#loadTable(GenTable)}
     *
     *                    eg:{
     *                          "table": "sys_config"
     *                        }
     *
     * @param condition condition
     * @return com.boman.common.core.web.domain.AjaxResult
     */
    @ApiOperation(value = "获取表单查询字段")
    @PostMapping("/getQueryParam")
    public AjaxResult getQueryParam(@RequestBody FormDataDto condition) {
        return tableServiceCmdService.getQueryParam(condition);
    }

//
//    /**
//     * 功能描述: 获取表单表头
//     * 注意: 都是从redis中拿的，如果数据库和redis不一致，则需刷新一下redis
//     * 刷新的入口为 {@link MyController#loadTable(com.boman.domain.GenTable)}
//     *
//     *                    eg:{
//     *                          "table": "sys_config"
//     *                        }
//     *
//     * @param condition condition
//     * @return com.boman.common.core.web.domain.AjaxResult
//     */
//    @ApiOperation(value = "获取表单表头")
//    @PostMapping("/getTableHead")
//    public AjaxResult getTableHead(@RequestBody FormDataDto condition) {
//        return tableServiceCmdService.getTableHead(condition);
//    }

    /**
     * 功能描述: 获取表单按钮
     * 注意: 都是从redis中拿的，如果数据库和redis不一致，则需刷新一下redis
     * 刷新的入口为 {@link MyController#loadTable(GenTable)}
     *
     *                    eg:{
     *                          "table": "sys_config"
     *                        }
     *
     * @param condition condition
     * @return com.boman.common.core.web.domain.AjaxResult
     */
    @ApiOperation(value = "获取表单按钮")
    @PostMapping("/getButton")
    public AjaxResult getButton(@RequestBody FormDataDto condition) {
        return AjaxResult.success( tableServiceCmdService.getButton(condition.getTable()));
    }

    /**
     * 功能描述: 获取表单子表
     *
     * @param condition condition
     * @return com.boman.common.core.web.domain.AjaxResult
     */
    @ApiOperation(value = "获取表单子表")
    @PostMapping("/objectTab")
    public AjaxResult objectTab(@RequestBody FormDataDto condition) {
        return tableServiceCmdService.objectTab(condition);
    }

    /**
     * 功能描述: 表单提交接口, 更改的字段类型和字段值都是一致的
     *              {
     *                  "table": "sys_test",
     *                  "commitData": [
     *                      {
     *                          "id": 1,
     *                          "status": "2",
     *                          "remark": "提交了吧"
     *                      }
     *                  ]
     *              }
     *
     * @param condition condition
     * @return com.boman.common.core.web.domain.AjaxResult
     */
    @ApiOperation(value = "表单提交接口")
    @PostMapping("/objectSubmit")
    public AjaxResult objectSubmit(@RequestBody FormDataDto condition) {
        return tableServiceCmdService.objectSubmit(condition, true);
    }


    /**
     * 功能描述: 反提交接口, 更改的字段类型和字段值都是一致的
     *                {
     *                    "table": "sys_config",
     *                    "fixedData": {
     *                        "idList": [
     *                            3
     *                        ],
     *                        "status": "1",
     *                        "remark": "理由不够充分"
     *                    }
     *                }
     *
     *
     * @param condition condition
     * @return com.boman.common.core.web.domain.AjaxResult
     */
    @ApiOperation(value = "反提交接口")
    @PostMapping("/objectUnSubmit")
    public AjaxResult objectUnSubmit(@RequestBody FormDataDto condition) {
        return tableServiceCmdService.objectSubmit(condition, false);
    }


}
