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.UserGroupDetailDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserGroupDetail;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserGroupDetailService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserGroupDetailSearchContext;

@Api(tags = {"UserGroupDetail" })
@RestController
@RequestMapping("")
public class UserGroupDetailResource {

    @Autowired
    private IUserGroupDetailService usergroupdetailService;

    public IUserGroupDetailService getUsergroupdetailService() {
        return this.usergroupdetailService;
    }

    @ApiOperation(value = "Get", tags = {"UserGroupDetail" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/usergroupdetails/{usergroupdetail_id}")
    public ResponseEntity<UserGroupDetailDTO> get(@PathVariable("usergroupdetail_id") String usergroupdetail_id) {
        UserGroupDetailDTO dto = new UserGroupDetailDTO();
        UserGroupDetail domain = usergroupdetailService.get(usergroupdetail_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "GetDraft", tags = {"UserGroupDetail" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/usergroupdetails/{usergroupdetail_id}/getdraft")

    public ResponseEntity<UserGroupDetailDTO> getDraft(@PathVariable("usergroupdetail_id") String usergroupdetail_id, @RequestBody UserGroupDetailDTO usergroupdetaildto) {
        UserGroupDetail usergroupdetail = usergroupdetaildto.toDO();
    	usergroupdetail = usergroupdetailService.getDraft(usergroupdetail) ;
    	usergroupdetaildto.fromDO(usergroupdetail);
        return ResponseEntity.status(HttpStatus.OK).body(usergroupdetaildto);
    }

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

    public ResponseEntity<UserGroupDetailDTO> create(@RequestBody UserGroupDetailDTO usergroupdetaildto) {
        UserGroupDetailDTO dto = new UserGroupDetailDTO();
        UserGroupDetail domain = usergroupdetaildto.toDO();
		usergroupdetailService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Save", tags = {"UserGroupDetail" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/usergroupdetails/{usergroupdetail_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserGroupDetailDTO usergroupdetaildto) {
        UserGroupDetail usergroupdetail = usergroupdetaildto.toDO();
    	Boolean b = usergroupdetailService.save(usergroupdetail) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

    @ApiOperation(value = "Update", tags = {"UserGroupDetail" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/usergroupdetails/{usergroupdetail_id}")

    public ResponseEntity<UserGroupDetailDTO> update(@PathVariable("usergroupdetail_id") String usergroupdetail_id, @RequestBody UserGroupDetailDTO usergroupdetaildto) {
		UserGroupDetail domain = usergroupdetaildto.toDO();
        domain.setUserGroupDetailId(usergroupdetail_id);
		usergroupdetailService.update(domain);
		UserGroupDetailDTO dto = new UserGroupDetailDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Remove", tags = {"UserGroupDetail" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/usergroupdetails/{usergroupdetail_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("usergroupdetail_id") String usergroupdetail_id) {
        UserGroupDetailDTO usergroupdetaildto = new UserGroupDetailDTO();
		UserGroupDetail domain = new UserGroupDetail();
		usergroupdetaildto.setUserGroupDetailId(usergroupdetail_id);
		domain.setUserGroupDetailId(usergroupdetail_id);
        Boolean rst = usergroupdetailService.remove(domain.getUserGroupDetailId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

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


}
