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

@RestController
@RequestMapping(value = "")
public class UserRoleTypeController {
	@Autowired
    UserRoleTypeService userroletypeservice;


	@RequestMapping(method = RequestMethod.POST, value = "/userroletypes/checkkey")
    @PreAuthorize("@userroletype_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody UserRoleType userroletype) {
        boolean b = userroletypeservice.checkKey(userroletype);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroletypes/getdraft")
    @PreAuthorize("@userroletype_pms.check('CREATE')")
    public ResponseEntity<UserRoleType> getDraft() {
        //UserRoleType userroletype = userroletypeservice.getDraft( userroletype_id);
        return ResponseEntity.status(HttpStatus.OK).body(new UserRoleType());
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroletypes/{userroletype_id}/save")
    @PreAuthorize("@userroletype_pms.check(#userroletype_id,'')")
    public ResponseEntity<UserRoleType> save(@PathVariable("userroletype_id") String userroletype_id, @RequestBody UserRoleType userroletype) {
        UserRoleType userroletype2 = userroletypeservice.save(userroletype_id, userroletype);
        return ResponseEntity.status(HttpStatus.OK).body(userroletype2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroletypes")
    @PreAuthorize("@userroletype_pms.check('CREATE')")
    public ResponseEntity<UserRoleType> create(@RequestBody UserRoleType userroletype) {
        UserRoleType userroletype2 = userroletypeservice.create(userroletype);
        return ResponseEntity.status(HttpStatus.OK).body(userroletype2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroletypes/{userroletype_id}")
    @PreAuthorize("@userroletype_pms.check(#userroletype_id,'READ')")
    public ResponseEntity<UserRoleType> get(@PathVariable("userroletype_id") String userroletype_id) {
        UserRoleType userroletype = userroletypeservice.get( userroletype_id);
        return ResponseEntity.status(HttpStatus.OK).body(userroletype);
    }

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

	@RequestMapping(method = RequestMethod.PUT, value = "/userroletypes/{userroletype_id}")
    @PreAuthorize("@userroletype_pms.check(#userroletype_id,'UPDATE')")
    public ResponseEntity<UserRoleType> update(@PathVariable("userroletype_id") String userroletype_id, @RequestBody UserRoleType userroletype) {
        UserRoleType userroletype2 = userroletypeservice.update(userroletype_id, userroletype);
        return ResponseEntity.status(HttpStatus.OK).body(userroletype2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/userroletypes/{userroletype_id}")
    @PreAuthorize("@userroletype_pms.check(#userroletype_id,'UPDATE')")
    public ResponseEntity<UserRoleType> api_update(@PathVariable("userroletype_id") String userroletype_id, @RequestBody UserRoleType userroletype) {
        UserRoleType userroletype2 = userroletypeservice.update(userroletype_id, userroletype);
        return ResponseEntity.status(HttpStatus.OK).body(userroletype2);
    }

    @RequestMapping(method= RequestMethod.GET , value="/userroletypes/fetchdefault")
    @PreAuthorize("@userroletype_pms.check('READ')")
	public ResponseEntity<List<UserRoleType>> fetchDefault(UserRoleTypeSearchContext searchContext,Pageable pageable) {
        
        Page<UserRoleType> page = userroletypeservice.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());
    }


}
