package com.vctgo.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.utils.poi.ExcelUtil;
import com.vctgo.common.core.web.controller.BaseController;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vctgo.common.log.annotation.Log;
import com.vctgo.common.log.enums.BusinessType;
import com.vctgo.common.security.annotation.Logical;
import com.vctgo.common.security.annotation.RequiresPermissions;
import com.vctgo.system.api.domain.SysService;
import com.vctgo.system.service.ISysServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 服务Controller
 *
 * @author caesar
 * @date 2023-10-08
 */
@Api(tags = "权限架构--开通业务服务", value = "权限架构--开通业务服务")
@RestController
@RequestMapping("/business/service")
public class SysServiceController extends BaseController {

    @Autowired
    private ISysServiceService sysServiceService;

    /**
     * 查询服务分页列表
     */
    @ApiOperation(value = "查询服务分页列表")
    @RequiresPermissions("system:service:page")
    @GetMapping("/page")
    public AjaxResult page(SysService sysService) {
        IPage<SysService> pageList = sysServiceService.selectSysServicePage(sysService);
        return AjaxResult.success(pageList);
    }

    /**
     * 导出服务列表
     */
    @ApiOperation(value = "导出服务列表")
    @RequiresPermissions("system:service:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysService sysService) {
        List<SysService> list = sysServiceService.selectSysServiceList(sysService);
        ExcelUtil<SysService> util = new ExcelUtil<SysService>(SysService.class);
        util.exportExcel(response, list, "服务数据");
    }

    /**
     * 获取服务详细信息
     */
    @ApiOperation(value = "获取服务详细信息")
    @RequiresPermissions(value = {"system:service:query","system:service:edit"}, logical = Logical.OR)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success((sysServiceService.selectSysServiceById(id)));
    }

    /**
     * 新增服务
     */
    @ApiOperation(value = "新增服务")
    @RequiresPermissions("system:service:add")
    @PostMapping
    public AjaxResult add(@RequestBody SysService sysService) {
        return toAjax(sysServiceService.insertSysService(sysService));
    }

    /**
     * 修改服务
     */
    @ApiOperation(value = "修改服务")
    @RequiresPermissions("system:service:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SysService sysService) {
        return toAjax(sysServiceService.updateSysService(sysService));
    }

    /**
     * 逻辑删除服务
     */
    @ApiOperation(value = "逻辑删除服务")
    @RequiresPermissions("system:service:abandon")
    @PutMapping("/abandon/{ids}")
    public AjaxResult abandon(@PathVariable Long[] ids) {
        return toAjax(sysServiceService.logicalDeleteSysServiceByIds(ids));
    }

    /**
     * 物理删除服务
     */
    @ApiOperation(value = "物理删除服务")
    @RequiresPermissions("system:service:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysServiceService.deleteSysServiceByIds(ids));
    }
}
