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);
    }
}