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

@RestController
@RequestMapping(value = "")
public class UserRoleDataActionController {
	@Autowired
    UserRoleDataActionService userroledataactionservice;


	@RequestMapping(method = RequestMethod.POST, value = "/userroledataactions/{userroledataaction_id}/save")
    @PreAuthorize("@userroledataaction_pms.check(#userroledataaction_id,'')")
    public ResponseEntity<UserRoleDataAction> save(@PathVariable("userroledataaction_id") String userroledataaction_id, @RequestBody UserRoleDataAction userroledataaction) {
        UserRoleDataAction userroledataaction2 = userroledataactionservice.save(userroledataaction_id, userroledataaction);
        return ResponseEntity.status(HttpStatus.OK).body(userroledataaction2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroledataactions")
    @PreAuthorize("@userroledataaction_pms.check('CREATE')")
    public ResponseEntity<UserRoleDataAction> create(@RequestBody UserRoleDataAction userroledataaction) {
        UserRoleDataAction userroledataaction2 = userroledataactionservice.create(userroledataaction);
        return ResponseEntity.status(HttpStatus.OK).body(userroledataaction2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/userroledataactions/{userroledataaction_id}")
    @PreAuthorize("@userroledataaction_pms.check(#userroledataaction_id,'UPDATE')")
    public ResponseEntity<UserRoleDataAction> update(@PathVariable("userroledataaction_id") String userroledataaction_id, @RequestBody UserRoleDataAction userroledataaction) {
        UserRoleDataAction userroledataaction2 = userroledataactionservice.update(userroledataaction_id, userroledataaction);
        return ResponseEntity.status(HttpStatus.OK).body(userroledataaction2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/userroledataactions/{userroledataaction_id}")
    @PreAuthorize("@userroledataaction_pms.check(#userroledataaction_id,'UPDATE')")
    public ResponseEntity<UserRoleDataAction> api_update(@PathVariable("userroledataaction_id") String userroledataaction_id, @RequestBody UserRoleDataAction userroledataaction) {
        UserRoleDataAction userroledataaction2 = userroledataactionservice.update(userroledataaction_id, userroledataaction);
        return ResponseEntity.status(HttpStatus.OK).body(userroledataaction2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroledataactions/getdraft")
    @PreAuthorize("@userroledataaction_pms.check('CREATE')")
    public ResponseEntity<UserRoleDataAction> getDraft() {
        //UserRoleDataAction userroledataaction = userroledataactionservice.getDraft( userroledataaction_id);
        return ResponseEntity.status(HttpStatus.OK).body(new UserRoleDataAction());
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroledataactions/{userroledataaction_id}")
    @PreAuthorize("@userroledataaction_pms.check(#userroledataaction_id,'READ')")
    public ResponseEntity<UserRoleDataAction> get(@PathVariable("userroledataaction_id") String userroledataaction_id) {
        UserRoleDataAction userroledataaction = userroledataactionservice.get( userroledataaction_id);
        return ResponseEntity.status(HttpStatus.OK).body(userroledataaction);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroledataactions/checkkey")
    @PreAuthorize("@userroledataaction_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody UserRoleDataAction userroledataaction) {
        boolean b = userroledataactionservice.checkKey(userroledataaction);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

    @RequestMapping(method= RequestMethod.GET , value="/userroledataactions/fetchdefault")
    @PreAuthorize("@userroledataaction_pms.check('READ')")
	public ResponseEntity<List<UserRoleDataAction>> fetchDefault(UserRoleDataActionSearchContext searchContext,Pageable pageable) {
        
        Page<UserRoleDataAction> page = userroledataactionservice.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());
    }


}
