package com.ozs.mdm.controller;

import com.ozs.mdm.dto.MdmInfoDataSourceInterfaceProjectDTO;
import com.ozs.mdm.service.InterfaceProjectService;
import com.ozs.mdm.service.util.ReturnUtil;
import com.ozs.mdm.util.R;
import com.ozs.mdm.util.S;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = { "数据中心-接口分类" })
@RestController
public class InterfaceDataSourceProjectController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private InterfaceProjectService interfaceProjectService;

    @ApiOperation(value = "数据源管理数据源信息接口分类查询", notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "接口分类名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, paramType = "query", dataType = "String") ,
            @ApiImplicitParam(name = "sort", value = "排序字段", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "order", value = "排序方式", dataType = "String", paramType = "query", required = false, defaultValue = "DESC"),
    })

    @GetMapping("/mdm/interfaceDataSources/listInterfaceProject")
    public R<List<MdmInfoDataSourceInterfaceProjectDTO>> listInterfaceProject(@RequestParam(name="projectName",required = false) String projectName,
                                                                              @RequestParam(required = false, defaultValue = "1") Integer page,
                                                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                              @RequestParam(name = "sort", required = false) String sort,
                                                                              @RequestParam(name = "order", required = false, defaultValue = "DESC") String order
    )  {
        try {

            return interfaceProjectService.listInterfaceProject(projectName,sort, order, page, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return new R<>(S.ERROR, e.getMessage());
        }
    }

    @ApiOperation( value = "数据源管理数据源信息接口分类新增", notes = "新增")
    @PostMapping(value = "/mdm/interfaceDataSources/addInterfaceProject")
    public R save(@RequestBody  MdmInfoDataSourceInterfaceProjectDTO m) {
        try {
            interfaceProjectService.addInterfaceProject(m);
            return new R();
        } catch (Exception e) {
            log.error("添加数据源接口新增信息异常==>" + e.getMessage());
            return ReturnUtil.createExceptionReturn(e);
        }
    }

    @ApiOperation( value = "数据源管理数据源信息接口分类修改", notes = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "projectId",
                    dataType = "String", paramType = "path",
                    required = true),
    })
    @PutMapping(value = "/mdm/interfaceDataSources/updateInterfaceProjectById/{projectId}", produces = "application/json;charset=UTF-8")
    public R updateInterfaceProjectById(
            @PathVariable(name = "projectId") String projectId,
            @ApiParam @RequestBody MdmInfoDataSourceInterfaceProjectDTO mdmInfoDataSourceInterfaceProjectDTO) {
        R r = new R();
        if (null == projectId && projectId.equals("")) {
            return new R("-1", "id不能为空");
        }
        try {
            mdmInfoDataSourceInterfaceProjectDTO.setProjectId(projectId);
            interfaceProjectService.updateInterfaceProjectById(mdmInfoDataSourceInterfaceProjectDTO);
            return r;
        } catch (Exception e) {
            log.error(e.getMessage());
            r.setCode("-1");
            r.setMsg(e.getMessage());
            return r;
        }

    }
    @ApiOperation( value = "数据源管理数据源信息接口分类删除", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "主键",
                    dataType = "String", paramType = "path",
                    required = true),
    })
    @DeleteMapping(value = "/mdm/interfaceDataSources/deleteInterfaceProject/{projectId}", produces = "application/json;charset=UTF-8")
    public R deleteInterfaceProject(@PathVariable(name = "projectId") String projectId) {
        try {
            R r = new R();
            if (null == projectId && projectId.equals("")) {
                return new R("-1", "id不能为空");
            }
            interfaceProjectService.deleteInterfaceProject(projectId);
            return r;
        } catch (Exception e) {
            log.error("删除接口信息异常,{}", e.getMessage());
            return ReturnUtil.createExceptionReturn(e);
        }
    }

    @ApiOperation( value = "数据源管理数据源信息接口分类单条查询", notes = "详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "接口分类id",
                    dataType = "String", paramType = "path",
                    required = true),
    })
    @GetMapping(value = "/mdm/interfaceDataSources/getInterfaceProjectById/{projectId}", produces = "application/json;charset=UTF-8")
    public R<MdmInfoDataSourceInterfaceProjectDTO> getInterfaceProjectById(@PathVariable(name = "projectId") String projectId) {
        try {
            R<MdmInfoDataSourceInterfaceProjectDTO> r = new R<>();
            if (null == projectId && projectId.equals("")) {
                return new R("-1", "projectId不能为空");
            }
            r.setData(interfaceProjectService.getInterfaceProjectById(projectId));
            return r;
        } catch (Exception e) {
            log.error("获取接口分类信息详情异常,{}", e);
            return ReturnUtil.createExceptionReturn(e);
        }
    }


}
