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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
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.DateUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wangfeng
 * @Description:入院后没有连续记录3天
 * @date 2020-06-29 17:16
 */
@Component
public class THR03010 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getBeHospitalizedDoc() == null || inputInfo.getBeHospitalizedDoc().getStructureMap().size() == 0) {
            return;
        }
        if (inputInfo.getFirstPageRecordDoc() == null || inputInfo.getFirstPageRecordDoc().getStructureMap().size() == 0) {
            return;
        }
        Map<String, String> structureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
        String behospitalDate = structureMap.get("入院日期");
        //String leaveHospitalDate = structureMap.get("leaveHospitalDate") == null ? null : structureMap.get("leaveHospitalDate");
        Map<String, String> structureMaps = inputInfo.getFirstPageRecordDoc().getStructureMap();
        String leaveHospitalDate = structureMaps.get("出院时间");
        if (behospitalDate != null && leaveHospitalDate != null) {
            Date beDate = StringUtil.parseDateTime(behospitalDate);
            Date leaveDate = StringUtil.parseDateTime(leaveHospitalDate);
            long l = (leaveDate.getTime() - beDate.getTime()) / (24 * 60 * 60 * 1000);
            if (l > 3) {
                List<String> stringDate = new ArrayList<>();

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                for (int i = 1; i < 4; i++) {
                    Date firstTimeOfDay = DateUtil.getFirstTimeOfDay(DateUtil.addDay(beDate, i));
                    stringDate.add(formatter.format(firstTimeOfDay));
                }
                List<String> stringList = new ArrayList<>();
                List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs().get(0).getAllDoctorWradDocs();
                for (ThreeLevelWardDoc t : threeLevelWardDocs) {
                    Date threeLevelDate = StringUtil.parseDateTime(t.getStructureMap().get("查房日期"));
                    stringList.add(formatter.format(threeLevelDate));
                }
                if (stringList.size() > 2) {//查房日期取出没有3天时间， 直接报错
                    //去重
                    List<String> listTemp = new ArrayList<String>();
                    for (int i = 0; i < stringList.size(); i++) {
                        if (!listTemp.contains(stringList.get(i))) {
                            listTemp.add(stringList.get(i));
                        }
                    }
                    int i = 0;
                    for (String str : stringDate) {
                        for (String s : listTemp) {
                            if (str.equals(s)) {
                                i++;
                            }
                        }
                    }
                    if (i < 3) {
                        status.set("-1");
                    }
                } else {
                    status.set("-1");
                }

            }
        }

    }
}
