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.UserRoleTypeDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleType;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleTypeService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleTypeSearchContext;

@Api(tags = {"UserRoleType" })
@RestController
@RequestMapping("")
public class UserRoleTypeResource {

    @Autowired
    private IUserRoleTypeService userroletypeService;

    public IUserRoleTypeService getUserroletypeService() {
        return this.userroletypeService;
    }

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

    @ApiOperation(value = "GetDraft", tags = {"UserRoleType" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroletypes/{userroletype_id}/getdraft")

    public ResponseEntity<UserRoleTypeDTO> getDraft(@PathVariable("userroletype_id") String userroletype_id, @RequestBody UserRoleTypeDTO userroletypedto) {
        UserRoleType userroletype = userroletypedto.toDO();
    	userroletype = userroletypeService.getDraft(userroletype) ;
    	userroletypedto.fromDO(userroletype);
        return ResponseEntity.status(HttpStatus.OK).body(userroletypedto);
    }

    @ApiOperation(value = "Save", tags = {"UserRoleType" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userroletypes/{userroletype_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserRoleTypeDTO userroletypedto) {
        UserRoleType userroletype = userroletypedto.toDO();
    	Boolean b = userroletypeService.save(userroletype) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

    public ResponseEntity<UserRoleTypeDTO> create(@RequestBody UserRoleTypeDTO userroletypedto) {
        UserRoleTypeDTO dto = new UserRoleTypeDTO();
        UserRoleType domain = userroletypedto.toDO();
		userroletypeService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Get", tags = {"UserRoleType" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroletypes/{userroletype_id}")
    public ResponseEntity<UserRoleTypeDTO> get(@PathVariable("userroletype_id") String userroletype_id) {
        UserRoleTypeDTO dto = new UserRoleTypeDTO();
        UserRoleType domain = userroletypeService.get(userroletype_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Remove", tags = {"UserRoleType" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/userroletypes/{userroletype_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("userroletype_id") String userroletype_id) {
        UserRoleTypeDTO userroletypedto = new UserRoleTypeDTO();
		UserRoleType domain = new UserRoleType();
		userroletypedto.setUserRoleTypeId(userroletype_id);
		domain.setUserRoleTypeId(userroletype_id);
        Boolean rst = userroletypeService.remove(domain.getUserRoleTypeId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "Update", tags = {"UserRoleType" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/userroletypes/{userroletype_id}")

    public ResponseEntity<UserRoleTypeDTO> update(@PathVariable("userroletype_id") String userroletype_id, @RequestBody UserRoleTypeDTO userroletypedto) {
		UserRoleType domain = userroletypedto.toDO();
        domain.setUserRoleTypeId(userroletype_id);
		userroletypeService.update(domain);
		UserRoleTypeDTO dto = new UserRoleTypeDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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


}
