Untitled

 avatar
unknown
plain_text
a year ago
13 kB
3
Indexable
public JoomlaMessage jobCalculateAvgPerBox(String shiftDate, String plant, ConnectionStructure conPP) {
    JoomlaMessage rtn = new JoomlaMessage();
    DATQCAverageBox daTQCAvg = null;
    DATQcTrnsHeader daTQTrnsH = null;
    DAMPPSetting daMPpSetting = null;
    DATQcTrnsVisual daTQcTrnsvis = null;
    DAMQcStandarWghtDtl daMQcStandarWghtDtl = null; // PQS-309 Start
    debugMessage(Title + " : jobCalculateAvgPerBox() start.");
    String rtnMessage = "";
    String msgType = MessageLevel.MSG_WARN;
    
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
    Calendar cal = Calendar.getInstance();
    
    HashMap<String, String> mappTwghtMatnr = new HashMap<String, String>();
    HashMap<String, String> mappTwghtEntryVal = new HashMap<String, String>();
    HashMap<String, String> mappMatnrEntryVal = new HashMap<String, String>();
    try {
        daTQCAvg = new DATQCAverageBox(conPP, isDebug, isAlwaysNotify);
        daTQTrnsH = new DATQcTrnsHeader();
        daTQTrnsH.setIsDebug(isDebug);
        daTQTrnsH.setSqlConnection(daTQCAvg.getSqlConnection());
        
        daTQcTrnsvis = new DATQcTrnsVisual();
        daTQcTrnsvis.setIsDebug(isDebug);
        daTQcTrnsvis.setSqlConnection(daTQCAvg.getSqlConnection());
        
        daMPpSetting = new DAMPPSetting();
        daMPpSetting.setIsDebug(isDebug);
        daMPpSetting.setSqlConnection(daTQCAvg.getSqlConnection());

        daMQcStandarWghtDtl = new DAMQcStandarWghtDtl(); // PQS-309 Start
        daMQcStandarWghtDtl.setIsDebug(isDebug); // PQS-309 Start
        daMQcStandarWghtDtl.setSqlConnection(daTQCAvg.getSqlConnection()); // PQS-309 Start
        
        //get settingan parameter_id from table m_pp_setting            
        ArrayList<String> rtnListMppSetting = daMPpSetting.getListValue("param_berat_cw_echeckpoint");
        if(rtnListMppSetting != null && rtnListMppSetting.size() > 0){
            //get current date if shift date not input
            if(shiftDate == null || shiftDate.equals("")){
                Date currDate = cal.getTime();
                shiftDate = formatter.format(currDate);
            }
            
            BETQcTrnsHeader objParam = new BETQcTrnsHeader();
            objParam.setShiftDate(shiftDate);
            objParam.setWerks(plant);
            ArrayList<BETQcTrnsHeader> rtnTrnsH = daTQTrnsH.getListForJobAvg(objParam);
            if(rtnTrnsH != null && rtnTrnsH.size() > 0){
                String parameterIdIn = "";
                String twghtIdIn = "";
                //get list twghtId from t_qc_trns_header
                for (int i = 0; i < rtnTrnsH.size(); i++) {
                    //skip if matnr is null or blank
                    if(rtnTrnsH.get(i).getMatnr() == null || rtnTrnsH.get(i).getMatnr().equals("")){
                        continue;
                    }
                    if(!twghtIdIn.equals("")){
                        twghtIdIn += ",";
                    }
                    twghtIdIn += "'" + rtnTrnsH.get(i).getTwghtId() + "'";
                    
                    //mapping mappTwghtMatnr into twghtid => matnr
                    mappTwghtMatnr.put(String.valueOf(rtnTrnsH.get(i).getTwghtId()), rtnTrnsH.get(i).getMatnr());
                }
                debugMessage(Title + "-mappTwghtMatnr : " + mappTwghtMatnr.toString());
                //get list parameter id from m_pp_setting
                for (int i = 0; i < rtnListMppSetting.size(); i++) {
                    if(!parameterIdIn.equals("")){
                        parameterIdIn += ",";
                    }
                    parameterIdIn += "'" + rtnListMppSetting.get(i) + "'";
                }
                ArrayList<BETQcTrnsVisual> rtnListVis = daTQcTrnsvis.getListForJobAvgperBox(twghtIdIn, parameterIdIn);
                if(rtnListVis.size() > 0){
                    // PQS-309 Start
                    // Extract all matnr values for batch processing
                    List<String> matnrList = new ArrayList<>(mappTwghtMatnr.values());
                    DBSearch dbs = new DBSearch();
                    ArrayList<BEMQcStandarWghtDtl> qcStandarWghtDtlList = daMQcStandarWghtDtl.getList(dbs, matnrList);
                    Map<String, BEMQcStandarWghtDtl> qcStandarWghtDtlMap = new HashMap<>();
                    for (BEMQcStandarWghtDtl detail : qcStandarWghtDtlList) {
                        qcStandarWghtDtlMap.put(detail.matnr, detail);
                    }
                    // PQS-309 End

                    for (int i = 0; i < rtnListVis.size(); i++) {
                        double entryVal = 0.0;
                        String matnr = mappTwghtMatnr.get(rtnListVis.get(i).getTwghtId()); // PQS-309 Start
                        
                        debugMessage(Title + "-matnr yang di proses : " + matnr);
                        if (matnr == null || matnr.isEmpty()) { // PQS-309 Start
                            continue; // PQS-309 Start
                        } // PQS-309 End

                        // PQS-309 Start
                        BEMQcStandarWghtDtl qcStandarWghtDtl = qcStandarWghtDtlMap.get(matnr);
                        if (qcStandarWghtDtl == null) {
                            continue; // skip if no weight standard details found for this matnr
                        }

                        double minWeight = qcStandarWghtDtl.min_berat_cw;
                        double maxWeight = qcStandarWghtDtl.max_berat_cw;

                        if (minWeight == 0.0 && maxWeight == 0.0) {
                            continue; // skip if both min and max weights are zero
                        }
                        // PQS-309 End

                        //if entry value cannot convert to double then continue to next loop data
                        try {
                            entryVal = Double.parseDouble(rtnListVis.get(i).getEntryValue());
                            //check if value between min and max settingan
                            if (!(entryVal >= minWeight && entryVal <= maxWeight)) { // PQS-309 Start
                                continue;
                            } // PQS-309 End
                        } catch(NumberFormatException e) {
                            continue;
                        }
                        //mapping mappTwghtEntryVal into twghtId => entry_value; total data
                        String key = String.valueOf(rtnListVis.get(i).getTwghtId());
                        if(mappTwghtEntryVal.containsKey(key)){
                            String[] spltVal = mappTwghtEntryVal.get(key).split(";");
                            double currVal = Double.parseDouble(spltVal[0]);
                            int totData = Integer.parseInt(spltVal[1]);
                            totData++;
                            currVal += entryVal;
                            String val = String.valueOf(currVal) + ";" + String.valueOf(totData);
                            mappTwghtEntryVal.put(key, val);
                        }else{
                            //default value 1 if twghtId not contains in mapping
                            String val = rtnListVis.get(i).getEntryValue() + ";" + "1";
                            mappTwghtEntryVal.put(key, val);
                        }
                    }
                }
                debugMessage(Title + "-mappTwghtEntryVal : " + mappTwghtEntryVal.toString());
                //Mapping mappMatnrEntryVal into matnr => entry_value;total_data
                if(mappTwghtMatnr.size() > 0){
                    int cntTest = 0;
                    for (Map.Entry<String, String> entry : mappTwghtMatnr.entrySet()) {
                        cntTest++;
                        String twghtId = entry.getKey();
                        String matnr = entry.getValue();
                        String[] entryValTotalSplt = {"0","0"};
                        int totData = 1;
                        double entryVal = 0;
                        if(mappMatnrEntryVal.containsKey(matnr)){
                            
                            if(mappTwghtEntryVal.containsKey(twghtId)){
                                entryValTotalSplt = mappTwghtEntryVal.get(twghtId).split(";");
                            }
                            //current entry value and totdata based on matnr
                            String[] tmpEntryVal = mappMatnrEntryVal.get(matnr).split(";");
                            //add current value from current loop and data inside mapping
                            entryVal = Double.parseDouble(tmpEntryVal[0]) + Double.parseDouble(entryValTotalSplt[0]);
                            totData = Integer.parseInt(tmpEntryVal[1]) + Integer.parseInt(entryValTotalSplt[1]);
                            mappMatnrEntryVal.put(matnr, String.valueOf(entryVal) + ";" + String.valueOf(totData));
                        }else{
                            //new entry value and totdata based on matnr
                            if(mappTwghtEntryVal.containsKey(twghtId)){
                                entryValTotalSplt = mappTwghtEntryVal.get(twghtId).split(";");
                            }
                            entryVal = Double.parseDouble(entryValTotalSplt[0]);
                            
                            mappMatnrEntryVal.put(matnr, String.valueOf(entryVal) + ";" + String.valueOf(entryValTotalSplt[1]));
                        }
                        debugMessage(Title + "-mappTwghtMatnr #" + cntTest + " : twghtId=" + twghtId + ", matnr=" + matnr + " with value=" + String.valueOf(entryVal) + ";" + String.valueOf(totData));
                    }
                }
                debugMessage(Title + "-mappMatnrEntryVal : " + mappMatnrEntryVal.toString());
                // CORE logic to process insert data to table t_qc_avg_wght
                int idxLoop = 0;
                if(mappMatnrEntryVal.size() > 0){
                    ArrayList<BETQCAvgWght> listValue = new ArrayList<BETQCAvgWght>();
                    
                    for (Map.Entry<String, String> entry : mappMatnrEntryVal.entrySet()) {
                        String matnr = entry.getKey();
                        String[] entryValTotdata = entry.getValue().split(";");
                        
                        // mapping to object
                        BETQCAvgWght objTmpInsert = new BETQCAvgWght();
                        objTmpInsert.setWerks(plant);
                        objTmpInsert.setMatnr(matnr);
                        objTmpInsert.setErfme("GR");
                        objTmpInsert.setMeinh("KG");
                        double erfmg = 0.0;
                        if(Double.parseDouble(entryValTotdata[0]) > 0){
                           erfmg = Double.parseDouble(entryValTotdata[0]) / Double.parseDouble(entryValTotdata[1]);
                        }
                        objTmpInsert.setErfmg(erfmg);
                        double menge = 0.0;
                        if(erfmg > 0){
                            menge = erfmg / 1000;
                        }
                        objTmpInsert.setMenge(menge);
                        if(menge > 0.0){
                            listValue.add(objTmpInsert);
                            idxLoop++;
                        }
                        
                        debugMessage(Title + "-objTmpInsert #" + idxLoop + " : " + objTmpInsert.toString());
                    }
                    if(listValue.size() > 0){
                        daTQCAvg.insertOrUpdateRecord(listValue);
                    }
                }
                msgType = MessageLevel.MSG_OK;
                rtnMessage = "Total Data processed " + idxLoop + " data";
            } else {
                rtnMessage = "no data to processed in date " + shiftDate + " and plant " + plant;
                msgType = MessageLevel.MSG_ERR;
            }
        } else {
            rtnMessage = "please maintain table m_pp_setting for parameter_id";
            msgType = MessageLevel.MSG_ERR;
        }
        debugMessage(Title + " : jobCalculateAvgPerBox() finish.");
    } catch (Exception ex) {
        errorMessage(Title + "-getAllList : " + ex.getMessage());
        rtnMessage = "Something went wrong, Please contact your support!";
        ex.printStackTrace();
    } finally {
        if (daTQCAvg != null) {
            daTQCAvg.closeConnection();
            daTQCAvg = null;
        }
    }
    rtn = new JoomlaMessage(rtnMessage, msgType);
    return rtn;
}
Editor is loading...
Leave a Comment