From af7b716a60d889acea95560abba0e46eee008b8f Mon Sep 17 00:00:00 2001
From: buli <137736985@qq.com>
Date: 星期三, 05 七月 2023 13:54:50 +0800
Subject: [PATCH] backup0705

---
 force-app/main/default/classes/Batch_FixAttachmentToFiles.cls |  116 ++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 64 insertions(+), 52 deletions(-)

diff --git a/force-app/main/default/classes/Batch_FixAttachmentToFiles.cls b/force-app/main/default/classes/Batch_FixAttachmentToFiles.cls
index 749b18b..e7b7f7a 100644
--- a/force-app/main/default/classes/Batch_FixAttachmentToFiles.cls
+++ b/force-app/main/default/classes/Batch_FixAttachmentToFiles.cls
@@ -1,57 +1,69 @@
-global without sharing class Batch_FixAttachmentToFiles implements Database.Batchable<sObject>{
-    // Id batchJobId = Database.executeBatch(new Batch_FixAttachmentToFiles(Datetime.newInstance(2023, 5, 20, 8, 0, 0),'Consumable_order__c'),2000);
-    private Datetime endDate = null;
-    private String objectApiName = null;
-    public Batch_FixAttachmentToFiles(Datetime setTime,String objectType) {
-        endDate = setTime;
-        objectApiName = objectType;
+global without sharing class Batch_FixAttachmentToFiles implements Database.Batchable<sObject> {
+  // Id batchJobId = Database.executeBatch(new Batch_FixAttachmentToFiles('Consumable_order__c',Datetime.newInstance(2023, 1, 1, 8, 0, 0),Datetime.newInstance(2024, 1, 1, 8, 0, 0)),2000);
+  private Datetime creStartDate = null;
+  private Datetime creEndDate = null;
+  private String objectApiName = null;
+  private Set<String> parentIds = new Set<String>(); //Add by Li Jun 20230703
+  public Batch_FixAttachmentToFiles(
+    String objectType,
+    Datetime startTime,
+    Datetime endTime
+  ) {
+    creStartDate = startTime;
+    creEndDate = endTime;
+    objectApiName = objectType;
+  }
+  //Add by Li Jun 20230703 Start
+  public Batch_FixAttachmentToFiles(Set<String> parentIds) {
+    this.parentIds = parentIds;
+  }
+  //Add by Li Jun 20230703 End
+  global Database.QueryLocator start(Database.BatchableContext BC) {
+    String queryObject = 'SELECT Id, Name, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM Attachment WHERE Parent.Type =:objectApiName AND CreatedDate >=:creStartDate AND CreatedDate <:creEndDate Order by CreatedDate ASC';
+    if (parentIds.size() > 0) {
+      queryObject = 'SELECT Id, Name, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM Attachment WHERE ParentId in:parentIds';
     }
+    return Database.getQueryLocator(queryObject);
+  }
 
-    global Database.QueryLocator start(Database.BatchableContext BC) {
-        String queryObject = 'SELECT Id FROM ' + objectApiName + ' WHERE CreatedDate >=:endDate' + ' order by CreatedDate desc';
-        System.debug('queryObject:' + queryObject);
-        return Database.getQueryLocator(queryObject);//鍙栨寚瀹歄bject涓嬮潰鐨処d鎴栬�呭鐞嗗叏閮�
+  global void execute(Database.BatchableContext BC, List<Attachment> scope) {
+    List<ContentVersion> insertContents = new List<ContentVersion>();
+    Map<string, id> nameParentMaps = new Map<string, id>();
+    for (Attachment att : scope) {
+      nameParentMaps.put(att.Name, att.ParentId);
+      ContentVersion cVersion = new ContentVersion();
+      cVersion.ContentLocation = 'S'; //S-Document is in Salesforce. E-Document is outside of Salesforce. L-Document is on a Social Netork.
+      cVersion.PathOnClient = att.Name; //File name with extention
+      cVersion.Origin = 'C'; //C-Content Origin. H-Chatter Origin.
+      cVersion.Title = att.Name; //Name of the file
+      cVersion.VersionData = att.Body; //File content
+      insertContents.add(cVersion);
     }
-
-    global void execute(Database.BatchableContext BC, List<sObject> scope) {
-        Set<Id> setId = new Set<Id>();
-        for(sObject sc: scope){
-            setId.add(sc.Id);
-        }
-        List<ContentVersion> insertContents = new List<ContentVersion>();
-        Map<string,id> nameParentMaps = new Map<string,id>();
-        for (Attachment att : [select Id,Name ,OwnerId,ParentId, Body, CreatedById from Attachment where ParentId in: setId Order by Name desc]) {
-            nameParentMaps.put(att.name, att.ParentId);   
-            ContentVersion cVersion = new ContentVersion();
-            cVersion.ContentLocation = 'S';     //S-Document is in Salesforce. E-Document is outside of Salesforce. L-Document is on a Social Netork.
-            cVersion.PathOnClient = att.Name;   //File name with extention
-            cVersion.Origin = 'C';              //C-Content Origin. H-Chatter Origin.   
-            cVersion.Title = att.Name;          //Name of the file
-            cVersion.VersionData = att.Body;    //File content
-            insertContents.add(cVersion);                            
-        }
-        if(insertContents.isEmpty()){
-            return;
-        }
-        Insert insertContents;
-        set<Id> contentIds = new set<Id>();
-        for(ContentVersion cv : insertContents){
-            contentIds.add(cv.id);
-        }
-        List<ContentVersion> conDocuments = [SELECT ContentDocumentId, Title FROM ContentVersion WHERE Id in: contentIds];
-        List<ContentDocumentLink> insertDocLinks = new List<ContentDocumentLink>();  
-        for(ContentVersion cv : conDocuments){
-            ContentDocumentLink cDocLink = new ContentDocumentLink();
-            cDocLink.ContentDocumentId = cv.ContentDocumentId;          //Add ContentDocumentId
-            cDocLink.LinkedEntityId = nameParentMaps.get(cv.Title);     //Add attachment parentId
-            cDocLink.ShareType = 'V';                                   //V - Viewer permission. C - Collaborator permission. I - Inferred permission.
-            cDocLink.Visibility = 'AllUsers';
-            insertDocLinks.add(cDocLink);
-        }
-        Insert insertDocLinks;
+    if (insertContents.isEmpty()) {
+      return;
     }
-
-    global void finish(Database.BatchableContext BC) {
-
+    insert insertContents;
+    set<Id> contentIds = new Set<Id>();
+    for (ContentVersion cv : insertContents) {
+      contentIds.add(cv.id);
     }
-}
\ No newline at end of file
+    List<ContentVersion> conDocuments = [
+      SELECT ContentDocumentId, Title
+      FROM ContentVersion
+      WHERE Id IN :contentIds
+    ];
+    List<ContentDocumentLink> insertDocLinks = new List<ContentDocumentLink>();
+    for (ContentVersion cv : conDocuments) {
+      ContentDocumentLink cDocLink = new ContentDocumentLink();
+      cDocLink.ContentDocumentId = cv.ContentDocumentId; //Add ContentDocumentId
+      cDocLink.LinkedEntityId = nameParentMaps.get(cv.Title); //Add attachment parentId
+      cDocLink.ShareType = 'V'; //V - Viewer permission. C - Collaborator permission. I - Inferred permission.
+      cDocLink.Visibility = 'AllUsers';
+      insertDocLinks.add(cDocLink);
+    }
+    insert insertDocLinks;
+  }
+
+  global void finish(Database.BatchableContext BC) {
+  }
+}

--
Gitblit v1.9.1