package com.yihu.hos.standard.controller;

import com.yihu.hos.standard.service.adapter.AdapterDatasetService;
import com.yihu.hos.web.framework.model.DetailModelResult;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;

/**
 * Created by LLH on 2016/1/6.
 */
@RestController("AdapterDatasetController")
@RequestMapping("/adapterCenter")
//@Api(protocols = "https", value = "AdapterDatasetController", description = "适配数据集管理", tags = {"适配数据集"})
public class AdapterDatasetController extends BaseController {

    @Resource(name = AdapterDatasetService.BEAN_ID)
    private AdapterDatasetService datasetService;
    /**
     * 查询数据集列表信息（get）
     * @param adapterVersion
     * @param condition
     * @param order
     * @param rows
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/searchDatasetList")
    @ApiOperation(value = "获取数据集列表", response = DetailModelResult.class, produces = "application/json", notes = "获取数据集列表")
    public DetailModelResult getDatasetList(
            @ApiParam(name = "adapterVersion", value = "适配方案版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "condition", value = "查询条件：编码或名称")
            @RequestParam(value = "condition", required = false) String condition,
            @ApiParam(name = "order", value = "Order param,Must be json.ascending:asc,descending:desc")
            @RequestParam(value = "order", required = false) String order,
            @ApiParam(name = "rows", value = "Limit the size of result set. Must be an integer")
            @RequestParam(value = "rows", required = false) Integer rows,
            @ApiParam(name = "page", value = "Start position of result set. Must be an integer")
            @RequestParam(value = "page", required = false) Integer page) {

        return datasetService.getDetailModelResult(adapterVersion, condition, order, rows, page);
    }

    @RequestMapping("/getForDatasetId")
    @ApiOperation(value = "获取适配数据集", response = Result.class, produces = "application/json", notes = "获取适配数据集")
    public Result getForDatasetId(
            @ApiParam(name = "adapterVersion", value = "适配版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "datasetId", value = "适配数据集ID")
            @RequestParam(value = "datasetId") Integer datasetId) {

        return datasetService.get(adapterVersion, datasetId);
    }

    @RequestMapping(value = "/delDataset")
    @ApiOperation(value = "删除数据集", response = Result.class, produces = "application/json", notes = "删除适配数据集")
    public Result delDataset(
            @ApiParam(name = "adapterVersion", value = "适配版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "datasetId", value = "适配数据集ID")
            @RequestParam(value = "datasetId") Integer datasetId) {
       return datasetService.delete(adapterVersion, datasetId);
    }

    @RequestMapping(value = "/updateDataset")
    @ApiOperation(value = "修改适配数据集", response = Result.class, produces = "application/json", notes = "修改适配数据集")
    public Result modify(
            @ApiParam(name = "adapterVersion", value = "适配版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "dataset", value = "数据集,JSON")
            @RequestParam(value = "dataset") String dataset) {
        return datasetService.modify(adapterVersion, dataset);
    }

    @RequestMapping(value = "/addDataset")
    @ApiOperation(value = "保存适配数据集", response = Result.class, produces = "application/json", notes = "保存数据集")
    public Result add(
            @ApiParam(name = "adapterVersion", value = "适配版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "dataset", value = "数据集信息")
            @RequestParam(value = "dataset") String dataset) {
        return datasetService.add(adapterVersion, dataset);
    }

    @RequestMapping(value = "/getAdapterDataset")
    @ApiOperation(value = "获取定制数据集列表", response = DetailModelResult.class, responseContainer = "List",produces = "application/json", notes = "获取数据集列表")
    public DetailModelResult getDatasetCustomize(
            @ApiParam(name = "adapterVersion", value = "适配版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "stdVersion", value = "标准版本号")
            @RequestParam(value = "stdVersion") String stdVersion) {
        return datasetService.getDatasetCustomize(adapterVersion, stdVersion);
    }

    @RequestMapping(value = "/saveAdapterDataset", method = RequestMethod.POST)
    @ApiOperation(value = "定制适配数据集", response = Result.class, responseContainer = "List",produces = "application/json", notes = "获取数据集列表")
    public Result adapterDataSet(
            @ApiParam(name = "schemeId", value = "适配方案ID")
            @RequestParam(value = "schemeId") Integer schemeId,
            @ApiParam(name = "adapterVersion", value = "适配版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "stdVersion", value = "标准版本号")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "datasetIdStr", value = "适配数据集信息")
            @RequestParam(value = "datasetIdStr") String datasetIdStr,
            @ApiParam(name = "metaDataIdStr", value = "适配数据元信息")
            @RequestParam(value = "metaDataIdStr") String metaDataIdStr) throws UnsupportedEncodingException {
        return datasetService.updateCustomize(schemeId, adapterVersion, stdVersion, datasetIdStr,metaDataIdStr);
    }
}
