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.UserRoleDataDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleData;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleDataService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleDataSearchContext;

@Api(tags = {"UserRoleData" })
@RestController
@RequestMapping("")
public class UserRoleDataResource {

    @Autowired
    private IUserRoleDataService userroledataService;

    public IUserRoleDataService getUserroledataService() {
        return this.userroledataService;
    }

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

    @ApiOperation(value = "GetDraft", tags = {"UserRoleData" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroledata/{userroledata_id}/getdraft")

    public ResponseEntity<UserRoleDataDTO> getDraft(@PathVariable("userroledata_id") String userroledata_id, @RequestBody UserRoleDataDTO userroledatadto) {
        UserRoleData userroledata = userroledatadto.toDO();
    	userroledata = userroledataService.getDraft(userroledata) ;
    	userroledatadto.fromDO(userroledata);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatadto);
    }

    @ApiOperation(value = "获取数据能力接口", tags = {"UserRoleData" },  notes = "获取数据能力接口")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroledata/{userroledata_id}/getuserroledataapi")

    public ResponseEntity<UserRoleDataDTO> getUserRoleDataAPI(@PathVariable("userroledata_id") String userroledata_id, @RequestBody UserRoleDataDTO userroledatadto) {
        UserRoleData userroledata = userroledatadto.toDO();
    	userroledata = userroledataService.getUserRoleDataAPI(userroledata) ;
    	userroledatadto.fromDO(userroledata);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatadto);
    }

    @ApiOperation(value = "Get", tags = {"UserRoleData" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userroledata/{userroledata_id}")
    public ResponseEntity<UserRoleDataDTO> get(@PathVariable("userroledata_id") String userroledata_id) {
        UserRoleDataDTO dto = new UserRoleDataDTO();
        UserRoleData domain = userroledataService.get(userroledata_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Remove", tags = {"UserRoleData" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/userroledata/{userroledata_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("userroledata_id") String userroledata_id) {
        UserRoleDataDTO userroledatadto = new UserRoleDataDTO();
		UserRoleData domain = new UserRoleData();
		userroledatadto.setUserRoleDataId(userroledata_id);
		domain.setUserRoleDataId(userroledata_id);
        Boolean rst = userroledataService.remove(domain.getUserRoleDataId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "判断是否有表格权限", tags = {"UserRoleData" },  notes = "判断是否有表格权限")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userroledata/{userroledata_id}/haspermissiongv")

    public ResponseEntity<UserRoleDataDTO> hasPermissionGV(@PathVariable("userroledata_id") String userroledata_id, @RequestBody UserRoleDataDTO userroledatadto) {
        UserRoleData userroledata = userroledatadto.toDO();
    	userroledata = userroledataService.hasPermissionGV(userroledata) ;
    	userroledatadto.fromDO(userroledata);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatadto);
    }

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

    public ResponseEntity<UserRoleDataDTO> create(@RequestBody UserRoleDataDTO userroledatadto) {
        UserRoleDataDTO dto = new UserRoleDataDTO();
        UserRoleData domain = userroledatadto.toDO();
		userroledataService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Update", tags = {"UserRoleData" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/userroledata/{userroledata_id}")

    public ResponseEntity<UserRoleDataDTO> update(@PathVariable("userroledata_id") String userroledata_id, @RequestBody UserRoleDataDTO userroledatadto) {
		UserRoleData domain = userroledatadto.toDO();
        domain.setUserRoleDataId(userroledata_id);
		userroledataService.update(domain);
		UserRoleDataDTO dto = new UserRoleDataDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "判断是否有表单权限", tags = {"UserRoleData" },  notes = "判断是否有表单权限")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userroledata/{userroledata_id}/haspermissionev")

    public ResponseEntity<UserRoleDataDTO> hasPermissionEV(@PathVariable("userroledata_id") String userroledata_id, @RequestBody UserRoleDataDTO userroledatadto) {
        UserRoleData userroledata = userroledatadto.toDO();
    	userroledata = userroledataService.hasPermissionEV(userroledata) ;
    	userroledatadto.fromDO(userroledata);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatadto);
    }

    @ApiOperation(value = "Save", tags = {"UserRoleData" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userroledata/{userroledata_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserRoleDataDTO userroledatadto) {
        UserRoleData userroledata = userroledatadto.toDO();
    	Boolean b = userroledataService.save(userroledata) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @ApiOperation(value = "数据同步", tags = {"UserRoleData" },  notes = "数据同步")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userroledata/{userroledata_id}/syncdata")

    public ResponseEntity<UserRoleDataDTO> syncData(@PathVariable("userroledata_id") String userroledata_id, @RequestBody UserRoleDataDTO userroledatadto) {
        UserRoleData userroledata = userroledatadto.toDO();
    	userroledata = userroledataService.syncData(userroledata) ;
    	userroledatadto.fromDO(userroledata);
        return ResponseEntity.status(HttpStatus.OK).body(userroledatadto);
    }

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


}
