binxie
2024-01-18 22bd41f13147fce1df6ff35b592720c9cb387de9
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
trigger TrackConsumableSalesForecastTrigger on TrackConsumableSalesForecast__c (before insert, before update) {
 
    //deloitte-zhj 20231124 本地化导入 start
    if((!Test.isRunningTest())&&System.Label.ByPassTrigger.contains(UserInfo.getUserId())){
        return;
    }
    //deloitte-zhj 20231124 本地化导入 end
    
    String recordIdET = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ET').getRecordTypeId();
    String recordIdENG = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ENG').getRecordTypeId();
    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
        List<TrackConsumableSalesForecast__c> newList = Trigger.new;
        for(TrackConsumableSalesForecast__c tcsf : newList){
            //根据产品分类分配记录类型
            if(tcsf.Type__c == 'ENG'){
                tcsf.RecordTypeId = recordIdENG;
            }else {
                tcsf.RecordTypeId = recordIdET;
            }
            //根据快照月取到半年前
            Date sixMonthAgo;
            Date lastMonth;
            Integer year = tcsf.SnapshotMonth__c.year();
            Integer month = tcsf.SnapshotMonth__c.month();
            //失单预警赋值,不为1就为0
            if(tcsf.BillLostWarning__c != 1){
                tcsf.BillLostWarning__c = 0;
            }
            //医院/分类2赋值
            if(tcsf.HospitalAndCategory2__c == null || tcsf.HospitalAndCategory2__c == ''){
                tcsf.HospitalAndCategory2__c = tcsf.Management_Code__c + '-' + tcsf.Consumable_Category2__c;
            }
            if(month <= 6){
                sixMonthAgo = Date.newInstance(year-1, 6+month, 1);
            }else {
                sixMonthAgo = Date.newInstance(year, month-6, 1);
            }
            if(month <= 1){
                lastMonth = Date.newInstance(year-1, 11+month, 1);
            }else {
                lastMonth = Date.newInstance(year, month-1, 1);
            }
            if(tcsf.LastMonthDeliveryNumber__c == null){
                tcsf.LastMonthDeliveryNumber__c = 0;
            }
            if(tcsf.LastSixMonthsAverage__c == null){
                tcsf.LastSixMonthsAverage__c = 0;
            }
            if(tcsf.LastShipmentDate__c != null && tcsf.LastShipmentDate__c >= sixMonthAgo){
                if(tcsf.LastShipmentDate__c >= lastMonth && tcsf.LastShipmentDate__c < tcsf.SnapshotMonth__c && tcsf.LastSixMonthsAverage__c == 0){
                    tcsf.SaleStatus1__c = '新开';
                    tcsf.SaleStatus2__c = '新开';
                    tcsf.BillLostWarning__c = 0;
                }else{
                    tcsf.SaleStatus1__c = '存量';
                    if(tcsf.Type__c == 'ENG'){
                        if(tcsf.ThisMonthDeliveryNumber__c == 0 && tcsf.LastMonthDeliveryNumber__c == 0){
                            tcsf.SaleStatus2__c = '存量-';
                        }else if(tcsf.ThisMonthDeliveryNumber__c <= tcsf.LastMonthDeliveryNumber__c){
                            tcsf.SaleStatus2__c = '存量-';
                        }else if(tcsf.ThisMonthDeliveryNumber__c > tcsf.LastMonthDeliveryNumber__c){
                            tcsf.SaleStatus2__c = '存量+';
                        }
                    }else{
                        if(tcsf.ThisMonthDeliveryNumber__c <= tcsf.LastSixMonthsAverage__c){
                            tcsf.SaleStatus2__c = '存量-';
                        }else if(tcsf.ThisMonthDeliveryNumber__c > tcsf.LastSixMonthsAverage__c){
                            tcsf.SaleStatus2__c = '存量+';
                        }
                    }
                }
            }else if(tcsf.ThisMonthDeliveryNumber__c == null || tcsf.ThisMonthDeliveryNumber__c == 0){
                tcsf.SaleStatus1__c = '丢单';
                tcsf.SaleStatus2__c = '丢单';
            }
        }
    }
}