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

@RestController
@RequestMapping(value = "")
public class UserGroupDetailController {
	@Autowired
    UserGroupDetailService usergroupdetailservice;


	@RequestMapping(method = RequestMethod.GET, value = "/usergroupdetails/{usergroupdetail_id}")
    @PreAuthorize("@usergroupdetail_pms.check(#usergroupdetail_id,'READ')")
    public ResponseEntity<UserGroupDetail> get(@PathVariable("usergroupdetail_id") String usergroupdetail_id) {
        UserGroupDetail usergroupdetail = usergroupdetailservice.get( usergroupdetail_id);
        return ResponseEntity.status(HttpStatus.OK).body(usergroupdetail);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/usergroupdetails/getdraft")
    @PreAuthorize("@usergroupdetail_pms.check('CREATE')")
    public ResponseEntity<UserGroupDetail> getDraft() {
        //UserGroupDetail usergroupdetail = usergroupdetailservice.getDraft( usergroupdetail_id);
        return ResponseEntity.status(HttpStatus.OK).body(new UserGroupDetail());
    }

	@RequestMapping(method = RequestMethod.POST, value = "/usergroupdetails")
    @PreAuthorize("@usergroupdetail_pms.check('CREATE')")
    public ResponseEntity<UserGroupDetail> create(@RequestBody UserGroupDetail usergroupdetail) {
        UserGroupDetail usergroupdetail2 = usergroupdetailservice.create(usergroupdetail);
        return ResponseEntity.status(HttpStatus.OK).body(usergroupdetail2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/usergroupdetails/{usergroupdetail_id}/save")
    @PreAuthorize("@usergroupdetail_pms.check(#usergroupdetail_id,'')")
    public ResponseEntity<UserGroupDetail> save(@PathVariable("usergroupdetail_id") String usergroupdetail_id, @RequestBody UserGroupDetail usergroupdetail) {
        UserGroupDetail usergroupdetail2 = usergroupdetailservice.save(usergroupdetail_id, usergroupdetail);
        return ResponseEntity.status(HttpStatus.OK).body(usergroupdetail2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/usergroupdetails/checkkey")
    @PreAuthorize("@usergroupdetail_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody UserGroupDetail usergroupdetail) {
        boolean b = usergroupdetailservice.checkKey(usergroupdetail);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/usergroupdetails/{usergroupdetail_id}")
    @PreAuthorize("@usergroupdetail_pms.check(#usergroupdetail_id,'UPDATE')")
    public ResponseEntity<UserGroupDetail> update(@PathVariable("usergroupdetail_id") String usergroupdetail_id, @RequestBody UserGroupDetail usergroupdetail) {
        UserGroupDetail usergroupdetail2 = usergroupdetailservice.update(usergroupdetail_id, usergroupdetail);
        return ResponseEntity.status(HttpStatus.OK).body(usergroupdetail2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/usergroupdetails/{usergroupdetail_id}")
    @PreAuthorize("@usergroupdetail_pms.check(#usergroupdetail_id,'UPDATE')")
    public ResponseEntity<UserGroupDetail> api_update(@PathVariable("usergroupdetail_id") String usergroupdetail_id, @RequestBody UserGroupDetail usergroupdetail) {
        UserGroupDetail usergroupdetail2 = usergroupdetailservice.update(usergroupdetail_id, usergroupdetail);
        return ResponseEntity.status(HttpStatus.OK).body(usergroupdetail2);
    }

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

    @RequestMapping(method= RequestMethod.GET , value="/usergroupdetails/fetchdefault")
    @PreAuthorize("@usergroupdetail_pms.check('READ')")
	public ResponseEntity<List<UserGroupDetail>> fetchDefault(UserGroupDetailSearchContext searchContext,Pageable pageable) {
        
        Page<UserGroupDetail> page = usergroupdetailservice.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());
    }


}
