package com.yihu.hos.standard.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.standard.service.standard.StdDatasetService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by LLH on 2016/1/6.
 */
@RestController("StdDatasetController")
@RequestMapping("/standardCenter")
//@Api(protocols = "https", value = "StdDatasetController", description = "标准数据集管理", tags = {"标准数据集"})
public class StdDatasetController extends BaseController {
    @Resource(name = StdDatasetService.BEAN_ID)
    private StdDatasetService stdDatasetService;
    /**
     * 查询数据集列表信息（get）
     * @param stdVersion
     * @param condition
     * @param order
     * @param rows
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping("/searchDatasetList")
    @ApiOperation(value = "获取数据集列表", response = DetailModelResult.class, produces = "application/json", notes = "获取数据集列表")
    public DetailModelResult searchDatasetList(
            @ApiParam(name = "stdVersion", value = "标准版本号")
            @RequestParam(value = "stdVersion") String stdVersion,
            @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 stdDatasetService.getDetailModelList(stdVersion, condition, order, rows, page);
    }

    @RequestMapping("/getForDatasetId")
    @ApiOperation(value = "获取数据集", response = Result.class, produces = "application/json", notes = "获取数据集")
    public Result getForDatasetId(
            @ApiParam(name = "stdVersion", value = "标准版本号")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "datasetId", value = "数据集ID")
            @RequestParam(value = "datasetId") Integer datasetId) {
        return stdDatasetService.get(stdVersion, datasetId);
    }

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

    @RequestMapping("/updateDataset")
    @ApiOperation(value = "修改数据集", response = Result.class, produces = "application/json", notes = "修改数据集")
    public Result updateDataset(
            @ApiParam(name = "stdVersion", value = "标准版本号")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "dataset", value = "数据集")
            @RequestParam(value = "dataset") String dataset) {
        return stdDatasetService.update(stdVersion, dataset);
    }

    @RequestMapping("/addDataset")
    @ApiOperation(value = "保存数据集", response = Result.class, produces = "application/json", notes = "保存数据集")
    public Result add(
            @ApiParam(name = "stdVersion", value = "标准版本号")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "dataset", value = "数据集")
            @RequestParam(value = "dataset") String dataset) {
        return stdDatasetService.add(stdVersion, dataset);
    }

    @RequestMapping(value="/getDatasetCodeValues")
    @ApiOperation(value = "获取数据集下拉框信息", response = DetailModelResult.class, produces = "application/json", notes = "获取机构数据元下拉框信息")
    public DetailModelResult getSelectList(
            @ApiParam(name = "stdVersion", value = "标准版本号")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "q", value = "数据集名称或编码", required = false)
            @RequestParam(value = "q", required = false) String q,
            @ApiParam(name = "initVal", value = "初始化数据集名称或编码", required = false)
            @RequestParam(value = "initVal", required = false) String initVal,
            @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) {
        Map<String,String> condition = new HashMap<String,String>();
        ObjectMapper mapper = new ObjectMapper();
        if(!StringUtil.isEmpty(q)){
            condition.put("name", q);
            rows = 10;
        }
        else if(!StringUtil.isEmpty(initVal)){
            condition.put("initVal", initVal);//默认值
        }
        String conditionStr = null;
        try {
             conditionStr = mapper.writeValueAsString(condition);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return stdDatasetService.getDatasetSelectList(stdVersion, conditionStr, order, rows, 0);
    }
}
