package com.yihu.hos.standard.controller;

import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.DictionaryResult;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import com.yihu.hos.standard.service.standard.StandardVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * Created by LLH on 2016/1/6.
 */
@RestController("StandardVersionController")
@RequestMapping("/standardCenter")
//@Api(protocols = "https", value = "StandardVersionController", description = "标准版本管理", tags = {"标准版本"})
public class StandardVersionController extends BaseController {
    @Resource(name = StandardVersionService.BEAN_ID)
    private StandardVersionService standardVersionService;
    /**
     * 根据版本id获取标准版本信息（get）
     * @param versionId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getForVersionId")
    @ApiOperation(value = "获取标准版本", response = Result.class, produces = "application/json", notes = "获取标准版本")
    public Result getForVersionId(
            @ApiParam(name = "versionId", value = "标准版本ID")
            @RequestParam(value = "versionId") Integer versionId) {
        return standardVersionService.get(versionId);
    }

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

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

    /**
     * 发布版本（post）
     * @param versionId
     * @param publisher
     * @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") String publisher) throws Exception {
        return standardVersionService.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,
            @ApiParam(name = "version", value = "标准版本值")
            @RequestParam(value = "version") String version) throws Exception {
        return standardVersionService.delete(versionId,version);
    }

    /**
     * 获取基础版本名称列表
     * @param standardId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getVersions")
    @ApiOperation(value = "获取标准版本列表", response = DictionaryResult.class, produces = "application/json", notes = "获取标准版本列表")
    public DictionaryResult getVersions(
            @ApiParam(name = "standardId", value = "标准ID")
            @RequestParam(value = "standardId") Integer standardId,
            @ApiParam(name = "condition", value = "Must be Json")
            @RequestParam(value = "condition", required = false) String condition,
            @ApiParam(name = "order", value = "Must be Json")
            @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 standardVersionService.getDetailResultModelList(standardId, condition, order, rows, page);
    }
    /**
     * 根据类型文件上传保存对应的数据
     *
     * @param type
     * @param file
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/importFile", method = RequestMethod.POST)
    @ApiOperation(value = "根据type导入数据接口", response = Object.class, produces = "application/json", notes = "导入数据接口")
    public Result importFile(@ApiParam(value = "类型（1:数据集 2:字典 3:机构数据 4：机构字典）", required = true) @RequestParam("type") String type,
                             @ApiParam(value = "项目版本", required = true) @RequestParam("version") String version,
                             @ApiParam(value = "项目版本", required = true) @RequestParam("standardId") Integer standardId,
                             @ApiParam(value = "文件", required = true, allowMultiple = true) @RequestParam("file") MultipartFile file) throws Exception {
        ActionResult actionResult = new ActionResult();
        try {
            String id = "";
            //根據类别判断上传的文件1:数据集 2:字典
            if("1".equals(type)){
                version = standardVersionService.saveDatasetExcel(file, version,standardId);
            }else {
                version = standardVersionService.saveDictExcel(file, version,standardId);
            }
            //解析excel文件 保存
            actionResult.setData("{\"version\":\"" + version + "\"}");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return actionResult;
    }

    @ResponseBody
    @RequestMapping(value = "/isUpdateDatasetAndDict", method = RequestMethod.POST)
    @ApiOperation(value = "项目列表", response = Object.class, produces = "application/json", notes = "项目列表")
    public Result isUpdateDatasetAndDict(
            @RequestParam(required = true) String version) throws Exception {
        ActionResult actionResult = new ActionResult();
        try {
            actionResult.setData(standardVersionService.isUpdateDatasetAndDict(version));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return actionResult;
    }

    /**
     * 根据类型和主键删除数据
     *
     * @param type
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/deleteByType", method = RequestMethod.POST)
    @ApiOperation(value = "文件上传成功后的删除", response = Object.class, produces = "application/json", notes = "文件上传成功后的删除")
    public Result deleteByType(
            @ApiParam(value = "类型（1:数据集 2:字典 ）", required = true) @RequestParam("type") String type,
            @ApiParam(value = "主键(上传成功后返回的ID)", required = true) @RequestParam("version") String version) throws Exception {
        ActionResult actionResult = new ActionResult();
        try {
            //根據类别判断上传的文件1:数据集 2:字典 删除数据
            switch (type) {
                case "1": {
                    standardVersionService.deleteDataset(version);
                    break;
                }
                case "2": {
                    standardVersionService.deleteDict(version);
                    break;
                }
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return actionResult;
    }

}

