package com.lantone.qc.kernel.catalogue.threelevelward;

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.Content;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName : THR0140
 * @Description : 查房记录无医师签名
 * @Author : 胡敬
 * @Date: 2020-03-19 19:20
 */
@Component
public class THR0140 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        if (threeLevelWardDocs.size() == 0) {
            status.set("0");
            return;
        }
        List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDocs.get(0).getAllDoctorWradDocs();
        if (allDoctorWradDocs.size() == 0) {
            status.set("0");
            return;
        }
        List<ThreeLevelWardDoc> recordDoctorList = allDoctorWradDocs
                .stream()
                .filter(doc -> StringUtil.isBlank(doc.getStructureMap().get("记录医师")))
                .collect(Collectors.toList());
        if (recordDoctorList.size() == 0) {
            status.set("0");
        }

        StringBuffer message = new StringBuffer();
        //长兴：返回所有医师未签名的记录日期
        int i = 0;
        for (ThreeLevelWardDoc threeLevelWardDoc : allDoctorWradDocs) {
            if (StringUtil.isBlank(threeLevelWardDoc.getStructureMap().get("记录医师"))) {
                if (threeLevelWardDoc.getStructureMap() != null && threeLevelWardDoc.getStructureMap().get("记录时间") != null) {
                    if (i > 1) {
                        break;
                    }
                    if (message.toString().length() == 0) {
                        message.append(threeLevelWardDoc.getStructureMap().get("记录时间"));
                    } else {
                        message.append(",").append(threeLevelWardDoc.getStructureMap().get("记录时间"));
                    }
                    i++;
                }
            }
        }
        info.set(message.append("...").toString());
        //        if (inputInfo.getDifficultCaseDiscussDocs().size() > 0) {
        //            processSign(inputInfo, "疑难患者");
        //        }
        //        if (inputInfo.getRescueDocs().size() > 0) {
        //            processSign(inputInfo, "抢救患者");
        //        }
    }

    private void processSign(InputInfo inputInfo, String message) {
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        for (ThreeLevelWardDoc threeLevelWardDoc : threeLevelWardDocs) {
            Map<String, String> threeLevelWardDocStructureMap = threeLevelWardDoc.getStructureMap();
            String title = threeLevelWardDocStructureMap.get("查房标题");
            String recordDoctor = threeLevelWardDocStructureMap.get("记录医师");
            if (CatalogueUtil.isEmpty(title)) {
                continue;
            }
            if (CatalogueUtil.subTitle(title).contains(Content.director) && CatalogueUtil.isEmpty(recordDoctor)) {
                status.set("-1");
                if (StringUtil.isBlank(message)) {
                    info.set(message);
                } else {
                    info.set(info.get() + "," + message);
                }
                break;
            }
        }
    }
}