package cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.service.UserRoleDataScopeService;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.domain.UserRoleDataScope;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.filter.*;

@RestController
@RequestMapping(value = "")
public class UserRoleDataScopeController {
	@Autowired
    UserRoleDataScopeService userroledatascopeservice;


	@RequestMapping(method = RequestMethod.POST, value = "/userroledatascopes")
    @PreAuthorize("@userroledatascope_pms.check('CREATE')")
    public ResponseEntity<UserRoleDataScope> create(@RequestBody UserRoleDataScope userroledatascope) {
        UserRoleDataScope userroledatascope2 = userroledatascopeservice.create(userroledatascope);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatascope2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroledatascopes/{userroledatascope_id}/save")
    @PreAuthorize("@userroledatascope_pms.check(#userroledatascope_id,'')")
    public ResponseEntity<UserRoleDataScope> save(@PathVariable("userroledatascope_id") String userroledatascope_id, @RequestBody UserRoleDataScope userroledatascope) {
        UserRoleDataScope userroledatascope2 = userroledatascopeservice.save(userroledatascope_id, userroledatascope);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatascope2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroledatascopes/checkkey")
    @PreAuthorize("@userroledatascope_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody UserRoleDataScope userroledatascope) {
        boolean b = userroledatascopeservice.checkKey(userroledatascope);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroledatascopes/getdraft")
    @PreAuthorize("@userroledatascope_pms.check('CREATE')")
    public ResponseEntity<UserRoleDataScope> getDraft() {
        //UserRoleDataScope userroledatascope = userroledatascopeservice.getDraft( userroledatascope_id);
        return ResponseEntity.status(HttpStatus.OK).body(new UserRoleDataScope());
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/userroledatascopes/{userroledatascope_id}")
    @PreAuthorize("@userroledatascope_pms.check(#userroledatascope_id,'UPDATE')")
    public ResponseEntity<UserRoleDataScope> update(@PathVariable("userroledatascope_id") String userroledatascope_id, @RequestBody UserRoleDataScope userroledatascope) {
        UserRoleDataScope userroledatascope2 = userroledatascopeservice.update(userroledatascope_id, userroledatascope);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatascope2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/userroledatascopes/{userroledatascope_id}")
    @PreAuthorize("@userroledatascope_pms.check(#userroledatascope_id,'UPDATE')")
    public ResponseEntity<UserRoleDataScope> api_update(@PathVariable("userroledatascope_id") String userroledatascope_id, @RequestBody UserRoleDataScope userroledatascope) {
        UserRoleDataScope userroledatascope2 = userroledatascopeservice.update(userroledatascope_id, userroledatascope);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatascope2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroledatascopes/{userroledatascope_id}")
    @PreAuthorize("@userroledatascope_pms.check(#userroledatascope_id,'READ')")
    public ResponseEntity<UserRoleDataScope> get(@PathVariable("userroledatascope_id") String userroledatascope_id) {
        UserRoleDataScope userroledatascope = userroledatascopeservice.get( userroledatascope_id);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatascope);
    }

	@RequestMapping(method = RequestMethod.DELETE, value = "/userroledatascopes/{userroledatascope_id}")
    @PreAuthorize("@userroledatascope_pms.check(#userroledatascope_id,'DELETE')")
    public ResponseEntity<Boolean> remove(@PathVariable("userroledatascope_id") String userroledatascope_id) {
        boolean b = userroledatascopeservice.remove( userroledatascope_id);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @RequestMapping(method= RequestMethod.GET , value="/userroledatascopes/fetchdefault")
    @PreAuthorize("@userroledatascope_pms.check('READ')")
	public ResponseEntity<List<UserRoleDataScope>> fetchDefault(UserRoleDataScopeSearchContext searchContext,Pageable pageable) {
        
        Page<UserRoleDataScope> page = userroledatascopeservice.fetchDefault(searchContext);
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(pageable.getPageNumber()))
        		.header("x-per-page", String.valueOf(pageable.getPageSize()))
        		.header("x-total", String.valueOf(page.getTotalElements()))
        		.body(page.getContent());
    }


}
