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.UserRoleResDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleRes;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleResService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleResSearchContext;

@Api(tags = {"UserRoleRes" })
@RestController
@RequestMapping("")
public class UserRoleResResource {

    @Autowired
    private IUserRoleResService userroleresService;

    public IUserRoleResService getUserroleresService() {
        return this.userroleresService;
    }

    @ApiOperation(value = "GetDraft", tags = {"UserRoleRes" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroleres/{userroleres_id}/getdraft")

    public ResponseEntity<UserRoleResDTO> getDraft(@PathVariable("userroleres_id") String userroleres_id, @RequestBody UserRoleResDTO userroleresdto) {
        UserRoleRes userroleres = userroleresdto.toDO();
    	userroleres = userroleresService.getDraft(userroleres) ;
    	userroleresdto.fromDO(userroleres);
        return ResponseEntity.status(HttpStatus.OK).body(userroleresdto);
    }

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

    @ApiOperation(value = "Update", tags = {"UserRoleRes" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/userroleres/{userroleres_id}")

    public ResponseEntity<UserRoleResDTO> update(@PathVariable("userroleres_id") String userroleres_id, @RequestBody UserRoleResDTO userroleresdto) {
		UserRoleRes domain = userroleresdto.toDO();
        domain.setUserRoleResId(userroleres_id);
		userroleresService.update(domain);
		UserRoleResDTO dto = new UserRoleResDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Get", tags = {"UserRoleRes" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroleres/{userroleres_id}")
    public ResponseEntity<UserRoleResDTO> get(@PathVariable("userroleres_id") String userroleres_id) {
        UserRoleResDTO dto = new UserRoleResDTO();
        UserRoleRes domain = userroleresService.get(userroleres_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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

    public ResponseEntity<UserRoleResDTO> create(@RequestBody UserRoleResDTO userroleresdto) {
        UserRoleResDTO dto = new UserRoleResDTO();
        UserRoleRes domain = userroleresdto.toDO();
		userroleresService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Save", tags = {"UserRoleRes" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userroleres/{userroleres_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserRoleResDTO userroleresdto) {
        UserRoleRes userroleres = userroleresdto.toDO();
    	Boolean b = userroleresService.save(userroleres) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @ApiOperation(value = "Remove", tags = {"UserRoleRes" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/userroleres/{userroleres_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("userroleres_id") String userroleres_id) {
        UserRoleResDTO userroleresdto = new UserRoleResDTO();
		UserRoleRes domain = new UserRoleRes();
		userroleresdto.setUserRoleResId(userroleres_id);
		domain.setUserRoleResId(userroleres_id);
        Boolean rst = userroleresService.remove(domain.getUserRoleResId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

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


}
