package com.sinosig.service.web.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.transaction.annotation.Transactional;
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 com.sinosig.service.web.dto.YDJJDCDTO;
import com.sinosig.core.module2.domain.YDJJDC;
import com.sinosig.core.module2.service.IYDJJDCService;
import com.sinosig.util.SearchContext;
import com.sinosig.core.module2.filter.YDJJDCSearchContext;

@Api(tags = {"YDJJDC" })
@RestController
@RequestMapping("")
public class YDJJDCResource {

    @Autowired
    private IYDJJDCService ydjjdcService;

    public IYDJJDCService getYdjjdcService() {
        return this.ydjjdcService;
    }

    @ApiOperation(value = "Remove", tags = {"YDJJDC" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/web/ydjjdcs/{ydjjdc_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("ydjjdc_id") String ydjjdc_id) {
        YDJJDCDTO ydjjdcdto = new YDJJDCDTO();
		YDJJDC domain = new YDJJDC();
		ydjjdcdto.setYDJJDCId(ydjjdc_id);
		domain.setYDJJDCId(ydjjdc_id);

        Boolean rst = ydjjdcService.remove(domain.getYDJJDCId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "Update", tags = {"YDJJDC" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/web/ydjjdcs/{ydjjdc_id}")
    @Transactional
    public ResponseEntity<YDJJDCDTO> update(@PathVariable("ydjjdc_id") String ydjjdc_id, @RequestBody YDJJDCDTO ydjjdcdto) {
		YDJJDC domain = ydjjdcdto.toDO();
        domain.setYDJJDCId(ydjjdc_id);
		ydjjdcService.update(domain);
		YDJJDCDTO dto = new YDJJDCDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "GetDraft", tags = {"YDJJDC" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/web/ydjjdcs/{ydjjdc_id}/getdraft")
    @Transactional
    public ResponseEntity<YDJJDCDTO> getDraft(@PathVariable("ydjjdc_id") String ydjjdc_id, @RequestBody YDJJDCDTO ydjjdcdto) {
        YDJJDC ydjjdc = ydjjdcdto.toDO();
    	ydjjdc = ydjjdcService.getDraft(ydjjdc) ;
    	ydjjdcdto.fromDO(ydjjdc);
        return ResponseEntity.status(HttpStatus.OK).body(ydjjdcdto);
    }

    @ApiOperation(value = "Create", tags = {"YDJJDC" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/web/ydjjdcs")
    @Transactional
    public ResponseEntity<YDJJDCDTO> create(@RequestBody YDJJDCDTO ydjjdcdto) {
        YDJJDCDTO dto = new YDJJDCDTO();
        YDJJDC domain = ydjjdcdto.toDO();
		ydjjdcService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Save", tags = {"YDJJDC" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/web/ydjjdcs/save")
    public ResponseEntity<Boolean> save(@RequestBody YDJJDCDTO ydjjdcdto) {
        YDJJDC ydjjdc = ydjjdcdto.toDO();
    	Boolean b = ydjjdcService.save(ydjjdc) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @ApiOperation(value = "CheckKey", tags = {"YDJJDC" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/web/ydjjdcs/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody YDJJDCDTO ydjjdcdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "Get", tags = {"YDJJDC" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/web/ydjjdcs/{ydjjdc_id}")
    public ResponseEntity<YDJJDCDTO> get(@PathVariable("ydjjdc_id") String ydjjdc_id) {
        YDJJDCDTO dto = new YDJJDCDTO();
        YDJJDC domain =new YDJJDC();
        domain.setYDJJDCId(ydjjdc_id);
		ydjjdcService.get(domain);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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


}
