package com.yihu.figure.controller.system;

import com.yihu.figure.controller.BaseController;
import com.yihu.figure.model.dict.SystemDict;
import com.yihu.figure.service.dict.SystemDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by chenweida on 2017/3/14.
 */
@RestController
@RequestMapping(value = "/dict", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "系统字典")
public class DictController extends BaseController {
    @Autowired
    private SystemDictService systemDictService;

    /**
     * 根据字典code获取字典内容
     *
     * @return
     */
    @ApiOperation(value = "根据字典code获取字典内容")
    @RequestMapping(value = "getDictByDictName", method = RequestMethod.GET)
    public String getDictByDictName(
            @ApiParam(name = "name", value = "字典名称", required = true) @RequestParam(value = "name", required = true) String name) {
        try {
            List<SystemDict> systemDicts = systemDictService.getDictByDictName(name);
            JSONArray ja=new JSONArray();
            systemDicts.stream().forEach( dict->{
                JSONObject jo=new JSONObject();
                jo.put("code",dict.getCode());
                jo.put("value",dict.getValue());
                ja.put(jo);
            });
            return write(200,"成功","dict",ja);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, e.getMessage());
        }
    }
}
