package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.entity.thmz.WorkspaceConfig;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.service.thmz.WorkspaceConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@RestController
public class WorkspaceConfigController {
    @Autowired
    private WorkspaceConfigService workspaceConfigService;

    /**
     * 保存医生工作台配置
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveWorkspaceConfig", method = {RequestMethod.POST})
    public Map<String, Object> saveWorkspaceConfig(@RequestBody WorkspaceConfig workspaceConfig, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (workspaceConfig == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存医生工作台配置,参数为空");
                return resultMap;
            }
            if (workspaceConfig.getSymptomFlag() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "主诉配置参数为空");
                return resultMap;
            }
            if (workspaceConfig.getEmrHpiFlag() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "现病史配置参数为空");
                return resultMap;
            }
            if (workspaceConfig.getEmrPsFlag() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "既往史配置参数为空");
                return resultMap;
            }
            if (workspaceConfig.getPersonalHistoryFlag() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "个人史配置参数为空");
                return resultMap;
            }
            if (workspaceConfig.getFamilyHistoryFlag() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "家族史配置参数为空");
                return resultMap;
            }
            if (workspaceConfig.getObstericalHistoryFlag() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "婚育史配置参数为空");
                return resultMap;
            }
            if (workspaceConfig.getPressureLeftFlag() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", " 血压（左）配置参数为空");
                return resultMap;
            }
            if (workspaceConfig.getTentativeDiagnosisFlag() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", " 初步诊断配置参数为空");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            workspaceConfigService.save(workspaceConfig,tokenUser);
            resultMap.put("code", 0);
            resultMap.put("message", " 保存工作台配置成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存医生工作台配置失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "保存医生工作台配置失败,错误原因【" + e.getMessage() + "】");
            return resultMap;
        }
    }


    /**
     * 查询当前医生的工作台配置
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getWorkspaceConfigForCurrent", method = {RequestMethod.GET})
    public Map<String, Object> getWorkspaceConfigForCurrent(HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            WorkspaceConfig workspaceConfig= workspaceConfigService.queryWorkspaceConfigByUserIdCode(tokenUser.getUserIdCode());
            if(workspaceConfig==null){
                workspaceConfig=new WorkspaceConfig();
                workspaceConfig.setSymptomFlag(YesNoEnum.NO.code);
                workspaceConfig.setEmrHpiFlag(YesNoEnum.NO.code);
                workspaceConfig.setEmrPsFlag(YesNoEnum.YES.code);
                workspaceConfig.setPersonalHistoryFlag(YesNoEnum.YES.code);
                workspaceConfig.setFamilyHistoryFlag(YesNoEnum.NO.code);
                workspaceConfig.setObstericalHistoryFlag(YesNoEnum.YES.code);
                workspaceConfig.setPressureLeftFlag(YesNoEnum.YES.code);
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询当前医生的工作台配置成功");
            resultMap.put("data", workspaceConfig);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询当前医生的工作台配置失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询当前医生的工作台配置失败");
            return resultMap;
        }
    }

}
