package com.yihu.hos.standard.controller;

import com.yihu.hos.standard.service.adapter.AdapterSchemeVersionService;
import com.yihu.hos.web.framework.model.DataGridResult;
import com.yihu.hos.web.framework.model.DictionaryResult;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.model.TreeResult;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController("SchemeVersionController")
@RequestMapping("/adapterCenter")
//@Api(protocols = "https", value = "SchemeVersionController", description = "适配版本管理", tags = {"适配版本"})
public class SchemeVersionController extends BaseController {
    @Resource(name = AdapterSchemeVersionService.BEAN_ID)
    private AdapterSchemeVersionService adapterSchemeVersion;
    /**
     * 根据版本id获取适配方案信息（get）
     * @param versionId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getForVersionId")
    @ApiOperation(value = "获取适配版本", response = Result.class, produces = "application/json", notes = "获取适配版本")
    public Result get(
            @ApiParam(name = "versionId", value = "适配版本ID")
            @RequestParam(value = "versionId") Integer versionId) {
        return adapterSchemeVersion.get(versionId);
    }

    /**
     * 新增版本（post）
     * @param version
     * @return
     * @throws Exception
     */
    @RequestMapping("/addVersion")
    @ApiOperation(value = "保存适配版本", response = Result.class, produces = "application/json", notes = "保存适配版本")
    public Result add(
            @ApiParam(name = "version", value = "适配版本信息")
            @RequestParam(value = "version") String version) {
        return adapterSchemeVersion.add(version);
    }

    /**
     * 修改版本（put）
     * @param version
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateVersion")
    @ApiOperation(value = "修改适配版本", response = Result.class, produces = "application/json", notes = "修改适配版本")
    public Result modify(
            @ApiParam(name = "version", value = "标准版本")
            @RequestParam(value = "version") String version) {
        return adapterSchemeVersion.update(version);
    }

    /**
     * 发布版本（put）
     * @param versionId
     * @return
     * @throws Exception
     */
    @RequestMapping("/publishVersion")
    @ApiOperation(value = "发布适配版本", response = Result.class, produces = "application/json", notes = "发布适配版本")
    public Result publish(
            @ApiParam(name = "versionId", value = "适配版本ID")
            @RequestParam(value = "versionId") Integer versionId,
            @ApiParam(name = "publisher", value = "发布者")
            @RequestParam(value = "publisher", required = false) String publisher) {
        return adapterSchemeVersion.publish(versionId, publisher);
    }

    /**
     * 删除版本（put）
     * @param versionId
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteVersion")
    @ApiOperation(value = "删除版本", response = Result.class, produces = "application/json", notes = "删除版本")
    public Result delete(
            @ApiParam(name = "versionId", value = "标准版本ID")
            @RequestParam(value = "versionId") Integer versionId) {
        return adapterSchemeVersion.delete(versionId);
    }

    @RequestMapping("getVersion")
    @ResponseBody
    public DataGridResult getVersion() {
        try {
            return adapterSchemeVersion.getVersion();
        } catch (Exception ex) {
            return null;
        }
    }

    @RequestMapping("getVersionTree")
    @ResponseBody
    public List<TreeResult> getVersionTree() {
        try {
            return adapterSchemeVersion.getVersionTree();
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 获取基础版本列表
     * @param schemeId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getVersions")
    @ApiOperation(value = "获取适配方案版本列表", response = DictionaryResult.class, produces = "application/json", notes = "获取标准版本列表")
    public DictionaryResult getList(
            @ApiParam(name = "schemeId", value = "适配方案ID")
            @RequestParam(value = "schemeId") Integer schemeId,
            @ApiParam(name = "condition", value = "Search param,Must be json")
            @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 adapterSchemeVersion.getDictionaryResult(schemeId, condition, order, rows, page);
    }
}
