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

@RestController
@RequestMapping(value = "")
public class UniResController {
	@Autowired
    UniResService uniresservice;


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

	@RequestMapping(method = RequestMethod.POST, value = "/unires/{unires_id}/save")
    @PreAuthorize("@unires_pms.check(#unires_id,'')")
    public ResponseEntity<UniRes> save(@PathVariable("unires_id") String unires_id, @RequestBody UniRes unires) {
        UniRes unires2 = uniresservice.save(unires_id, unires);
        return ResponseEntity.status(HttpStatus.OK).body(unires2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/unires/{unires_id}")
    @PreAuthorize("@unires_pms.check(#unires_id,'UPDATE')")
    public ResponseEntity<UniRes> update(@PathVariable("unires_id") String unires_id, @RequestBody UniRes unires) {
        UniRes unires2 = uniresservice.update(unires_id, unires);
        return ResponseEntity.status(HttpStatus.OK).body(unires2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/unires/{unires_id}")
    @PreAuthorize("@unires_pms.check(#unires_id,'UPDATE')")
    public ResponseEntity<UniRes> api_update(@PathVariable("unires_id") String unires_id, @RequestBody UniRes unires) {
        UniRes unires2 = uniresservice.update(unires_id, unires);
        return ResponseEntity.status(HttpStatus.OK).body(unires2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/unires/{unires_id}")
    @PreAuthorize("@unires_pms.check(#unires_id,'READ')")
    public ResponseEntity<UniRes> get(@PathVariable("unires_id") String unires_id) {
        UniRes unires = uniresservice.get( unires_id);
        return ResponseEntity.status(HttpStatus.OK).body(unires);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/unires")
    @PreAuthorize("@unires_pms.check('CREATE')")
    public ResponseEntity<UniRes> create(@RequestBody UniRes unires) {
        UniRes unires2 = uniresservice.create(unires);
        return ResponseEntity.status(HttpStatus.OK).body(unires2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/unires/getdraft")
    @PreAuthorize("@unires_pms.check('CREATE')")
    public ResponseEntity<UniRes> getDraft() {
        //UniRes unires = uniresservice.getDraft( unires_id);
        return ResponseEntity.status(HttpStatus.OK).body(new UniRes());
    }

	@RequestMapping(method = RequestMethod.GET, value = "/unires/{unires_id}/getresource")
    @PreAuthorize("@unires_pms.check(#unires_id,'READ')")
    public ResponseEntity<UniRes> getResource(@PathVariable("unires_id") String unires_id, @RequestBody UniRes unires) {
        UniRes unires2 = uniresservice.getResource(unires_id, unires);
        return ResponseEntity.status(HttpStatus.OK).body(unires2);
    }

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

    @RequestMapping(method= RequestMethod.GET , value="/unires/fetchselectuniresall")
    @PreAuthorize("@unires_pms.check('READ')")
	public ResponseEntity<List<UniRes>> fetchSelectUniresAll(UniResSearchContext searchContext,Pageable pageable) {
        
        Page<UniRes> page = uniresservice.fetchSelectUniresAll(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());
    }

    @RequestMapping(method= RequestMethod.GET , value="/unires/fetchdefault")
    @PreAuthorize("@unires_pms.check('READ')")
	public ResponseEntity<List<UniRes>> fetchDefault(UniResSearchContext searchContext,Pageable pageable) {
        
        Page<UniRes> page = uniresservice.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());
    }


}
