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();
        setRepairAgainName();
    }
    protected override void beforeUpdate() {
        setPhotoImgText();
        setStatusChange();
    }

    protected override void afterUpdate(){
        submitRepair();
    }
    
    public static Set<Id> RA_Ids = new Set<Id>();

    //再注册分析 需求 新增字段 20220811 sx start
    public static List<Id> openRepairAgainRepairIds = new List<Id>();
    public static List<Id> finalRepairAgainRepairIds = new List<Id>();
    //再注册分析 需求 新增字段 20220811 sx end
    
    
    private void setRepairAgainName() {

        Set<Id> repairSet = new Set<Id>();

        for(RepairAgainAn__c ra : newList){
            repairSet.add(ra.Repair__c);
        }

        Map<Id,Repair__c> repairMap = new Map<Id,Repair__c>([SELECT Id,Name, (select Id from reparirAgain01__r) from Repair__c where id in :repairSet]);

        for(RepairAgainAn__c ra : newList){

            if(repairMap.containsKey(ra.Repair__c)){

                Integer repairSize = repairMap.get(ra.Repair__c).reparirAgain01__r.size();

                String repairSizeString = '000' + repairMap.get(ra.Repair__c).reparirAgain01__r.size();

                String cutString = repairSizeString.substring(repairSizeString.length()-3, repairSizeString.length());

                if(repairSize == 0) {
                    ra.Name = repairMap.get(ra.Repair__c).Name + '_001';
                }else{
                    ra.Name = repairMap.get(ra.Repair__c).Name + '_' + cutString;
                }
            }
        }

    }


    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);
            }
        }
    }

    public void setStatusChange() {
        Map<String,id> rtmaps = new Map<String,id>();
        List<RecordType> rtIds = [SELECT Id,Name FROM RecordType where SobjectType='RepairAgainAn__c'];

        Date todayTemp = Date.today();
        for (RecordType rt : rtIds) {
           rtmaps.put(rt.Name, rt.Id);
        }
        for (RepairAgainAn__c raa : newList) {
            RepairAgainAn__c raaOld = oldMap.get(raa.id);
            //状态变更为草案中
            if (String.isBlank(raa.RepairAgain_Status__c)) {
                raa.RepairAgain_Status__c = '草案中';
            }

            //当RC的内容填写完毕
            if(raa.RepairAgain_Status__c == 'RC填写完毕' && raaOld.RepairAgain_Status__c != 'RC填写完毕') {
                raa.RecordTypeId = rtmaps.get('2.OSH');
                // 发起再注册分析时间
                raa.Start_RepairAgainAn_Day__c = todayTemp;
                // TODO 和SAP交互  OPEN
                openRepairAgainRepairIds.add(raa.Repair__c);
            }

            //当OSH受理
            if(raa.RepairAgain_Status__c == 'OSH受理' && raaOld.RepairAgain_Status__c != 'OSH受理') {
                // OSH受理日
                raa.OSH_Accept_Day__c = todayTemp;
            }

            //当OSH内容填写完毕
            if(raa.RepairAgain_Status__c == 'OSH填写完毕' && raaOld.RepairAgain_Status__c != 'OSH填写完毕') {
                raa.RecordTypeId = rtmaps.get('3.OCSM');
                // OSH判定结论确认日
                raa.OSH_End_Day__c = todayTemp;
            }

            //当OCSM跟进完毕
            if(raa.RepairAgain_Status__c == '完毕' && raaOld.RepairAgain_Status__c != '完毕') {
                raa.RecordTypeId = rtmaps.get('4.Final完毕');
                // OCSM判定结论确认日
                raa.OCSM_End_Day__c = todayTemp;
                // TODO 和SAP交互
                finalRepairAgainRepairIds.add(raa.Repair__c);
            }
        }
    }

    public void submitRepair(){

        if(openRepairAgainRepairIds.size() > 0 || finalRepairAgainRepairIds.size() > 0){

            BatchIF_Log__c iflog = new BatchIF_Log__c();
                iflog.Type__c = 'NFM103';
                iflog.Log__c  = 'callout start\n';
                insert iflog;

            if(openRepairAgainRepairIds.size() > 0) {
                //关联一个open的再修理分析，传X
                NFM103Controller.callout(iflog.Id, openRepairAgainRepairIds,'X');
            }

            if(finalRepairAgainRepairIds.size() > 0) {
                //关联再修理分析完毕，传F
                NFM103Controller.callout(iflog.Id, finalRepairAgainRepairIds,'F');
            }

        }
       

    }
}