/**
 * On-Callの「On-Call跟进结果」項目を更新する機能を提供します。
 */
public without sharing class OncallFinalStatusManager {

    public static Integer callCount = 0;

    /**
     * 主処理の対象
     */
    public static List<On_Call__c> getOncallList(Set<Id> cncallIds) {
        return [SELECT Id, Name FROM On_Call__C WHERE Id IN :cncallIds];
    }

    /**
     * 主処理の対象
     */
    public static List<Repair__c> getRepairListFromOncalls(List<On_Call__c> oncalls) {
        return [SELECT Id, Name, On_Call_ID__c FROM Repair__c WHERE On_Call_ID__c IN :oncalls];
    }

    /**
     * 主処理の対象
     */
    public static List<QIS_Report__c> getQisListFronOncalls(List<On_Call__c> oncalls) {
        return [SELECT Id, Name, Source_OnCall__c FROM QIS_Report__c WHERE Source_OnCall__c IN :oncalls];
    }

    /**
     * 主処理です。
     */
    public static void setFinalStatus( List<On_Call__c> oncalls, List<Repair__c> repairs, List<QIS_Report__c> qiss) {
        
        if ( callCount > 0) {
            return;
        }
        
        callCount += 1;
        
System.debug( '-----:OnCalls.size()=' + oncalls.size());
System.debug( '-----:repairs.size()=' + repairs.size());
System.debug( '-----:qiss.size()=' + qiss.size());

        Boolean isRepair = false, isQis = false;
        for ( On_Call__c local : oncalls) {
    
            isRepair = false;
            isQis = false;
        
            // 現在注目しているon-callレコード配下に修理レコードがあるか？
            for ( Repair__c repair : repairs) {
                if ( local.Id == repair.On_Call_ID__c) {
                    isRepair = true;
                    break;
                }
            }
            // 現在注目しているon-callレコード配下にQISレコードがあるか？
            for ( QIS_Report__c qis : qiss) {
                if ( local.Id == qis.Source_OnCall__c) {
                    isQis = true;
                    break;
                }
            }
            
            if ( isRepair == true && isQis == true) {
                local.FinalStatus__c = '修理及投诉(QIS)';
            }
            else if ( isRepair == true) {
                local.FinalStatus__c = '修理';
            }
            else if ( isQis == true) {
                local.FinalStatus__c = '投诉(QIS)';
            }
            else {
                local.FinalStatus__c = 'OnCall完毕';
            }
        }
        
    }

    /**
     * On_Call__cの更新
     */
    public static void updOncalls(List<On_Call__c> oncalls) {
        if (oncalls.size() > 0) update oncalls;
    }

    public static List<Maintenance_Contract_Asset__c> selectMaintenance_Contract_Asset_List(Set<ID> assetIds) {
        return [SELECT Asset__c, Maintenance_Contract__c, Maintenance_Contract__r.Status__c, 
        Maintenance_Contract__r.Contract_Start_Date__c, Maintenance_Contract__r.Contract_End_Date__c,
        startDateGurantee_Text__c,endDateGurantee_Text__c,Maintenance_Contract__r.RecordType.DeveloperName,Maintenance_Contract__r.SalesOfficeCode_selection__c,
        Maintenance_Contract__r.RecordType.Name 
        ,Maintenance_Contract__r.URF_Contract__c,Series_MaxRepairCount_F__c,Series_RepairCount_F__c
        //上限合同 LY 20230117 start 
        ,Maintenance_Contract__r.Remaining_Amount__c //剩余金额
        ,Maintenance_Contract__r.Limit_Price__c      //限定金额 
        //上限合同 LY 20230117 end 
        FROM Maintenance_Contract_Asset__c 
        WHERE Asset__c IN :assetIds];
    }

}