package thyyxxk.webserver.service.outpatient.triage;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.outpatient.triage.RoomScreenDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.outpatient.mangedoctorinfo.DoctorInfo;
import thyyxxk.webserver.entity.outpatient.triage.MessageForPush;
import thyyxxk.webserver.http.websocket.SocketV2;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.util.List;

/**
 * @author dj
 */
@Slf4j
@Service
public class RoomScreenService {
    private final RoomScreenDao dao;
    private final SocketV2 socketV2;

    @Autowired
    public RoomScreenService(RoomScreenDao dao, SocketV2 socketV2) {
        this.dao = dao;
        this.socketV2 = socketV2;
    }

    public ResultVo<String> getPageUrl() {
        String url = dao.getPageUrl();
        if (StringUtil.isBlank(url)) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(url);
    }

    public ResultVo<List<String>> getVideoList() {
        return ResultVoUtil.success(dao.getVideoList());
    }

    public ResultVo<DoctorInfo> getDoctorInfo(String code) {
        String doctorCode = dao.getRoomDoctorCode(code);
        DoctorInfo entity =  dao.getDoctorInfo(doctorCode);
        if (null == entity) {
            entity = dao.getDoctorInfo(code);
        }
        return ResultVoUtil.success(entity);
    }

    public ResultVo<CodeName> getRoomNoAndDept(String roomCode) {
        return ResultVoUtil.success(dao.getRoomNoAndDept(roomCode));
    }

    public ResultVo<String> pushSocketMsg(MessageForPush param) {
        param.setSid(param.getRoomCode());
        socketV2.sendMsgToRoomScreen(param);
        return ResultVoUtil.success();
    }

    public ResultVo<MessageForPush> getNextJz(MessageForPush param) {
        MessageForPush temp = dao.getNextJz(param);
        temp.setName(StringUtil.desensitizeName(temp.getName()));
        return ResultVoUtil.success(temp);
    }

    public ResultVo<List<MessageForPush>> getNextJzList(MessageForPush param) {
        log.info("获取 诊室：{} 的候诊列表。", param.getRoomCode());
        List<MessageForPush> list = dao.getNextJzList(param);
        for (MessageForPush temp : list) {
            temp.setName(StringUtil.desensitizeName(temp.getName()));
        }
        return ResultVoUtil.success(list);
    }

    public ResultVo<String> getRoomCodeByIp(String ip) {
        String roomCode = dao.getRoomCodeByIp(ip);
        if (StringUtil.isBlank(roomCode)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "未找到 IP【" + ip +
                    "】对应的诊室，请联系信息中心（8321）进行配置。");
        }
        return ResultVoUtil.success(roomCode);
    }

    public void notifyComplete(Integer serialNo) {
        log.info("通知完毕>>> {}", serialNo);
        dao.updateNotifyDatetime(serialNo);
    }
}
