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

import com.alibaba.fastjson.JSONObject;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
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.FirstPageRecordDoc;
import com.lantone.qc.pub.model.doc.LeaveHospitalDoc;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.util.ListUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : FIRP0272
 * @Description : 出院诊断顺序不规范
 * 与出院小结里的出院诊断顺序做比较
 * @Date: 2020-03-14 16:24
 */
@Component
public class FIRP0272 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getFirstPageRecordDoc() == null) {
            status.set("0");
            return;
        }
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        FirstPageRecordDoc firstPageRecordDoc = inputInfo.getFirstPageRecordDoc();
        if (leaveHospitalDoc != null) {
            Map<String, Object> firstPageMap = firstPageRecordDoc.getStructureExtMap();
            if (firstPageMap != null) {
                List<JSONObject> outDiags = (List<JSONObject>) firstPageMap.get(Content.dischargeDiag);
                if (ListUtil.isEmpty(outDiags)) {
                    status.set("0");
                    return;
                }
                List<String> jsonDiagList = getJsonDiagList(outDiags);
                DiagLabel leaveDiagLabel = leaveHospitalDoc.getLeaveDiagLabel();
                if (leaveDiagLabel != null) {
                    //出院诊断
                    List<Diag> diags = leaveDiagLabel.getDiags();
                    if (diags != null) {
                        List<String> diagList = getDiagList(diags);
                        if (jsonDiagList.size() == diagList.size() && isOrder(jsonDiagList, diagList)) {
                            status.set("0");
                        }
                    } else {
                        status.set("0");
                    }
                }
            }
        }
    }

    private boolean isOrder(List<String> firstList, List<String> secondList) {
        for (int i = 0; i < firstList.size(); i++) {
            if (!firstList.get(i).equals(secondList.get(i))) {
                return false;
            }
        }
        return true;
    }

    private List<String> getJsonDiagList(List<JSONObject> diags) {
        List<String> diagList = new ArrayList<>();
        for (JSONObject diag : diags) {
            if (diag.get(Content.diagnoseName) != null) {
                diagList.add(diag.get(Content.diagnoseName).toString());
            }
        }
        return diagList;
    }

    private List<String> getDiagList(List<Diag> diags) {
        List<String> diagList = new ArrayList<>();
        for (Diag diag : diags) {
            if (diag.getHospitalDiagName() != null) {
                diagList.add(diag.getHospitalDiagName());
            } else if (diag.getName() != null) {
                diagList.add(diag.getName());
            }
        }
        return diagList;
    }
}