liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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;
        }                   
 
    }
}