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.StdDictEntryService;
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("StdDictEntryController")
@RequestMapping("/standardCenter")
//@Api(protocols = "https", value = "StdDictEntryController", description = "标准字典项管理", tags = {"标准字典项"})
public class StdDictEntryController extends BaseController {
    @Resource(name = StdDictEntryService.BEAN_ID)
    StdDictEntryService stdDictEntryService;
    /**
     * 查询字典项列表信息（get）
     * @param stdVersion
     * @param dictId
     * @param condition
     * @param order
     * @param rows
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping("/searchDictEntryList")
    @ApiOperation(value = "获取字典项列表", response = DetailModelResult.class, responseContainer = "List",produces = "application/json", notes = "获取字典项列表")
    public DetailModelResult getDictEntryList(
            @ApiParam(name = "stdVersion", value = "标准版本号")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "dictId", value = "字典ID")
            @RequestParam(value = "dictId") Integer dictId,
            @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 stdDictEntryService.getDetailModelList(stdVersion, dictId, condition, order, rows, page);
    }

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

    @RequestMapping("/delDictEntry")
    @ApiOperation(value = "删除字典", response = Result.class, produces = "application/json", notes = "删除字典")
    public Result delete(
            @ApiParam(name = "stdVersion", value = "stdVersion")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "dictEntryId", value = "字典ID")
            @RequestParam(value = "dictEntryId") Integer dictEntryId) {
        return stdDictEntryService.delete(stdVersion, dictEntryId);
    }

    @RequestMapping("/updateDictEntry")
    @ApiOperation(value = "修改字典", response = Result.class, produces = "application/json", notes = "修改字典")
    public Result modify(
            @ApiParam(name = "stdVersion", value = "stdVersion")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "dictEntry", value = "字典")
            @RequestParam(value = "dictEntry") String dictEntry) {
        return stdDictEntryService.update(stdVersion, dictEntry);
    }

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

    @RequestMapping(value="/getDictEntryCodeValues")
    @ApiOperation(value = "获取字典下拉框信息", response = DetailModelResult.class, produces = "application/json", notes = "获取适配字典下拉框信息")
    public DetailModelResult searchOrgDictItem(
            @ApiParam(name = "stdVersion", value = "标准版本号")
            @RequestParam(value = "stdVersion") String stdVersion,
            @ApiParam(name = "dictId", value = "数据集ID")
            @RequestParam(value = "dictId") Integer dictId,
            @ApiParam(name = "q", value = "字典名称或编码", required = false)
            @RequestParam(value = "q", required = false) String q,
            @ApiParam(name = "initDictValue", value = "初始化字典名称或编码", required = false)
            @RequestParam(value = "initDictValue", required = false) String initDictValue,
            @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(initDictValue)) {
            initDictValue = "";
        }
        if(StringUtil.isEmpty(q)){
            condition.put("name", initDictValue);
        }else{
            condition.put("name", q);
        }

        ObjectMapper mapper = new ObjectMapper();
        String conditionStr = null;
        try {
            conditionStr = mapper.writeValueAsString(condition);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return stdDictEntryService.getDictEntrySelectList(stdVersion, dictId, conditionStr, order, rows, 0);
    }
}
