package cn.ibizlab.r7.runtime.service.runtimemgr.resource;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.PageRequest;
import cn.ibizlab.r7.runtime.service.runtimemgr.dto.DataEntityDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.DataEntity;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IDataEntityService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.DataEntitySearchContext;

@Api(tags = {"DataEntity" })
@RestController
@RequestMapping("")
public class DataEntityResource {

    @Autowired
    private IDataEntityService dataentityService;

    public IDataEntityService getDataentityService() {
        return this.dataentityService;
    }

    @ApiOperation(value = "Remove", tags = {"DataEntity" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/dataentities/{dataentity_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("dataentity_id") String dataentity_id) {
        DataEntityDTO dataentitydto = new DataEntityDTO();
		DataEntity domain = new DataEntity();
		dataentitydto.setDataEntityId(dataentity_id);
		domain.setDataEntityId(dataentity_id);
        Boolean rst = dataentityService.remove(domain.getDataEntityId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "Create", tags = {"DataEntity" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/dataentities")

    public ResponseEntity<DataEntityDTO> create(@RequestBody DataEntityDTO dataentitydto) {
        DataEntityDTO dto = new DataEntityDTO();
        DataEntity domain = dataentitydto.toDO();
		dataentityService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Update", tags = {"DataEntity" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/dataentities/{dataentity_id}")

    public ResponseEntity<DataEntityDTO> update(@PathVariable("dataentity_id") String dataentity_id, @RequestBody DataEntityDTO dataentitydto) {
		DataEntity domain = dataentitydto.toDO();
        domain.setDataEntityId(dataentity_id);
		dataentityService.update(domain);
		DataEntityDTO dto = new DataEntityDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "初始化", tags = {"DataEntity" },  notes = "初始化")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/dataentities/{dataentity_id}/initall")

    public ResponseEntity<DataEntityDTO> initAll(@PathVariable("dataentity_id") String dataentity_id, @RequestBody DataEntityDTO dataentitydto) {
        DataEntity dataentity = dataentitydto.toDO();
    	dataentity = dataentityService.initAll(dataentity) ;
    	dataentitydto.fromDO(dataentity);
        return ResponseEntity.status(HttpStatus.OK).body(dataentitydto);
    }

    @ApiOperation(value = "GetDraft", tags = {"DataEntity" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/dataentities/{dataentity_id}/getdraft")

    public ResponseEntity<DataEntityDTO> getDraft(@PathVariable("dataentity_id") String dataentity_id, @RequestBody DataEntityDTO dataentitydto) {
        DataEntity dataentity = dataentitydto.toDO();
    	dataentity = dataentityService.getDraft(dataentity) ;
    	dataentitydto.fromDO(dataentity);
        return ResponseEntity.status(HttpStatus.OK).body(dataentitydto);
    }

    @ApiOperation(value = "Get", tags = {"DataEntity" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/dataentities/{dataentity_id}")
    public ResponseEntity<DataEntityDTO> get(@PathVariable("dataentity_id") String dataentity_id) {
        DataEntityDTO dto = new DataEntityDTO();
        DataEntity domain = dataentityService.get(dataentity_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Save", tags = {"DataEntity" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/dataentities/{dataentity_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody DataEntityDTO dataentitydto) {
        DataEntity dataentity = dataentitydto.toDO();
    	Boolean b = dataentityService.save(dataentity) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @ApiOperation(value = "CheckKey", tags = {"DataEntity" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/dataentities/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody DataEntityDTO dataentitydto) {
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

	@ApiOperation(value = "获取SelectDEAll", tags = {"DataEntity" } ,notes = "获取SelectDEAll")
    @RequestMapping(method= RequestMethod.GET , value="/runtimemgr/dataentities/fetchselectdeall")
	public ResponseEntity<Page<DataEntityDTO>> fetchSelectDEAll(DataEntitySearchContext context,Pageable pageable ,ServletRequest request) {
        context.setPageable(pageable);
        List<DataEntityDTO> list = new ArrayList<DataEntityDTO>();
        
        Page<DataEntity> domains = dataentityService.searchSelectDEAll(context) ;
        for(DataEntity dataentity : domains.getContent()){
            DataEntityDTO dto = new DataEntityDTO();
            dto.fromDO(dataentity);
            list.add(dto);
        }
	    return ResponseEntity.status(HttpStatus.OK).body(new PageImpl(list,context.getPageable(),domains.getTotalElements()));
	}

	@ApiOperation(value = "获取DEFAULT", tags = {"DataEntity" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/runtimemgr/dataentities/fetchdefault")
	public ResponseEntity<Page<DataEntityDTO>> fetchDefault(DataEntitySearchContext context,Pageable pageable ,ServletRequest request) {
        context.setPageable(pageable);
        List<DataEntityDTO> list = new ArrayList<DataEntityDTO>();
        
        Page<DataEntity> domains = dataentityService.searchDefault(context) ;
        for(DataEntity dataentity : domains.getContent()){
            DataEntityDTO dto = new DataEntityDTO();
            dto.fromDO(dataentity);
            list.add(dto);
        }
	    return ResponseEntity.status(HttpStatus.OK).body(new PageImpl(list,context.getPageable(),domains.getTotalElements()));
	}


}
