package thyyxxk.webserver.service.zhuyuanyisheng;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.constants.Message;
import thyyxxk.webserver.dao.his.zhuyuanyisheng.PatientCriticalValuesDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.inpatient.patient.Patient;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.entity.socketmessage.ApiMessageBody;
import thyyxxk.webserver.service.externalhttp.WebSocketService;
import thyyxxk.webserver.service.redislike.RedisLikeService;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.SocketMsg;
import thyyxxk.webserver.utils.StringUtil;

import java.util.List;

@Service
@Slf4j
public class PatientCriticalValuesService {

    private final PatientCriticalValuesDao dao;
    private final RedisLikeService redisLikeService;
    private final WebSocketService socketService;

    public PatientCriticalValuesService(PatientCriticalValuesDao dao, RedisLikeService redisLikeService, WebSocketService socketService) {
        this.dao = dao;
        this.redisLikeService = redisLikeService;
        this.socketService = socketService;
    }

    public ResultVo<String> acceptCriticalValues(String data) {
        JSONObject strToJson = JSONObject.parseObject(data);
        String message = strToJson.getString("Content");
        if (StringUtil.isBlank(message)) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "消息体为空。");
        }
        String patNo = message.substring(message.indexOf("住院号") + 4, message.indexOf(",姓名"));
        Patient p = dao.selectPatientInfoByPatNo(patNo);
        if (p == null) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "未查询到该住院患者信息。");
        }
        dao.insertAMessage(patNo, p.getAdmissTimes(), message);
        List<String> codeList = dao.selectDoctorByDept(p.getDeptCode());
        JSONObject js = new JSONObject();
        js.put("message", message);
        String msg = SocketMsg.socketVo(Message.critical_value, js);
        ApiMessageBody messageBody = new ApiMessageBody(msg);
        codeList.forEach(item -> {
            messageBody.setUserCode(item);
            socketService.sendMessageByUserCode(messageBody);
        });

        return ResultVoUtil.success();
    }

    public ResultVo<List<JSONObject>> getCriticalValuePatientList(String patNo) {
        QueryWrapper<?> qw = new QueryWrapper<>();
        UserInfo u = redisLikeService.getUserInfoByToken();
        if (StringUtil.isBlank(patNo)) {
            qw.eq("b.zk_ward", u.getDeptCode());
        } else {
            qw.eq("a.pat_no", patNo);
        }
        List<JSONObject> patientList = dao.selectCriticalValuePatientList(qw);
        if (patientList == null) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(patientList);
    }

    public ResultVo<List<String>> getCriticalValueByPatInfo(String patNo, Integer times) {
        return ResultVoUtil.success(dao.selectCriticalValueByPatInfo(patNo, times));
    }

}
