/**
 * Copyright (C), 2015-2019
 * Author:   HJL
 * Date:     2019/8/9 16:12
 * Description:
 */
package com.emr.controller;

import com.emr.entity.Emr_Dictionary;
import com.emr.entity.Emr_Fault_Type;
import com.emr.entity.Power_User;
import com.emr.service.Emr_DictionaryService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
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 javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/dictionary")
public class DictionaryController {
    @Autowired
    private Emr_DictionaryService emrDictionaryService;

    @RequestMapping(value = "/dictionarys")
    public String inHospitals(Model model) {
        return "dictionaryDir/dictionary";
    }

    @ResponseBody
    @RequestMapping(value = "/dicList")
    public List<Emr_Dictionary> dicList(HttpServletRequest request, HttpServletResponse response, Emr_Dictionary emrDictionary) {
        List<Emr_Dictionary> list = emrDictionaryService.dicByClo(emrDictionary);
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/delById")
    public int delById(HttpServletRequest request, HttpServletResponse response, Integer id) {
        int bol = 0;
        if (id != null) {
            //删除
            bol = emrDictionaryService.delById(id);
        }
        return bol;
    }

    @ResponseBody
    @RequestMapping(value = "/saveInfo")
    public String saveInfo(HttpServletRequest request, HttpServletResponse response, Emr_Dictionary emrDictionary) {
        response.setCharacterEncoding("utf-8");
        String result="";
        int bol=0;
        Emr_Dictionary dic2 = new Emr_Dictionary();
        // 从session获取用户名
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        String username = user.getUserName();//获取前面登录的用户名

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = format1.format(new Date());

        if(emrDictionary.getId()!=null){
            //判断类别代码或代码是否已经存在
            //存在修改
            emrDictionary.setUpdater(username);
            emrDictionary.setUpdateTime(nowTime);
            bol=emrDictionaryService.updateCloById(emrDictionary);
            if (bol == 1) {
                result = "修改成功!";
            } else {
                result = "修改失败!";
            }
        }else{
            emrDictionary.setCreater(username);
            emrDictionary.setCreateTime(nowTime);
            //判断类别代码或代码是否已经存在
            if(emrDictionary.getCode()!=null && emrDictionary.getCode()!=""){
                dic2.setCode(emrDictionary.getCode());
                dic2.setParentId(emrDictionary.getParentId());
                //根据代码查询该父类下是否存在是否已经存在
                List<Emr_Dictionary> list = emrDictionaryService.dicByClo(dic2);
                //添加叶子节点
                if(list.size()<=0){
                    //添加叶子
                    bol = emrDictionaryService.insertSel(emrDictionary);
                    if(bol==1){
                        result = "添加成功!";
                    }else{
                        result = "添加失败!";
                    }
                }else{
                    result="代码已存在!";
                }
            }else{
                dic2.setTypecode(emrDictionary.getTypecode());
                //根据代码查询是否已经存在
                List<Emr_Dictionary> list = emrDictionaryService.dicByClo(dic2);
                //添加叶子节点
                if (list.size() <= 0) {
                    //emrDictionary.setEffective(1);
                    //添加类别
                    bol = emrDictionaryService.insertSel(emrDictionary);
                    if (bol == 1) {
                        result = "添加成功!";
                    } else {
                        result = "添加失败!";
                    }
                } else {
                    result = "代码已存在!";
                }

            }
        }

        return result;
    }
}
 

