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.UniResDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UniRes;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUniResService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UniResSearchContext;

@Api(tags = {"UniRes" })
@RestController
@RequestMapping("")
public class UniResResource {

    @Autowired
    private IUniResService uniresService;

    public IUniResService getUniresService() {
        return this.uniresService;
    }

    @ApiOperation(value = "Remove", tags = {"UniRes" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/unires/{unires_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("unires_id") String unires_id) {
        UniResDTO uniresdto = new UniResDTO();
		UniRes domain = new UniRes();
		uniresdto.setUniResId(unires_id);
		domain.setUniResId(unires_id);
        Boolean rst = uniresService.remove(domain.getUniResId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "Save", tags = {"UniRes" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/unires/{unires_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UniResDTO uniresdto) {
        UniRes unires = uniresdto.toDO();
    	Boolean b = uniresService.save(unires) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @ApiOperation(value = "Update", tags = {"UniRes" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/unires/{unires_id}")

    public ResponseEntity<UniResDTO> update(@PathVariable("unires_id") String unires_id, @RequestBody UniResDTO uniresdto) {
		UniRes domain = uniresdto.toDO();
        domain.setUniResId(unires_id);
		uniresService.update(domain);
		UniResDTO dto = new UniResDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Get", tags = {"UniRes" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/unires/{unires_id}")
    public ResponseEntity<UniResDTO> get(@PathVariable("unires_id") String unires_id) {
        UniResDTO dto = new UniResDTO();
        UniRes domain = uniresService.get(unires_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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

    public ResponseEntity<UniResDTO> create(@RequestBody UniResDTO uniresdto) {
        UniResDTO dto = new UniResDTO();
        UniRes domain = uniresdto.toDO();
		uniresService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "GetDraft", tags = {"UniRes" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/unires/{unires_id}/getdraft")

    public ResponseEntity<UniResDTO> getDraft(@PathVariable("unires_id") String unires_id, @RequestBody UniResDTO uniresdto) {
        UniRes unires = uniresdto.toDO();
    	unires = uniresService.getDraft(unires) ;
    	uniresdto.fromDO(unires);
        return ResponseEntity.status(HttpStatus.OK).body(uniresdto);
    }

    @ApiOperation(value = "获取权限资源数据", tags = {"UniRes" },  notes = "获取权限资源数据")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/unires/{unires_id}/getresource")

    public ResponseEntity<UniResDTO> getResource(@PathVariable("unires_id") String unires_id, @RequestBody UniResDTO uniresdto) {
        UniRes unires = uniresdto.toDO();
    	unires = uniresService.getResource(unires) ;
    	uniresdto.fromDO(unires);
        return ResponseEntity.status(HttpStatus.OK).body(uniresdto);
    }

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

	@ApiOperation(value = "获取查询全部实体结果", tags = {"UniRes" } ,notes = "获取查询全部实体结果")
    @RequestMapping(method= RequestMethod.GET , value="/runtimemgr/unires/fetchselectuniresall")
	public ResponseEntity<Page<UniResDTO>> fetchSelectUniresAll(UniResSearchContext context,Pageable pageable ,ServletRequest request) {
        context.setPageable(pageable);
        List<UniResDTO> list = new ArrayList<UniResDTO>();
        
        Page<UniRes> domains = uniresService.searchSelectUniresAll(context) ;
        for(UniRes unires : domains.getContent()){
            UniResDTO dto = new UniResDTO();
            dto.fromDO(unires);
            list.add(dto);
        }
	    return ResponseEntity.status(HttpStatus.OK).body(new PageImpl(list,context.getPageable(),domains.getTotalElements()));
	}

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


}
