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.UserRoleDataActionDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleDataAction;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleDataActionService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleDataActionSearchContext;

@Api(tags = {"UserRoleDataAction" })
@RestController
@RequestMapping("")
public class UserRoleDataActionResource {

    @Autowired
    private IUserRoleDataActionService userroledataactionService;

    public IUserRoleDataActionService getUserroledataactionService() {
        return this.userroledataactionService;
    }

    @ApiOperation(value = "Save", tags = {"UserRoleDataAction" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userroledataactions/{userroledataaction_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserRoleDataActionDTO userroledataactiondto) {
        UserRoleDataAction userroledataaction = userroledataactiondto.toDO();
    	Boolean b = userroledataactionService.save(userroledataaction) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

    public ResponseEntity<UserRoleDataActionDTO> create(@RequestBody UserRoleDataActionDTO userroledataactiondto) {
        UserRoleDataActionDTO dto = new UserRoleDataActionDTO();
        UserRoleDataAction domain = userroledataactiondto.toDO();
		userroledataactionService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Update", tags = {"UserRoleDataAction" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/userroledataactions/{userroledataaction_id}")

    public ResponseEntity<UserRoleDataActionDTO> update(@PathVariable("userroledataaction_id") String userroledataaction_id, @RequestBody UserRoleDataActionDTO userroledataactiondto) {
		UserRoleDataAction domain = userroledataactiondto.toDO();
        domain.setUserRoleDataActionId(userroledataaction_id);
		userroledataactionService.update(domain);
		UserRoleDataActionDTO dto = new UserRoleDataActionDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "GetDraft", tags = {"UserRoleDataAction" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroledataactions/{userroledataaction_id}/getdraft")

    public ResponseEntity<UserRoleDataActionDTO> getDraft(@PathVariable("userroledataaction_id") String userroledataaction_id, @RequestBody UserRoleDataActionDTO userroledataactiondto) {
        UserRoleDataAction userroledataaction = userroledataactiondto.toDO();
    	userroledataaction = userroledataactionService.getDraft(userroledataaction) ;
    	userroledataactiondto.fromDO(userroledataaction);
        return ResponseEntity.status(HttpStatus.OK).body(userroledataactiondto);
    }

    @ApiOperation(value = "Get", tags = {"UserRoleDataAction" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroledataactions/{userroledataaction_id}")
    public ResponseEntity<UserRoleDataActionDTO> get(@PathVariable("userroledataaction_id") String userroledataaction_id) {
        UserRoleDataActionDTO dto = new UserRoleDataActionDTO();
        UserRoleDataAction domain = userroledataactionService.get(userroledataaction_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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

    @ApiOperation(value = "Remove", tags = {"UserRoleDataAction" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/userroledataactions/{userroledataaction_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("userroledataaction_id") String userroledataaction_id) {
        UserRoleDataActionDTO userroledataactiondto = new UserRoleDataActionDTO();
		UserRoleDataAction domain = new UserRoleDataAction();
		userroledataactiondto.setUserRoleDataActionId(userroledataaction_id);
		domain.setUserRoleDataActionId(userroledataaction_id);
        Boolean rst = userroledataactionService.remove(domain.getUserRoleDataActionId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

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


}
