package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.mz.Employee;
import cn.hnthyy.thmz.entity.thmz.Template;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.enums.TemplateTypeEnum;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.service.his.mz.EmployeeService;
import cn.hnthyy.thmz.service.thmz.TemplateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class TemplateController {
    @Autowired
    private TemplateService templateService;
    @Autowired
    private EmployeeService employeeService;

    /**
     * 保存医疗范文
     *
     * @param template
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveTemplate", method = {RequestMethod.POST})
    public Map<String, Object> saveTemplate(@RequestBody Template template, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(template==null){
                resultMap.put("code", -1);
                resultMap.put("message", "保存医疗范文失败,参数为空");
                return resultMap;
            }
            if(template.getTemplateType()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "保存医疗范文失败,范文类型参数为空");
                return resultMap;
            }
            if(template.getTemplateStatus()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "保存医疗范文失败,范文状态参数为空");
                return resultMap;
            }
            if(StringUtils.isBlank(template.getTemplateData())){
                resultMap.put("code", -1);
                resultMap.put("message", "保存医疗范文失败,范文内容参数为空");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            if(TemplateTypeEnum.SECTION_TEMPLATE.code.equals(template.getTemplateType())){
                Employee employee= employeeService.queryByUserCode(tokenUser.getUserIdCode());
                if(employee!=null && StringUtils.isNotBlank(employee.getDeptCode())){
                    template.setDeptNo(employee.getDeptCode());
                }
            }
            if(template.getId()==null){
                template.setCreateId(tokenUser.getUserIdCode());
            }else {
                template.setUpdateId(tokenUser.getUserIdCode());
            }
            int num = templateService.saveTemplate(template);
            if (num == 1) {
                resultMap.put("code", 0);
                resultMap.put("message", "保存医疗范文成功");
                return resultMap;
            }
            resultMap.put("code", -1);
            resultMap.put("message", "保存医疗范文失败");
            return resultMap;
        } catch (MzException e) {
            resultMap.put("code", -1);
            resultMap.put("message", e.getMessage());
            log.error("系统异常，错误信息{}", e.getMessage());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "保存医疗范文失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }




    /**
     * 删除医疗范文
     *
     * @param template
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/removeTemplate", method = {RequestMethod.POST})
    public Map<String, Object> removeTemplate(@RequestBody Template template, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(template==null){
                resultMap.put("code", -1);
                resultMap.put("message", "删除医疗范文失败,参数为空");
                return resultMap;
            }
            if(template.getId()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "删除医疗范文失败,范文主键参数为空");
                return resultMap;
            }
            template.setTemplateStatus(YesNoEnum.YES.code);
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            template.setUpdateId(tokenUser.getUserIdCode());
            int num = templateService.saveTemplate(template);
            if (num == 1) {
                resultMap.put("code", 0);
                resultMap.put("message", "删除医疗范文成功");
                return resultMap;
            }
            resultMap.put("code", -1);
            resultMap.put("message", "删除医疗范文失败");
            return resultMap;
        } catch (MzException e) {
            resultMap.put("code", -1);
            resultMap.put("message", e.getMessage());
            log.error("系统异常，错误信息{}", e.getMessage());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "删除医疗范文失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }






    /**
     * 查询医生自己的，科室和全院的医疗范文  ，不查询内容
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getTemplate", method = {RequestMethod.GET})
    public Map<String, Object> getTemplate(HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            Employee employee= employeeService.queryByUserCode(tokenUser.getUserIdCode());
            String deptNo=null;
            if(employee!=null && StringUtils.isNotBlank(employee.getDeptCode())){
                deptNo=employee.getDeptCode();
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询医疗范文成功");
            resultMap.put("data",templateService.queryTemplate(tokenUser.getUserIdCode(), deptNo));
            return resultMap;
        } catch (MzException e) {
            resultMap.put("code", -1);
            resultMap.put("message", e.getMessage());
            log.error("系统异常，错误信息{}", e.getMessage());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询医疗范文失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }

    /**
     * 根据id查询医疗范文
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getTemplateById", method = {RequestMethod.GET})
    public Map<String, Object> getTemplateById(@RequestParam("id") Long id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(id==null){
                resultMap.put("code", -1);
                resultMap.put("message", "查询医疗范文失败,主键为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询医疗范文成功");
            resultMap.put("data",templateService.queryById(id));
            return resultMap;
        }catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "根据id查询医疗范文失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }

}
