public with sharing class RepairAgainAnHandler extends Oly_TriggerHandler{
    private Map<Id, RepairAgainAn__c> newMap;
    private Map<Id, RepairAgainAn__c> oldMap;
    private List<RepairAgainAn__c> newList;
    private List<RepairAgainAn__c> oldList;

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

    protected override void beforeInsert() {
        setPhotoImgText();
    }
    protected override void beforeUpdate() {
        setPhotoImgText();
    }
    public static Set<Id> RA_Ids = new Set<Id>();
    
    private void setPhotoImgText() {
        for (RepairAgainAn__c ra : newList) {
            System.debug('ra.Dectect_Picture1__c2:' + ra.Dectect_Picture1__c);
            // 禁止重复执行
            if (RA_Ids.contains(ra.Id)) continue;
            // 本次报价检测图片1
            if (Trigger.isAfter && Trigger.isInsert && String.isBlank(ra.Dectect_Picture1__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).Dectect_Picture1__c != ra.Dectect_Picture1__c)
                    || (Trigger.isUpdate && String.isBlank(ra.Dectect_Picture1__c) == false && String.isBlank(ra.Dectect_Picture1_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.Dectect_Picture1__c) == false) {
                    Integer pStt = ra.Dectect_Picture1__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.Dectect_Picture1__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.Dectect_Picture1__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.Dectect_Picture1__c.substring(pStt + 5, pEnd);
                                System.debug('ra.Dectect_Picture1__c1:' + ra.Dectect_Picture1__c);
                                System.debug('phtTxt1:' + phtTxt);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                                System.debug('phtTxt2:' + phtTxt);
                            }
                        }
                    }
                }
                ra.Dectect_Picture1_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
            // 本次报价检测图片2
            if (Trigger.isAfter && Trigger.isInsert && String.isBlank(ra.Dectect_Picture2__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).Dectect_Picture2__c != ra.Dectect_Picture2__c)
                    || (Trigger.isUpdate && String.isBlank(ra.Dectect_Picture2__c) == false && String.isBlank(ra.Dectect_Picture2_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.Dectect_Picture2__c) == false) {
                    Integer pStt = ra.Dectect_Picture2__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.Dectect_Picture2__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.Dectect_Picture2__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.Dectect_Picture2__c.substring(pStt + 5, pEnd);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                ra.Dectect_Picture2_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片1
            if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture1__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture1__c != ra.OSH_Dectect_Picture1__c)
                    || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture1__c) == false && String.isBlank(ra.OSH_Dectect_Picture1_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.OSH_Dectect_Picture1__c) == false) {
                    Integer pStt = ra.OSH_Dectect_Picture1__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.OSH_Dectect_Picture1__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.OSH_Dectect_Picture1__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.OSH_Dectect_Picture1__c.substring(pStt + 5, pEnd);
                                System.debug('ra.OSH_Dectect_Picture1__c1:' + ra.OSH_Dectect_Picture1__c);
                                System.debug('phtTxt3:' + phtTxt);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                                System.debug('phtTxt4:' + phtTxt);
                            }
                        }
                    }
                }
                ra.OSH_Dectect_Picture1_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片2
            if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture2__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture2__c != ra.OSH_Dectect_Picture2__c)
                    || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture2__c) == false && String.isBlank(ra.OSH_Dectect_Picture2_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.OSH_Dectect_Picture2__c) == false) {
                    Integer pStt = ra.OSH_Dectect_Picture2__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.OSH_Dectect_Picture2__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.OSH_Dectect_Picture2__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.OSH_Dectect_Picture2__c.substring(pStt + 5, pEnd);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                ra.OSH_Dectect_Picture2_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片3
            if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture3__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture3__c != ra.OSH_Dectect_Picture3__c)
                    || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture3__c) == false && String.isBlank(ra.OSH_Dectect_Picture3_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.OSH_Dectect_Picture3__c) == false) {
                    Integer pStt = ra.OSH_Dectect_Picture3__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.OSH_Dectect_Picture3__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.OSH_Dectect_Picture3__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.OSH_Dectect_Picture3__c.substring(pStt + 5, pEnd);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                ra.OSH_Dectect_Picture3_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片4
            if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture4__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture4__c != ra.OSH_Dectect_Picture4__c)
                    || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture4__c) == false && String.isBlank(ra.OSH_Dectect_Picture4_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.OSH_Dectect_Picture4__c) == false) {
                    Integer pStt = ra.OSH_Dectect_Picture4__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.OSH_Dectect_Picture4__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.OSH_Dectect_Picture4__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.OSH_Dectect_Picture4__c.substring(pStt + 5, pEnd);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                ra.OSH_Dectect_Picture4_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
        }
    }
}