# QueryLotHistoryExpRowSetTask.java

    package com.mycim.server.wip.dao.impl.threadtask;

    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.framework.utils.lang.time.DateUtils;
    import com.mycim.valueobject.consts.TransactionNames;

    import java.util.*;
    import java.util.function.Consumer;

    public class QueryLotHistoryExpRowSetTask implements Runnable {

        private final List<Map<String, Object>> result;

        private final Map row;

        private final Long seq;

        private final Long lotRrn;

        private final Set<Long>transRrns;

        public QueryLotHistoryExpRowSetTask(Map row, long seq, List<Map<String, Object>> result, Long lotRrn,Set<Long>transRrns) {
            this.row = row;
            this.result = result;
            this.seq = seq;
            this.lotRrn = lotRrn;
            this.transRrns=transRrns;
        }

        @Override
        public void run() {
            Map<String, Object> lotTransmap = new HashMap();
            lotTransmap.put("seq", seq);
            lotTransmap.put("operationDesc", row.get("OPERATION_DESC"));
            lotTransmap.put("operationType", row.get("STEP_TYPE"));
            lotTransmap.put("workArea", row.get("WORK_AREA"));
            lotTransmap.put("flowSeq", row.get("FLOW_SEQ"));
            lotTransmap.put("lotRrn", row.get("LOT_RRN"));
            lotTransmap.put("lot_id", row.get("LOT_ID"));
            String transId = MapUtils.getString(row, "TRANS_ID","");
            lotTransmap.put("trans_id", transId);
            lotTransmap.put("carrier_rrn", row.get("CARRIER_RRN"));
            lotTransmap.put("trans_start_timestamp", DateUtils.formatDate((Date) row.get("TRANS_START_TIMESTAMP")));
            lotTransmap.put("trans_end_timestamp", DateUtils.formatDate((Date) row.get("TRANS_END_TIMESTAMP")));
            lotTransmap.put("trans_performed_by",
                            row.get("TRANS_PERFORMED_BY") == null ? "SYSTEM" : row.get("TRANS_PERFORMED_BY"));
            lotTransmap.put("target_carrier_rrn", row.get("TARGET_CARRIER_RRN"));
            lotTransmap.put("qty_1", row.get("IN_QTY1"));
            lotTransmap.put("qty_2", row.get("OUT_QTY1"));
            lotTransmap.put("productRrn", row.get("PRODUCT_RRN"));
            String processStepIdVer = MapUtils.getString(row, "PROCESS_STEP_ID_VERSION","");
            lotTransmap.put("route_id", parseRoute(processStepIdVer));
            lotTransmap.put("comments", MapUtils.getString(row, "TRANS_COMMENTS", ""));

            final String transComments = MapUtils.getString(row, "TRANS_COMMENTS","");
            final Long transRrn = MapUtils.getLong(row, "TRANS_RRN",0L);
            if (StringUtils.isEmpty(transComments)) {
                transRrns.add(transRrn);
                Consumer<Map<Long,String>> callback=(Map<Long,String> transMap)->{
                    lotTransmap.put("transComments", MapUtils.getString(transMap,transRrn));
                    lotTransmap.remove("transCommentsCallBack");
                };
                lotTransmap.put("transCommentsCallBack",callback);
            } else {
                if (StringUtils.equalsIgnoreCase(transId, TransactionNames.HOLD_KEY) || StringUtils.equalsIgnoreCase(transId, TransactionNames.HOLD_RUNNING_LOT)) {
                    lotTransmap.put("transComments", transComments);
                } else {
                    transRrns.add(transRrn);
                    Consumer<Map<Long,String>> callback=(Map<Long,String> transMap)->{
                        String transReason = MapUtils.getString(transMap,transRrn);
                        if (StringUtils.equalsIgnoreCase(transComments, transReason)) {
                            lotTransmap.put("transComments", transReason);
                        } else {
                            lotTransmap.put("transcomments", transComments + " " + transReason);
                        }
                        lotTransmap.remove("transCommentsCallBack");
                    };
                    lotTransmap.put("transCommentsCallBack",callback );
                }
            }

            lotTransmap.put("process_rrn", row.get( "PROCESS_RRN"));
            lotTransmap.put("stageId", row.get("STAGE_ID"));
            lotTransmap.put("operation_rrn", row.get("OPERATION_RRN"));
            lotTransmap.put("eqpt_rrn", row.get("EQPT_RRN"));
            lotTransmap.put("lotPollutionLevel", row.get("POLLUTION_LEVEL"));
            lotTransmap.put("lotPollutionLevelAfter", row.get("POLLUTION_LEVEL_AFTER"));
            lotTransmap.put("operationSeq", row.get("OPERATION_SEQ"));
            lotTransmap.put("routeSeq", row.get("ROUTE_SEQ"));
            lotTransmap.put("hotFlag", row.get("HOT_FLAG"));
            int priority = MapUtils.getInteger(row, "PRIORITY",0);
            lotTransmap.put("priority", priority == 0 ? "" : String.valueOf(priority));

            lotTransmap.put("createCategory", row.get("CREATE_CATEGORY"));
            lotTransmap.put("runRrn", row.get("RUN_RRN"));

            String processStepString = MapUtils.getString(row, "PROCESS_STEP_VERSION");
            int firstSep = processStepString.indexOf("|");
            int secondSep = processStepString.indexOf("|", firstSep + 1);
            String routeRrn;
            if (secondSep >= 0) {
                routeRrn = processStepString.substring(firstSep + 1, processStepString.indexOf(",", firstSep));
            } else {
                routeRrn = processStepString.substring(0, processStepString.indexOf(","));
            }
            long recipeLogicalRrn = MapUtils.getLong(row, "RECIPE_LOGICAL_RRN",0l);
            lotTransmap.put("recipe_logical_rrn", recipeLogicalRrn);
            if (recipeLogicalRrn != 0) {
                String recipeParam = MapUtils.getString(row, "RECIPE_STRING");
                lotTransmap.put("recipeParam", recipeParam);
            }
            lotTransmap.put("recipePhysicalId", row.get("RECIPE_PHYSICAL_ID"));
            lotTransmap.put("routeRrn", routeRrn);
            lotTransmap.put("reasonCode", row.get("REASON_CODE"));
            lotTransmap.put("reason", row.get("REASON"));
            lotTransmap.put("from_lot_id", row.get("SOURCE_LOT_ID"));
            lotTransmap.put("to_lot_id", row.get("TARGET_LOT_ID"));
            lotTransmap.put("step_sequence", row.get("STEP_SEQUENCE"));
            lotTransmap.put("processStepIdVersion", row.get("PROCESS_STEP_ID_VERSION"));
            lotTransmap.put("processStepVersion", processStepString);
            long reticleRrn = MapUtils.getLong(row, "RETICLE_RRN",0L);
            lotTransmap.put("reticle_rrn", reticleRrn);
            lotTransmap.put("targetUnitId", row.get("TARGET_UNIT_ID"));
            result.add(lotTransmap);
        }

        private String parseRoute(String processStepIdVersion) {
            String routeId = null;

            if (StringUtils.isNotEmpty(processStepIdVersion)) {
                int start = processStepIdVersion.indexOf("|");
                int _start = processStepIdVersion.lastIndexOf("|");
                if (start == _start) {
                    start = -1;
                }

                int end = processStepIdVersion.indexOf(",", start);

                routeId = processStepIdVersion.substring(start + 1, end);
            }

            return routeId;
        }

    }