/**
     * @Author    lijinhuan
     * @DateTime  2023-02-07T17:49:45+0800
     * 测试类：FrameNumUploadControllerTest
*/
public without sharing class DNDetailsHandler  extends Oly_TriggerHandler {

    private Map<Id, Statu_Achievements_DN_details__c > newMap;
    private Map<Id, Statu_Achievements_DN_details__c > oldMap;
    private List<Statu_Achievements_DN_details__c > newList;
    private List<Statu_Achievements_DN_details__c > oldList;
    public DNDetailsHandler() {
        this.newMap = (Map<Id, Statu_Achievements_DN_details__c >) Trigger.newMap;
        this.oldMap = (Map<Id, Statu_Achievements_DN_details__c >) Trigger.oldMap;
        this.newList = (List<Statu_Achievements_DN_details__c >) Trigger.new;
        this.oldList = (List<Statu_Achievements_DN_details__c >) Trigger.old;
    }
    protected override void afterInsert() {
        insertFrameNumManag();
    }
    protected override void afterUpdate() {
        // insertFrameNumManag(); // 测试用   
    }

    protected override void afterDelete() {
        
    }

    protected override void beforeInsert() {
    }
    
    protected override void beforeUpdate() {

    }

    protected override void beforeDelete() {
        // 进口单证 start
        // 删除进口单证数据
        deleteFrameNumManage();
        // 进口单证 end
    }

    private Void insertFrameNumManag(){
        Set<String> IdSet = new Set<String>();
        // 进口单证 增加发货日设置 start
        Set<Date> dateset = new Set<Date>();
        Map<Date, String> olycals = new Map<Date, String>();
        // 进口单证 增加发货日设置 end
        Map<String,Id> eSignFormMap = new Map<String,Id>();
        Map<String,String> DNMap  = new Map<String,String>();
        for (Statu_Achievements_DN_details__c nObj : newList) {
            IdSet.add(nObj.Id);
        }
        // 20231017 ljh DB202308311963 update start
        // Map<String,Statu_Achievements_DN_details__c> sandMap = new Map<String,Statu_Achievements_DN_details__c>();
        Map<String,List<Statu_Achievements_DN_details__c>> sandMap = new Map<String,List<Statu_Achievements_DN_details__c>>();
        // 20231017 ljh DB202308311963 update end
        // 查询明细 在机身编码中有没，没有生成
        for(Statu_Achievements_DN_details__c dn:[SELECT Id,SerialNumber__c,asset__c,
                 Statu_Achievements_DN__c,SerialNoorLotNo_Raw__c,TracingCodeRawSys__c,
                 OTCode_Raw__c,asset__r.Name,QTY__c,Statu_Achievements_DN__r.Name 
                 // 进口单证 增加发货日设置 start
                 , Statu_Achievements_DN__r.DeliveryDate__c 
                 // 进口单证 增加发货日设置 end
                 FROM Statu_Achievements_DN_details__c
                 WHERE Id =:IdSet ]){
            // 20230915 ljh DB202308311963 start
            // sandMap.put(dn.TracingCode_Raw__c,dn);
            // DNMap.put(dn.TracingCode_Raw__c,dn.Statu_Achievements_DN__r.Name);
            List<Statu_Achievements_DN_details__c> tempObjList;
            if(sandMap.containsKey(dn.TracingCodeRawSys__c)){
                tempObjList = sandMap.get(dn.TracingCodeRawSys__c);
            }else{
                tempObjList = new List<Statu_Achievements_DN_details__c>();
            }
            tempObjList.add(dn);                        
            sandMap.put(dn.TracingCodeRawSys__c,tempObjList);
            DNMap.put(dn.TracingCodeRawSys__c,dn.Statu_Achievements_DN__r.Name);
            // 20230915 ljh DB202308311963 end
            // 取所有发货日
            dateset.add(dn.Statu_Achievements_DN__r.DeliveryDate__c);
        }
        for(FrameNumManage__c f:[SELECT ManagementCode__c  FROM FrameNumManage__c WHERE ManagementCode__c in :sandMap.keySet()]){
            sandMap.remove(f.ManagementCode__c);
        }
        for(eSignForm__c es:[SELECT Id,DNName__c FROM eSignForm__c WHERE DNName__c IN: DNMap.values()]){
            for(String key:DNMap.keySet()){
                if(DNMap.get(key) == es.DNName__c){
                    eSignFormMap.put(key,es.Id);
                }
            } 
        }
        // 取发货日的奥林巴斯日历
        for (OlympusCalendar__c oc : [select Id, Date__c from OlympusCalendar__c where Date__c in :dateset]) {
            olycals.put(oc.Date__c, oc.Id);
        }

        if(sandMap.size() > 0){
            List<FrameNumManage__c> infnmList = new List<FrameNumManage__c>();
            // 20231017 ljh DB202308311963 start
            for (String skey:sandMap.keySet()) {
            // for (Statu_Achievements_DN_details__c s:sandMap.values()) {
            // 20231017 ljh DB202308311963 end
                // 20230915 ljh DB202308311963 start
                // if(eSignFormMap.get(s.TracingCode_Raw__c) != null){
                //     FrameNumManage__c temp = new FrameNumManage__c();
                //     temp.Name = s.TracingCode_Raw__c;
                //     temp.ManagementCode__c = s.TracingCode_Raw__c;
                //     temp.SerialNumberS__c = s.SerialNoorLotNo_Raw__c+'('+s.TracingCode_Raw__c+')';
                if(eSignFormMap.get(skey) != null){
                    // 20231017 ljh DB202308311963 start
                    Integer qty =  sandMap.get(skey).size();
                    Statu_Achievements_DN_details__c s = sandMap.get(skey)[0];
                    // 20231017 ljh DB202308311963 end
                    FrameNumManage__c temp = new FrameNumManage__c();
                    temp.Name = s.TracingCodeRawSys__c;
                    temp.ManagementCode__c = s.TracingCodeRawSys__c;
                    temp.SerialNumberS__c = s.SerialNoorLotNo_Raw__c+'('+s.TracingCodeRawSys__c+')';
                    // 20230915 ljh DB202308311963 end
                    temp.SerialNumber__c = s.SerialNoorLotNo_Raw__c;
                    // 20231017 ljh DB202308311963 start
                    // temp.Num__c = s.QTY__c;
                    temp.Num__c = qty;
                    // 20231017 ljh DB202308311963 end
                    temp.CheckOutNo__c  = s.Statu_Achievements_DN__r.Name.subString(2);//发货DN 的name去掉前两位的 00

                    temp.Statu_Achievements_DN_details__c = s.Id;
                    temp.Asset__c = s.asset__c;
                    // 20230915 ljh DB202308311963 start
                    // temp.signInForm__c = eSignFormMap.get(s.TracingCode_Raw__c);
                    temp.signInForm__c = eSignFormMap.get(s.TracingCodeRawSys__c);
                    // 20230915 ljh DB202308311963 end

                    // 设置发货日和奥林巴斯日历
                    temp.DeliveryDate__c = s.Statu_Achievements_DN__r.DeliveryDate__c;
                    temp.DeliveryDate_select__c = temp.DeliveryDate__c != null && olycals.size() > 0 && String.isNotBlank(olycals.get(temp.DeliveryDate__c)) ? olycals.get(temp.DeliveryDate__c) : null;
                    
                    temp.Material__c = s.OTCode_Raw__c;
                    temp.MaterialDepict__c = s.asset__r.Name;
                    infnmList.add(temp);
                }
            }
            if(infnmList.size() > 0){
                insert infnmList;
            }    
        }
    }

    private void deleteFrameNumManage() {
        List<FrameNumManage__c> rnmList = [select Id from FrameNumManage__c where Statu_Achievements_DN_details__c in :oldMap.keySet()];
        if (rnmList != null && rnmList.size() > 0) {
            delete rnmList;
        }
    }
}