高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
@isTest
private class OPDPlanMarkBatchTest {
    
    static Date thismonth_Lastday ;
    static Date lastmonth_Lastday ;
    
    static void  basicData() {
        // カスタム設定を定義
        SS_Batch_Column_Mapping__c opdMapping = new SS_Batch_Column_Mapping__c(Name = 'OPDPlan__c', SS_TableName__c='SS_OPDPlan__c');
        opdMapping.From_Column_001__c = 'Id';
        opdMapping.SS_Column_001__c = 'OPDPlan__c.Name';
        opdMapping.From_Column_002__c = 'Name';
        opdMapping.SS_Column_002__c = 'Name';
        insert new SS_Batch_Column_Mapping__c[] {opdMapping};
 
        SS_BatchColumnMapping__c opdMapping2 = new SS_BatchColumnMapping__c(Name = 'OPDPlan__c', SS_TableName__c='SS_OPDPlan__c');
        opdMapping2.From_Column_001__c = 'Id';
        opdMapping2.SS_Column_001__c = 'OPDPlan__c.Name';
        opdMapping2.From_Column_002__c = 'Name';
        opdMapping2.SS_Column_002__c = 'Name';
        insert new SS_BatchColumnMapping__c[] {opdMapping2};
 
 
        Date toDate = Date.today();
        //本月的第一天
        Date mon1stDate = Date.newInstance(toDate.year(), toDate.month(), 1);
        //本月的最后一天
        thismonth_Lastday = Date.toDay().addMonths(1).toStartOfMonth().addDays(-1);
        System.debug('thismonth_Lastday6666' + thismonth_Lastday);
        //上月的第一天
        Date lastmonth_firstday = Date.toDay().addMonths(-1).toStartOfMonth();
        //上月的最后一天
        lastmonth_Lastday = Date.newInstance(toDate.year(), toDate.month(), 1).addDays(-1);
        System.debug('lastmonth_Lastday1234' + lastmonth_Lastday);
        //下月的第一天
        Date nextmonth_firstday = Date.toDay().addMonths(1).toStartOfMonth();
        //下月的最后一天
        Date nextmonth_Lastday = Date.toDay().addMonths(2).toStartOfMonth().addDays(-1);
 
        OPDPlan__c oPDPlan0 = new OPDPlan__c();
        oPDPlan0.Status__c = '计划中';
        oPDPlan0.OPDPlan_ImplementDate__c = Date.toDay();//mon1stDate;
        oPDPlan0.NoOpp_Reason__c = 'HCP对应';   //2020-11-04  mzy  SWAG-BSC5WP   add
        oPDPlan0.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
        insert oPDPlan0;
 
        OPDPlan__c oPDPlan1 = new OPDPlan__c();
        oPDPlan1.Status__c = '计划中';
        oPDPlan1.OPDPlan_ImplementDate__c = thismonth_Lastday;
        oPDPlan1.NoOpp_Reason__c = 'HCP对应';   //2020-11-04  mzy  SWAG-BSC5WP   add
        oPDPlan1.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
        insert oPDPlan1;
 
        OPDPlan__c oPDPlan2 = new OPDPlan__c();
        oPDPlan2.Status__c = '计划中';
        oPDPlan2.OPDPlan_ImplementDate__c = nextmonth_firstday;
        oPDPlan2.NoOpp_Reason__c = 'HCP对应';   //2020-11-04  mzy  SWAG-BSC5WP   add
        oPDPlan2.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
        insert oPDPlan2;
 
        OPDPlan__c oPDPlan3 = new OPDPlan__c();
        oPDPlan3.Status__c = '计划中';
        oPDPlan3.OPDPlan_ImplementDate__c = nextmonth_Lastday;
        oPDPlan3.NoOpp_Reason__c = 'HCP对应';   //2020-11-04  mzy  SWAG-BSC5WP   add
        oPDPlan3.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
        insert oPDPlan3;
 
        OPDPlan__c oPDPlan4 = new OPDPlan__c();
        oPDPlan4.Status__c = '计划中';
        oPDPlan4.OPDPlan_ImplementDate__c = Date.today();//lastmonth_firstday;
        oPDPlan4.NoOpp_Reason__c = 'HCP对应';    //2020-11-04  mzy  SWAG-BSC5WP   add
        oPDPlan4.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
        insert oPDPlan4;
        
        OPDPlan__c oPDPlan5 = new OPDPlan__c();
        oPDPlan5.Status__c = '计划中';
        oPDPlan5.OPDPlan_ImplementDate__c = Date.today();//lastmonth_Lastday;
        oPDPlan5.NoOpp_Reason__c = 'HCP对应';    //2020-11-04  mzy  SWAG-BSC5WP   add
        oPDPlan5.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
        insert oPDPlan5;
    }
    //正常执行
    @isTest 
    static void test_method_one() {
 
        basicData();
        Id execBTId = null;
        System.Test.StartTest();
        List<OPDPlan__c> oPDPlans = [Select Id,Name,Status__c,lastMonth_Plan__c,ThisMonthPlan__c,If_ThisMonth_Plan__c 
                                        from OPDPlan__c
                                         Where (( lastMonth_Plan__c != 0 and ThisMonthPlan__c = 0) or 
                                              ( lastMonth_Plan__c = 0 and ThisMonthPlan__c != 0) or 
                                              ( ThisMonthPlan__c = 0 and If_ThisMonth_Plan__c != 0) or 
                                              ( ThisMonthPlan__c != 0 and If_ThisMonth_Plan__c = 0))
                                              and OPDPlan_ImplementDate__c <= :thismonth_Lastday];
        System.assertEquals(4, oPDPlans.size());
        execBTId = Database.executeBatch(new OPDPlanMarkBatch());
        
        System.Test.StopTest();
        
    }
    ////指定日期执行
    //@isTest 
    //static void test_method_two() {
    //    basicData();
 //       Id execBTId = null;
 //       List<OPDPlan__c> oPDPlans = [Select Id,Name,Status__c,lastMonth_Plan__c,ThisMonthPlan__c,If_ThisMonth_Plan__c 
 //                                       from OPDPlan__c
 //                                        Where (( lastMonth_Plan__c != 0 and ThisMonthPlan__c = 0) or 
 //                                             ( lastMonth_Plan__c = 0 and ThisMonthPlan__c != 0) or 
 //                                             ( ThisMonthPlan__c = 0 and If_ThisMonth_Plan__c != 0) or 
 //                                             ( ThisMonthPlan__c != 0 and If_ThisMonth_Plan__c = 0) or 
 //                                             ( lastMonth_Execute__c = 0 and ThisMonthExecute__c != 0) or
 //                                             ( lastMonth_Execute__c != 0 and ThisMonthExecute__c = 0)) 
 //                                             and OPDPlan_ImplementDate__c <= :lastmonth_Lastday];
 //       System.assertEquals(2, oPDPlans.size());
 //       System.Test.StartTest();
 //       execBTId = Database.executeBatch(new OPDPlanMarkBatch(String.valueOf(lastmonth_Lastday)));
 //       System.Test.StopTest();
    //}
    //指定"OPD计划"的Id执行
    @isTest 
    static void test_method_three() {
        basicData();
        List<String> oPDPlanIds = new List<String>();
        List<OPDPlan__c> oDPlans = [ select Id,Name from OPDPlan__c];
        System.assertEquals(6, oDPlans.size());
        for (OPDPlan__c opdPlan : oDPlans) {
            oPDPlanIds.add(opdPlan.Id);
        }
        Id execBTId = null;
        System.Test.StartTest();
        execBTId = Database.executeBatch(new OPDPlanMarkBatch(oPDPlanIds));
        System.Test.StopTest();
    }
    
}