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

@RestController
@RequestMapping(value = "")
public class DataEntityController {
	@Autowired
    DataEntityService dataentityservice;


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

	@RequestMapping(method = RequestMethod.POST, value = "/dataentities")
    @PreAuthorize("@dataentity_pms.check('CREATE')")
    public ResponseEntity<DataEntity> create(@RequestBody DataEntity dataentity) {
        DataEntity dataentity2 = dataentityservice.create(dataentity);
        return ResponseEntity.status(HttpStatus.OK).body(dataentity2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/dataentities/{dataentity_id}")
    @PreAuthorize("@dataentity_pms.check(#dataentity_id,'UPDATE')")
    public ResponseEntity<DataEntity> update(@PathVariable("dataentity_id") String dataentity_id, @RequestBody DataEntity dataentity) {
        DataEntity dataentity2 = dataentityservice.update(dataentity_id, dataentity);
        return ResponseEntity.status(HttpStatus.OK).body(dataentity2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/dataentities/{dataentity_id}")
    @PreAuthorize("@dataentity_pms.check(#dataentity_id,'UPDATE')")
    public ResponseEntity<DataEntity> api_update(@PathVariable("dataentity_id") String dataentity_id, @RequestBody DataEntity dataentity) {
        DataEntity dataentity2 = dataentityservice.update(dataentity_id, dataentity);
        return ResponseEntity.status(HttpStatus.OK).body(dataentity2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/dataentities/{dataentity_id}/initall")
    @PreAuthorize("@dataentity_pms.check(#dataentity_id,'')")
    public ResponseEntity<DataEntity> initAll(@PathVariable("dataentity_id") String dataentity_id, @RequestBody DataEntity dataentity) {
        DataEntity dataentity2 = dataentityservice.initAll(dataentity_id, dataentity);
        return ResponseEntity.status(HttpStatus.OK).body(dataentity2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/dataentities/getdraft")
    @PreAuthorize("@dataentity_pms.check('CREATE')")
    public ResponseEntity<DataEntity> getDraft() {
        //DataEntity dataentity = dataentityservice.getDraft( dataentity_id);
        return ResponseEntity.status(HttpStatus.OK).body(new DataEntity());
    }

	@RequestMapping(method = RequestMethod.GET, value = "/dataentities/{dataentity_id}")
    @PreAuthorize("@dataentity_pms.check(#dataentity_id,'READ')")
    public ResponseEntity<DataEntity> get(@PathVariable("dataentity_id") String dataentity_id) {
        DataEntity dataentity = dataentityservice.get( dataentity_id);
        return ResponseEntity.status(HttpStatus.OK).body(dataentity);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/dataentities/{dataentity_id}/save")
    @PreAuthorize("@dataentity_pms.check(#dataentity_id,'')")
    public ResponseEntity<DataEntity> save(@PathVariable("dataentity_id") String dataentity_id, @RequestBody DataEntity dataentity) {
        DataEntity dataentity2 = dataentityservice.save(dataentity_id, dataentity);
        return ResponseEntity.status(HttpStatus.OK).body(dataentity2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/dataentities/checkkey")
    @PreAuthorize("@dataentity_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody DataEntity dataentity) {
        boolean b = dataentityservice.checkKey(dataentity);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @RequestMapping(method= RequestMethod.GET , value="/dataentities/fetchselectdeall")
    @PreAuthorize("@dataentity_pms.check('READ')")
	public ResponseEntity<List<DataEntity>> fetchSelectDEAll(DataEntitySearchContext searchContext,Pageable pageable) {
        
        Page<DataEntity> page = dataentityservice.fetchSelectDEAll(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="/dataentities/fetchdefault")
    @PreAuthorize("@dataentity_pms.check('READ')")
	public ResponseEntity<List<DataEntity>> fetchDefault(DataEntitySearchContext searchContext,Pageable pageable) {
        
        Page<DataEntity> page = dataentityservice.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());
    }


}
