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.UserRoleDataScopeDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleDataScope;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleDataScopeService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleDataScopeSearchContext;

@Api(tags = {"UserRoleDataScope" })
@RestController
@RequestMapping("")
public class UserRoleDataScopeResource {

    @Autowired
    private IUserRoleDataScopeService userroledatascopeService;

    public IUserRoleDataScopeService getUserroledatascopeService() {
        return this.userroledatascopeService;
    }

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

    public ResponseEntity<UserRoleDataScopeDTO> create(@RequestBody UserRoleDataScopeDTO userroledatascopedto) {
        UserRoleDataScopeDTO dto = new UserRoleDataScopeDTO();
        UserRoleDataScope domain = userroledatascopedto.toDO();
		userroledatascopeService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Save", tags = {"UserRoleDataScope" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userroledatascopes/{userroledatascope_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserRoleDataScopeDTO userroledatascopedto) {
        UserRoleDataScope userroledatascope = userroledatascopedto.toDO();
    	Boolean b = userroledatascopeService.save(userroledatascope) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

    @ApiOperation(value = "GetDraft", tags = {"UserRoleDataScope" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroledatascopes/{userroledatascope_id}/getdraft")

    public ResponseEntity<UserRoleDataScopeDTO> getDraft(@PathVariable("userroledatascope_id") String userroledatascope_id, @RequestBody UserRoleDataScopeDTO userroledatascopedto) {
        UserRoleDataScope userroledatascope = userroledatascopedto.toDO();
    	userroledatascope = userroledatascopeService.getDraft(userroledatascope) ;
    	userroledatascopedto.fromDO(userroledatascope);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatascopedto);
    }

    @ApiOperation(value = "Update", tags = {"UserRoleDataScope" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/userroledatascopes/{userroledatascope_id}")

    public ResponseEntity<UserRoleDataScopeDTO> update(@PathVariable("userroledatascope_id") String userroledatascope_id, @RequestBody UserRoleDataScopeDTO userroledatascopedto) {
		UserRoleDataScope domain = userroledatascopedto.toDO();
        domain.setUserRoleDataScopeId(userroledatascope_id);
		userroledatascopeService.update(domain);
		UserRoleDataScopeDTO dto = new UserRoleDataScopeDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Get", tags = {"UserRoleDataScope" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroledatascopes/{userroledatascope_id}")
    public ResponseEntity<UserRoleDataScopeDTO> get(@PathVariable("userroledatascope_id") String userroledatascope_id) {
        UserRoleDataScopeDTO dto = new UserRoleDataScopeDTO();
        UserRoleDataScope domain = userroledatascopeService.get(userroledatascope_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Remove", tags = {"UserRoleDataScope" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/userroledatascopes/{userroledatascope_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("userroledatascope_id") String userroledatascope_id) {
        UserRoleDataScopeDTO userroledatascopedto = new UserRoleDataScopeDTO();
		UserRoleDataScope domain = new UserRoleDataScope();
		userroledatascopedto.setUserRoleDataScopeId(userroledatascope_id);
		domain.setUserRoleDataScopeId(userroledatascope_id);
        Boolean rst = userroledatascopeService.remove(domain.getUserRoleDataScopeId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

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


}
