package com.sunxung.factoring.web.sys;

import java.util.HashMap;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunxung.factoring.component.annotation.Authority;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.service.sys.DictionaryService;

@Controller
@RequestMapping("/dictionary")
public class DictionaryController {

    private static Logger log = LogManager.getLogger(DictionaryController.class);
    private static String PREFIX = "sys/dictionary/";
    @Autowired
    private DictionaryService dictionaryService;

    @RequestMapping("/add")
    @Authority("addDictionary")
    public String add(Dictionary dictionary, Model model) {
        log.info("add");
        List<Dictionary> dictionarys = dictionaryService.findByParentIdIsNull();
        model.addAttribute("dictionarys", dictionarys);
        return PREFIX + "add";
    }

    @RequestMapping("/do_add")
    @Authority("addDictionary")
    @ResponseBody
    public Object do_add(Dictionary dictionary) {
        dictionaryService.add(dictionary);
        return new HashMap<>();
    }

    @RequestMapping("/do_findByParentId")
    @ResponseBody
    public Object do_findByParentId(Long parentId) {
        return dictionaryService.findByParentId(parentId);
    }

    @RequestMapping("/do_findByParentCode")
    @ResponseBody
    public Object do_findByParnetName(String parentCode) {
        return dictionaryService.findByParentCode(parentCode);
    }

}
