public without sharing class AssetGuaranteeTimeChangeHandler extends Oly_TriggerHandler{
    
    private Map<Id, GuaranteePeriodAlterationApplication__c> newMap;
    private Map<Id, GuaranteePeriodAlterationApplication__c> oldMap;
    private List<GuaranteePeriodAlterationApplication__c> newList;
    private List<GuaranteePeriodAlterationApplication__c> oldList;
    private List<String> idList;
    private List<Asset> assetList;
    private List<String> statu_AchievementsIds;
    public AssetGuaranteeTimeChangeHandler(){
        this.newMap = (Map<Id, GuaranteePeriodAlterationApplication__c>) Trigger.newMap;
        this.oldMap = (Map<Id, GuaranteePeriodAlterationApplication__c>) Trigger.oldMap;
        this.newList = (List<GuaranteePeriodAlterationApplication__c>) Trigger.new;
        this.oldList = (List<GuaranteePeriodAlterationApplication__c>) Trigger.old;
        this.idList = new List<String>();
        this.assetList = new List<Asset>();
        this.statu_AchievementsIds = new List<String>();
    }
    protected override void beforeInsert() {
        ambChange_Account();
    }

    protected override void beforeUpdate(){
        ambChange_Account();
        updateambChange_Account();
    }
    //给"保修期变更申请"的所有人相关字段赋值，用于审批流
    private void ambChange_Account(){

        List<String> ownerIds = new List<String>();
        for(GuaranteePeriodAlterationApplication__c gpaa :newList){
            ownerIds.add(gpaa.OwnerId);
        }
        User ownerUser = null;
        if (ownerUser == null ) {
            ownerUser = [SELECT Id, Name, SalesManager__c, 
                            JingliApprovalManager__c, BuchangApprovalManager__c,
                            BuchangApprovalManagerSales__c, ZongjianApprovalManager__c,
                            TongkuoZongjian__c
                        FROM User WHERE Id = :ownerIds];
        }
        

        for(GuaranteePeriodAlterationApplication__c gpaa :newList){

            gpaa.OwnerManager__c  = ownerUser.SalesManager__c ;
            gpaa.OwnerSectionChief__c  = ownerUser.BuchangApprovalManager__c;
            gpaa.OwnerMajordomo__c = ownerUser.ZongjianApprovalManager__c;
        }

    }

    //审批通过将保修期限赋值给保有设备
    private void updateambChange_Account() {

        //List<String> statu_AchievementsIds = new List<String>();
        Map<String,GuaranteePeriodAlterationApplication__c> updateStatu_AchievementsMap = new Map<String,GuaranteePeriodAlterationApplication__c>();
        List<Statu_Achievements__c> statu_AchievementsList = new List<Statu_Achievements__c>();
        for(GuaranteePeriodAlterationApplication__c gpaa :newList){
            if(oldMap.get(gpaa.Id).get('Change_status__c') != gpaa.Change_status__c && gpaa.Change_status__c == '批准'){
                idList.add(gpaa.Id);
                statu_AchievementsIds.add(gpaa.GpaaStatu_Achievements__c);
                updateStatu_AchievementsMap.put(gpaa.GpaaStatu_Achievements__c,gpaa);
            }
        }

        if (statu_AchievementsIds != null && statu_AchievementsIds.size() > 0) {
            for (String aId : statu_AchievementsIds) {
                Statu_Achievements__c statu_Achievements = new Statu_Achievements__c();
                if (updateStatu_AchievementsMap.containsKey(aId)) {
                    statu_Achievements.Id = aId;
                    statu_Achievements.ChangeWarranty__c = true;
                    if (String.isBlank(updateStatu_AchievementsMap.get(aId).ChangeReason_text__c)) {
                        statu_Achievements.ChangeWarrantyReason__c = updateStatu_AchievementsMap.get(aId).ChangeReason__c;
                    } else {
                        statu_Achievements.ChangeWarrantyReason__c = updateStatu_AchievementsMap.get(aId).ChangeReason_text__c;
                    } 

                    statu_AchievementsList.add(statu_Achievements);
                    
                }

            }

            if (statu_AchievementsList != null && statu_AchievementsList.size() > 0) {
                update statu_AchievementsList;
            }
        }
        

        if(idList != null && idList.size() > 0){
            
            List<GuaranteePeriodAlterationDetail__c> ambcDetailList ;
            ambcDetailList = [select Id,Asset__c,GuaranteeTime__c,Asset__r.guarantee_period__c,
                                    GuaranteePeriodAlteration__r.WarrantyStartDate__c
                                    //GuaranteePeriodAlteration__r.GuaranteeTime__c
                                from GuaranteePeriodAlterationDetail__c
                                where GuaranteePeriodAlteration__c In :idList];

            for (GuaranteePeriodAlterationDetail__c gpaDetail :ambcDetailList){

                Asset asset = new Asset();
                asset.Id = gpaDetail.Asset__c;
                asset.WarrantyChanges__c = true;
                asset.Guarantee_period_for_products__c = gpaDetail.GuaranteeTime__c; 
                asset.ChangeWarrantyStartDate__c = gpaDetail.GuaranteePeriodAlteration__r.WarrantyStartDate__c;
                asset.InstallDate = gpaDetail.GuaranteePeriodAlteration__r.WarrantyStartDate__c;
                assetList.add(asset);
            }
        }
        if(assetList != null && assetList.size() > 0 ){
            update assetList;
        }                   

    }
}