public without sharing class QISTrigger {
    // QISのId
    public static Set<Id> QIS_Ids = new Set<Id>();
    
    // 
    public static void setPhotoImgText(List<QIS_Report__c> newList, List<QIS_Report__c> oldList, Map<Id, QIS_Report__c> newMap, Map<Id, QIS_Report__c> oldMap) {
        for (QIS_Report__c qr : newList) {
            // 二重実行禁止
            if (QIS_Ids.contains(qr.Id)) continue;
            // Photo1
            if (Trigger.isInsert && String.isBlank(qr.Photo_1__c) == false
                    || (Trigger.isUpdate && ((QIS_Report__c) oldMap.get(qr.Id)).Photo_1__c != qr.Photo_1__c)
                    || (Trigger.isUpdate && String.isBlank(qr.Photo_1__c) == false && String.isBlank(qr.Photo_1_Text__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(qr.Photo_1__c) == false) {
                    Integer pStt = qr.Photo_1__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = qr.Photo_1__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = qr.Photo_1__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = qr.Photo_1__c.substring(pStt + 5, pEnd);
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                qr.Photo_1_Text__c = phtTxt;
                QIS_Ids.add(qr.Id);
            }
            // Photo2
            if (Trigger.isInsert && String.isBlank(qr.Photo_2__c) == false
                    || (Trigger.isUpdate && ((QIS_Report__c) oldMap.get(qr.Id)).Photo_2__c != qr.Photo_2__c)
                    || (Trigger.isUpdate && String.isBlank(qr.Photo_2__c) == false && String.isBlank(qr.Photo_2_Text__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(qr.Photo_2__c) == false) {
                    Integer pStt = qr.Photo_2__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = qr.Photo_2__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = qr.Photo_2__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = qr.Photo_2__c.substring(pStt + 5, pEnd);
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                qr.Photo_2_Text__c = phtTxt;
                QIS_Ids.add(qr.Id);
            }
            // Photo3
            if (Trigger.isInsert && String.isBlank(qr.Photo_3__c) == false
                    || (Trigger.isUpdate && ((QIS_Report__c) oldMap.get(qr.Id)).Photo_3__c != qr.Photo_3__c)
                    || (Trigger.isUpdate && String.isBlank(qr.Photo_3__c) == false && String.isBlank(qr.Photo_3_Text__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(qr.Photo_3__c) == false) {
                    Integer pStt = qr.Photo_3__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = qr.Photo_3__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = qr.Photo_3__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = qr.Photo_3__c.substring(pStt + 5, pEnd);
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                qr.Photo_3_Text__c = phtTxt;
                QIS_Ids.add(qr.Id);
            }
            // Photo4
            if (Trigger.isInsert && String.isBlank(qr.Photo_4__c) == false
                    || (Trigger.isUpdate && ((QIS_Report__c) oldMap.get(qr.Id)).Photo_4__c != qr.Photo_4__c)
                    || (Trigger.isUpdate && String.isBlank(qr.Photo_4__c) == false && String.isBlank(qr.Photo_4_Text__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(qr.Photo_4__c) == false) {
                    Integer pStt = qr.Photo_4__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = qr.Photo_4__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = qr.Photo_4__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = qr.Photo_4__c.substring(pStt + 5, pEnd);
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                qr.Photo_4_Text__c = phtTxt;
                QIS_Ids.add(qr.Id);
            }



            //加4个字段 20230627
             // Photo5
            if (Trigger.isInsert && String.isBlank(qr.Photo_5__c) == false
                    || (Trigger.isUpdate && ((QIS_Report__c) oldMap.get(qr.Id)).Photo_5__c != qr.Photo_5__c)
                    || (Trigger.isUpdate && String.isBlank(qr.Photo_5__c) == false && String.isBlank(qr.Photo_5_Text__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(qr.Photo_5__c) == false) {
                    Integer pStt = qr.Photo_5__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = qr.Photo_5__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = qr.Photo_5__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = qr.Photo_5__c.substring(pStt + 5, pEnd);
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                qr.Photo_5_Text__c = phtTxt;
                QIS_Ids.add(qr.Id);
            }
             // Photo6
            if (Trigger.isInsert && String.isBlank(qr.Photo_6__c) == false
                    || (Trigger.isUpdate && ((QIS_Report__c) oldMap.get(qr.Id)).Photo_6__c != qr.Photo_6__c)
                    || (Trigger.isUpdate && String.isBlank(qr.Photo_6__c) == false && String.isBlank(qr.Photo_6_Text__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(qr.Photo_6__c) == false) {
                    Integer pStt = qr.Photo_6__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = qr.Photo_6__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = qr.Photo_6__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = qr.Photo_6__c.substring(pStt + 5, pEnd);
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                qr.Photo_6_Text__c = phtTxt;
                QIS_Ids.add(qr.Id);
            }
             // Photo7
            if (Trigger.isInsert && String.isBlank(qr.Photo_7__c) == false
                    || (Trigger.isUpdate && ((QIS_Report__c) oldMap.get(qr.Id)).Photo_7__c != qr.Photo_7__c)
                    || (Trigger.isUpdate && String.isBlank(qr.Photo_7__c) == false && String.isBlank(qr.Photo_7_Text__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(qr.Photo_7__c) == false) {
                    Integer pStt = qr.Photo_7__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = qr.Photo_7__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = qr.Photo_7__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = qr.Photo_7__c.substring(pStt + 5, pEnd);
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                qr.Photo_7_Text__c = phtTxt;
                QIS_Ids.add(qr.Id);
            }
             // Photo8
            if (Trigger.isInsert && String.isBlank(qr.Photo_8__c) == false
                    || (Trigger.isUpdate && ((QIS_Report__c) oldMap.get(qr.Id)).Photo_8__c != qr.Photo_8__c)
                    || (Trigger.isUpdate && String.isBlank(qr.Photo_8__c) == false && String.isBlank(qr.Photo_8_Text__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(qr.Photo_8__c) == false) {
                    Integer pStt = qr.Photo_8__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = qr.Photo_8__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = qr.Photo_8__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = qr.Photo_8__c.substring(pStt + 5, pEnd);
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                qr.Photo_8_Text__c = phtTxt;
                QIS_Ids.add(qr.Id);
            }
        }
    }
}