高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
/**
 * 系统每个月末最后一天运行Batch统计日报中对应这个客户以下项目
 * 巡检次数、上门次数、点检次数、点检设备数、培训次数、维修委托书填写、更新日期
 */
global class UpdateMonthlyContactBatch implements Database.Batchable<sObject> {
 
    public string testid;
    public List<String> idList;
    Boolean IsNeedExecute = false; // 2021-03-03  mzy  WLIG-BYHD79  SFDC环境batch合并调查   是否符合执行条件
 
    global UpdateMonthlyContactBatch() {
        idList = null;
 
    }
    global UpdateMonthlyContactBatch(List<String> idlist) {
        this.idList = idlist;
    }
    global UpdateMonthlyContactBatch(string testid) {
        this.testid = testid;
    }
 
    //2021-03-03  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
    global UpdateMonthlyContactBatch(Boolean needExecute) {
        idList = null;
        this.IsNeedExecute = needExecute;
    }
    //2021-03-03  mzy  WLIG-BYHD79  SFDC环境batch合并调查  end
 
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String sql = 'select id, Name,Campaign__c,pollingTime__c,VisitTime__c,InspectTime__c,InspectEquipmentTime__c,' +
                     'TeachingTime__c, ServiceBookInput__c from Contact where Campaign__c !=null ';
        if (idList != null ) {
            sql += ' and id in: idList';
        }
        if (!string.isblank(testid)) {
            sql +=  ' and id =: testid';
        }
 
        // 2021-03-03  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
        //  每月 1日   不满足运行时间,可以使查询到的结果为空
        //Date mon1stDate = Date.newInstance(today.year(), today.month(), 1);
        Integer executeMonth = Date.today().month();
        Integer executeDay = Date.today().day();
        Boolean inexecutionDateFlag = true;
        if (executeDay == 1){
              inexecutionDateFlag = false;
        }
        if(inexecutionDateFlag && IsNeedExecute ==true){
            sql = 'Select Id from Contact where Name = \'\' AND Name != \'\' ';
        }
        //2021-03-03  mzy  WLIG-BYHD79  SFDC环境batch合并调查 end
 
        return Database.getQueryLocator(sql);
    }
 
    global void execute(Database.BatchableContext BC, List<Contact> Contactlist) {
        List<InstructStatusMonthly__c> TMCList = new List<InstructStatusMonthly__c>();
        String temp = '';
        Integer m = 0;
        if (Date.today().day() == 1 && Date.today().month() != 1) {
            m =  Date.today().month() - 1;
        } else if (Date.today().day() == 1 && Date.today().month() == 1) {
            m = 12;
        } else {
            m = Date.today().month();
        }
 
        for (Contact clist : Contactlist) {
            InstructStatusMonthly__c TMC = new InstructStatusMonthly__c();
            TMC.Name = clist.Name;
            TMC.Campaign__c = clist.Campaign__c;
            TMC.pollingTime__c = clist.pollingTime__c;
            TMC.VisitTime__c = clist.VisitTime__c;
            TMC.InspectTime__c = clist.InspectTime__c;
            TMC.InspectEquipmentTime__c = clist.InspectEquipmentTime__c;
            TMC.TeachingTime__c = clist.TeachingTime__c;
            TMC.ServiceBookInput__c = clist.ServiceBookInput__c;
            TMC.Contact__c = clist.id;
            Integer y = Date.today().month() == 1 ? Date.today().year() : Date.today().year() - 1;
            TMC.CreatedYear__c = String.valueOf(y);
            TMC.CreatedMonth__c = m + '';
            TMC.CreatedDate__c = Date.today();
            TMCList.add(TMC);
        }
        insert TMCList;
 
 
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
 
}