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
global class Send_Have_Arrival_wh_Email_Batch implements Database.Batchable<sObject>  {
    Boolean IsNeedExecute = false; // 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  是否符合执行条件
 
    global Send_Have_Arrival_wh_Email_Batch() {
 
    }
 
    // 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
    global Send_Have_Arrival_wh_Email_Batch(Boolean NeedExecute) {
        this.IsNeedExecute = NeedExecute;
    }
    // 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  end
 
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator([SELECT Id
                 FROM ReceivingNote__c
                WHERE RN_Status__c = :FixtureUtil.RNStatusMap.get(FixtureUtil.RNStatus.Yi_Ru_Ku.ordinal())
                  AND Send_Arrival_wh_Email__c = false]);
    }
 
    global void execute(Database.BatchableContext BC, List<ReceivingNote__c> rowDataList) {
        Map<Id, List<ReceivingNoteDetail__c>> rndMap = new Map<Id, List<ReceivingNoteDetail__c>>();
        for (ReceivingNoteDetail__c rnd : [SELECT Id
                        , Name
                        , Fixture_Model_No_F__c
                        , Is_First_RND_F__c
                        , ReceivingNote__c
                     FROM ReceivingNoteDetail__c
                    WHERE ReceivingNote__c = :rowDataList
                      AND Delete_Flag__c = false
                    ORDER BY ReceivingNoteDetailNo__c]) {
            if (rndMap.containsKey(rnd.ReceivingNote__c) == false) {
                rndMap.put(rnd.ReceivingNote__c, new List<ReceivingNoteDetail__c>());
            }
            rndMap.get(rnd.ReceivingNote__c).add(rnd);
        }
        List<ReceivingNote__c> rnList = new List<ReceivingNote__c>();
        for (Id rnId : rndMap.keySet()) {
            String emailBody = '';
            ReceivingNoteDetail__c lastRnd = null;
            Integer lastModelCount = 0;
            for (ReceivingNoteDetail__c rnd : rndMap.get(rnId)) {
                if (lastRnd == null){
                    lastRnd = rnd;
                }
 
                if(rnd.Fixture_Model_No_F__c != lastRnd.Fixture_Model_No_F__c){
                    if(lastRnd.Is_First_RND_F__c){
                        emailBody += '\n'+lastRnd.Fixture_Model_No_F__c+' ';
                    }
                    else{
                        emailBody += lastRnd.Fixture_Model_No_F__c;
                        if (lastModelCount > 1){
                            emailBody += ' * ' + lastModelCount;
                        }
                        if(!rnd.Is_First_RND_F__c){
                            emailBody +=',';
                        }
                    }
                    lastModelCount = 0;
                }
                lastRnd = rnd;
                lastModelCount += 1;
            }
            if(lastRnd!=null){
                if(lastRnd.Is_First_RND_F__c){
                    emailBody += '\n'+lastRnd.Fixture_Model_No_F__c+' ';
                }
                else{
                    emailBody += lastRnd.Fixture_Model_No_F__c;
                    if (lastModelCount > 1){
                        emailBody += ' * ' + lastModelCount;
                    }
                }
            }
            ReceivingNote__c rn = new ReceivingNote__c(Id = rnId, Have_Arrival_wh_Email__c = emailBody);
            rnList.add(rn);
        }
        update rnList;
    }
 
    global void finish(Database.BatchableContext BC) {
        // 今回はやることないです
    }
 
    public static void run() {
        Database.executeBatch( new Send_Have_Arrival_wh_Email_Batch(),200);
    }
}