package cn.ibizlab.api.rest;

import cn.ibizlab.core.data.dto.BaseData;
import cn.ibizlab.core.data.dto.RequestData;
import cn.ibizlab.core.data.dto.ResponseData;
import cn.ibizlab.core.data.service.IDataService;
import cn.ibizlab.util.filter.SearchContextBase;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = {"数据" })
@RestController("api-data")
@RequestMapping("")
public class DataResource
{

    @Autowired
    public IDataService dataService;

	@ApiOperation(value = "保存数据", tags = {"数据" },  notes = "保存数据")
	@RequestMapping(method = RequestMethod.POST, value = {"/{system}/{entity}/{method}","/{system}/{scope}/{entity}/{method}"})
	public ResponseEntity<BaseData> call(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "scope",required = false) String scope,@PathVariable(name = "entity",required = true) String entity,@PathVariable(name = "method",required = true) String method,@RequestParam(name = "datasource",required = false) String datasource,@RequestBody BaseData baseData) {
		ResponseData<BaseData> responseData=dataService.call(system,scope,entity,method,datasource,new RequestData().setBody(baseData));
		return ResponseEntity.status(HttpStatus.OK).body(responseData.getBody());
	}



    @ApiOperation(value = "新建数据", tags = {"数据" },  notes = "新建数据")
	@RequestMapping(method = RequestMethod.POST, value = "/{system}/{entity}")
    public ResponseEntity<BaseData> create(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@RequestParam(name = "datasource",required = false) String datasource, @Validated @RequestBody BaseData baseData) {
		dataService.create(system,entity,datasource,baseData);
		return ResponseEntity.status(HttpStatus.OK).body(baseData);
    }

    @ApiOperation(value = "批量新建数据", tags = {"数据" },  notes = "批量新建数据")
	@RequestMapping(method = RequestMethod.POST, value = "/{system}/{entity}/batch")
    public ResponseEntity<Boolean> createBatch(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@RequestParam(name = "datasource",required = false) String datasource, @RequestBody List<BaseData> baseDatas) {
        dataService.createBatch(system,entity,datasource,baseDatas);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "获取数据", tags = {"数据" },  notes = "获取数据")
	@RequestMapping(method = RequestMethod.GET, value = "/{system}/{entity}/{key}")
    public ResponseEntity<BaseData> get(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@RequestParam(name = "datasource",required = false) String datasource, @PathVariable(name = "key",required = true) String key) {
        BaseData baseData = dataService.get(system,entity,datasource,key);
        return ResponseEntity.status(HttpStatus.OK).body(baseData);
    }

    @ApiOperation(value = "删除数据", tags = {"数据" },  notes = "删除数据")
	@RequestMapping(method = RequestMethod.DELETE, value = "/{system}/{entity}/{key}")
    public ResponseEntity<Boolean> remove(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@RequestParam(name = "datasource",required = false) String datasource, @PathVariable(name = "key",required = true) String key) {
         return ResponseEntity.status(HttpStatus.OK).body(dataService.remove(system,entity,datasource,key));
    }

    @ApiOperation(value = "批量删除数据", tags = {"数据" },  notes = "批量删除数据")
	@RequestMapping(method = RequestMethod.DELETE, value = "/{system}/{entity}/batch")
    public ResponseEntity<Boolean> removeBatch(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@RequestParam(name = "datasource",required = false) String datasource, @RequestBody List ids) {
        dataService.removeBatch(system,entity,datasource,ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "更新数据", tags = {"数据" },  notes = "更新数据")
	@RequestMapping(method = RequestMethod.PUT, value = "/{system}/{entity}/{key}")
    public ResponseEntity<BaseData> update(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@RequestParam(name = "datasource",required = false) String datasource, @PathVariable(name = "key",required = true) String key, @RequestBody BaseData baseData) {
		dataService.update(system,entity,datasource,baseData.setKey(key));
        return ResponseEntity.status(HttpStatus.OK).body(baseData);
    }

    @ApiOperation(value = "批量更新数据", tags = {"数据" },  notes = "批量更新数据")
	@RequestMapping(method = RequestMethod.PUT, value = "/{system}/{entity}/batch")
    public ResponseEntity<Boolean> updateBatch(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@RequestParam(name = "datasource",required = false) String datasource, @RequestBody List<BaseData> baseDatas) {
        dataService.updateBatch(system,entity,datasource,baseDatas);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "检查数据", tags = {"数据" },  notes = "检查数据")
	@RequestMapping(method = RequestMethod.POST, value = "/{system}/{entity}/checkkey")
    public ResponseEntity<Boolean> checkKey(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@RequestParam(name = "datasource",required = false) String datasource, @RequestBody BaseData baseData) {
        return  ResponseEntity.status(HttpStatus.OK).body(dataService.checkKey(system,entity,datasource,baseData));
    }

    @ApiOperation(value = "获取数据草稿", tags = {"数据" },  notes = "获取数据草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/{system}/{entity}/getdraft")
    public ResponseEntity<BaseData> getDraft(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@RequestParam(name = "datasource",required = false) String datasource,  @RequestBody(required = false) BaseData baseData) {
		dataService.getDraft(system,entity,datasource,baseData);
        return ResponseEntity.status(HttpStatus.OK).body(baseData);
    }

    @ApiOperation(value = "保存数据", tags = {"数据" },  notes = "保存数据")
	@RequestMapping(method = RequestMethod.POST, value = "/{system}/{entity}/save")
    public ResponseEntity<BaseData> save(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@RequestParam(name = "datasource",required = false) String datasource,@RequestBody BaseData baseData) {
        dataService.save(system,entity,datasource,baseData);
        return ResponseEntity.status(HttpStatus.OK).body(baseData);
    }

    @ApiOperation(value = "批量保存数据", tags = {"数据" },  notes = "批量保存数据")
	@RequestMapping(method = RequestMethod.POST, value = "/{system}/{entity}/savebatch")
    public ResponseEntity<Boolean> saveBatch(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@RequestParam(name = "datasource",required = false) String datasource,@RequestBody List<BaseData> baseDatas) {
        dataService.saveBatch(system,entity,datasource,baseDatas);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

	@ApiOperation(value = "获取数据集", tags = {"数据" } ,notes = "获取数据集")
    @RequestMapping(method= RequestMethod.GET , value="/{system}/{entity}/fetch{dataset}")
	public ResponseEntity<List<BaseData>> fetchDefault(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@PathVariable(name = "dataset",required = true) String dataset,@RequestParam(name = "datasource",required = false) String datasource,
			SearchContextBase context) {
        Page<BaseData> baseDatas = dataService.fetch(system,entity,dataset,datasource,context) ;
        List<BaseData> list = baseDatas.getContent();
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(baseDatas.getTotalElements()))
                .body(list);
	}

	@ApiOperation(value = "查询数据集", tags = {"数据" } ,notes = "查询数据集")
    @RequestMapping(method= RequestMethod.POST , value="/{system}/{entity}/search{dataset}")
	public ResponseEntity<Page<BaseData>> searchDefault(@PathVariable(name = "system",required = true) String system,@PathVariable(name = "entity",required = true) String entity,@PathVariable(name = "dataset",required = true) String dataset,@RequestParam(name = "datasource",required = false) String datasource,@RequestBody SearchContextBase context) {
		Page<BaseData> baseDatas = dataService.fetch(system,entity,dataset,datasource,context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(baseDatas.getContent(), context.getPageable(), baseDatas.getTotalElements()));
	}



}

