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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.CatalogueUtil;
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.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : THR0588
 * @Description : 住院期间连续3天无病程记录
 * @Author : 王世延
 * @Date: 2020-09-8 14:22
 */
@Component
public class THR0588 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        //查房记录
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        if (threeLevelWardDocs.size() == 0) {
            return;
        }
        //所有查房记录的日期天
        List<Date> dateThreeLevelDay = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        //所有的查房记录
        List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDocs.get(0).getAllDoctorWradDocs();
        for (ThreeLevelWardDoc doc : allDoctorWradDocs) {
            Date threeLevelDate = StringUtil.parseDateTime(doc.getStructureMap().get("查房日期"));
            if (threeLevelDate == null) {
                continue;
            }
            dateThreeLevelDay.add(threeLevelDate);
        }
        //获取连续3天无查房记录的时间
        for (int i = 0; i < dateThreeLevelDay.size(); i++) {
            if (i + 1 < dateThreeLevelDay.size()) {
                if (CatalogueUtil.compareTime(dateThreeLevelDay.get(i), dateThreeLevelDay.get(i + 1), 72 * 60L)) {
                    infoAppend(sb, dateThreeLevelDay.get(i), dateThreeLevelDay.get(i + 1));
                }
            }
        }
        if (sb.toString().length() > 0) {
            status.set("-1");
            info.set(sb.toString().substring(0, sb.toString().length() - 1));
        }
    }

    /**
     * 拼接提示信息
     *
     * @param sb
     * @param bfDate
     * @param afDate
     */
    private void infoAppend(StringBuffer sb, Date bfDate, Date afDate) {
        sb.append("(").append(DateUtil.formatDate(bfDate))
                .append("->").append(DateUtil.formatDate(afDate)).append(")").append("、");
    }
}