package org.xyzh.system.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xyzh.api.system.service.ViewService;
import org.xyzh.api.system.vo.PermissionVO;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.sys.TbSysViewDTO;

@RestController
@RequestMapping("/system/view")
public class ViewController {

    private static final Logger logger = LoggerFactory.getLogger(ViewController.class);

    @Autowired
    private ViewService viewService;

    // ================= 视图相关接口 =================
    @PostMapping
    @PreAuthorize("hasAuthority('system:permission:manage')")
    public ResultDomain<TbSysViewDTO> createView(@RequestBody TbSysViewDTO viewDTO) {
        return viewService.insertView(viewDTO);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('system:permission:manage')")
    public ResultDomain<TbSysViewDTO> updateView(@RequestBody TbSysViewDTO viewDTO) {
        return viewService.updateView(viewDTO);
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('system:permission:manage')")
    public ResultDomain<Boolean> deleteView(@RequestBody TbSysViewDTO viewDTO) {
        return viewService.deleteView(viewDTO);
    }

    @PostMapping("/page")
    @PreAuthorize("hasAuthority('system:permission:view')")
    public ResultDomain<PermissionVO> getViewPage(@RequestBody PageRequest<PermissionVO> pageRequest) {
        return viewService.getViewPage(pageRequest);
    }

    @PostMapping("/list")
    @PreAuthorize("hasAuthority('system:permission:view')")
    public ResultDomain<PermissionVO> getViewList(@RequestBody PermissionVO filter) {
        return viewService.getViewList(filter);
    }

    // ================= 视图权限相关接口 ==================
    @PostMapping("/permission/bind")
    @PreAuthorize("hasAuthority('system:permission:manage')")
    public ResultDomain<PermissionVO> bindViewPermission(@RequestBody PermissionVO permissionVO) {
        return viewService.setViewPermissions(permissionVO);
    }

    @PostMapping("/permission/list")
    @PreAuthorize("hasAuthority('system:permission:view')")
    public ResultDomain<PermissionVO> getViewPermissions(@RequestBody PermissionVO permissionVO) {
        return viewService.getViewPermissionList(permissionVO);
    }
}
