From 0793b78361e77ac25bb3a38da75678ff5d40eaed Mon Sep 17 00:00:00 2001
From: 高章伟 <gaozhangwei@prec-tech.com>
Date: 星期四, 24 三月 2022 18:33:18 +0800
Subject: [PATCH] PDF空白优化

---
 force-app/main/default/classes/QISToETQWebServiceTest.cls-meta.xml |    5 
 force-app/main/default/classes/QISToETQWebService.cls-meta.xml     |    5 
 force-app/main/default/classes/QISToETQWebServiceTest.cls          |  171 ++++++++++++++++++++++++
 force-app/main/default/classes/RepairSendToETQBatch.cls            |   18 +-
 force-app/main/default/classes/QISToETQWebService.cls              |  169 ++++++++++++++++++++++++
 5 files changed, 361 insertions(+), 7 deletions(-)

diff --git a/force-app/main/default/classes/QISToETQWebService.cls b/force-app/main/default/classes/QISToETQWebService.cls
new file mode 100644
index 0000000..bcba6d8
--- /dev/null
+++ b/force-app/main/default/classes/QISToETQWebService.cls
@@ -0,0 +1,169 @@
+global without sharing class QISToETQWebService {
+    public static List<ImgObj> ImgObjs = new List<ImgObj>();
+    webservice static String sendToETQ(String iflog_Id,BatchIF_Log__c rowDataSFDC, List<String> repairIds,String statu){
+        List<QIS_Report__c> temp = [select id,name,GeneratedPDFField__c,problem_detail_photo__c,Photo_1__c,Photo_2__c,Photo_3__c,Photo_4__c,
+        Photo_OSH_1__c,Photo_OSH_2__c,Photo_OSH_3__c,Photo_OSH_4__c
+        from QIS_Report__c where GeneratedPDFField__c = null and id in :repairIds ];
+        List<Repair__c> tempRe = [select id,name,GeneratedPDFField__c,ProblemDescription__c,ASReportedCodeAC__c,AE_DetermineResult__c,PAE_Determine__c,PAE_DetermineAC__c from Repair__c where GeneratedPDFField__c = null
+         and id in :repairIds ];
+        if(temp != null && temp.size() > 0){
+            // try {
+                // 闇�瑕佺敓鎴恜df鐨勫瓧娈�
+                List<String> apiNames = new List<String>();
+                apiNames.add('problem_detail_photo__c');
+                apiNames.add('Photo_1__c');
+                apiNames.add('Photo_2__c');
+                apiNames.add('Photo_3__c');
+                apiNames.add('Photo_4__c');
+                apiNames.add('Photo_OSH_1__c');
+                apiNames.add('Photo_OSH_2__c');
+                apiNames.add('Photo_OSH_3__c');
+                apiNames.add('Photo_OSH_4__c');
+                List<QIS_Report__c> updateQis = new List<QIS_Report__c>();
+                List<Attachment> attachments = new List<Attachment>();
+                PageReference pdfPage ;
+                for(QIS_Report__c qis : temp){
+                    for (String apiName : apiNames) {
+                        String generatedFields =  qis.GeneratedPDFField__c;
+                        if (checkFieldisGeneratedPDF(apiName,qis)) {
+                            continue;
+                        }
+                        if (qis.get(apiName) == null) {
+                            continue;
+                        }
+                        setImgSrcs(qis, apiName);
+                        pdfPage = new PageReference('/apex/repairandqistopdf?api=qis&id='+qis.Id+'&field='+apiName);
+                        Blob pdfBody;
+                        if(Test.isRunningTest()) { 
+                            pdfBody = blob.valueOf('Unit.Test');
+                        } else {
+                            pdfBody = pdfPage.getContentAsPDF();
+                        }
+                        Attachment attach = new Attachment();
+                        attach.Body = pdfBody;
+                        if ('problem_detail_photo__c'.equals(apiName)) {
+                            attach.Name =  substringApiName(qis.name) + '_Problem_Discription.pdf';
+                        }else {
+                            attach.Name = substringApiName(qis.name) + '_' + 'Problem_' + apiName.substring(0,apiName.length()-3) + '.pdf';
+                        }
+                        attach.ParentId = qis.id;
+        
+                        attachments.add(attach);
+                        if (generatedFields == null) {
+                            qis.GeneratedPDFField__c = apiName;
+                        }else {
+                            qis.GeneratedPDFField__c += ',' + apiName;
+                        }
+                    }
+                    updateQis.add(qis);
+                }
+                insert attachments;
+                update updateQis;
+                // RepairAndQISToPDFController.generateAttachment(temp);
+                NFM401Controller.callout(null, null, repairIds, statu);
+            // }
+            // catch (Exception e) {
+            //     return '鏇存柊QIS鎶ラ敊:'+ e.getMessage();
+            // }
+            // Database.executeBatch(new QISToPDFBatch(iflog_Id, rowDataSFDC,repairIds,statu),50); //鐢熸垚PDF
+        }else{
+
+            BatchIF_Log__c iflog = new BatchIF_Log__c();
+            iflog.Type__c = 'sendToETQ';
+            iflog.ErrorLog__c = '';
+            iflog.Log__c = 'NFM401WebService start--';
+
+            Repair__c updateRe = new Repair__c();
+            updateRe.Id = tempRe[0].id;
+            updateRe.INTERFACE_RECORD_ID__c = null; 
+            updateRe.ETQ_UPLOAD_STATUS__c = null; 
+            updateRe.ETQ_UPLOAD_MESSAGE__c = null; 
+            updateRe.OSH_ConfirmationDate__c = Date.today(); 
+            updateRe.OSH_Affirmant__c = UserInfo.getUserId(); 
+            updateRe.AWS_Interface_Time__c = Datetime.now();
+            updateRe.AsyncData__c = true; 
+            //update by rentx 2021-03-23 start 闇�瑕佸厛鏇存柊淇悊鐨勪俊鎭啀鎺�401鎺ュ彛 涓嶇劧鐨勮瘽鍙兘浼氬嚭鐜� 璋冪敤鎴愬姛浣嗘槸閮ㄥ垎淇悊鏇存柊澶辫触鐨勬儏鍐� 灏嗘洿鏂颁慨鐞嗙殑姝ラ鎻愬墠 鍒欏綋淇悊鏇存柊澶辫触鏃� 灏变笉浼氬線涓嬫墽琛�401鐨勬帴鍙d簡 
+            // try{
+                update updateRe;
+                // Database.executeBatch(new RepairToPDFBatch(iflog_Id, rowDataSFDC,repairIds,statu)); //鐢熸垚PDF
+                // RepairAndQISToPDFController.generateAttachment(reList);
+                PageReference pdfPage ;
+            
+                List<Repair__c> updateRpr = new List<Repair__c>();
+                List<Attachment> attachments = new List<Attachment>();
+                for(Repair__c re : tempRe){
+                    String generatedFields = re.GeneratedPDFField__c;
+                    if (checkFieldisGeneratedPDF('ProblemDescription__c',re)) {
+                        continue;                
+                    }
+                    if (re.get('ProblemDescription__c') == null) {
+                        continue;
+                    }
+                    setImgSrcs(re, 'ProblemDescription__c');            
+        
+                    pdfPage = new PageReference('/apex/repairandqistopdf?api=repair&id='+re.Id);
+                    Blob pdfBody;
+                    if(Test.isRunningTest()) { 
+                        pdfBody = blob.valueOf('Unit.Test');
+                    } else {
+                        pdfBody = pdfPage.getContentAsPDF();
+                    }
+                    Attachment attach = new Attachment();
+                    attach.Body = pdfBody;
+                    attach.Name = re.name + '_' + 'Problem_Discription.pdf';
+                    attach.ParentId = re.id;
+                    attachments.add(attach);
+                    re.GeneratedPDFField__c = 'ProblemDescription__c';
+                    updateRpr.add(re);
+                }
+                insert attachments;
+                update updateRpr;
+                
+                Database.executeBatch(new RepairSendToETQBatch(null, null,repairIds,statu),1);
+                
+                iflog.Log__c += '\n淇悊:'+updateRe+' 鏇存柊鎴愬姛';
+                iflog.Log__c = '\nNFM401WebService end--';
+                insert iflog;
+            // }catch(Exception ex){
+            //     iflog.ErrorLog__c += '淇悊:'+updateRe+' 鏇存柊澶辫触,鍥犱负::'+ex.getMessage();
+            //     iflog.Log__c = '\nNFM401WebService end--';
+            //     // System.debug('鏇存柊淇悊鎶ラ敊::36'+updateRe);
+            //     insert iflog;
+            //     return '鏇存柊淇悊鎶ラ敊:'+ ex.getMessage();
+            // }
+        }
+        return '鍙戦�佹垚鍔�!';
+    }
+
+    // 涓昏鐢ㄤ簬鐢熸垚qis涓棶棰樻弿杩板搴旂殑pdf鏂囦欢鍚嶇О(姣旇緝鐗规畩)
+    public static String substringApiName(String name){
+        for(Integer i = 0; i < 2; i++){
+            name = name.substring(name.indexOf('-')+1);
+        }
+        return name;
+    }
+
+    public static Boolean checkFieldisGeneratedPDF(String apiName,SObject obj){
+        String generatedFields = (String) obj.get('GeneratedPDFField__c');
+        if (generatedFields != null) {
+            List<String> ele = generatedFields.split(',');
+            return ele.contains(apiName);
+        }
+        return false;
+    }
+    public class ImgObj{
+        public String imgSrc {get; set;}
+        public String height {get; set;}
+    }
+    // 璁剧疆椤甸潰灞曠ず鐨勬暟鎹�
+    public static void setImgSrcs(SObject obj,String apiName){
+        ImgObjs = new List<ImgObj>();
+        String content = (String) obj.get(apiName);
+        ImgObj ImgObj = new ImgObj();
+        ImgObj.imgSrc = content;
+        ImgObj.height = '900px';
+        ImgObjs.add(ImgObj);
+        System.debug('ImgObjs++'+ImgObjs);
+        
+    }
+}
\ No newline at end of file
diff --git a/force-app/main/default/classes/QISToETQWebService.cls-meta.xml b/force-app/main/default/classes/QISToETQWebService.cls-meta.xml
new file mode 100644
index 0000000..40d6793
--- /dev/null
+++ b/force-app/main/default/classes/QISToETQWebService.cls-meta.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
+    <apiVersion>54.0</apiVersion>
+    <status>Active</status>
+</ApexClass>
diff --git a/force-app/main/default/classes/QISToETQWebServiceTest.cls b/force-app/main/default/classes/QISToETQWebServiceTest.cls
new file mode 100644
index 0000000..4e43493
--- /dev/null
+++ b/force-app/main/default/classes/QISToETQWebServiceTest.cls
@@ -0,0 +1,171 @@
+@isTest
+public with sharing class QISToETQWebServiceTest {
+    @testSetup
+    static void makeTestQIS() {
+        StaticParameter.EscapeNFM001Trigger = true;
+        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
+        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
+        StaticParameter.EscapeNFM007Trigger = true;
+        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
+        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
+
+        // List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '鐥呴櫌'];
+        // if (rectCo.size() == 0) {
+        //     throw new ControllerUtil.myException('not found 鐥呴櫌 recodetype');
+        // }
+        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '鎴︾暐绉戝鍒嗛 娑堝寲绉�'];
+        if (rectSct.size() == 0) {
+            throw new ControllerUtil.myException('not found 鎴︾暐绉戝鍒嗛 鍛煎惛绉� recodetype');
+        }
+        // List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Department_GI'];
+        // if (rectDpt.size() == 0) {
+        //     throw new ControllerUtil.myException('not found 瑷虹檪绉� 娑堝寲绉� recodetype');
+        // }
+        // 銉嗐偣銉堛儑銉笺偪
+        Account company = new Account();
+        company.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
+        // company.RecordTypeId = rectCo[0].Id;
+        company.Name         = 'NFM105TestCompany';
+        upsert company;
+        Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];
+
+        Account depart = new Account();
+        depart.RecordTypeId        = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();
+        depart.Name                = '*';
+        depart.Department_Name__c  = 'NFM105TestDepart';
+        depart.ParentId            = section.Id;
+        depart.Department_Class__c = section.Id;
+        depart.Hospital__c         = company.Id;
+        upsert depart;
+
+        // 鍐嶅彇寰�
+        List<Account> accList = new List<Account>();
+        company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
+        accList.add(company);
+        accList.add(section);
+        depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
+        accList.add(depart);
+
+        List<Product2> prdList = new List<Product2>();
+        Product2 prd1 = new Product2();
+        prd1.ProductCode_Ext__c     = 'NFM105Prd1';
+        prd1.ProductCode            = 'NFM105Prd1';
+        prd1.Repair_Product_Code__c = 'NFM105Prd1_RP';
+        prd1.Name                   = 'NFM105Prd1';
+        prd1.Manual_Entry__c        = false;
+        prdList.add(prd1);
+        Product2 prd2 = new Product2();
+        prd2.ProductCode_Ext__c     = 'NFM105Prd2';
+        prd2.ProductCode            = 'NFM105Prd2';
+        prd2.Repair_Product_Code__c = 'NFM105Prd2_RP';
+        prd2.Name                   = 'NFM105Prd2';
+        prd2.Manual_Entry__c        = false;
+        prdList.add(prd2);
+        insert prdList;
+
+        Asset ast = new Asset();
+        ast.Name                   = 'NFM105Ast1';
+        ast.AccountId              = depart.Id;
+        ast.Department_Class__c    = section.Id;
+        ast.Hospital__c            = company.Id;
+        ast.Product2Id             = prd1.Id;
+        ast.SerialNumber           = 'NFM105SerialNumber';
+        ast.Guarantee_period_for_products__c = Date.today();
+        ast.InstallDate                      = Date.today();
+        insert ast;
+        ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
+               from Asset
+               where Id = :ast.Id];
+
+
+        // User testUser = new User();
+        // testUser.Job_Category__c = '閿�鍞湇鍔�';
+
+        Repair__c rpr = new Repair__c();
+        rpr.SAPRepairNo__c   = 'NFM105Rpr1';
+        rpr.Account__c             = depart.Id;
+        rpr.Department_Class__c    = section.Id;
+        rpr.Hospital__c            = company.Id;
+        rpr.Delivered_Product__c   = ast.Id;
+        rpr.Status__c              = '1.鍙楃悊瀹屾瘯';
+        rpr.Incharge_Staff__c = Userinfo.getUserId();
+        rpr.Repair_Detail__c = 'test';
+        // rpr.ProblemDescription__c = '<img alt="鐢ㄦ埛娣诲姞鐨勫浘鐗�" src="https://ocsm--stagefull--c.documentforce.com/servlet/rtaImage?eid=a0J10000008VbEm&amp;feoid=00N10000008rsVu&amp;refid=0EM1s0000005LCs" style="height: 666px; width: 500px;"></img><img alt="鐢ㄦ埛娣诲姞鐨勫浘鐗�" src="https://ocsm--stagefull--c.documentforce.com/servlet/rtaImage?eid=a0J10000008VbEm&amp;feoid=00N10000008rsVu&amp;refid=0EM1s0000005LD2" style="height: 666px; width: 500px;"></img><br>2020/06/24';
+        rpr.GeneratedPDFField__c = null;
+
+        //update by rentx 2020-10-1
+        System.runAs(new User(Id = Userinfo.getUserId())){
+            upsert rpr;
+        }
+
+
+        QIS_Report__c qr = new QIS_Report__c(
+            RC__c = UserInfo.getUserId(),
+            // Photo_1__c = '<img alt="鐢ㄦ埛娣诲姞鐨勫浘鐗�" src="https://ocsm--stagefull--c.documentforce.com/servlet/rtaImage?eid=a0J10000008VbEm&amp;feoid=00N10000008rsVu&amp;refid=0EM1s0000005LCs" style="height: 666px; width: 500px;"></img>',
+            Damage_For_Doc_Or_Pat__c = '鏈�',
+            Relation_With_The_Problem__c = '鏈夊彲鑳�',
+            Report_For_Goz__c = '涓嶇煡閬�',
+            Name = 'QIS012345',
+            GeneratedPDFField__c = null,
+            QIS_Submit_day__c = Date.Today(),
+            problem_detail_photo__c = '1231'
+        );
+        insert qr;
+    }
+
+    // @IsTest
+    // static void myTest(){
+        
+    //     QIS_Report__c qis =  [select id,name,GeneratedPDFField__c,problem_detail_photo__c,Photo_1__c,Photo_2__c,Photo_3__c,
+    //                                     Photo_4__c,Photo_OSH_1__c,Photo_OSH_2__c,Photo_OSH_3__c,Photo_OSH_4__c from QIS_Report__c];
+
+    //     Test.startTest();
+    //         List<String> ids = new List<String>();
+    //         ids.add(qis.Id);
+    //         Database.executeBatch(new QISToPDFBatch(ids));
+    //     Test.stopTest();
+        
+    // }
+
+        @IsTest
+    static void myTest1(){
+        
+        QIS_Report__c qis =  [select id,name,GeneratedPDFField__c,problem_detail_photo__c,Photo_1__c,Photo_2__c,Photo_3__c,
+                                        Photo_4__c,Photo_OSH_1__c,Photo_OSH_2__c,Photo_OSH_3__c,Photo_OSH_4__c from QIS_Report__c];
+
+        Test.startTest();
+            List<String> ids = new List<String>();
+            ids.add(qis.Id);
+            QISToETQWebService.sendToETQ(null, null,ids,null);
+            // Database.executeBatch(new QISToPDFBatch(null,null,ids,null));
+        Test.stopTest();
+        
+    }
+
+
+
+    // 淇悊
+    @isTest
+    static void myTest(){
+        StaticParameter.EscapeNFM001Trigger = true;
+        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
+        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
+        StaticParameter.EscapeNFM007Trigger = true;
+        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
+        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
+        Repair__c res = [select id,name,GeneratedPDFField__c,ProblemDescription__c from Repair__c];
+        List<String> ids = new List<String>();
+        ids.add(res.Id);
+        Test.startTest();
+            QISToETQWebService.sendToETQ(null, null,ids,'');
+            // Database.executeBatch(new RepairToPDFBatch(ids));
+            // //add by rentx 2020-10-21 start
+            // Database.executeBatch(new RepairToPDFBatch(null,null,ids,''));
+            // Database.executeBatch(new RepairToPDFBatch());
+            //add by rentx 2020-10-21 end        
+        Test.stopTest();
+        
+    }
+
+
+}
diff --git a/force-app/main/default/classes/QISToETQWebServiceTest.cls-meta.xml b/force-app/main/default/classes/QISToETQWebServiceTest.cls-meta.xml
new file mode 100644
index 0000000..40d6793
--- /dev/null
+++ b/force-app/main/default/classes/QISToETQWebServiceTest.cls-meta.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
+    <apiVersion>54.0</apiVersion>
+    <status>Active</status>
+</ApexClass>
diff --git a/force-app/main/default/classes/RepairSendToETQBatch.cls b/force-app/main/default/classes/RepairSendToETQBatch.cls
index 636d838..b9ede3d 100644
--- a/force-app/main/default/classes/RepairSendToETQBatch.cls
+++ b/force-app/main/default/classes/RepairSendToETQBatch.cls
@@ -201,14 +201,18 @@
 
 
     public void finish(Database.BatchableContext BC) {
-        //HHOA-C3LJWQ 鍏堟洿鏂颁慨鐞嗗啀鍙戦�乪tq 20210616 start byrentx
-        if (this.idss != null && this.idss.size() > 0) {
-            NFM401Controller.callout(null,null,this.idss,statu);
-            
-        }
-        //HHOA-C3LJWQ 鍏堟洿鏂颁慨鐞嗗啀鍙戦�乪tq 20210616 end byrentx
+        // gzw PDF绌虹櫧浼樺寲 璺宠繃娴嬭瘯绋嬪簭
+        if (!Test.isRunningTest()) {
+            //HHOA-C3LJWQ 鍏堟洿鏂颁慨鐞嗗啀鍙戦�乪tq 20210616 start byrentx
+            if (this.idss != null && this.idss.size() > 0) {
+                NFM401Controller.callout(null,null,this.idss,statu);
+                
+            }
+            //HHOA-C3LJWQ 鍏堟洿鏂颁慨鐞嗗啀鍙戦�乪tq 20210616 end byrentx
 
-        Database.executeBatch(new NFM402Batch(),100);
+            Database.executeBatch(new NFM402Batch(),100);
+        }
+        // gzw PDF绌虹櫧浼樺寲 璺宠繃娴嬭瘯绋嬪簭
     }
 
 }
\ No newline at end of file

--
Gitblit v1.9.1