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

@RestController
@RequestMapping(value = "")
public class UserRoleResController {
	@Autowired
    UserRoleResService userroleresservice;


	@RequestMapping(method = RequestMethod.GET, value = "/userroleres/getdraft")
    @PreAuthorize("@userroleres_pms.check('CREATE')")
    public ResponseEntity<UserRoleRes> getDraft() {
        //UserRoleRes userroleres = userroleresservice.getDraft( userroleres_id);
        return ResponseEntity.status(HttpStatus.OK).body(new UserRoleRes());
    }

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

	@RequestMapping(method = RequestMethod.PUT, value = "/userroleres/{userroleres_id}")
    @PreAuthorize("@userroleres_pms.check(#userroleres_id,'UPDATE')")
    public ResponseEntity<UserRoleRes> update(@PathVariable("userroleres_id") String userroleres_id, @RequestBody UserRoleRes userroleres) {
        UserRoleRes userroleres2 = userroleresservice.update(userroleres_id, userroleres);
        return ResponseEntity.status(HttpStatus.OK).body(userroleres2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/userroleres/{userroleres_id}")
    @PreAuthorize("@userroleres_pms.check(#userroleres_id,'UPDATE')")
    public ResponseEntity<UserRoleRes> api_update(@PathVariable("userroleres_id") String userroleres_id, @RequestBody UserRoleRes userroleres) {
        UserRoleRes userroleres2 = userroleresservice.update(userroleres_id, userroleres);
        return ResponseEntity.status(HttpStatus.OK).body(userroleres2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroleres/{userroleres_id}")
    @PreAuthorize("@userroleres_pms.check(#userroleres_id,'READ')")
    public ResponseEntity<UserRoleRes> get(@PathVariable("userroleres_id") String userroleres_id) {
        UserRoleRes userroleres = userroleresservice.get( userroleres_id);
        return ResponseEntity.status(HttpStatus.OK).body(userroleres);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroleres")
    @PreAuthorize("@userroleres_pms.check('CREATE')")
    public ResponseEntity<UserRoleRes> create(@RequestBody UserRoleRes userroleres) {
        UserRoleRes userroleres2 = userroleresservice.create(userroleres);
        return ResponseEntity.status(HttpStatus.OK).body(userroleres2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroleres/{userroleres_id}/save")
    @PreAuthorize("@userroleres_pms.check(#userroleres_id,'')")
    public ResponseEntity<UserRoleRes> save(@PathVariable("userroleres_id") String userroleres_id, @RequestBody UserRoleRes userroleres) {
        UserRoleRes userroleres2 = userroleresservice.save(userroleres_id, userroleres);
        return ResponseEntity.status(HttpStatus.OK).body(userroleres2);
    }

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

    @RequestMapping(method= RequestMethod.GET , value="/userroleres/fetchdefault")
    @PreAuthorize("@userroleres_pms.check('READ')")
	public ResponseEntity<List<UserRoleRes>> fetchDefault(UserRoleResSearchContext searchContext,Pageable pageable) {
        
        Page<UserRoleRes> page = userroleresservice.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());
    }


}
