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.UserRoleDatasDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleDatas;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleDatasService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleDatasSearchContext;

@Api(tags = {"UserRoleDatas" })
@RestController
@RequestMapping("")
public class UserRoleDatasResource {

    @Autowired
    private IUserRoleDatasService userroledatasService;

    public IUserRoleDatasService getUserroledatasService() {
        return this.userroledatasService;
    }

    @ApiOperation(value = "Save", tags = {"UserRoleDatas" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userroledatas/{userroledatas_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserRoleDatasDTO userroledatasdto) {
        UserRoleDatas userroledatas = userroledatasdto.toDO();
    	Boolean b = userroledatasService.save(userroledatas) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

    public ResponseEntity<UserRoleDatasDTO> create(@RequestBody UserRoleDatasDTO userroledatasdto) {
        UserRoleDatasDTO dto = new UserRoleDatasDTO();
        UserRoleDatas domain = userroledatasdto.toDO();
		userroledatasService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Update", tags = {"UserRoleDatas" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/userroledatas/{userroledatas_id}")

    public ResponseEntity<UserRoleDatasDTO> update(@PathVariable("userroledatas_id") String userroledatas_id, @RequestBody UserRoleDatasDTO userroledatasdto) {
		UserRoleDatas domain = userroledatasdto.toDO();
        domain.setUserRoleDatasId(userroledatas_id);
		userroledatasService.update(domain);
		UserRoleDatasDTO dto = new UserRoleDatasDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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

    @ApiOperation(value = "Get", tags = {"UserRoleDatas" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroledatas/{userroledatas_id}")
    public ResponseEntity<UserRoleDatasDTO> get(@PathVariable("userroledatas_id") String userroledatas_id) {
        UserRoleDatasDTO dto = new UserRoleDatasDTO();
        UserRoleDatas domain = userroledatasService.get(userroledatas_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "GetDraft", tags = {"UserRoleDatas" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroledatas/{userroledatas_id}/getdraft")

    public ResponseEntity<UserRoleDatasDTO> getDraft(@PathVariable("userroledatas_id") String userroledatas_id, @RequestBody UserRoleDatasDTO userroledatasdto) {
        UserRoleDatas userroledatas = userroledatasdto.toDO();
    	userroledatas = userroledatasService.getDraft(userroledatas) ;
    	userroledatasdto.fromDO(userroledatas);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatasdto);
    }

    @ApiOperation(value = "Remove", tags = {"UserRoleDatas" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/userroledatas/{userroledatas_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("userroledatas_id") String userroledatas_id) {
        UserRoleDatasDTO userroledatasdto = new UserRoleDatasDTO();
		UserRoleDatas domain = new UserRoleDatas();
		userroledatasdto.setUserRoleDatasId(userroledatas_id);
		domain.setUserRoleDatasId(userroledatas_id);
        Boolean rst = userroledatasService.remove(domain.getUserRoleDatasId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

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


}
