package com.emr.controller;

import com.alibaba.fastjson.JSON;
import com.emr.annotation.OptionalLog;
import com.emr.dao.Emr_DictionaryMapper;
import com.emr.entity.Emr_Dictionary;
import com.emr.entity.ResultUtil;
import com.emr.service.emrDictionary.EmrDictionaryService;
import com.emr.util.ExceptionPrintUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ProjectName:DictionaryController
 * @Description:数据字典管理
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/5/24 16:33
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/5/24 16:33
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Controller
@RequestMapping("dictionary/")
public class DictionaryController {
    @Autowired
    private Emr_DictionaryMapper emrDictionaryMapper;
    @Autowired
    private EmrDictionaryService emrDictionaryService;
    /**
     * @MethodName: dictionaryList
     * @Description: 跳转到字典管理页面
     * @Param  无
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2019-07-17
     * @UpdateUser: 曾文和
     * @UpdateDate: 2019-07-17
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    @RequiresPermissions("/dictionary/dictionaryList")
    @OptionalLog(module = "查看",methods = "字典列表页面")
    @RequestMapping(value="dictionaryList")
    public String dictionaryList(Model model) {
        return "otherManage/dictionaryList";
    }

    /**
     * @MethodName: selectDictionaryByTypeCode
     * @Description: 根据字典code查询集合
     * @Param  无
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2019-05-24
     * @UpdateUser: 曾文和
     * @UpdateDate: 2019-05-24
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    @RequestMapping(value="selectDictionaryByTypeCode",produces = {"text/json;charset=UTF-8"})
    @ResponseBody
    public String cutomSearchTable(String typeCode) {
        try{
            List<Emr_Dictionary> dictionaries = emrDictionaryMapper.selectDictionaryByTypeCode(typeCode);
            return JSON.toJSONString(dictionaries);
        }catch (Exception e){
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @MethodName: getDictionaryTree
     * @Description: 获取字典树
     * @Param  无
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2019-05-24
     * @UpdateUser: 曾文和
     * @UpdateDate: 2019-05-24
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    @RequestMapping(value="getDictionaryTree",produces = {"text/json;charset=UTF-8"})
    @ResponseBody
    public String getDictionaryTree(String searchContext) {
        try{
            List<Emr_Dictionary> treeList = emrDictionaryMapper.selectAll(searchContext);
            //添加根目录 字典列表
            Emr_Dictionary tree = new Emr_Dictionary();
            tree.setId(0);
            tree.setNewName("字典列表");
            treeList.add(tree);
            return JSON.toJSONString(treeList);
        }catch (Exception e){
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @MethodName: add
     * @Description: 保存
     * @Param  无
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2019-09-09
     * @UpdateUser: 曾文和
     * @UpdateDate: 2019-09-09
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    @RequiresPermissions("/dictionary/add")
    @OptionalLog(module = "新增",methods = "字典",fieldName = "typename")
    @RequestMapping("add")
    @ResponseBody
    public ResultUtil add(Emr_Dictionary dictionary, HttpServletRequest request) throws Exception{
       if(StringUtils.isNotBlank(dictionary.getParentId()) && "0".equals(dictionary.getParentId())){
           if(StringUtils.isBlank(dictionary.getTypecode())){
               return ResultUtil.error("类别代码不能为空!");
           }
           if(StringUtils.isBlank(dictionary.getTypename())){
               return ResultUtil.error("类别名称不能为空!");
           }
       }else{
           if(StringUtils.isBlank(dictionary.getCode())){
               return ResultUtil.error("代码不能为空!");
           }
           if(StringUtils.isBlank(dictionary.getName())){
               return ResultUtil.error("名称不能为空!");
           }
       }
       emrDictionaryService.add(dictionary,request);
       return ResultUtil.ok(dictionary.getId().toString());
    }

    /**
     * @MethodName: update
     * @Description: 保存
     * @Param  无
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2019-09-09
     * @UpdateUser: 曾文和
     * @UpdateDate: 2019-09-09
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    @RequiresPermissions("/dictionary/update")
    @OptionalLog(module = "修改",methods = "字典",fieldName = "typename")
    @RequestMapping("update")
    @ResponseBody
    public ResultUtil update(Emr_Dictionary dictionary, HttpServletRequest request) throws Exception{
       if(StringUtils.isNotBlank(dictionary.getParentId()) && "0".equals(dictionary.getParentId())){
           if(StringUtils.isBlank(dictionary.getTypecode())){
               return ResultUtil.error("类别代码不能为空!");
           }
           if(StringUtils.isBlank(dictionary.getTypename())){
               return ResultUtil.error("类别名称不能为空!");
           }
       }else{
           if(StringUtils.isBlank(dictionary.getCode())){
               return ResultUtil.error("代码不能为空!");
           }
           if(StringUtils.isBlank(dictionary.getName())){
               return ResultUtil.error("名称不能为空!");
           }
       }
       emrDictionaryService.update(dictionary,request);
       return ResultUtil.ok();
    }

    /**
     * @MethodName: delete
     * @Description: 保存
     * @Param  无
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2019-09-09
     * @UpdateUser: 曾文和
     * @UpdateDate: 2019-09-09
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    @RequiresPermissions("/dictionary/delete")
    @OptionalLog(module = "删除",methods = "字典")
    @RequestMapping("delete")
    @ResponseBody
    public ResultUtil delete(Integer id) throws Exception{
        emrDictionaryService.delete(id);
        return ResultUtil.ok();
    }

    /**
     * @MethodName: getDictionaryByParentId
     * @Description: 根据父id获取父id名
     * @Param  无
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2019-11-13
     * @UpdateUser: 曾文和
     * @UpdateDate: 2019-11-13
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    @RequestMapping("getDictionaryByParentId")
    @ResponseBody
    public String getDictionaryByParentId(Integer parentId) {
        try{
            return JSON.toJSONString(emrDictionaryService.getDictionaryByParentId(parentId));
        }catch (Exception e){
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
            return null;
        }
    }
}
