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

@RestController
@RequestMapping(value = "")
public class UserRoleDetailController {
	@Autowired
    UserRoleDetailService userroledetailservice;


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

	@RequestMapping(method = RequestMethod.PUT, value = "/userroledetails/{userroledetail_id}")
    @PreAuthorize("@userroledetail_pms.check(#userroledetail_id,'UPDATE')")
    public ResponseEntity<UserRoleDetail> update(@PathVariable("userroledetail_id") String userroledetail_id, @RequestBody UserRoleDetail userroledetail) {
        UserRoleDetail userroledetail2 = userroledetailservice.update(userroledetail_id, userroledetail);
        return ResponseEntity.status(HttpStatus.OK).body(userroledetail2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/userroledetails/{userroledetail_id}")
    @PreAuthorize("@userroledetail_pms.check(#userroledetail_id,'UPDATE')")
    public ResponseEntity<UserRoleDetail> api_update(@PathVariable("userroledetail_id") String userroledetail_id, @RequestBody UserRoleDetail userroledetail) {
        UserRoleDetail userroledetail2 = userroledetailservice.update(userroledetail_id, userroledetail);
        return ResponseEntity.status(HttpStatus.OK).body(userroledetail2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroledetails/{userroledetail_id}/save")
    @PreAuthorize("@userroledetail_pms.check(#userroledetail_id,'')")
    public ResponseEntity<UserRoleDetail> save(@PathVariable("userroledetail_id") String userroledetail_id, @RequestBody UserRoleDetail userroledetail) {
        UserRoleDetail userroledetail2 = userroledetailservice.save(userroledetail_id, userroledetail);
        return ResponseEntity.status(HttpStatus.OK).body(userroledetail2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroledetails")
    @PreAuthorize("@userroledetail_pms.check('CREATE')")
    public ResponseEntity<UserRoleDetail> create(@RequestBody UserRoleDetail userroledetail) {
        UserRoleDetail userroledetail2 = userroledetailservice.create(userroledetail);
        return ResponseEntity.status(HttpStatus.OK).body(userroledetail2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroledetails/getdraft")
    @PreAuthorize("@userroledetail_pms.check('CREATE')")
    public ResponseEntity<UserRoleDetail> getDraft() {
        //UserRoleDetail userroledetail = userroledetailservice.getDraft( userroledetail_id);
        return ResponseEntity.status(HttpStatus.OK).body(new UserRoleDetail());
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroledetails/checkkey")
    @PreAuthorize("@userroledetail_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody UserRoleDetail userroledetail) {
        boolean b = userroledetailservice.checkKey(userroledetail);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroledetails/{userroledetail_id}")
    @PreAuthorize("@userroledetail_pms.check(#userroledetail_id,'READ')")
    public ResponseEntity<UserRoleDetail> get(@PathVariable("userroledetail_id") String userroledetail_id) {
        UserRoleDetail userroledetail = userroledetailservice.get( userroledetail_id);
        return ResponseEntity.status(HttpStatus.OK).body(userroledetail);
    }

    @RequestMapping(method= RequestMethod.GET , value="/userroledetails/fetchdefault")
    @PreAuthorize("@userroledetail_pms.check('READ')")
	public ResponseEntity<List<UserRoleDetail>> fetchDefault(UserRoleDetailSearchContext searchContext,Pageable pageable) {
        
        Page<UserRoleDetail> page = userroledetailservice.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());
    }


}
