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.UserRoleDatasService;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.domain.UserRoleDatas;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.filter.*;

@RestController
@RequestMapping(value = "")
public class UserRoleDatasController {
	@Autowired
    UserRoleDatasService userroledatasservice;


	@RequestMapping(method = RequestMethod.POST, value = "/userroledatas/{userroledatas_id}/save")
    @PreAuthorize("@userroledatas_pms.check(#userroledatas_id,'')")
    public ResponseEntity<UserRoleDatas> save(@PathVariable("userroledatas_id") String userroledatas_id, @RequestBody UserRoleDatas userroledatas) {
        UserRoleDatas userroledatas2 = userroledatasservice.save(userroledatas_id, userroledatas);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatas2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroledatas")
    @PreAuthorize("@userroledatas_pms.check('CREATE')")
    public ResponseEntity<UserRoleDatas> create(@RequestBody UserRoleDatas userroledatas) {
        UserRoleDatas userroledatas2 = userroledatasservice.create(userroledatas);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatas2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/userroledatas/{userroledatas_id}")
    @PreAuthorize("@userroledatas_pms.check(#userroledatas_id,'UPDATE')")
    public ResponseEntity<UserRoleDatas> update(@PathVariable("userroledatas_id") String userroledatas_id, @RequestBody UserRoleDatas userroledatas) {
        UserRoleDatas userroledatas2 = userroledatasservice.update(userroledatas_id, userroledatas);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatas2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/userroledatas/{userroledatas_id}")
    @PreAuthorize("@userroledatas_pms.check(#userroledatas_id,'UPDATE')")
    public ResponseEntity<UserRoleDatas> api_update(@PathVariable("userroledatas_id") String userroledatas_id, @RequestBody UserRoleDatas userroledatas) {
        UserRoleDatas userroledatas2 = userroledatasservice.update(userroledatas_id, userroledatas);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatas2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroledatas/checkkey")
    @PreAuthorize("@userroledatas_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody UserRoleDatas userroledatas) {
        boolean b = userroledatasservice.checkKey(userroledatas);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroledatas/{userroledatas_id}")
    @PreAuthorize("@userroledatas_pms.check(#userroledatas_id,'READ')")
    public ResponseEntity<UserRoleDatas> get(@PathVariable("userroledatas_id") String userroledatas_id) {
        UserRoleDatas userroledatas = userroledatasservice.get( userroledatas_id);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatas);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroledatas/getdraft")
    @PreAuthorize("@userroledatas_pms.check('CREATE')")
    public ResponseEntity<UserRoleDatas> getDraft() {
        //UserRoleDatas userroledatas = userroledatasservice.getDraft( userroledatas_id);
        return ResponseEntity.status(HttpStatus.OK).body(new UserRoleDatas());
    }

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

    @RequestMapping(method= RequestMethod.GET , value="/userroledatas/fetchdefault")
    @PreAuthorize("@userroledatas_pms.check('READ')")
	public ResponseEntity<List<UserRoleDatas>> fetchDefault(UserRoleDatasSearchContext searchContext,Pageable pageable) {
        
        Page<UserRoleDatas> page = userroledatasservice.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());
    }


}
