package org.nl.system.controller.permission;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.nl.common.base.TableDataInfo;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.logging.annotation.Log;
import org.nl.system.service.permission.ISysDataPermissionService;
import org.nl.system.service.permission.dao.SysDataPermission;
import org.nl.system.service.permission.dto.SysDataPermissionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * <p>
 * 数据数据权限表 前端控制器
 * </p>
 *
 * @author generator
 * @since 2022-12-20
 */
@Slf4j
@RestController
@Api(tags = "数据管理")
@RequestMapping("/api/dataPermission")
public class SysDataPermissionController {

    @Autowired
    private ISysDataPermissionService dataPermissionService;

    @GetMapping
    @Log("查询数据权限")
    @ApiOperation("查询数据权限")
    //@SaCheckPermission("@el.check('dataPermission:list')")
    public ResponseEntity<Object> query(SysDataPermissionQuery dataPermissionQuery, PageQuery page) {
        return new ResponseEntity<>(TableDataInfo.build(dataPermissionService.queryAll(dataPermissionQuery, page)), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增数据权限")
    @ApiOperation("新增数据权限")
    //@SaCheckPermission("@el.check('dataPermission:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody SysDataPermission permission) {
        dataPermissionService.create(permission);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改数据权限")
    @ApiOperation("修改数据权限")
    //@SaCheckPermission("@el.check('dataPermission:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody SysDataPermission permission) {
        dataPermissionService.update(permission);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除数据权限")
    @ApiOperation("删除数据权限")
    //@SaCheckPermission("@el.check('dataPermission:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Set<String> ids) {
        dataPermissionService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/scopeType")
    @Log("获取数据权限范围")
    @ApiOperation("获取数据权限范围")
    public ResponseEntity<Object> getDataScopeType(){
        return new ResponseEntity<>(dataPermissionService.getDataScopeType(),HttpStatus.OK);
    }

    @GetMapping("/dataPermissionOption")
    @Log("获取数据权限下拉框")
    @ApiOperation("获取数据权限下拉框")
    public ResponseEntity<Object> getDataPermissionOption(){
        return new ResponseEntity<>(dataPermissionService.getDataPermissionOption(),HttpStatus.OK);
    }

    @PostMapping("/saveDataPermission")
    @Log("保存数据权限")
    @ApiOperation("保存数据权限")
    public ResponseEntity<Object> saveDataPermission(@RequestBody JSONObject datas){
        dataPermissionService.savePermission(datas);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PostMapping("/dataShow")
    @Log("查看数据权限")
    @ApiOperation("查看数据权限")
    public ResponseEntity<Object> getDataShow(@RequestBody String id){
        return new ResponseEntity<>(dataPermissionService.getDataShow(id),HttpStatus.OK);
    }

    @PostMapping("/dataDetail")
    @Log("查看数据明细")
    @ApiOperation("查看数据明细")
    public ResponseEntity<Object> getDataDetail(@RequestBody JSONObject data){
        return new ResponseEntity<>(dataPermissionService.getDataDetail(data),HttpStatus.OK);
    }
}

