package com.liangjian.dataplatform.member.controller;

import com.liangjian.dataplatform.member.entity.po.primary.SqlEntity;
import com.liangjian.dataplatform.member.entity.response.ResultObject;
import com.liangjian.dataplatform.member.service.ISqlEntityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 黄子奇
 * @Date: 2019/10/21
 */
@RestController
@RequestMapping("/sql")
@Api(value = "sql配置控制器", tags = "sql配置控制器", description = "sql配置控制器")
public class SqlEntityController {

    @Autowired
    private ISqlEntityService iSqlEntityService;

    @GetMapping("/sqls")
    @ApiOperation(value="分页获取sql配置")
    public Page<SqlEntity> getSqlEntities(@ApiParam(value = "描述") @RequestParam(required = false) String description,
                                          @ApiParam(value = "页码", required = true) @RequestParam Integer page,
                                          @ApiParam(value = "每页数量", required = true) @RequestParam Integer size) {
        return iSqlEntityService.queryByDescription(description,page-1,size);
    }

    @PostMapping("/sql")
    @ApiOperation(value = "新增sql配置")
    public ResultObject<Object> addSqlEntity(@ApiParam(value = "sql配置对象") @RequestBody SqlEntity entity) {
        if(entity.getId() != null && !entity.getId().isEmpty()) {
            return ResultObject.error("提交的对象不合规");
        }

        if(iSqlEntityService.addSqlEntity(entity)) {
            return ResultObject.success("保存成功", null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @PutMapping("/sql")
    @ApiOperation(value = "修改sql配置")
    public ResultObject<Object> updateSqlEntity(@ApiParam(value = "sql配置对象") @RequestBody SqlEntity entity) {
        if(entity.getId() == null || entity.getId().isEmpty()) {
            return ResultObject.error("提交的对象不合规");
        }

        if(iSqlEntityService.updateSqlEntity(entity)) {
            return ResultObject.success("保存成功", null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @DeleteMapping("/sql")
    @ApiOperation(value = "删除sql配置")
    public ResultObject<Object> deleteSqlEntity(@ApiParam(value = "id") @RequestBody String id) {
        if(iSqlEntityService.deleteSqlEntity(id)) {
            return ResultObject.success("删除成功",null);
        } else {
            return ResultObject.error("删除失败");
        }
    }
}
