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.SJZDService;
import com.sinosig.web.module.domain.SJZD;
import com.sinosig.web.module.filter.*;

@RestController
@RequestMapping(value = "")
public class SJZDController {
	@Autowired
    SJZDService sjzdservice;


	@RequestMapping(method = RequestMethod.PUT, value = "/sjzds/{sjzd_id}")
    public ResponseEntity<SJZD> update(@PathVariable("sjzd_id") String sjzd_id, @RequestBody SJZD sjzd) {
        SJZD sjzd2 = sjzdservice.update(sjzd_id, sjzd);
        return ResponseEntity.status(HttpStatus.OK).body(sjzd2);
    }

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

	@RequestMapping(method = RequestMethod.GET, value = "/sjzds/getdraft")
    public ResponseEntity<SJZD> getDraft() {
        //SJZD sjzd = sjzdservice.getDraft( sjzd_id);
        return ResponseEntity.status(HttpStatus.OK).body(new SJZD());
    }

	@RequestMapping(method = RequestMethod.GET, value = "/sjzds/{sjzd_id}")
    public ResponseEntity<SJZD> get(@PathVariable("sjzd_id") String sjzd_id) {
        SJZD sjzd = sjzdservice.get( sjzd_id);
        return ResponseEntity.status(HttpStatus.OK).body(sjzd);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/sjzds/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody SJZD sjzd) {
        boolean b = sjzdservice.checkKey(sjzd);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/sjzds")
    public ResponseEntity<SJZD> create(@RequestBody SJZD sjzd) {
        SJZD sjzd2 = sjzdservice.create(sjzd);
        return ResponseEntity.status(HttpStatus.OK).body(sjzd2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/sjzds/{sjzd_id}/save")
    public ResponseEntity<SJZD> save(@PathVariable("sjzd_id") String sjzd_id, @RequestBody SJZD sjzd) {
        SJZD sjzd2 = sjzdservice.save(sjzd_id, sjzd);
        return ResponseEntity.status(HttpStatus.OK).body(sjzd2);
    }

    @RequestMapping(method= RequestMethod.GET , value="/sjzds/fetchdefault")
	public ResponseEntity<List<SJZD>> fetchDefault(SJZDSearchContext searchContext,Pageable pageable) {
        
        searchContext.setPageable(pageable);
        Page<SJZD> page = sjzdservice.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());
    }


}
