/**
 * Copyright (C), 2015-2019
 * Author:   HJL
 * Date:     2019/7/19 15:32
 * Description:缺陷分类
 */
package com.emr.controller;

import com.emr.entity.Emr_Fault_Type;
import com.emr.service.Emr_Fault_TypeService;
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.util.List;

@Controller
@RequestMapping("/faultType")
public class faultTypeController {
    @Autowired
    private Emr_Fault_TypeService emrFaultTypeService;

    @RequestMapping(value = "/faultTypes")
    public String faultTypes(Model model) {
        return "faultTypeDir/faultTypeList";
    }

    @RequestMapping(value = "/monitoring")
    public String monitoring() {
        return "workHome/workHomeList";
    }


    /**
     * 病案数据
     * @return
     */
    @RequestMapping("recordData")
    public String recordData(){
        return "workHome/recordData";
    }

    /**
     * 用户浏览记录
     * @return
     */
    @RequestMapping("userBrowse")
    public String callBackStatistics(){
        return "workHome/userBrowse";
    }

    /**
     *用户是否需要审批
     * @return
     */
    @RequestMapping("userApproval")
    public String userApproval(){
        return "workHome/userApproval";
    }

    /**
     * 工作看板内容
     * @return
     */
    @RequestMapping("workHomeIframe")
    public String workHomeIframe(){
        return "workHome/workHomeIframe";
    }

    @ResponseBody
    @RequestMapping(value = "/faultTypeList")
    public List<Emr_Fault_Type> faultTypeList(HttpServletRequest request, HttpServletResponse response, Emr_Fault_Type emrFaultType) {
        List<Emr_Fault_Type> list = emrFaultTypeService.selectByCol(emrFaultType);
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/updateByClo")
    public String updateByClo(HttpServletRequest request, HttpServletResponse response, Emr_Fault_Type emrFaultType) throws Exception{
        String result="";
        int bol=0;
        //判断code是否已经存在
        Emr_Fault_Type emrFaultTypeTj=new Emr_Fault_Type();
        emrFaultTypeTj.setTypeFlag(emrFaultType.getTypeFlag());
        List<Emr_Fault_Type> list=emrFaultTypeService.selectByCol(emrFaultTypeTj);
        //判断id是否存在
        if(emrFaultType.getId()==null && list.size()<1){
            //不存在则添加缺陷类别记录
            //查询id最大的数据，取id+1作为新id的值
            /* Emr_Fault_Type emrFaultTypeNew=emrFaultTypeService.selectByColDesc();
            emrFaultType.setId(emrFaultTypeNew.getId()+1);*/
            bol= emrFaultTypeService.insertClo(emrFaultType);
            if(bol==1){
                result="添加类别成功!";
            }else{
                result="添加类别失败!";
            }
        }else if(emrFaultType.getId()!=null &&  list.size()<2){
            //判断是否存在记录
            if (emrFaultTypeService.selectById(emrFaultType.getId()) != null) {
                bol = emrFaultTypeService.updateByClo(emrFaultType);
                if(bol==1){
                    result="修改类别成功!";
                }else{
                    result="修改类别失败!";
                }
            }

        }else {
            result="类别代码不可重复!";
        }
       return result;

    }


    @ResponseBody
    @RequestMapping(value = "/delById")
    public int delById(HttpServletRequest request, HttpServletResponse response,Integer id) {
        int bol = 0;
        //判断id是否存在
        if (id!= null) {
            //判断是否存在记录
            bol = emrFaultTypeService.delById(id);
        }
        return bol;

    }
}
 

