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.UserRoleDetailDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleDetail;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleDetailService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleDetailSearchContext;

@Api(tags = {"UserRoleDetail" })
@RestController
@RequestMapping("")
public class UserRoleDetailResource {

    @Autowired
    private IUserRoleDetailService userroledetailService;

    public IUserRoleDetailService getUserroledetailService() {
        return this.userroledetailService;
    }

    @ApiOperation(value = "Remove", tags = {"UserRoleDetail" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/userroledetails/{userroledetail_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("userroledetail_id") String userroledetail_id) {
        UserRoleDetailDTO userroledetaildto = new UserRoleDetailDTO();
		UserRoleDetail domain = new UserRoleDetail();
		userroledetaildto.setUserRoleDetailId(userroledetail_id);
		domain.setUserRoleDetailId(userroledetail_id);
        Boolean rst = userroledetailService.remove(domain.getUserRoleDetailId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "Update", tags = {"UserRoleDetail" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/userroledetails/{userroledetail_id}")

    public ResponseEntity<UserRoleDetailDTO> update(@PathVariable("userroledetail_id") String userroledetail_id, @RequestBody UserRoleDetailDTO userroledetaildto) {
		UserRoleDetail domain = userroledetaildto.toDO();
        domain.setUserRoleDetailId(userroledetail_id);
		userroledetailService.update(domain);
		UserRoleDetailDTO dto = new UserRoleDetailDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Save", tags = {"UserRoleDetail" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userroledetails/{userroledetail_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserRoleDetailDTO userroledetaildto) {
        UserRoleDetail userroledetail = userroledetaildto.toDO();
    	Boolean b = userroledetailService.save(userroledetail) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

    public ResponseEntity<UserRoleDetailDTO> create(@RequestBody UserRoleDetailDTO userroledetaildto) {
        UserRoleDetailDTO dto = new UserRoleDetailDTO();
        UserRoleDetail domain = userroledetaildto.toDO();
		userroledetailService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "GetDraft", tags = {"UserRoleDetail" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroledetails/{userroledetail_id}/getdraft")

    public ResponseEntity<UserRoleDetailDTO> getDraft(@PathVariable("userroledetail_id") String userroledetail_id, @RequestBody UserRoleDetailDTO userroledetaildto) {
        UserRoleDetail userroledetail = userroledetaildto.toDO();
    	userroledetail = userroledetailService.getDraft(userroledetail) ;
    	userroledetaildto.fromDO(userroledetail);
        return ResponseEntity.status(HttpStatus.OK).body(userroledetaildto);
    }

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

    @ApiOperation(value = "Get", tags = {"UserRoleDetail" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroledetails/{userroledetail_id}")
    public ResponseEntity<UserRoleDetailDTO> get(@PathVariable("userroledetail_id") String userroledetail_id) {
        UserRoleDetailDTO dto = new UserRoleDetailDTO();
        UserRoleDetail domain = userroledetailService.get(userroledetail_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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


}
