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.StdDictService;
import com.yihu.hos.standard.service.standard.StdMetadataService;
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("StdMetadataController")
@RequestMapping("/standardCenter")
//@Api(protocols = "https", value = "StdMetadataController", description = "标准数据元管理", tags = {"标准数据元"})
public class StdMetadataController extends BaseController {
    @Resource(name = StdMetadataService.BEAN_ID)
    StdMetadataService stdMetadataService;
    @Resource(name = StdDictService.BEAN_ID)
    StdDictService stdDictService;
    /**
     * 查询数据元列表信息（get）
     * @param stdVersion
     * @param datasetId
     * @param condition
     * @param order
     * @param rows
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping("/searchMetadataList")
    @ApiOperation(value = "获取数据元列表", response = DetailModelResult.class, produces = "application/json", notes = "获取数据元列表")
    public DetailModelResult getMetadataList(
            @ApiParam(name = "stdVersion", value = "标准版本号")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "datasetId", value = "数据集ID")
            @RequestParam(value = "datasetId") Integer datasetId,
            @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 stdMetadataService.getDetailModelList(stdVersion, datasetId, condition, order, rows, page);
    }

    @RequestMapping("/getForMetadataId")
    @ApiOperation(value = "获取数据元", response = Result.class, produces = "application/json", notes = "获取数据元")
    public Result getForMetadataId(
            @ApiParam(name = "stdVersion", value = "stdVersion")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "metadataId", value = "数据元ID")
            @RequestParam(value = "metadataId") Integer metadataId) {
        return stdMetadataService.get(stdVersion, metadataId);
    }

    @RequestMapping("/delMetaData")
    @ApiOperation(value = "删除数据元", response = Result.class, produces = "application/json", notes = "删除数据元")
    public Result delMetaData(
            @ApiParam(name = "stdVersion", value = "stdVersion")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "metadataId", value = "数据元ID")
            @RequestParam(value = "metadataId") Integer metadataId) {
        return stdMetadataService.delete(stdVersion, metadataId);
    }

    @RequestMapping("/updateMetadata")
    @ApiOperation(value = "修改数据元", response = Result.class, produces = "application/json", notes = "修改数据元")
    public Result updateMetadata(
            @ApiParam(name = "stdVersion", value = "stdVersion")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "metadata", value = "数据元")
            @RequestParam(value = "metadata") String metadata) {
        return stdMetadataService.update(stdVersion, metadata);
    }

    @RequestMapping("/addMetadata")
    @ApiOperation(value = "保存数据元", response = Result.class, produces = "application/json", notes = "保存数据元")
    public Result addMetadata(
            @ApiParam(name = "stdVersion", value = "stdVersion")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "metadata", value = "数据元")
            @RequestParam(value = "metadata") String metadata) {

        return stdMetadataService.add(stdVersion, metadata);
    }

    @RequestMapping(value="/getMetadataCodeValues")
    @ApiOperation(value = "获取数据元下拉框信息", response = DetailModelResult.class, produces = "application/json", notes = "获取机构数据元下拉框信息")
    public DetailModelResult getSelectList(
            @ApiParam(name = "stdVersion", value = "标准版本号")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "datasetId", value = "数据集ID")
            @RequestParam(value = "datasetId") Integer datasetId,
            @ApiParam(name = "q", value = "数据元名称或编码", required = false)
            @RequestParam(value = "q", required = false) String q,
            @ApiParam(name = "initMetadataNm", value = "初始化数据元名称或编码", required = false)
            @RequestParam(value = "initMetadataNm", required = false) String initMetadataNm,
            @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>();
        if (StringUtil.isEmpty(initMetadataNm)) {

            initMetadataNm = "";
        }
        if(StringUtil.isEmpty(q)){
            condition.put("name", initMetadataNm);
        }else{
            condition.put("name", q);
        }
        ObjectMapper mapper = new ObjectMapper();
        String conditionStr = null;
        try {
            conditionStr = mapper.writeValueAsString(condition);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return stdMetadataService.getMetadataSelectList(stdVersion, datasetId, conditionStr, order, rows, 0);
    }
}
