/**
     * [SetFrameNumManageBatch 进口单证上传后查找字段batch更新]
     * @Author   lijinhuan
     * @DateTime 2022-11-04T18:31:03+0800
     * @return   []
*/
global  without sharing  class SetFrameNumManageBatch implements Database.Batchable<sObject> {
    public String query;

    global SetFrameNumManageBatch() {
        this.query = query;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'select Id,SerialNumberS__c,InspectionCard__c,ApplyList__c  from FrameNumManage__c where ';
        // query += '(';
        query += ' signInForm__c = null  ';
        query += ' OR Statu_Achievements_DN_details__c = null ';
        query += ' OR Asset__c = null ';
        query += ' OR (InspectionCard_Select__c = null AND InspectionCard__c != null AND InspectionCard__c !=\'不出证\')';
        query += ' OR (InspectionCard_Select__c != null AND (InspectionCard__c =\'不出证\' OR InspectionCard__c = null))';// 清空
        query += ' OR (ApplyList_Select__c = null AND ApplyList__c != null)';
        query += ' OR (ApplyList_Select__c != null AND ApplyList__c = null)';// 清空
        // query += ')';
        // query += ' AND id = \'a5D9D0000000WqF\'';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<FrameNumManage__c> fnmList) {
        Map<String,String> InspectionCardMap = new Map<String,String>();
        Map<String,String> ApplyListMap = new Map<String,String>();
        Map<String,Id> InspectionCardIdMap = new Map<String,Id>();
        Map<String,Id> ApplyListIdMap = new Map<String,Id>();
        Set<String> SerialNumberSet = new Set<String>();
        Set<Id> clearSet = new Set<Id>();
        for(FrameNumManage__c fnm:fnmList){
            // 清空
            if(String.isBlank(fnm.ApplyList__c) 
                || String.isBlank(fnm.InspectionCard__c) 
                ||(!String.isBlank(fnm.InspectionCard__c) && fnm.InspectionCard__c == '不出证')){
                clearSet.add(fnm.Id);
            }else{
                //赋值
                SerialNumberSet.add(fnm.SerialNumberS__c);
                InspectionCardMap.put(fnm.SerialNumberS__c,fnm.InspectionCard__c);
                ApplyListMap.put(fnm.SerialNumberS__c,fnm.ApplyList__c);
            }
            
        }
        // 发货DN明细
        Map<String,Id> DNdetailMap = new Map<String,Id>();
        Map<String,String> DNMap  = new Map<String,String>();
        for(Statu_Achievements_DN_details__c dn:[SELECT Id,SerialNumber__c,Statu_Achievements_DN__c,Statu_Achievements_DN__r.Name FROM Statu_Achievements_DN_details__c WHERE SerialNumber__c IN: SerialNumberSet]){
            DNdetailMap.put(dn.SerialNumber__c,dn.Id);
            DNMap.put(dn.SerialNumber__c,dn.Statu_Achievements_DN__r.Name);
        }
        // 签收单
        Map<String,Id> eSignFormMap = new Map<String,Id>();
        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);
                }
            } 
        }
        // 保有设备
        Map<String,Id> AssetMap = new Map<String,Id>();
        for(Asset ass:[SELECT Id,SerialNumber FROM Asset WHERE SerialNumber IN: SerialNumberSet]){
            AssetMap.put(ass.SerialNumber,ass.Id);
        }
        Set<String> FilesNameSet = new Set<String>();
        FilesNameSet.addAll(InspectionCardMap.values());
        FilesNameSet.addAll(ApplyListMap.values());
        // 商检证   InspectionCard_Select__c 报关单   ApplyList_Select__c
        for(FileAddress__c fa:[SELECT Id,OnlyFileName__c FROM FileAddress__c WHERE OnlyFileName__c IN: FilesNameSet]){
            for(String key:InspectionCardMap.keySet()){
                if(InspectionCardMap.get(key) == fa.OnlyFileName__c){
                    InspectionCardIdMap.put(key,fa.Id);
                }
                if(ApplyListMap.get(key) == fa.OnlyFileName__c){
                    ApplyListIdMap.put(key,fa.Id);
                }
            }
        }
        // 最后整合
        List<FrameNumManage__c> upFnmList = new List<FrameNumManage__c>();
        for(FrameNumManage__c fnm:fnmList){
            FrameNumManage__c temp = new FrameNumManage__c();
            temp.Id = fnm.Id;
            if(clearSet.contains(fnm.Id)){
                temp.InspectionCard_Select__c = null;
                temp.ApplyList_Select__c = null;
            }else{
                temp.Statu_Achievements_DN_details__c = DNdetailMap.get(fnm.SerialNumberS__c);
                temp.signInForm__c  = eSignFormMap.get(fnm.SerialNumberS__c);
                temp.Asset__c = AssetMap.get(fnm.SerialNumberS__c);
                temp.InspectionCard_Select__c = InspectionCardIdMap.get(fnm.SerialNumberS__c);
                temp.ApplyList_Select__c = ApplyListIdMap.get(fnm.SerialNumberS__c);
            }
            upFnmList.add(temp);
        }
        // System.debug('zhelieupFnmList:'+upFnmList.size());
        if(upFnmList.size() > 0){
            update upFnmList;
        }
        
    }

    global void finish(Database.BatchableContext BC) {

    }
}