package com.yihu.hos.system.controller;

import com.yihu.hos.common.Services;
import com.yihu.hos.system.model.SystemDict;
import com.yihu.hos.system.model.SystemDictList;
import com.yihu.hos.system.service.BaseDictManager;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by hzp on 2016/01/11.
 */
@RequestMapping("/basedict")
@Controller
public class BaseDictController extends BaseController {

    @Resource(name = Services.BaseDict)
    private BaseDictManager dictManager;



    /****************** 字典目录 *****************************/
    /**
     * 获取字典目录
     */
    @RequestMapping("getDictList")
    @ResponseBody
    public Result getDictList(String condition,int page, int rows) {
        try {
            return dictManager.getSystemDictList(condition, page, rows);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 新增字典
     */
    @RequestMapping("addDict")
    @ResponseBody
    public Result addDict() {
        try {
            SystemDictList dict = new SystemDictList();
            return dictManager.addDict(dict);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 修改字典
     */
    @RequestMapping("updateDict")
    @ResponseBody
    public Result updateDict() {
        try {
            SystemDictList dict = new SystemDictList();
            return dictManager.updateDict(dict);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 删除字典
     */
    @RequestMapping("deleteDict")
    @ResponseBody
    public Result deleteDict() {
        try {
            SystemDictList dict = new SystemDictList();
            return dictManager.deleteDict(dict);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

    /****************** 字典维护 *****************************/

    /**
     * 根据字典名称获取字典项(常用)
     */
    @RequestMapping(value = "getDict",produces = "application/json")
    @ResponseBody
    public Result getDict(String name,String where,String type) {
        try {
            return dictManager.getDict(name, where, type);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }


    /**
     * 新增字典项
     */
    @RequestMapping("addDictItem")
    @ResponseBody
    public Result addDictItem() {
        try {
            SystemDict dict = new SystemDict();
            return dictManager.addDictItem(dict);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 新增字典项
     */
    @RequestMapping("updateDictItem")
    @ResponseBody
    public Result updateDictItem() {
        try {
            SystemDict dict = new SystemDict();
            return dictManager.updateDictItem(dict);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 删除字典项
     */
    @RequestMapping("deleteDictItem")
    @ResponseBody
    public Result deleteDictItem() {
        try {
            SystemDict dict = new SystemDict();
            return dictManager.deleteDictItem(dict);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }
}
