public without sharing class RepairOrderHandler extends Oly_TriggerHandler {
    private Map<Id, Repair__c> newMap;
    private Map<Id, Repair__c> oldMap;
    private List<Repair__c> newList;
    private List<Repair__c> oldList;
    public RepairOrderHandler() {
        this.newMap = (Map<Id, Repair__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Repair__c>) Trigger.oldMap;
        this.newList = (List<Repair__c>) Trigger.new;
        this.oldList = (List<Repair__c>) Trigger.old;
    }
	protected override void afterInsert() {
		RepairInsert();
        // RepairUpdatePAE();
	}
    protected override void afterUpdate() {
        RepairUpdatePAE();
    }
    public void RepairInsert(){
        List<RepairSubOrder__c> repairSubOrderList = new List<RepairSubOrder__c>();
        List<String> repairSoIdList = new List<String>();
    	for (Repair__c repair : newList) {
    		if (Trigger.isInsert) {
    		    if (String.isNotBlank(repair.RepairSubOrder__c)) {
    		        RepairSubOrder__c rso = new RepairSubOrder__c();
    		        rso.Id = repair.RepairSubOrder__c;
    		        rso.SFDCRepairApplyDate__c = Date.today();
                    // 2023-4-7   zyh   add   start 
                    rso.DelayReportReason__c = repair.DelayReportReason__c;
                    rso.FailureQInHospital__c = repair.FailureQInHospital__c;
                    rso.IfDeadHurt__c = repair.IfDeadHurt__c;
                    rso.ProblemOccurred__c = repair.ProblemOccurred__c;
                    rso.ProblemOccurredSelect__c = repair.ProblemOccurredSelect__c;
                    rso.ReportAdverseEvents__c = repair.ReportAdverseEvents__c;
                    rso.WhatProject__c = repair.WhatProject__c;
                    rso.OperationOrExaminationName__c = repair.OperationOrExaminationName__c;
                    rso.BreakORFallOff__c = repair.BreakORFallOff__c;
                    rso.UseFailProductFinish__c = repair.UseFailProductFinish__c;
                    rso.SupportingProducts__c = repair.SupportingProducts__c;
                    rso.AfterFailureInformation__c = repair.AfterFailureInformation__c;
                    rso.Delay15Min__c = repair.Delay15Min__c;
                    rso.InformationFrom__c = repair.InformationFrom__c;
                    rso.IsProductFaultRelated__c = repair.ProductFailureRelated__c;
                    rso.ReturnWayType__c = repair.Returns_Product_way__c;
                    // 2023-4-7   zyh   add   end 
    		        repairSubOrderList.add(rso);
                    repairSoIdList.add(rso.Id);
    		    }
    		}
    	}
    	if (repairSubOrderList.size() > 0) {
    	    update repairSubOrderList;
    	}
        if (repairSoIdList.size() > 0) {
            System.debug('NFM613S====' + repairSoIdList);
            BatchIF_Log__c iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM613';
            iflog.Log__c = 'callout start\n';
            System.debug('NFM613S===RepairInsert====callout start' + repairSoIdList);
            System.debug('NFM613S===RepairInsert====callout start' + repairSoIdList.size());
            insert iflog;
            NFM613Controller.callout(iflog.Id, repairSoIdList);
        }
    }
    public void RepairUpdatePAE() {
        List<RepairSubOrder__c> repairSubOrderList = new List<RepairSubOrder__c>();
        List<String> repairSoIdList = new List<String>();
        System.debug(repairSubOrderList + '=====testjinlai');
        for (Repair__c nObj : newList) {
            Repair__c oObj = new Repair__c();
            oObj = oldMap.get(nObj.Id);
            if(String.isNotBlank(nObj.RepairSubOrder__c) && 
                ((nObj.Status__c == '0.申请完毕' && 
                // nObj.Status__c != '0.删除' &&
                // nObj.Status__c != '0.取消' &&
                oObj.Status__c == '草案中') || String.isBlank(nObj.SAP_Service_Repair_No__c)) ){
                System.debug(repairSubOrderList + '=====testfor');
                /*if(Trigger.isUpdate){
                }*/
                
                
                if ((nObj.FailureQInHospital__c != oObj.FailureQInHospital__c || // 在医院，故障发生的频率是多少
                    nObj.IfDeadHurt__c != oObj.IfDeadHurt__c || // 是否有死亡、伤害、感染
                    nObj.ProblemOccurred__c != oObj.ProblemOccurred__c || // 问题发生时间情况
                    nObj.ProblemOccurredSelect__c != oObj.ProblemOccurredSelect__c || // 问题发生时间情况 其他
                    nObj.ReportAdverseEvents__c != oObj.ReportAdverseEvents__c || //医院有没有向政府机关报告不良事件或疑似不良事件
                    nObj.WhatProject__c != oObj.WhatProject__c || // 做的是哪个项目
                    nObj.OperationOrExaminationName__c != oObj.OperationOrExaminationName__c || // 手术/检查名称
                    nObj.BreakORFallOff__c != oObj.BreakORFallOff__c || // 是否有发生断裂或脱落
                    nObj.UseFailProductFinish__c != oObj.UseFailProductFinish__c || // 是否用该产品完成了手术/检查
                    nObj.SupportingProducts__c != oObj.SupportingProducts__c || // 配套使用产品
                    nObj.AfterFailureInformation__c != oObj.AfterFailureInformation__c || // 发现故障后，医院对患者的处理情况
                    nObj.Delay15Min__c != oObj.Delay15Min__c || // 发现故障现象后，有延误15分钟以上的手术实施的情况发生吗
                    nObj.InformationFrom__c != oObj.InformationFrom__c || // 信息是从医院的谁那里得到的
                    // nObj.FailureQInHospital__c != oObj.FailureQInHospital__c || // 在医院，故障发生的频率是多少
                    nObj.DelayReportReason__c != oObj.DelayReportReason__c || // 超时报告的理由
                    nObj.ProductFailureRelated__c != oObj.ProductFailureRelated__c // 是否和这次的产品故障有关
                    // ) || (nObj.Status__c == '0.申请完毕' && oObj.Status__c == '草案中')) {FSE_ApplyForRepair_time__c
                    ) || (nObj.FSE_ApplyForRepair_time__c != null && oObj.FSE_ApplyForRepair_time__c == null)) {
                        System.debug('butong=======FSE_ApplyForRepair_time__c' + nObj.FSE_ApplyForRepair_time__c);
                        System.debug('butong=======FSE_ApplyForRepair_time__c' + oObj.FSE_ApplyForRepair_time__c);
                        System.debug('butong=======Status__c' + nObj.Status__c);

                        System.debug('butong=======Status__c' + oObj.Status__c);

                        if(nObj.FailureQInHospital__c != oObj.FailureQInHospital__c ){
                            System.debug('butong=======FailureQInHospital__c');
                        }
                        if(nObj.IfDeadHurt__c != oObj.IfDeadHurt__c ){
                            System.debug('butong=======IfDeadHurt__c');
                        }
                        if(nObj.ProblemOccurred__c != oObj.ProblemOccurred__c ){
                            System.debug('butong=======ProblemOccurred__c');
                        }
                        if(nObj.ProblemOccurredSelect__c != oObj.ProblemOccurredSelect__c ){
                            System.debug('butong=======oObj.ProblemOccurredSelect__c');
                        }
                        if(nObj.ReportAdverseEvents__c != oObj.ReportAdverseEvents__c ){
                            System.debug('butong=======ReportAdverseEvents__c');
                        }
                        if(nObj.WhatProject__c != oObj.WhatProject__c ){
                            System.debug('butong=======WhatProject__c');
                        }
                        if(nObj.OperationOrExaminationName__c != oObj.OperationOrExaminationName__c ){
                            System.debug('butong=======OperationOrExaminationName__c');
                        }
                        if(nObj.BreakORFallOff__c != oObj.BreakORFallOff__c ){
                            System.debug('butong=======BreakORFallOff__c');
                        }
                        if(nObj.UseFailProductFinish__c != oObj.UseFailProductFinish__c ){
                            System.debug('butong=======UseFailProductFinish__c');
                        }
                        if(nObj.SupportingProducts__c != oObj.SupportingProducts__c ){
                            System.debug('butong=======SupportingProducts__c');
                        }
                        if(nObj.AfterFailureInformation__c != oObj.AfterFailureInformation__c ){
                            System.debug('butong=======AfterFailureInformation__c');
                        }
                        if(nObj.Delay15Min__c != oObj.Delay15Min__c ){
                            System.debug('butong=======Delay15Min__c');
                        }
                        if(nObj.InformationFrom__c != oObj.InformationFrom__c ){
                            System.debug('butong=======InformationFrom__c');
                        }
                        if(nObj.DelayReportReason__c != oObj.DelayReportReason__c ){
                            System.debug('butong=======DelayReportReason__c');
                        }
                        if(nObj.ProductFailureRelated__c != oObj.ProductFailureRelated__c ){
                            System.debug('butong=======ProductFailureRelated__c');
                        }





                        RepairSubOrder__c rso = new RepairSubOrder__c();
                        rso.Id = nObj.RepairSubOrder__c;
                        // if(nObj.Returns_Product_way__c != oObj.Returns_Product_way__c){
                            rso.ReturnWayType__c = nObj.Returns_Product_way__c;
                        // }
                        rso.DelayReportReason__c = nObj.DelayReportReason__c;
                        rso.FailureQInHospital__c = nObj.FailureQInHospital__c;
                        rso.IfDeadHurt__c = nObj.IfDeadHurt__c;
                        rso.ProblemOccurred__c = nObj.ProblemOccurred__c;
                        rso.ProblemOccurredSelect__c = nObj.ProblemOccurredSelect__c;
                        rso.ReportAdverseEvents__c = nObj.ReportAdverseEvents__c;
                        rso.WhatProject__c = nObj.WhatProject__c;
                        rso.OperationOrExaminationName__c = nObj.OperationOrExaminationName__c;
                        rso.BreakORFallOff__c = nObj.BreakORFallOff__c;
                        rso.UseFailProductFinish__c = nObj.UseFailProductFinish__c;
                        rso.SupportingProducts__c = nObj.SupportingProducts__c;
                        rso.AfterFailureInformation__c = nObj.AfterFailureInformation__c;
                        rso.Delay15Min__c = nObj.Delay15Min__c;
                        rso.InformationFrom__c = nObj.InformationFrom__c;
                        // rso.FailureQInHospital__c = nObj.FailureQInHospital__c;
                        // rso.DelayReportReason__c = nObj.DelayReportReason__c;
                        rso.IsProductFaultRelated__c = nObj.ProductFailureRelated__c;
                        repairSubOrderList.add(rso);
                        repairSoIdList.add(rso.Id);
                        System.debug(repairSubOrderList + '=====test');
                }
                
            }
        }
        if (repairSubOrderList.size() > 0) {
            update repairSubOrderList;
        }
        if (repairSoIdList.size() > 0) {
            System.debug('NFM613S====' + repairSoIdList);
            BatchIF_Log__c iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM613';
            iflog.Log__c = 'callout start\n';
            System.debug('NFM613S===RepairUpdatePAE====callout start' + repairSoIdList);
            System.debug('NFM613S===RepairUpdatePAE====callout start' + repairSoIdList.size());
            insert iflog;
            NFM613Controller.callout(iflog.Id, repairSoIdList);
        }
    } // 2023-4-7   zyh   add   注释：小程序需要
}