package com.vctgo.system.controller;

import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.utils.poi.ExcelUtil;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.domain.SysReServiceTenant;
import com.vctgo.system.service.ISysReServiceTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.vctgo.common.log.annotation.Log;
import com.vctgo.common.log.enums.BusinessType;
import com.vctgo.common.security.annotation.RequiresPermissions;
import com.vctgo.common.core.web.controller.BaseController;
import com.vctgo.common.core.web.domain.AjaxResult;

import java.util.List;

/**
 * 服务与租户关联Controller
 *
 * @author caesar
 * @date 2023-10-08
 */
@RestController
@RequestMapping("/re/service/tenant")
public class SysReServiceTenantController extends BaseController {
    @Autowired
    private ISysReServiceTenantService sysReServiceTenantService;

    /**
     * 查询服务与租户关联分页列表
     */
    @RequiresPermissions("service:tenant:page")
    @GetMapping("/page")
    public AjaxResult page(SysReServiceTenant sysReServiceTenant) {
        IPage<SysReServiceTenant> pageList = sysReServiceTenantService.selectSysReServiceTenantPage(sysReServiceTenant);
        return AjaxResult.success(pageList);
    }

    /**
     * 导出服务与租户关联列表
     */
    @RequiresPermissions("service:tenant:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysReServiceTenant sysReServiceTenant) {
        List<SysReServiceTenant> list = sysReServiceTenantService.selectSysReServiceTenantList(sysReServiceTenant);
        ExcelUtil<SysReServiceTenant> util = new ExcelUtil<SysReServiceTenant>(SysReServiceTenant.class);
        util.exportExcel(response, list, "服务与租户关联数据");
    }

    /**
     * 获取服务与租户关联详细信息
     */
    @RequiresPermissions("service:tenant:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success((sysReServiceTenantService.selectSysReServiceTenantById(id)));
    }

    /**
     * 获取服务与租户关联详细信息
     */
//    @RequiresPermissions("service:tenant:query")
    @GetMapping(value = "/list")
    public AjaxResult list() {
        return success((sysReServiceTenantService.selectSysReServiceTenantListByTenantId(SecurityUtils.getTenantId().toString())));
    }

    /**
     * 新增服务与租户关联
     */
    @RequiresPermissions("service:tenant:add:service")
    @PostMapping
    public AjaxResult add(@RequestBody SysReServiceTenant sysReServiceTenant) {
        return toAjax(sysReServiceTenantService.insertSysReServiceTenant(sysReServiceTenant));
    }

    /**
     * 修改服务与租户关联
     */
    @RequiresPermissions("service:tenant:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SysReServiceTenant sysReServiceTenant) {
        return toAjax(sysReServiceTenantService.updateSysReServiceTenant(sysReServiceTenant));
    }

    /**
     * 删除服务与租户关联
     */
    @RequiresPermissions("service:tenant:abandon")
    @PutMapping("/abandon/{ids}")
    public AjaxResult abandon(@PathVariable Long[] ids) {
        return toAjax(sysReServiceTenantService.logicalDeleteSysReServiceTenantByIds(ids));
    }

    /**
     * 删除服务与租户关联
     */
    @RequiresPermissions("service:tenant:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysReServiceTenantService.deleteSysReServiceTenantByIds(ids));
    }
}
