package cn.hnthyy.thmz.service.impl.thmz;

import cn.hnthyy.thmz.entity.his.mz.MzChargeDetail;
import cn.hnthyy.thmz.entity.his.mz.MzPatientMi;
import cn.hnthyy.thmz.enums.SocketSenderTypeEnum;
import cn.hnthyy.thmz.service.his.mz.MzChargeDetailService;
import cn.hnthyy.thmz.service.his.mz.MzPatientMiService;
import cn.hnthyy.thmz.service.thmz.DispensingSocketService;
import cn.hnthyy.thmz.socket.PrescriptionPrintSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DispensingSocketServiceImpl implements DispensingSocketService {
    @Autowired
    PrescriptionPrintSocket prescriptionPrintSocket;
    @Autowired
    MzPatientMiService mzPatientMiService;
    @Autowired
    MzChargeDetailService mzChargeDetailService;


    @Override
    public int sendToMedicine(String patientId) {
        Map<String,Object> msgMap = new HashMap();
        msgMap.put("patient_id",patientId);
        MzPatientMi mzPatientMi = mzPatientMiService.queryByPatientId(patientId);
        msgMap.put("name",mzPatientMi.getName());
        msgMap.put("type",SocketSenderTypeEnum.SEND_MEDICINE.code);
        List<String> objTypes = new ArrayList<>();
        objTypes.add(SocketSenderTypeEnum.CALL_NUMBER.code);
        return prescriptionPrintSocket.sendMessage(msgMap,objTypes);
    }

    @Override
    public int callNumberSendToMedicine(String patientId) {
        Map<String,Object> msgMap = new HashMap();
        msgMap.put("patient_id",patientId);
        MzPatientMi mzPatientMi = mzPatientMiService.queryByPatientId(patientId);
        msgMap.put("name",mzPatientMi.getName());
        msgMap.put("type",SocketSenderTypeEnum.CALL_NUMBER.code);
        List<String> objTypes = new ArrayList<>();
        objTypes.add(SocketSenderTypeEnum.CALL_NUMBER.code);
        return prescriptionPrintSocket.sendMessage(msgMap,objTypes);
    }

    @Override
    public int sendToMedicineAndDispensing(String patientId, int times) {
        //查询缴费药品的发药药房
        MzChargeDetail mzChargeDetail = new MzChargeDetail();
        mzChargeDetail.setPatientId(patientId);
        mzChargeDetail.setTimes(times);
        mzChargeDetail.setGroupNo("71");
        List<MzChargeDetail> chargeDetail = mzChargeDetailService.getMzChargeDetailList(mzChargeDetail);
        chargeDetail.forEach(cd -> {
            if(cd.getIsRadSend() == 1){
                chargeDetail.remove(cd);
            }
        });
        if(chargeDetail.size() > 0){
            Set<Integer> orderNos = chargeDetail.stream().map(MzChargeDetail::getOrderNo).collect(Collectors.toSet());
            Map<String,Object> msgMap = new HashMap();
            msgMap.put("patient_id",patientId);
            MzPatientMi mzPatientMi = mzPatientMiService.queryByPatientId(patientId);
            msgMap.put("name",mzPatientMi.getName());
            msgMap.put("times",times);
            msgMap.put("orderNos",orderNos);
            msgMap.put("type",SocketSenderTypeEnum.PAY.code);
            List<String> objTypes = new ArrayList<>();
            objTypes.add(SocketSenderTypeEnum.CALL_NUMBER.code);
            objTypes.add(SocketSenderTypeEnum.DISPENSING_MEDICINE.code);
            return prescriptionPrintSocket.sendMessage(msgMap,objTypes);
        }
        return 0;
    }

    @Override
    public int sendToMedicineAndSendMedicine(String patientId) {
        Map<String,Object> msgMap = new HashMap();
        MzPatientMi mzPatientMi = mzPatientMiService.queryByPatientId(patientId);
        msgMap.put("name",mzPatientMi.getName());
        msgMap.put("patient_id",patientId);
        msgMap.put("type",SocketSenderTypeEnum.DISPENSING_MEDICINE.code);
        List<String> objTypes = new ArrayList<>();
        objTypes.add(SocketSenderTypeEnum.CALL_NUMBER.code);
        objTypes.add(SocketSenderTypeEnum.SEND_MEDICINE.code);
        return prescriptionPrintSocket.sendMessage(msgMap,objTypes);
    }
}
