global class InstallationDateShipmentDate implements Database.Batchable < sObject > {
    public String query;
    public List < String > strList;
    global class InstallationInformationRest {
        global InstallationInformation InstallationInformation;
    }
    global class InstallationInformation {
        global NFMUtil.Monitoring Monitoring;
        global InstallationInfo[] InstallationInfo;
    }
    global class InstallationInfo {
        global InstallationInfoH InstallationInfoH;
        global InstallationInfoI[] InstallationInfoI;
    }
    global class InstallationInfoH {
        global String InquiryNo;
        // 电子签收单 start
        global string DeliveryNote;
        // 电子签收单 end
        global String InstallationDateH; // 受信しない
        global String PostingDate;
        global String CompanyCode;
        global String DepartmentCode;
        global String PurposeOfAdvice;
        global String EntryDate;
    }
    global class InstallationInfoI {
        global String MaterialNo;
        global Double Quantity; // 受信しない
        global String SerialNo;
        global String InstallationDateI;
        global String ReturnExchangeReplenishMark; // 返品区分、1の場合、レコード削除
        global String SLMark;
        global String GuaranteePeriod;
        global String GuaranteeType;
        global String Location;
        global String PurposeOfAdvice;
        global String ProvistonPeriod; // 计提年限
    }
    global InstallationDateShipmentDate() {
        this.query = query;
    }
    global InstallationDateShipmentDate(List < String > strList) {
        this.strList = strList;
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        if (strList.size() > 0) {
            return Database.getQueryLocator([SELECT ID, RowDataFlg__c, Log__c,
                createdDate__c, Type__c, Log2__c from BatchIF_Log__c
                where Type__c = 'NFM008'
                AND RowDataFlg__c = TRUE
                AND id in : strList
            ]);
        } else {
            return Database.getQueryLocator([SELECT ID, RowDataFlg__c, Log__c,
                createdDate__c, Type__c, Log2__c from BatchIF_Log__c
                where Type__c = 'NFM008'
                AND RowDataFlg__c = TRUE
                AND createdDate__c >= 2020-12-01 order by createdDate__c
            ]);
        }
    }
    global void execute(Database.BatchableContext BC, list < BatchIF_Log__c > scope) {
        List < InstallationInfo > installationInfoList = new List < InstallationInfo > ();
        Map < String, String > productSerialNumberMap = new Map < String, String > ();
        for (BatchIF_Log__c rowData: scope) {
            String rowDataStr = rowData.Log__c;
            installationInfoList = (List < InstallationInfo > ) JSON.deserialize(rowDataStr, List < InstallationInfo > .class);
        }
        for (InstallationInfo Installation: installationInfoList) {
            if (Installation.InstallationInfoI != null) {
                for (InstallationInfoI infoI: Installation.InstallationInfoI) {
                    if (String.isNotBlank(infoI.MaterialNo) && String.isNotBlank(infoI.SerialNo) && String.isNotBlank(infoI.InstallationDateI)) {
                        String str = infoI.MaterialNo + ':' + infoI.SerialNo;
                        productSerialNumberMap.put(str, infoI.InstallationDateI);
                    }
                }
            }
        }
        System.debug('==================productSerialNumberMap ====================='+productSerialNumberMap);
        List < String > backStrIdList = new List < String > ();
        Map < String, Statu_Achievements__c > statuAchievementsMap = new Map < String, Statu_Achievements__c > ();
        List < Asset > assetList = [select Id, InstallDate, Backorder__c, Backorder__r.Actual_Installation_Day__c, Product_Serial_No__c, SerialNumber from Asset
            where Product_Serial_No__c in : productSerialNumberMap.keySet()
        ];
        Map < String, String > statuAchMap = new Map < String, String > ();
        for (Asset ass: assetList) {
            backStrIdList.add(ass.Backorder__c);
            String strDate = productSerialNumberMap.get(ass.Product_Serial_No__c);
            statuAchMap.put(ass.Backorder__c, strDate);
            if (ass.Backorder__r.Actual_Installation_Day__c == null) {
                Statu_Achievements__c statuAch = new Statu_Achievements__c();
                statuAch.id = ass.Backorder__c;
                statuAch.Actual_Installation_Day__c = NFMUtil.parseStr2Date(strDate, false);
                statuAchievementsMap.put(statuAch.id, statuAch);
            }
        }
        if (statuAchievementsMap.size() > 0) {
            update statuAchievementsMap.values();
        }
        List < Statu_Achievements_DN__c > statuSqlList = [
            select id, InstallDate__c, Statu_Achievements__c from Statu_Achievements_DN__c where Statu_Achievements__c in : backStrIdList
        ];
        for (Statu_Achievements_DN__c statu: statuSqlList) {
            statu.InstallDate__c = NFMUtil.parseStr2Date(statuAchMap.get(statu.Statu_Achievements__c), false);
        }
        update statuSqlList;
    }
    global void finish(Database.BatchableContext BC) {}
}