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.UserRoleDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRole;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleSearchContext;

@Api(tags = {"UserRole" })
@RestController
@RequestMapping("")
public class UserRoleResource {

    @Autowired
    private IUserRoleService userroleService;

    public IUserRoleService getUserroleService() {
        return this.userroleService;
    }

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

    public ResponseEntity<UserRoleDTO> create(@RequestBody UserRoleDTO userroledto) {
        UserRoleDTO dto = new UserRoleDTO();
        UserRole domain = userroledto.toDO();
		userroleService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Save", tags = {"UserRole" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userroles/{userrole_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserRoleDTO userroledto) {
        UserRole userrole = userroledto.toDO();
    	Boolean b = userroleService.save(userrole) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @ApiOperation(value = "Update", tags = {"UserRole" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/userroles/{userrole_id}")

    public ResponseEntity<UserRoleDTO> update(@PathVariable("userrole_id") String userrole_id, @RequestBody UserRoleDTO userroledto) {
		UserRole domain = userroledto.toDO();
        domain.setUserRoleId(userrole_id);
		userroleService.update(domain);
		UserRoleDTO dto = new UserRoleDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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

    @ApiOperation(value = "Get", tags = {"UserRole" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroles/{userrole_id}")
    public ResponseEntity<UserRoleDTO> get(@PathVariable("userrole_id") String userrole_id) {
        UserRoleDTO dto = new UserRoleDTO();
        UserRole domain = userroleService.get(userrole_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Remove", tags = {"UserRole" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/userroles/{userrole_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("userrole_id") String userrole_id) {
        UserRoleDTO userroledto = new UserRoleDTO();
		UserRole domain = new UserRole();
		userroledto.setUserRoleId(userrole_id);
		domain.setUserRoleId(userrole_id);
        Boolean rst = userroleService.remove(domain.getUserRoleId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "GetDraft", tags = {"UserRole" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroles/{userrole_id}/getdraft")

    public ResponseEntity<UserRoleDTO> getDraft(@PathVariable("userrole_id") String userrole_id, @RequestBody UserRoleDTO userroledto) {
        UserRole userrole = userroledto.toDO();
    	userrole = userroleService.getDraft(userrole) ;
    	userroledto.fromDO(userrole);
        return ResponseEntity.status(HttpStatus.OK).body(userroledto);
    }

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


}
