public without sharing class ReceivingNoteSummaryHandler extends Oly_TriggerHandler {

    private Map<Id, ReceivingNoteSummary__c> newMap;
    private Map<Id, ReceivingNoteSummary__c> oldMap;
    private List<ReceivingNoteSummary__c> newList;
    private List<ReceivingNoteSummary__c> oldList;

    public static String userId = UserInfo.getUserId();
    public static String nowStr = Datetime.now().format('yyyyMMddHHmmssms');

    public ReceivingNoteSummaryHandler() {
        this.newMap = (Map<Id, ReceivingNoteSummary__c>) Trigger.newMap;
        this.oldMap = (Map<Id, ReceivingNoteSummary__c>) Trigger.oldMap;
        this.newList = (List<ReceivingNoteSummary__c>) Trigger.new;
        this.oldList = (List<ReceivingNoteSummary__c>) Trigger.old;
    }

    protected override void beforeInsert() {
        beforeSetValue();
        setTextToHash();
    }
    protected override void beforeUpdate() {
        beforeSetValue();
        setTextToHash();
    }

    protected override void afterUpdate() {
        List<Id> rnsIdList = new List<Id>();
        for (ReceivingNoteSummary__c nObj : newList) {
            ReceivingNoteSummary__c oObj;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
            }
            if(oObj.Fixture_Set__c != nObj.Fixture_Set__c) {
                rnsIdList.add(nObj.Id);
            }
        }
        if(rnsIdList.size()>0){
            cleanFixtureDetails(rnsIdList);
        }
        //add by rentx 20210726 start 154P课题_SAP传输至SFDC后推送
        setNoteModelNoEmail();
    }


    private void setNoteModelNoEmail() {
        Map<Id,Map<String,Integer>> idMessageMap = new Map<Id,Map<String,Integer>>();
        for (ReceivingNoteSummary__c notrdetail : newList) {
            //给 第一条收货清单配套一览明细 的时候 给发邮件的那个赋值
            if (Trigger.isUpdate && notrdetail.First_RND__c != null && oldMap.get(notrdetail.Id).First_RND__c == null) {

                //IDmessageMap 的key 用于过滤 备品收货清单
                //value 是 一览上的备品配套明细型号 * 个数 的map
                if (!idMessageMap.containsKey(notrdetail.ReceivingNote__c)) {
                    idMessageMap.put(notrdetail.ReceivingNote__c, new Map<String,Integer>());
                }
                if (notrdetail.Fixture_Model_No_F__c != null && notrdetail.Fixture_Model_No_F__c != '') {
                    Map<String,Integer> tempMap = idMessageMap.get(notrdetail.ReceivingNote__c);
                    if(!tempMap.containsKey(notrdetail.Fixture_Model_No_F__c)){
                        tempMap.put(notrdetail.Fixture_Model_No_F__c,0);
                    }
                    Integer num = tempMap.get(notrdetail.Fixture_Model_No_F__c);
                    num = num + 1;
                    tempMap.put(notrdetail.Fixture_Model_No_F__c,num);
                    idMessageMap.put(notrdetail.ReceivingNote__c, tempMap);
                }
            }
        }

        //设置收货清单的值 然后更新
        if (idMessageMap.size() > 0) {
            List<ReceivingNote__c> updateList = new List<ReceivingNote__c>();
            List<ReceivingNote__c> updateTempList = new List<ReceivingNote__c>();
            for (Id suId : idMessageMap.keySet()) {
                ReceivingNote__c receNote = new ReceivingNote__c();
                ReceivingNote__c receNoteTemp = new ReceivingNote__c();
                receNote.Id = suId;
                receNoteTemp.Id = suId;
                Map<String,Integer> tempMap = idMessageMap.get(suId);
                String message1 = '';
                for(String key : tempMap.keySet()){
                    message1 += key+'&nbsp;*&nbsp;'+tempMap.get(key);
                    message1 += '<BR>';
                }
                receNoteTemp.Fixture_Model_No_Email__c = null;
                receNote.Fixture_Model_No_Email__c = message1;


                receNoteTemp.Reception_mail_address__c = null;

                updateList.add(receNote);
                updateTempList.add(receNoteTemp);

            }

            if (updateList.size() > 0) {
                update updateList;
                update updateTempList;
            }
        }
        





        //这个是获取当前收货清单下所有的一览 的个数 再赋值 start ------
        /*for (ReceivingNoteSummary__c nObj : newList) {
            //给 第一条收货清单配套一览明细 的时候 给发邮件的那个赋值
            if (Trigger.isUpdate && nObj.First_RND__c != null && oldMap.get(nObj.Id).First_RND__c == null) {
                idMessageMap.put(nObj.ReceivingNote__c, new Map<String,Integer>());
            }
        }
        if (idMessageMap.size() > 0) {
            List<ReceivingNoteSummary__c> sumList = [select id,Fixture_Model_No_F__c,ReceivingNote__c from ReceivingNoteSummary__c where ReceivingNote__c in :idMessageMap.keySet()];

            if (sumList != null && sumList.size() > 0) {
                for (ReceivingNoteSummary__c notrdetail : sumList) {

                    if (!idMessageMap.containsKey(notrdetail.ReceivingNote__c)) {
                        idMessageMap.put(notrdetail.ReceivingNote__c, new Map<String,Integer>());
                    }
                    if (notrdetail.Fixture_Model_No_F__c != null && notrdetail.Fixture_Model_No_F__c != '') {
                        Map<String,Integer> tempMap = idMessageMap.get(notrdetail.ReceivingNote__c);
                        if(!tempMap.containsKey(notrdetail.Fixture_Model_No_F__c)){
                            tempMap.put(notrdetail.Fixture_Model_No_F__c,0);
                        }
                        Integer num = tempMap.get(notrdetail.Fixture_Model_No_F__c);
                        num = num + 1;
                        tempMap.put(notrdetail.Fixture_Model_No_F__c,num);
                        idMessageMap.put(notrdetail.ReceivingNote__c, tempMap);
                    }
    
                    
                }
            }
            List<ReceivingNote__c> updateList = new List<ReceivingNote__c>();
            for (Id suId : idMessageMap.keySet()) {
                ReceivingNote__c receNote = new ReceivingNote__c();
                receNote.Id = suId;
                Map<String,Integer> tempMap = idMessageMap.get(suId);
                String message1 = '';
                for(String key : tempMap.keySet()){
                    message1 += key+'&nbsp;*&nbsp;'+tempMap.get(key);
                    message1 += '<BR>';
                }
                receNote.Fixture_Model_No_Email__c = message1;

                updateList.add(receNote);

            }

            if (updateList.size() > 0) {
                update updateList;
            }
        }*/
        //这个是获取当前收货清单下所有的一览 的个数 再赋值 end ------
    }

    //add by rentx 20210726 end 154P课题_SAP传输至SFDC后推送


    private void cleanFixtureDetails(List<Id> rnsIdList) {
        List<ReceivingNoteDetail__c> rndList = [
                SELECT Id
                     , RNDAssert__c
                  FROM ReceivingNoteDetail__c
                 WHERE ReceivingNoteSummary__c IN :rnsIdList];
        List<ReceivingNoteDetail__c> updateRndList = new List<ReceivingNoteDetail__c>();
        List<ReceivingNoteDetail__c> deleteRndList = new List<ReceivingNoteDetail__c>();
        for(ReceivingNoteDetail__c rnd : rndList) {
            if(String.isNotBlank(rnd.RNDAssert__c)) {
                rnd.Fixture_Set_Detail__c = null;
                rnd.Fixture_Arrival_Process__c = null;
                rnd.Fixture_Arrival_Product__c = null;
                rnd.Product_Arrival_Product__c = null;
                rnd.Set_Arrival_Product__c = null;
                updateRndList.add(rnd);
            }
            else {
                deleteRndList.add(rnd);
            }
        }
        FixtureUtil.withoutUpdate(updateRndList);
        FixtureUtil.withoutDelete(deleteRndList);
    }
    // 以收货清单为单位发清点NG邮件，此处不需要
    //private void sendEmail() {
    //    Set<Id> needSendEmailSet = new Set<Id>();
    //    for (ReceivingNoteSummary__c nObj : newList) {
    //        ReceivingNoteSummary__c oObj;
    //        if (Trigger.isUpdate) {
    //            oObj = oldMap.get(nObj.Id);
    //        }
    //        if (Trigger.isUpdate) {
    //            if (oObj.Wei_Acceptance_Cnt__c != nObj.Wei_Acceptance_Cnt__c
    //                    && nObj.Wei_Acceptance_Cnt__c == 0) {
    //                needSendEmailSet.add(nObj.Id);
    //            }
    //        }
    //    }
    //    if (needSendEmailSet.size() > 0) {
    //        List<ReceivingNoteDetail__c> rndList = [SELECT Id
    //                    , ReceivingNoteSummary__c
    //                    , Name
    //                    , Change_Point__c
    //                    , AcceptanceResult__c
    //                 FROM ReceivingNoteDetail__c
    //                WHERE ReceivingNoteSummary__c = :needSendEmailSet
    //                  AND Delete_Flag__c = false
    //                  AND (AcceptanceResult__c = 'NG'
    //                          OR Change_Point__c = '增加'
    //                          OR Change_Point__c = '新建'
    //                      )
    //                ORDER BY ReceivingNoteSummary__c];
    //        if (rndList.size() > 0) {
    //            List<ReceivingNoteSummary__c> rnmList = new List<ReceivingNoteSummary__c>();
    //            List<ReceivingNoteSummary__c> rnmList1 = new List<ReceivingNoteSummary__c>();
    //            String oRnsId = '';
    //            String message = '';
    //            for (ReceivingNoteDetail__c rnd : rndList) {
    //                if (String.isBlank(oRnsId)) {
    //                    oRnsId = rnd.ReceivingNoteSummary__c;
    //                }
    //                if (oRnsId != rnd.ReceivingNoteSummary__c) {
    //                    ReceivingNoteSummary__c rns1 = new ReceivingNoteSummary__c(Id = oRnsId, Acceptance_NG_Email__c = null);
    //                    ReceivingNoteSummary__c rns2 = new ReceivingNoteSummary__c(Id = oRnsId, Acceptance_NG_Email__c = message);

    //                    rnmList.add(rns1);
    //                    rnmList1.add(rns2);
    //                    message = '';
    //                }
    //                oRnsId = rnd.ReceivingNoteSummary__c;
    //                message += rnd.Name + ':  ' + rnd.AcceptanceResult__c + '  ' + rnd.Change_Point__c + '\r\n';
    //            }
    //            if (String.isNotBlank(oRnsId)) {
    //                ReceivingNoteSummary__c rns1 = new ReceivingNoteSummary__c(Id = oRnsId, Acceptance_NG_Email__c = null);
    //                ReceivingNoteSummary__c rns2 = new ReceivingNoteSummary__c(Id = oRnsId, Acceptance_NG_Email__c = message);
    //                rnmList.add(rns1);
    //                rnmList1.add(rns2);
    //            }
    //            update rnmList;
    //            update rnmList1;
    //        }
    //    }
    //}

    private void setTextToHash() {
        for (ReceivingNoteSummary__c nObj : newList) {
            ReceivingNoteSummary__c oObj;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
            }
            // if (Trigger.isInsert
            //         || oObj.No_Matching_Reminder_Email_Text__c !=  nObj.No_Matching_Reminder_Email_Text__c) {
            //     nObj.No_Matching_Reminder_Email_Hash__c = ReceivingNoteHandler.getHash('SHA-256', nObj.No_Matching_Reminder_Email_Text__c);
            // }
        }
    }


    public void beforeSetValue() {
        for (ReceivingNoteSummary__c nObj : newList) {
            nObj.Name = nObj.ReceivingNoteSummaryNo__c;
            nObj.Loaner_centre_mail_address__c = nObj.Loaner_centre_mail_address_F__c;
            if (String.isNotBlank(nObj.Fixture_Set__c)) {
                nObj.Select_Fixture_Set_Key__c = nObj.Fixture_Set__c + ':' + userId + ':' + nowStr;
            }
        }
    }
}