/**
 * MakeAssetHistory.triggerを動かすために一部ロジックを追加
 * 填写完毕 後、明細のAssetを更新、MakeAssetHistory.triggerにて履歴を削除、更新する
 */
public without sharing class OFSInsReportAssetHistoryController {
    // MakeAssetHistory.trigger用、何回も呼び出すので、ここで項目が重複しないようにするため
    public static Map<Id, Map<String, AssetHistory__c>> astHistoryMap = new Map<Id, Map<String, AssetHistory__c>>();
    public static Map<Id, Map<String, AssetHistory__c>> astHistoryUpsertMap = new Map<Id, Map<String, AssetHistory__c>>();

    // MakeAssetHistory.trigger用のメソッド
    public static void upsertAssetHistory(List<AssetHistory__c> ahList) {
        if (ahList.size() > 0) {
            upsert ahList;
        }
    }
    
    // MakeAssetHistory.trigger用のメソッド
    public static void upsertExtAssetHistory(List<AssetHistory__c> ahUpsertList) {
        if (ahUpsertList.size() > 0) {
            upsert ahUpsertList Asset_Inspection_ExtID__c;
        }
    }

    public static void delAssetHistoryExtIdList(List<String> ahDelExtIdList) {
        if (ahDelExtIdList.size() > 0) {
            List<AssetHistory__c> ahList = [Select Id from AssetHistory__c where Asset_Inspection_ExtID__c IN: ahDelExtIdList];
            if (ahList.size() > 0) delete ahList;
        }
    }

    public static void delAssetHistory() {
        List<AssetHistory__c> ahList = [Select Id from AssetHistory__c where AssetId__c = null];
        if (ahList.size() > 0) delete ahList;
    }
    
    public static void upsertInspection_Report(Inspection_Report__c ir) {
        system.debug(ir.Id);
        upsert ir;
    }

    public static void deleteInspection_Item(Inspection_Report__c ir, List<Inspection_Item__c> irItems) {
        if (irItems.size() > 0) {
            delete irItems;
            if (ir.status__c != '草案中') {
                List<Id> astIdList = new List<Id>();
                List<Asset> ahDelList = new List<Asset>();
                for (Inspection_Item__c irItem : irItems) {
                    if (irItem.AssetId__c != null) {
                        astIdList.add(irItem.AssetId__c);
                    }
                }
                
                if (astIdList.size() > 0) {
                    for (Asset ast : [Select Id, Inspection_report_number__c from Asset where Id IN :astIdList]) {
                        // 同じ点検番号のもの再度提出時、AssetHistoryを作り直します
                        if (ast.Inspection_report_number__c == ir.Name) {
                            ast.Inspection_report_number__c = '';
                            ast.Inspection_Report__c = null;
                            ast.Inspection_Result__c = '';
                            ast.Last_inspection_staff__c = null;
                            ast.Final_Examination_Date__c = null;
                            ast.Inspection_Comment__c = '';
                            ahDelList.add(ast);
                        }
                    }
                    if (ahDelList.size() > 0) update ahDelList;
                }
            }
        }
    }

    public static void upsertInspection_Item(Inspection_Report__c ir, List<Inspection_Item__c> irItems) {
        if (irItems.size() > 0) {
            upsert irItems;
            //2021-05-12  mzy  update LJPH-BZPB7W SFDC点检报告更新逻辑修改 start 
            //现在想要的是通过batch来更新保有设备的信息,在点检报告书提交时只把标识 √勾上就行
            //因为报告书提交的时候状态会发生改变,触发器(InspectionReportTriggerHandler)里会自动给报告书进行 打勾√ 
           /* List<Asset> ahList = new List<Asset>();
            for (Inspection_Item__c irItem : irItems) {
                if (irItem.AssetId__c != null) {
                    if (ir.status__c != '草案中') {
                        ahList.add(new Asset(Id = irItem.AssetId__c,
                            Inspection_report_number__c = ir.Name,
                            Inspection_Report__c = ir.id,
                            Inspection_Result__c = irItem.Inspection_Result__c,
                            Last_Inspection_staff__c = ir.Reporter__c,
                            Final_Examination_Date__c = ir.Inspection_Date__c,
                            Inspection_Comment__c = irItem.Inspection_Comment__c
                        ));
                    } else {
                        // TODO 同じ点検番号のもの再度提出時、AssetHistoryを作り直しますので、ここは AssetHistoryを削除しなくでもいいでしょう
                    }
                }
            }
            if (ahList.size() > 0) {
                update ahList;
            }*/
            //2021-05-12  mzy  update LJPH-BZPB7W SFDC点检报告更新逻辑修改 end
        }
    }
}