package com.sinosig.web.module.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import com.sinosig.web.module.service.TBYDKService;
import com.sinosig.web.module.domain.TBYDK;
import com.sinosig.web.module.filter.*;

@RestController
@RequestMapping(value = "")
public class TBYDKController {
	@Autowired
    TBYDKService tbydkservice;


	@RequestMapping(method = RequestMethod.POST, value = "/tbydks")
    public ResponseEntity<TBYDK> create(@RequestBody TBYDK tbydk) {
        TBYDK tbydk2 = tbydkservice.create(tbydk);
        return ResponseEntity.status(HttpStatus.OK).body(tbydk2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/tbydks/{tbydk_id}")
    public ResponseEntity<TBYDK> get(@PathVariable("tbydk_id") String tbydk_id) {
        TBYDK tbydk = tbydkservice.get( tbydk_id);
        return ResponseEntity.status(HttpStatus.OK).body(tbydk);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/tbydks/{tbydk_id}")
    public ResponseEntity<TBYDK> update(@PathVariable("tbydk_id") String tbydk_id, @RequestBody TBYDK tbydk) {
        TBYDK tbydk2 = tbydkservice.update(tbydk_id, tbydk);
        return ResponseEntity.status(HttpStatus.OK).body(tbydk2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/tbydks/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody TBYDK tbydk) {
        boolean b = tbydkservice.checkKey(tbydk);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/tbydks/{tbydk_id}/save")
    public ResponseEntity<TBYDK> save(@PathVariable("tbydk_id") String tbydk_id, @RequestBody TBYDK tbydk) {
        TBYDK tbydk2 = tbydkservice.save(tbydk_id, tbydk);
        return ResponseEntity.status(HttpStatus.OK).body(tbydk2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/tbydks/getdraft")
    public ResponseEntity<TBYDK> getDraft() {
        //TBYDK tbydk = tbydkservice.getDraft( tbydk_id);
        return ResponseEntity.status(HttpStatus.OK).body(new TBYDK());
    }

	@RequestMapping(method = RequestMethod.DELETE, value = "/tbydks/{tbydk_id}")
    public ResponseEntity<Boolean> remove(@PathVariable("tbydk_id") String tbydk_id) {
        boolean b = tbydkservice.remove( tbydk_id);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @RequestMapping(method= RequestMethod.GET , value="/tbydks/fetchdefault")
	public ResponseEntity<List<TBYDK>> fetchDefault(TBYDKSearchContext searchContext,Pageable pageable) {
        
        searchContext.setPageable(pageable);
        Page<TBYDK> page = tbydkservice.fetchDefault(searchContext);
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(pageable.getPageNumber()))
        		.header("x-per-page", String.valueOf(pageable.getPageSize()))
        		.header("x-total", String.valueOf(page.getTotalElements()))
        		.body(page.getContent());
    }

    @RequestMapping(method= RequestMethod.GET , value="/tbydks/fetchjqx")
	public ResponseEntity<List<TBYDK>> fetchJQX(TBYDKSearchContext searchContext,Pageable pageable) {
        
        searchContext.setPageable(pageable);
        Page<TBYDK> page = tbydkservice.fetchJQX(searchContext);
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(pageable.getPageNumber()))
        		.header("x-per-page", String.valueOf(pageable.getPageSize()))
        		.header("x-total", String.valueOf(page.getTotalElements()))
        		.body(page.getContent());
    }

    @RequestMapping(method= RequestMethod.GET , value="/tbydks/fetchsyx")
	public ResponseEntity<List<TBYDK>> fetchSYX(TBYDKSearchContext searchContext,Pageable pageable) {
        
        searchContext.setPageable(pageable);
        Page<TBYDK> page = tbydkservice.fetchSYX(searchContext);
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(pageable.getPageNumber()))
        		.header("x-per-page", String.valueOf(pageable.getPageSize()))
        		.header("x-total", String.valueOf(page.getTotalElements()))
        		.body(page.getContent());
    }


}
