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.model.standard.StdDictionaryModel;
import com.yihu.hos.standard.service.standard.StdDictService;
import com.yihu.hos.web.framework.model.DetailModelResult;
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 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.List;
import java.util.Map;

/**
 * Created by LLH on 2016/1/6.
 */
@RestController("StdDictController")
@RequestMapping("/standardCenter")
//@Api(protocols = "https", value = "StdDictController", description = "标准字典管理", tags = {"标准字典"})
public class StdDictController extends BaseController {
    @Resource(name = StdDictService.BEAN_ID)
    StdDictService stdDictService;
    /**
     * 查询字典列表信息（get）
     * @param stdVersion
     * @param condition
     * @param order
     * @param rows
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping("/searchDictList")
    @ApiOperation(value = "获取字典列表", response = DetailModelResult.class, produces = "application/json", notes = "获取字典列表")
    public DetailModelResult getDictList(
            @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 stdDictService.getDetailModelList(stdVersion, condition, order, rows, page);
    }

    @RequestMapping("/getForDictId")
    @ApiOperation(value = "获取字典", response = Result.class, produces = "application/json", notes = "获取字典")
    public Result get(
            @ApiParam(name = "stdVersion", value = "stdVersion")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "dictId", value = "字典ID")
            @RequestParam(value = "dictId") Integer dictId) {
        return stdDictService.get(stdVersion, dictId);
    }

    @RequestMapping("/delDict")
    @ApiOperation(value = "删除字典", response = Result.class, produces = "application/json", notes = "删除字典")
    public Result delete(
            @ApiParam(name = "stdVersion", value = "stdVersion")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "dictId", value = "字典ID")
            @RequestParam(value = "dictId") Integer dictId) {
        return stdDictService.delete(stdVersion, dictId);
    }
    
    @RequestMapping("/updateDict")
    @ApiOperation(value = "修改字典", response = Result.class, produces = "application/json", notes = "修改字典")
    public Result modify(
            @ApiParam(name = "stdVersion", value = "stdVersion")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "dict", value = "字典")
            @RequestParam(value = "dict") String dict) {
        return stdDictService.update(stdVersion, dict);
    }

    @RequestMapping("/addDict")
    @ApiOperation(value = "保存字典", response = Result.class, produces = "application/json", notes = "保存字典")
    public Result addDict(
            @ApiParam(name = "stdVersion", value = "stdVersion")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "dict", value = "字典")
            @RequestParam(value = "dict") String dict) {
        return stdDictService.add(stdVersion, dict);
    }

    @RequestMapping(value="/getDictCodeValues")
    @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>();
        if(!StringUtil.isEmpty(q)){
            condition.put("name", q);
        }
        else if(!StringUtil.isEmpty(initVal)){
            condition.put("initVal", initVal);//默认值
        }

        ObjectMapper mapper = new ObjectMapper();
        String conditionStr = null;
        try {
            conditionStr = mapper.writeValueAsString(condition);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        List<StdDictionaryModel> dictList = stdDictService.getDictList(StdDictionaryModel.class, stdVersion, conditionStr, order, rows, 0);
        return stdDictService.getDictSelectList(dictList);
    }

    /**
     * 获取对应字典/参考字典列表
     * @param stdVersion
     * @return
     * @throws Exception
     */
    @RequestMapping("/getDicts")
    @ApiOperation(value = "获取字典列表", response = DictionaryResult.class, produces = "application/json", notes = "获取字典列表")
    public DictionaryResult getList(
            @ApiParam(name = "stdVersion", value = "stdVersion")
            @RequestParam(value = "stdVersion") String stdVersion,
            @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 stdDictService.getDictionaryList(stdVersion, condition, order, rows, page);
    }
}
