package com.wsm.admin.api;

import com.wsm.admin.model.DataDictionary;
import com.wsm.admin.service.IDataDictionaryService;
import com.wsm.common.api.BaseController;
import com.wsm.common.util.AjaxJson;
import com.wsm.common.util.ConstantUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/admin/dataDictionary")
public class DataDictionaryController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
    private IDataDictionaryService dataDictionaryService;

    @RequestMapping(value = {"/list"})
    public String index(Model model) {
        Page<DataDictionary> dataDictionaryList = dataDictionaryService.findAll(getPageRequest(
                new Sort(Sort.Direction.DESC, "id")));
        model.addAttribute("dataDictionaryList", dataDictionaryList);
        return "/admin/dataDictionary/list";
    }

    @RequestMapping(value = {"/detail"}, method = RequestMethod.GET)
    public String detail(@RequestParam(required = false) String dataDictionaryId, Model model) {
        DataDictionary dataDictionary = new DataDictionary();
        if (dataDictionaryId != null) {
            dataDictionary = dataDictionaryService.find(Long.valueOf(dataDictionaryId));
        }
        model.addAttribute("dataDictionary", dataDictionary);
        return "/admin/dataDictionary/form";
    }

    @RequestMapping(value = {"/save"}, method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson edit(DataDictionary dataDictionary) {
        try {
        	if (dataDictionary.getId() == null) {
        		DataDictionary dbDictionary = dataDictionaryService.findByCodeAndDataKey(dataDictionary.getCode(), dataDictionary.getDataKey());
        		if (dbDictionary != null){
                    return AjaxJson.failure("您所输入的数据类型代码和数据键已存在，请修改后再提交！");
        		}
        	}
            dataDictionaryService.saveOrUpdate(dataDictionary);
        } catch (Exception e) {
        	logger.error("系统异常：", e);
            return AjaxJson.failure("系统异常："+e);
        }
        return AjaxJson.success();
    }
    
    /**
     * 删除
     */
    @RequestMapping(value = {"/remove"}, method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson remove(String dataDictionaryId) {
        AjaxJson ajaxJson = null;
        try {
            if (!StringUtils.isEmpty(dataDictionaryId)) {
                DataDictionary dbDataDictionary = dataDictionaryService.find(Long.valueOf(dataDictionaryId));
                dbDataDictionary.setRecStatus("I");
                dataDictionaryService.update(dbDataDictionary);
                ajaxJson =  AjaxJson.success(ConstantUtils.SUCCESS_MSG);
            }else{
                ajaxJson = AjaxJson.failure("新闻id不能为空");
            }
        } catch (Exception e) {
            logger.error("系统异常：", e);
            return AjaxJson.failure("系统异常：" + e);
        }
        return ajaxJson;
    }

}
