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

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.LeaveHospitalDoc;
import com.lantone.qc.pub.model.doc.StagesSummaryDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : DUT0296
 * @Description : 无阶段小结
 * 住院时间超过1个月需要阶段小结， 出院时间和入院时间做比较，超过一个月查找阶段小结
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class DUT0296 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");

        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        List<StagesSummaryDoc> stagesSummaryDocs = inputInfo.getStagesSummaryDocs();
        if (leaveHospitalDoc != null) {
            Map<String, String> structureMap_leave = leaveHospitalDoc.getStructureMap();
            String be_date = structureMap_leave.get("入院时间"); //2019-12-07 08:48
            String leave_date = structureMap_leave.get("出院时间");//2019-12-10 10:49
            if (StringUtil.isNotBlank(be_date) && StringUtil.isNotBlank(leave_date)) {
                int ffr = ffr(be_date, leave_date);
                if(ffr >= 31 && (stagesSummaryDocs == null || stagesSummaryDocs.size() == 0)){
                    status.set("-1");
                }
            }
        }
    }

    private int ffr(String be_date, String leave_date) {
        int day=0;
        try {
            Date date_in = StringUtil.parseDateTime(be_date);
            Date date_out = StringUtil.parseDateTime(leave_date);
            Calendar from = Calendar.getInstance();
            from.setTime(date_in);
            Calendar to = Calendar.getInstance();
            to.setTime(date_out);
            int fromMonth = from.get(Calendar.DAY_OF_YEAR);
            int toMonth = to.get(Calendar.DAY_OF_YEAR);
            day = toMonth - fromMonth;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return day;
    }
}