package com.ann.demo.entity.other;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ann.demo.entity.constant.AliasName;
import com.ann.demo.entity.filing.MessageSubordinate;
import com.ann.demo.entity.filing.dto.MessageDto;
import com.ann.demo.entity.filing.dto.PatientMainDto;
import com.ann.demo.entity.interfaceEntity.MessageLog;
import com.ann.demo.service.MessageService;
import com.ann.demo.service.MessageSubordinateService;

import java.util.Objects;

public class RunningItem {
    public MessageLog messageLog;
    public MessageDto messageDto;
    public MessageService messageService;
    public MessageSubordinateService messageSubordinateService;

    public RunningItem(MessageService ms, MessageSubordinateService mss, MessageDto md, MessageLog ml) {
        messageLog = ml;
        messageDto = md;
        messageService = ms;
        messageSubordinateService = mss;
    }

    public RunningItem(MessageSubordinateService mss, MessageDto md, MessageLog ms) {
        messageLog = ms;
        messageDto = md;
        messageService = null;
        messageSubordinateService = mss;
    }

    public RunningItem(MessageService ms, MessageSubordinateService mss, MessageLog ml) {
        messageLog = ml;
        messageDto = null;
        messageService = ms;
        messageSubordinateService = mss;
    }


    public void Save() {
        if (messageService != null && messageLog != null) {
            messageService.save(messageLog);
        }

        if (messageSubordinateService != null && messageDto != null && messageLog != null) {
            PatientMainDto patientMainDto = new PatientMainDto(messageDto.getInpNo(), messageDto.getVisitId());
            String patientMainStr = JSON.toJSONString(patientMainDto, SerializerFeature.WriteMapNullValue);
            String type = messageDto.getType();
            // 判断是否重复
            if (Objects.equals(type, AliasName.EKG_REPORT)
                    || Objects.equals(type, AliasName.PATHOLOGY_REPORT)
                    || Objects.equals(type, AliasName.RADIATION_REPORT)
                    || Objects.equals(type, AliasName.ULTRASONIC_REPORT)) {
               /* if (!messageSubordinateService.countByPatientMainAndContentJson(patientMainStr, JSON.toJSONString(messageLog))) {
                    return;
                }*/
                messageSubordinateService.save(new MessageSubordinate(messageLog.getId(), patientMainStr, JSON.toJSONString(messageLog)));
            }
        }
    }
}
