package thyyxxk.webserver.service.view;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.medicalinsurance.SiMzDao;
import thyyxxk.webserver.dao.his.view.Patient360Dao;
import thyyxxk.webserver.dao.his.zhuyuanyisheng.YiZhuLuRuDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.markmtfees.MzDepositFile;
import thyyxxk.webserver.entity.outpatient.medicalinfo.MzBlRecord;
import thyyxxk.webserver.entity.view.patient360.MedicalTrack;
import thyyxxk.webserver.entity.view.patient360.PatientInfo360;
import thyyxxk.webserver.entity.view.patient360.ZyFeeDetail;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrPatientData;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.YzTemperatureParam;
import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.XinZhenYzActOrder;
import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.YiZhuFeiYongChaXunTiaoJian;
import thyyxxk.webserver.service.zhuyuanyisheng.YiZhuLuRuServer;
import thyyxxk.webserver.service.zhuyuanyisheng.emr.EmrServer;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class Patient360Server {
    private final Patient360Dao dao;


    private final EmrServer emrServer;

    private final YiZhuLuRuDao yiZhuLuRuDao;

    private final SiMzDao siMzDao;

    public Patient360Server(Patient360Dao dao,
                            EmrServer emrServer,
                            YiZhuLuRuDao yiZhuLuRuDao,
                            SiMzDao siMzDao) {
        this.dao = dao;
        this.emrServer = emrServer;
        this.yiZhuLuRuDao = yiZhuLuRuDao;
        this.siMzDao = siMzDao;
    }

    public ResultVo<PatientInfo360> getMedicalTrack(String patientId, Integer outOrHosp) {
        PatientInfo360 patientInfo360 = dao.getPatientInfo(patientId, outOrHosp == 1 ? "mz_no" : "inpatient_no");

        if (patientInfo360 == null) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "未找到患者信息");
        }

        List<MedicalTrack> list = new ArrayList<>();
        //   住院时间
        list.addAll(dao.getHospMedicalTrack(patientInfo360.getInpatientNo()));
        // 门诊时间
        list.addAll(dao.getMzMedicalTrack(patientInfo360.getMzNo()));
        if (ListUtil.notBlank(list)) {
            // list 根据 date 字段字符串排序
            list = list.stream().sorted(Comparator.comparing(MedicalTrack::getDate)).collect(Collectors.toList());
        }
        patientInfo360.getMedicalTrackList().addAll(list);
        return ResultVoUtil.success(patientInfo360);
    }

    public ResultVo<List<XinZhenYzActOrder>> getYz(YiZhuFeiYongChaXunTiaoJian param) {
        return ResultVoUtil.success(YiZhuLuRuServer.getOrderList(yiZhuLuRuDao.selectOrderNo(param.buildQw())));
    }

    public ResultVo<JSONObject> getYzTemperature(YzTemperatureParam param) {
        return emrServer.getYzTemperature(param);
    }

    public ResultVo<List<ZyFeeDetail>> getZyFeeDetail(ZyFeeDetail.ZyFeeDetailParams params) {
        return ResultVoUtil.success(dao.getZyFeeDetail(params.queryWrapper()));
    }

    @Data
    public static class YzDetail {
        private List<XinZhenYzActOrder> yzList;
        private List<ZyFeeDetail> feeList;
    }

    public ResultVo<YzDetail> getOrderDetailByOrderNo(String orderNo) {
        YzDetail yzDetail = new YzDetail();
        YiZhuFeiYongChaXunTiaoJian orderNoInfo = dao.getOrderNoInfo(new BigDecimal(orderNo).abs());
        if (orderNoInfo == null) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }

        // 获取医嘱 父子医嘱
        yzDetail.setYzList(yiZhuLuRuDao.selectOrderNo(orderNoInfo.buildQw()));

        boolean isParent = StringUtil.notBlank(orderNoInfo.getParentNoStr());
        BigDecimal queryOrderNo = isParent ? orderNoInfo.getParentNo() : orderNoInfo.getActOrderNo();

        List<BigDecimal> orderList = new ArrayList<>();

        orderList.add(queryOrderNo);
        orderList.addAll(dao.getParentNoByOrderNo(orderNoInfo.getPatNo(), orderNoInfo.getTimes(), queryOrderNo));

        ZyFeeDetail.ZyFeeDetailParams params = new ZyFeeDetail.ZyFeeDetailParams() {{
            setPatNo(orderNoInfo.getPatNo());
            setTimes(orderNoInfo.getTimes());
            setOrderList(orderList);
        }};

        yzDetail.setFeeList(dao.getZyFeeDetail(params.queryWrapper()));
        return ResultVoUtil.success(yzDetail);
    }


    public ResultVo<List<MzDepositFile>> getMzReceipts(String mzNo, String date) {
        return ResultVoUtil.success(siMzDao.selectMzDepositFiles(mzNo, date + " 00:00:00", date + " 23:59:59"));
    }


    public ResultVo<MzBlRecord> selectMzBlRecord(String patNo, Integer times) {
        return ResultVoUtil.success(siMzDao.selectMzBlRecord(patNo, times));
    }


}
