package com.zd.system.controller;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.system.domain.SysPermit;
import com.zd.system.domain.SysPermitMenu;
import com.zd.system.service.ISysPermitService;
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.validation.Valid;
import java.util.List;

/**
 * 权限模板Controller
 *
 * @author liubo
 * @date 2022-03-21
 */
@RestController
@Api(tags = "【权限模板】")
@RequestMapping("/permit")
public class SysPermitController extends BaseController {
    @Autowired
    private ISysPermitService sysPermitService;

    /**
     * 查询权限模板列表
     */
    @PreAuthorize(hasPermi = PerPrefix.SYSTEM_PERMIT + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询分页列表", notes = "权限字符： " + PerPrefix.SYSTEM_PERMIT + PerFun.LIST)
    public TableDataInfo<SysPermit> list(SysPermit sysPermit) {
        startPage();
        List<SysPermit> list = sysPermitService.selectSysPermitList(sysPermit);
        return getDataTable(list);
    }

    /**
     * 查询权限模板下拉选项
     */
    @GetMapping("/option")
    @ApiOperation(value = "查询下拉选项")
    public List<SysPermit> queryOption(SysPermit sysPermit) {
        List<SysPermit> list = sysPermitService.selectSysPermitList(sysPermit);
        return list;
    }

    /**
     * 获取权限模板详细信息
     */
//    @ApiOperation(value = "获取模板详细信息", notes = "权限字符： " + PerPrefix.SYSTEM_PERMIT + PerFun.QUERY)
//    @PreAuthorize(hasPermi = PerPrefix.SYSTEM_PERMIT + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public SysPermit getInfo(@PathVariable("id") Long id) {
        return sysPermitService.selectSysPermitById(id);
    }

    /**
     * 获取用户权限详细信息
     */
    @ApiOperation(value = "获取用户权限详细信息")
    @GetMapping(value = "/userPermit/{userId}")
    public List<SysPermitMenu> getUserPermitInfo(@PathVariable("userId") Long userId) {
        return sysPermitService.selectSysPermitByUserId(userId);
    }

    /**
     * 新增权限模板
     */
    @ApiOperation(value = "新增权限模板", notes = "权限字符： " + PerPrefix.SYSTEM_PERMIT + PerFun.ADD)
    @PreAuthorize(hasPermi = PerPrefix.SYSTEM_PERMIT + PerFun.ADD)
    @Log(title = "权限模板", businessType = BusinessType.INSERT)
    @PostMapping
    public SysPermit add(@RequestBody @Valid SysPermit sysPermit) {
        return sysPermitService.insertSysPermit(sysPermit);
    }

    /**
     * 修改权限模板
     */
    @ApiOperation(value = "修改权限模板", notes = "权限字符： " + PerPrefix.SYSTEM_PERMIT + PerFun.EDIT)
    @PreAuthorize(hasPermi = PerPrefix.SYSTEM_PERMIT + PerFun.EDIT)
    @Log(title = "权限模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public SysPermit edit(@RequestBody SysPermit sysPermit) {
        return sysPermitService.updateSysPermit(sysPermit);
    }

    /**
     * 删除权限模板
     */
    @ApiOperation(value = "删除权限模板", notes = "权限字符： " + PerPrefix.SYSTEM_PERMIT + PerFun.REMOVE)
    @PreAuthorize(hasPermi = PerPrefix.SYSTEM_PERMIT + PerFun.REMOVE)
    @Log(title = "权限模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids) {
        return ResultData.result(sysPermitService.deleteSysPermitByIds(ids));
    }
}
