package com.ruoyi.interfaces.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.interfaces.core.page.Page;
import com.ruoyi.interfaces.core.page.PageParam;
import com.ruoyi.interfaces.core.page.PageUtils;
import com.ruoyi.interfaces.domain.SysCate;
import com.ruoyi.interfaces.service.SysCateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("服务目录配置")
@RestController
@RequestMapping("/pt/catalog")
public class PtCatalogController extends BaseController {

    private final SysCateService sysCateService;

    public PtCatalogController(SysCateService sysCateService) {
        this.sysCateService = sysCateService;
    }

    @ApiOperation("修改xxx")
    @PostMapping("")
    public AjaxResult insert(@RequestBody SysCate sysCate) {
        int ret = 0;
        if (StringUtils.isBlank(sysCate.getCateTable())) {
            sysCate.setCateTable("PT_SERVICE");
        }

        if (StringUtils.isBlank(sysCate.getCateCode())) {
            ret = sysCateService.insert(sysCate);
        } else {
            ret = sysCateService.update(sysCate);
        }
        return AjaxResult.success(sysCate);
    }

    @ApiOperation("根据ID删除xxx")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable String id) {
        int ret = sysCateService.delete(id);
        return AjaxResult.success();
    }

    @ApiOperation("根据ID获取xxx（单表）")
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable String id) {
        SysCate sysCate = sysCateService.get(id);
        return AjaxResult.success(sysCate);
    }

    @ApiOperation("数据列表")
    @RequestMapping(value = "/cate_list", method = RequestMethod.GET)
    public List<SysCate> list(String tabName) {
        SysCate criteria = new SysCate();
        criteria.setCateTable(tabName);
        return sysCateService.findAllO(criteria);
    }

    @ApiOperation("树数据列表")
    @PostMapping("/pageList")
    public Page pageList(PageParam<SysCate> sysCatePageParam) {
        return PageUtils.convert(sysCateService.pageList(sysCatePageParam));
    }

    @ApiOperation("树数据列表")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody(required = false) SysCate sysCate) {
        return AjaxResult.success(sysCateService.list(sysCate));
    }

    @RequestMapping(value = "/countNum", method = RequestMethod.GET)
    public AjaxResult countNum() {
        return AjaxResult.success(sysCateService.countNum());
    }

}
