/*
  20230302 ljh 测试类 ESignFormHandlerTest 可以 FrameNumUploadControllerTest 也可以
 */
public without sharing class ESignFormHandler extends Oly_TriggerHandler {
    private Map<Id, eSignForm__c> newMap;
        private Map<Id, eSignForm__c> oldMap;
        private List<eSignForm__c> newList;
        private List<eSignForm__c> oldList;

    public ESignFormHandler() {

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

    protected override void beforeInsert() {
        beforeExecute();
    }

    protected override void beforeUpdate() {
        beforeExecute();
    }
    protected override void afterInsert() {
        // afterExecute();
        // 进口单证 start
        // 签收单生成后插入进口单证对应的数据
        insertFrameNumManage();
        // 进口单证 end
    }

     protected override void afterUpdate() {
        // afterExecute();
    }

    private void beforeExecute() {
        Map<Id,eSignForm__c> accMap = new Map<Id,eSignForm__c>();
        for(eSignForm__c nObj : newList) {
            //DN号不为空
            if(String.isNotBlank(nObj.DNNameNo0__c)){
                Blob bodyBlob=Blob.valueOf(nObj.DNNameNo0__c);
                nObj.DNName0_base64__c =String.valueOf(EncodingUtil.base64Encode(bodyBlob));
            }
            //经销商管理编码不为空
            if(String.isNotBlank(nObj.AgencyNum__c)){
                Blob bodyBlob=Blob.valueOf(nObj.AgencyNum__c);
                nObj.AgencyNum_base64__c = String.valueOf(EncodingUtil.base64Encode(bodyBlob));
            }
        }
    }

    // private void afterExecute() {
    //     List<eSignFormEntry__c>  eSignFormEntryList = new List<eSignFormEntry__c>();

    //     for (eSignForm__c eSignForm : newList) {

    //             if(eSignForm.finalUpadteFrom__c != null){
    //                 eSignFormEntry__c es = new eSignFormEntry__c();
    //                 es.id = eSignForm.finalUpadteFrom__c;
    //                 es.IsHandled__c = true;
    //                 eSignFormEntryList.add(es);
    //             }

            
    //         }
    //     if(eSignFormEntryList.size() > 0){
    //         update eSignFormEntryList;
    //     }        
    // }

    private void insertFrameNumManage() {
        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 (eSignForm__c nObj : newList) {
            IdSet.add(nObj.Statu_Achievements_DN__c);
        }
        Map<String,Statu_Achievements_DN_details__c> sandMap = new Map<String,Statu_Achievements_DN_details__c>();
        Map<String,Statu_Achievements_DN_details__c> UpdateSandMap = new Map<String,Statu_Achievements_DN_details__c>();
        Map<String,String> UpdateFIdMap = new Map<String,String>();
        // 查询明细 在机身号管理中有没，没有生成,有更新 签收单、发货DN明细、保有设备
        for(Statu_Achievements_DN_details__c dn:[SELECT Id,SerialNumber__c,asset__c,
                    Statu_Achievements_DN__c,SerialNoorLotNo_Raw__c,TracingCode_Raw__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 Statu_Achievements_DN__c =:IdSet ]){
            sandMap.put(dn.TracingCode_Raw__c,dn);
            DNMap.put(dn.TracingCode_Raw__c,dn.Statu_Achievements_DN__r.Name);
            // 取所有发货日
            dateset.add(dn.Statu_Achievements_DN__r.DeliveryDate__c);
        }
        for(FrameNumManage__c f:[SELECT Id,ManagementCode__c  FROM FrameNumManage__c WHERE ManagementCode__c in :sandMap.keySet()]){
            // 更新机身号管理
            UpdateSandMap.put(f.ManagementCode__c,sandMap.get(f.ManagementCode__c));
            UpdateFIdMap.put(f.ManagementCode__c, f.Id);
            sandMap.remove(f.ManagementCode__c);
        }
        for(eSignForm__c es: newList){
            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>();
            for (Statu_Achievements_DN_details__c s:sandMap.values()) {
                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+')';
                temp.SerialNumber__c = s.SerialNoorLotNo_Raw__c;
                temp.Num__c = s.QTY__c;
                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;
                temp.signInForm__c = eSignFormMap.get(s.TracingCode_Raw__c);
                
                // 设置发货日和奥林巴斯日历
                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;
                // Database.executeBatch(new SetFrameNumManageBatch(), 100);
            }    
        }
        if(UpdateSandMap.size() > 0){
            List<FrameNumManage__c> upfnmList = new List<FrameNumManage__c>();
            for (Statu_Achievements_DN_details__c s:UpdateSandMap.values()) {
                FrameNumManage__c temp = new FrameNumManage__c();
                temp.id = UpdateFIdMap.get(s.TracingCode_Raw__c);
                temp.Statu_Achievements_DN_details__c = s.Id;
                temp.Asset__c = s.asset__c;
                temp.signInForm__c = eSignFormMap.get(s.TracingCode_Raw__c);
                upfnmList.add(temp);
            }
            update upfnmList;
        }
    }
}