public without sharing class RepairTrigger {

    //before insert, brfore update
    public static void sendEmailByInspectionFailureCause(List<Repair__c> newList, Map<Id, Repair__c> newMap, List<Repair__c> oldList, Map<Id, Repair__c> oldMap){
        
        EmailTemplate et=[Select id from EmailTemplate where name = '修理报价信息更新' limit 1];

        List<Id> needUpdateList = new List<Id>(); 
        for(Repair__c re : newList){
            if((re.Repair_Firstestimated_Date_formula__c != null && re.Repair_Quotation_Id__c != oldMap.get(re.Id).Repair_Quotation_Id__c) || re.InspectionFailureCause__c != oldMap.get(re.Id).InspectionFailureCause__c){
                needUpdateList.add(re.Id);
            }
        }

        String[] toccAddresses = System.Label.RepairUpdatecc.split(';');
        String[] toAddresses = System.Label.RepairUpdateTo.split(';');


        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

        for(Id repairId : needUpdateList){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail = Messaging.renderStoredEmailTemplate(et.Id, null , repairId);
            mail.setSaveAsActivity(false);

            mail.setToAddresses(toAddresses);
            mail.setCcAddresses(toccAddresses);
            emails.add(mail);
        }

    }


    // before insert, before update
    public static void setRepairWorkday(List<Repair__c> newList, Map<Id, Repair__c> newMap, List<Repair__c> oldList, Map<Id, Repair__c> oldMap) {
        // 日历查询开始
        Date sd = null;
        // 日历查询截至
        Date ed = null;
        for (Repair__c rp : newList) {
            Repair__c old = oldMap == null ? new Repair__c() : oldMap.get(rp.Id);
            // 修理工作日为空、修理开始日更新、修理最终检测日更新
            if (rp.Repair_Start_Date__c != null && rp.Repair_Final_Inspection_Date__c != null &&
                    (rp.TAT_elapsed_workday__c == null ||
                     rp.Repair_Start_Date__c != old.Repair_Start_Date__c ||
                     rp.Repair_Final_Inspection_Date__c != old.Repair_Final_Inspection_Date__c)) {
                // 日历查询开始
                if (sd == null || rp.Repair_Start_Date__c < sd) {
                    sd = rp.Repair_Start_Date__c;
                }
                if (rp.Repair_Final_Inspection_Date__c < sd) {
                    sd = rp.Repair_Final_Inspection_Date__c;
                }
                // 日历查询截至
                if (ed == null || rp.Repair_Start_Date__c > ed) {
                    ed = rp.Repair_Start_Date__c;
                }
                if (rp.Repair_Final_Inspection_Date__c > ed) {
                    ed = rp.Repair_Final_Inspection_Date__c;
                }
            }
        }
        // 取得日历
        Map<String, Integer> ocMap = new Map<String, Integer>();
        if (sd != null && ed != null) {
            List<OlympusCalendar__c> ocList = [
                                                  select Id, Date__c, IsWorkDay__c
                                                  from OlympusCalendar__c
                                                  where Date__c >= :sd
                                                  and Date__c <= :ed
                                                  order by Date__c];

            for (OlympusCalendar__c oc : ocList) {
                Date d = oc.Date__c;
                Decimal iswd = oc.IsWorkDay__c;
                ocMap.put(d.format(), iswd.intValue());
            }
        }

        // 设定TAT：修理时长（工作日）
        for (Repair__c rp : newList) {
            Repair__c old = oldMap == null ? new Repair__c() : oldMap.get(rp.Id);
            // 修理工作日为空、修理开始日更新、修理最终检测日更新
            if (rp.Repair_Start_Date__c != null && rp.Repair_Final_Inspection_Date__c != null &&
                    (rp.TAT_elapsed_workday__c == null ||
                     rp.Repair_Start_Date__c != old.Repair_Start_Date__c ||
                     rp.Repair_Final_Inspection_Date__c != old.Repair_Final_Inspection_Date__c)) {
                Date d1 = rp.Repair_Start_Date__c;
                Date d2 = rp.Repair_Final_Inspection_Date__c;
                Integer days = 0;
                for (Integer i = 0; i <= d1.daysBetween(d2); i++) {
                    Date d = d1 + i;
                    if (ocMap.containsKey(d.format())) {
                        days += ocMap.get(d.format());
                    }
                }
                rp.TAT_elapsed_workday__c = days;
            }
            // 修理开始日、修理最终检测日为空
            if (rp.Repair_Start_Date__c == null && rp.Repair_Start_Date__c != old.Repair_Start_Date__c ||
                    rp.Repair_Final_Inspection_Date__c == null && rp.Repair_Final_Inspection_Date__c != old.Repair_Final_Inspection_Date__c) {
                rp.TAT_elapsed_workday__c = null;
            }
        }
    }

    public static void UpdateAssert(List<Repair__c> newList, Map<Id, Repair__c> newMap, List<Repair__c> oldList, Map<Id, Repair__c> oldMap) {
        System.debug('===========> start');

        List<String> productIds = new List<String>();

        if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {
            for (Repair__c rpc : newList) {
                productIds.add(rpc.Delivered_Product__c);
            }
        }

        if (Trigger.isAfter && Trigger.isDelete) {
            for (Repair__c rpa : oldList) {
                productIds.add(oldMap.get(rpa.Id).Delivered_Product__c);
            }
        }
        System.debug('===========> productIds' + productIds);
        List<Asset> assList = new List<Asset>();
        if(System.Test.isRunningTest()){
            assList = [select id, Reson_Can_not_Warranty__c from Asset where id in :productIds limit 1];
        } else{
            assList = [select id, Reson_Can_not_Warranty__c from Asset where id in :productIds];
        }

        List<String> assetIds = new List<String>();
        //Map<String, Asset> assMap = new Map<String, Asset>();
        for (Asset a : assList) {
            //assMap.put(a.Id, a);
            assetIds.add(a.id);
        }


        Map<String, List<Repair__c>> repaMap = new Map<String, List<Repair__c>>();
        List<Repair__c> repList = [select id, Status1__c, Status2__c, Return_Without_Repair_Reason__c, Contract_status__c, Return_Without_Repair_IF__c, Failure_Occurrence_Date__c,CancellationReason__c,
                                   Delivered_Product__c
                                   // LJPH-C67A6E gzw 逻辑fix start
                                   ,FSE_ApplyForRepair_Day__c,Contract_Start_Date__c,Contract_End_Date__c
                                   // LJPH-C67A6E gzw 逻辑fix end
                                   from Repair__c
                                   // HWAG-AWMCG9 2018/3/7 已经确定只有【未修理返品】的数据状态1 和 状态2 同时为取消
                                   //where Delivered_Product__c in :assetIds and Status1__c not in ('0.删除','0.取消')
                                   where Delivered_Product__c in :assetIds and Status1__c not in ('0.删除')
                                   order by Failure_Occurrence_Date__c desc];
        for (Repair__c rep : repList) {
            List<Repair__c> tmpList = new List<Repair__c>();
            if (repaMap.containsKey(rep.Delivered_Product__c)) {
                tmpList = repaMap.get(rep.Delivered_Product__c);
            }

            tmpList.add(rep);
            repaMap.put(rep.Delivered_Product__c, tmpList);
        }

        List<Asset> newAssList = new List<Asset>();

        for (Asset a : assList) {
            List<Repair__c> rList = repaMap.get(a.id);
            String reason = a.Reson_Can_not_Warranty__c;
            if (rList != null && rList.size() > 0) {
                if (rList[0] != null) {
                    if (rList[0].Return_Without_Repair_IF__c == true) {
                        //判断弃修的逻辑
                        // 20230207“弃修”“修理中”设备判断逻辑修改 start
                        if (rList[0].Return_Without_Repair_Reason__c == 'I/F理由、確認' &&(rList[0].CancellationReason__c == '报价单到期' 
                            || rList[0].CancellationReason__c == '废弃' || rList[0].CancellationReason__c == '购买新品' || rList[0].CancellationReason__c == '零件停产'
                            || rList[0].CancellationReason__c == '零件已停止生产' || rList[0].CancellationReason__c == '到其他公司修理' || rList[0].CancellationReason__c == '其他'
                            || rList[0].CancellationReason__c == '修理价格高' || rList[0].CancellationReason__c == '坚持继续使用' || rList[0].CancellationReason__c == '无法修理'
                            || rList[0].CancellationReason__c == '修理价格太高' || rList[0].CancellationReason__c == '用户坚持继续使用' || rList[0].CancellationReason__c == '无法修理返品')) {
                                if (reason != null) {
                                    if (reason.contains('修理中')) {
                                        a.Reson_Can_not_Warranty__c = reason.replace('修理中', '');
                                    }
                                    if (!reason.contains('弃修')) {
                                        a.Reson_Can_not_Warranty__c = reason + '弃修';
                                    }
                                } else {
                                    a.Reson_Can_not_Warranty__c = '弃修';
                                }
                        }
                        // 20230207“弃修”“修理中”设备判断逻辑修改 end
                        if (rList[0].Return_Without_Repair_Reason__c == '1.修理价格太高' || rList[0].Return_Without_Repair_Reason__c == '2.到其他公司修理'
                                || rList[0].Return_Without_Repair_Reason__c == '4.坚持继续使用' || rList[0].Return_Without_Repair_Reason__c == '3.报价单超期'
                                || rList[0].Return_Without_Repair_Reason__c == '5.废弃' || rList[0].Return_Without_Repair_Reason__c == '6.购买新品'
                                || rList[0].Return_Without_Repair_Reason__c == '8.乾燥') {
                            //ass = assMap.get(rpc.Delivered_Product__c);
                            if (reason != null) {
                                if (reason.contains('修理中')) {
                                    a.Reson_Can_not_Warranty__c = reason.replace('修理中', '');
                                }
                                if (!reason.contains('弃修')) {
                                    a.Reson_Can_not_Warranty__c = reason + '弃修';
                                }
                            } else {
                                a.Reson_Can_not_Warranty__c = '弃修';
                            }

                        } else {
                            if (reason != null) {
                                if (reason.contains('弃修')) {
                                    a.Reson_Can_not_Warranty__c = reason.replace('弃修', '');
                                }
                                // HWAG-AX3BDM 按照一起沟通确认的结果同时取消修理中标识
                                if (reason.contains('修理中')) {
                                    a.Reson_Can_not_Warranty__c = reason.replace('修理中', '');
                                }
                            }

                        }
                    } else {
                        //判断修理中的逻辑
                        // LJPH-C67A6E gzw 逻辑fix start
                        // if (rList[0].Contract_status__c != '契約') {
                        if (!(rList[0].Contract_status__c == '契約' ||
                            (rList[0].FSE_ApplyForRepair_Day__c >= rList[0].Contract_Start_Date__c && rList[0].FSE_ApplyForRepair_Day__c <= rList[0].Contract_End_Date__c))) {
                        // LJPH-C67A6E gzw 逻辑fix end
                            if (rList[0].Status2__c == '01.分公司受理完毕' || rList[0].Status2__c == '02.RC受理完毕' || rList[0].Status2__c == '03.报价检查结束' || rList[0].Status2__c == '04.报价跟进中') {
                                if (reason != null) {
                                    if (!reason.contains('修理中')) {
                                        a.Reson_Can_not_Warranty__c = reason + '修理中';
                                    }
                                } else {
                                    a.Reson_Can_not_Warranty__c = '修理中';
                                }
                            } else {
                                if (reason != null) {
                                    if (reason.contains('修理中')) {
                                        a.Reson_Can_not_Warranty__c = reason.replace('修理中', '');
                                    }
                                }
                            }

                        } else {
                            if (reason != null) {
                                if (reason.contains('修理中')) {
                                    a.Reson_Can_not_Warranty__c = reason.replace('修理中', '');
                                }
                            }
                        }
                        if (reason != null) {
                            if (reason.contains('弃修')) {
                                a.Reson_Can_not_Warranty__c = reason.replace('弃修', '');
                            }
                        }

                    }

                } else {
                    if (reason != null) {
                        if (reason.contains('弃修')) {
                            reason = reason.replace('弃修', '');
                        }
                        if (reason.contains('修理中')) {
                            a.Reson_Can_not_Warranty__c = reason.replace('修理中', '');
                        }
                    }
                }
            } else {
                if (reason != null) {
                    if (reason.contains('弃修')) {
                        reason = reason.replace('弃修', '');
                    }
                    if (reason.contains('修理中')) {
                        a.Reson_Can_not_Warranty__c = reason.replace('修理中', '');
                    }
                }

            }
            // 20210622 SQL优化 Start
            //newAssList.add(a);
            if (a.Reson_Can_not_Warranty__c != reason) {
                newAssList.add(a);
            }
            // 20210622 SQL优化 End
        }
        // 20210622 SQL优化 Start
        // update newAssList;
        if (newAssList.size() > 0) {
            update newAssList;
        }
        // 20210622 SQL优化 End

    }

    //wwp   2021/10/15    维修中心赋值新加西安 杭州 成都              start
    //2021/11/18    update     西安rc
    public static  Repair__c updateWorkLocationSelect(Repair__c repair, Maintenance_Contract_Asset__c middleTable) {
        List<String> northProvince = new List<String> {'北京', '北京酒仙桥', '北京石景山', '北京RC', 'SORC', '北京共通办事处', '天津', '石家庄', '呼和浩特', '济南', '青岛'};
        //沈阳
        List<String> syProvince = new List<String>{'哈尔滨','长春', '大连', '沈阳', '沈阳RC'};
        //华东
        List<String> hdProvince = new List<String> {'上海', '上海张江', '上海备品中心', '上海RC', '南京', '南昌', '福州', '合肥','浙江舟山市','浙江绍兴市','浙江宁波市'};
        //广州
        List<String> gzProvince = new List<String> {'广州', '广州备品中心', '深圳', '广州RC', '南宁', '武汉', '长沙'};
        
        //西安
        List<String> xaProvince = new List<String>{'太原','西安', '郑州', '西宁', '银川','乌鲁木齐','兰州','西安RC'};
        //杭州
        List<String> hzProvince = new List<String>{'杭州','杭州RC'};
        //成都
        //没有找到  【西藏】
        List<String> cdProvince = new List<String>{'成都','西藏', '重庆', '贵阳', '昆明','成都RC'};

        boolean leakDefaultFlag = true;

        //List<String> contractList = new List<String>();
        String cdate = NFMUtil.formatDate2Str(repair.CreatedDate != null ? repair.CreatedDate.date() : date.today());
        Date afterDate = NFMUtil.parseStr2Date(System.Label.creatDate_After, false);
        String adate = NFMUtil.formatDate2Str(afterDate);
 
        String NewProductGuaranteeObject = '';

        NewProductGuaranteeObject  = repair.NewProductGuarante_Txt__c;
        System.debug('cdate = ' + cdate);
        System.debug('adate = ' + adate);
        System.debug('middleTable = ' + middleTable);
        System.debug('NewProductGuaranteeObject = ' + NewProductGuaranteeObject);
        System.debug('repair.Sales_Branch__c!= repair.Finance_of_Branch__c = ' + repair.Sales_Branch__c!= repair.Finance_of_Branch__c);
        System.debug('repair.Contract_Principal_Office__c = ' + repair.Contract_Principal_Office__c);
        if (cdate > adate) {
            if(middleTable == null){
                if ((NewProductGuaranteeObject=='2: 多年保修'
                    ||NewProductGuaranteeObject=='2: 服务多年保修'
                    ||NewProductGuaranteeObject=='8: 市场多年保修')
                    &&(repair.Sales_Branch__c!= repair.Finance_of_Branch__c)) {
 
                    if(repair.On_site_repair__c == 'RC修理'||repair.On_site_repair__c== '直送SORC修理'||repair.On_site_repair__c== '直送OGZ修理' ){
                        if (NewProductGuaranteeObject == '8: 市场多年保修' && repair.Maintenance_Contract__c !=null) {
                            if (repair.Contract_Principal_Office__c == '沈阳RC') {
                                repair.work_location_select__c = '沈阳办事处';
                                leakDefaultFlag = false;
                            } else if(repair.Contract_Principal_Office__c == '成都RC'){
                                repair.work_location_select__c = '成都办事处';
                                leakDefaultFlag = false;
                            } else if(repair.Contract_Principal_Office__c == '杭州RC'){
                                repair.work_location_select__c = '杭州办事处';
                                leakDefaultFlag = false;
                            } else if(repair.Contract_Principal_Office__c == '西安RC'){
                                repair.work_location_select__c = '西安办事处';
                                leakDefaultFlag = false;
                            } else if(repair.Contract_Principal_Office__c == '上海RC'){
                                if (hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '杭州共通办事处';
                                    leakDefaultFlag = false;
                                }else{
                                    repair.work_location_select__c = '上海办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if(repair.Contract_Principal_Office__c == '广州RC'){
                                if (cdProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '成都共通办事处';
                                    leakDefaultFlag = false;
                                }else{
                                    repair.work_location_select__c = '广东办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if (repair.Contract_Principal_Office__c == '北京RC') {
                                if (northProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '北京办事处';
                                    leakDefaultFlag = false;
                                } else if(xaProvince.contains(repair.SalesOfficeCode_selection__c)){
                                    repair.work_location_select__c = '西安共通办事处';
                                    leakDefaultFlag = false;
                                } else if (hdProvince.contains(repair.SalesOfficeCode_selection__c) || gzProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || syProvince.contains(repair.SalesOfficeCode_selection__c) || cdProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '共通办事处';
                                    leakDefaultFlag = false;
                                }
                            } else {
                                repair.work_location_select__c = '北京办事处';
                                leakDefaultFlag = false;
                            }
                        } else {
                             if (repair.Sales_Branch__c == '上海分公司') {
                                if (hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '杭州共通办事处';
                                    leakDefaultFlag = false;
                                } else {
                                    repair.work_location_select__c = '上海办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if (repair.Sales_Branch__c == '广州分公司') {
                                if (cdProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '成都共通办事处';
                                    leakDefaultFlag = false;
                                } else {
                                    repair.work_location_select__c = '广东办事处';
                                    leakDefaultFlag = false;
                                }
                            }else if (repair.Sales_Branch__c == '北京分公司' ) {
                                if (northProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '北京办事处';
                                    leakDefaultFlag = false;
                                } else if(xaProvince.contains(repair.SalesOfficeCode_selection__c)){
                                    repair.work_location_select__c = '西安共通办事处';
                                    leakDefaultFlag = false;
                                } else if (hdProvince.contains(repair.SalesOfficeCode_selection__c) || gzProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || syProvince.contains(repair.SalesOfficeCode_selection__c) || cdProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '共通办事处';
                                    leakDefaultFlag = false;
                                }
                            } else {
                                repair.work_location_select__c = '共通办事处';
                                leakDefaultFlag = false;
                            }
                        }
                    }else if(repair.On_site_repair__c == '办事处修理' || repair.On_site_repair__c== '现场修理'){
                      /** 20230313 you DB202303171085
                       if(repair.Sales_Branch__c == '上海分公司'){
                            if (hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                repair.work_location_select__c = '杭州共通办事处';
                                leakDefaultFlag = false;
                            }
                        } else **/
                        if(repair.Sales_Branch__c == '广州分公司'){
                            if (cdProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                repair.work_location_select__c = '成都共通办事处';
                                leakDefaultFlag = false;
                            }
                        } else if (repair.Sales_Branch__c == '北京分公司' ) {
                            if(xaProvince.contains(repair.SalesOfficeCode_selection__c)){
                                repair.work_location_select__c = '西安共通办事处';
                                leakDefaultFlag = false;
                            } else if (hdProvince.contains(repair.SalesOfficeCode_selection__c) || gzProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || syProvince.contains(repair.SalesOfficeCode_selection__c) || cdProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                repair.work_location_select__c = '共通办事处';
                                leakDefaultFlag = false;
                            }
                        }
                    }
                }else{
                    if(repair.Maintenance_Contract__c !=null){
                        if(repair.On_site_repair__c == 'RC修理'||repair.On_site_repair__c== '直送SORC修理'||repair.On_site_repair__c== '直送OGZ修理' ){
                            if (repair.Contract_Principal_Office__c == '沈阳RC') {
                                repair.work_location_select__c = '沈阳办事处';
                                leakDefaultFlag = false;
                            } else if(repair.Contract_Principal_Office__c == '成都RC'){
                                repair.work_location_select__c = '成都办事处';
                                leakDefaultFlag = false;
                            } else if(repair.Contract_Principal_Office__c == '杭州RC'){
                                repair.work_location_select__c = '杭州办事处';
                                leakDefaultFlag = false;
                            } else if(repair.Contract_Principal_Office__c == '西安RC'){
                                repair.work_location_select__c = '西安办事处';
                                leakDefaultFlag = false;
                            } else if(repair.Contract_Principal_Office__c == '上海RC'){
                                if (hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '杭州共通办事处';
                                    leakDefaultFlag = false;
                                }else{
                                    repair.work_location_select__c = '上海办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if(repair.Contract_Principal_Office__c == '广州RC'){
                                if (cdProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '成都共通办事处';
                                    leakDefaultFlag = false;
                                }else{
                                    repair.work_location_select__c = '广东办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if (repair.Contract_Principal_Office__c == '北京RC') {
                                if (northProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '北京办事处';
                                    leakDefaultFlag = false;
                                } else if(xaProvince.contains(repair.SalesOfficeCode_selection__c)){
                                    repair.work_location_select__c = '西安共通办事处';
                                    leakDefaultFlag = false;
                                } else if (hdProvince.contains(repair.SalesOfficeCode_selection__c) || gzProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || syProvince.contains(repair.SalesOfficeCode_selection__c) || cdProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '共通办事处';
                                    leakDefaultFlag = false;
                                }
                            } else {
                                repair.work_location_select__c = '北京办事处';
                                leakDefaultFlag = false;
                            }
                        }else if(repair.On_site_repair__c == '办事处修理' || repair.On_site_repair__c== '现场修理'){
                           /** 20230313 you DB202303171085
                            if(repair.Contract_Principal_Office__c == '上海RC'){
                                if (hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '杭州共通办事处';
                                    leakDefaultFlag = false;
                                }
                            } else **/
                            if(repair.Contract_Principal_Office__c == '广州RC'){
                                if (cdProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '成都共通办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if (repair.Contract_Principal_Office__c == '北京RC') {
                                if(xaProvince.contains(repair.SalesOfficeCode_selection__c)){
                                    repair.work_location_select__c = '西安共通办事处';
                                    leakDefaultFlag = false;
                                } else if (hdProvince.contains(repair.SalesOfficeCode_selection__c) || gzProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || syProvince.contains(repair.SalesOfficeCode_selection__c) || cdProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '共通办事处';
                                    leakDefaultFlag = false;
                                }
                            } 
                        }
                    }
                }
                    system.debug('NewProductGuaranteeObject:'+NewProductGuaranteeObject);
            } else {//维修合同不为空
                if ((NewProductGuaranteeObject=='2: 多年保修'
                    ||NewProductGuaranteeObject=='2: 服务多年保修'
                    ||NewProductGuaranteeObject=='8: 市场多年保修')
                    &&(repair.Sales_Branch__c!= repair.Finance_of_Branch__c)) {
                    //根据服务方式判断
                    if(repair.On_site_repair__c == 'RC修理'||repair.On_site_repair__c== '直送SORC修理'||repair.On_site_repair__c== '直送OGZ修理' ){
                        if (NewProductGuaranteeObject == '8: 市场多年保修' && repair.Maintenance_Contract__c !=null) {
                            if (repair.repairPlace__c == '沈阳RC') {
                                repair.work_location_select__c = '沈阳办事处';
                                leakDefaultFlag = false;
                            } else if(repair.repairPlace__c == '成都RC'){
                                repair.work_location_select__c = '成都办事处';
                                leakDefaultFlag = false;
                            } else if(repair.repairPlace__c == '杭州RC'){
                                repair.work_location_select__c = '杭州办事处';
                                leakDefaultFlag = false;
                            } else if(repair.repairPlace__c == '西安RC'){
                                repair.work_location_select__c = '西安办事处';
                                leakDefaultFlag = false;
                            } else if(repair.repairPlace__c == '上海RC'){
                                if (hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '杭州共通办事处';
                                    leakDefaultFlag = false;
                                }else{
                                    repair.work_location_select__c = '上海办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if(repair.repairPlace__c == '广州RC'){
                                if (cdProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '成都共通办事处';
                                    leakDefaultFlag = false;
                                }else{
                                    repair.work_location_select__c = '广东办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if (repair.repairPlace__c == '北京RC') {
                                if (northProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '北京办事处';
                                    leakDefaultFlag = false;
                                } else if(xaProvince.contains(repair.SalesOfficeCode_selection__c)){
                                    repair.work_location_select__c = '西安共通办事处';
                                    leakDefaultFlag = false;
                                } else if (hdProvince.contains(repair.SalesOfficeCode_selection__c) || gzProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || syProvince.contains(repair.SalesOfficeCode_selection__c) || cdProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '共通办事处';
                                    leakDefaultFlag = false;
                                }
                            } else {
                                repair.work_location_select__c = '北京办事处';
                                leakDefaultFlag = false;
                            }
                        } else {
                            if (repair.Sales_Branch__c == '上海分公司') {
                                if (hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '杭州共通办事处';
                                    leakDefaultFlag = false;
                                } else {
                                    repair.work_location_select__c = '上海办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if (repair.Sales_Branch__c == '广州分公司') {
                                if (cdProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '成都共通办事处';
                                    leakDefaultFlag = false;
                                } else {
                                    repair.work_location_select__c = '广东办事处';
                                    leakDefaultFlag = false;
                                }
                            }else if (repair.Sales_Branch__c == '北京分公司' ) {
                                if (northProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '北京办事处';
                                    leakDefaultFlag = false;
                                } else if(xaProvince.contains(repair.SalesOfficeCode_selection__c)){
                                    repair.work_location_select__c = '西安共通办事处';
                                    leakDefaultFlag = false;
                                } else if (hdProvince.contains(repair.SalesOfficeCode_selection__c) || gzProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || syProvince.contains(repair.SalesOfficeCode_selection__c) || cdProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '共通办事处';
                                    leakDefaultFlag = false;
                                }
                            } else {
                                repair.work_location_select__c = '共通办事处';
                                leakDefaultFlag = false;
                            }
                        }
                     }else if(repair.On_site_repair__c == '办事处修理' || repair.On_site_repair__c== '现场修理'){
                        /** 20230313 you DB202303171085
                        if(repair.Sales_Branch__c == '上海分公司'){
                            //根据修理品返送地为非北京
                            if (hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                repair.work_location_select__c = '杭州共通办事处';
                                leakDefaultFlag = false;
                            }
                        } else **/
                        if(repair.Sales_Branch__c == '广州分公司'){
                            //根据修理品返送地为非北京
                            if (cdProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                repair.work_location_select__c = '成都共通办事处';
                                leakDefaultFlag = false;
                            }
                        } else if (repair.Sales_Branch__c == '北京分公司' ) {
                            //根据修理品返送地为非北京
                            if(xaProvince.contains(repair.SalesOfficeCode_selection__c)){
                                repair.work_location_select__c = '西安共通办事处';
                                leakDefaultFlag = false;
                            } else if (hdProvince.contains(repair.SalesOfficeCode_selection__c) || gzProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || syProvince.contains(repair.SalesOfficeCode_selection__c) || cdProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                repair.work_location_select__c = '共通办事处';
                                leakDefaultFlag = false;
                            }
                        }  
                    }
                    system.debug('NewProductGuaranteeObject:'+NewProductGuaranteeObject);
                } else {
                    //根据服务方式判断
                    if(repair.Maintenance_Contract__c !=null){
                        if(repair.On_site_repair__c == 'RC修理'||repair.On_site_repair__c== '直送SORC修理'||repair.On_site_repair__c== '直送OGZ修理' ){
                            if (middleTable.Maintenance_Contract__r.SalesOfficeCode_selection__c == '沈阳RC') {
                                repair.work_location_select__c = '沈阳办事处';
                                leakDefaultFlag = false;
                            } else if(middleTable.Maintenance_Contract__r.SalesOfficeCode_selection__c == '成都RC'){
                                repair.work_location_select__c = '成都办事处';
                                leakDefaultFlag = false;
                            } else if(middleTable.Maintenance_Contract__r.SalesOfficeCode_selection__c == '杭州RC'){
                                repair.work_location_select__c = '杭州办事处';
                                leakDefaultFlag = false;
                            } else if(middleTable.Maintenance_Contract__r.SalesOfficeCode_selection__c == '西安RC'){
                                repair.work_location_select__c = '西安办事处';
                                leakDefaultFlag = false;
                            } else if(middleTable.Maintenance_Contract__r.SalesOfficeCode_selection__c == '上海RC'){
                                if (hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '杭州共通办事处';
                                    leakDefaultFlag = false;
                                }else{
                                    repair.work_location_select__c = '上海办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if(middleTable.Maintenance_Contract__r.SalesOfficeCode_selection__c == '广州RC'){
                                if (cdProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '成都共通办事处';
                                    leakDefaultFlag = false;
                                }else{
                                    repair.work_location_select__c = '广东办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if (middleTable.Maintenance_Contract__r.SalesOfficeCode_selection__c == '北京RC') {
                                if (northProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '北京办事处';
                                    leakDefaultFlag = false;
                                } else if(xaProvince.contains(repair.SalesOfficeCode_selection__c)){
                                    repair.work_location_select__c = '西安共通办事处';
                                    leakDefaultFlag = false;
                                } else if (hdProvince.contains(repair.SalesOfficeCode_selection__c) || gzProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || syProvince.contains(repair.SalesOfficeCode_selection__c) || cdProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '共通办事处';
                                    leakDefaultFlag = false;
                                }
                            } else {
                                repair.work_location_select__c = '北京办事处';
                                leakDefaultFlag = false;
                            }
                        }else if(repair.On_site_repair__c == '办事处修理' || repair.On_site_repair__c== '现场修理'){
                           /** 20230313 you DB202303171085
                            if(middleTable.Maintenance_Contract__r.SalesOfficeCode_selection__c == '上海RC'){
                                if (hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '杭州共通办事处';
                                    leakDefaultFlag = false;
                                }
                            } else**/
                            if(middleTable.Maintenance_Contract__r.SalesOfficeCode_selection__c == '广州RC'){
                                if (cdProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '成都共通办事处';
                                    leakDefaultFlag = false;
                                }
                            } else if (middleTable.Maintenance_Contract__r.SalesOfficeCode_selection__c == '北京RC') {
                                if(xaProvince.contains(repair.SalesOfficeCode_selection__c)){
                                    repair.work_location_select__c = '西安共通办事处';
                                    leakDefaultFlag = false;
                                } else if (hdProvince.contains(repair.SalesOfficeCode_selection__c) || gzProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || syProvince.contains(repair.SalesOfficeCode_selection__c) || cdProvince.contains(repair.SalesOfficeCode_selection__c) 
                                    || hzProvince.contains(repair.SalesOfficeCode_selection__c)) {
                                    repair.work_location_select__c = '共通办事处';
                                    leakDefaultFlag = false;
                                }
                            }
                        }  
                    } 
                }
            }
        }
        if(leakDefaultFlag){
            generalUpdateWorkLocationSelect(repair);
        }
        return repair;
    }

    /**
     * add        wangweipeng                 2021/12/09                     start
     * [generalUpdateWorkLocationSelect 一般修理品的维修中心赋值逻辑]
     * @param  repair [description]
     * @return        [description]
     * 增加一般修理品的逻辑，如果（多年保修标识“无”且维修合同标识“无”），那么按照以下逻辑赋值
     * 北京办事处：北京、天津、河北、内蒙、山东
     * 西安办事处：山西、陕西、河南、青海、宁夏、新疆、甘肃
     * 沈阳办事处：黑龙江、吉林、辽宁
     * 上海办事处：上海、江苏、江西、福建、安徽、浙江舟山市、浙江绍兴市、浙江宁波市
     * 杭州办事处：除浙江舟山市、浙江绍兴市、浙江宁波市以外的浙江地区
     * 广州办事处：广东、广西、海南、湖北、湖南、
     * 成都办事处：四川、西藏、重庆、贵州、云南
     */
    public static Repair__c generalUpdateWorkLocationSelect(Repair__c repair) {
        if(repair.On_site_repair__c == 'RC修理'||repair.On_site_repair__c== '直送SORC修理'||repair.On_site_repair__c== '直送OGZ修理' ){
           if(String.isNotBlank(repair.SalesOfficeCode_selection__c)){
                //北京
                List<String> northProvince = new List<String> {'北京', '北京酒仙桥', '北京石景山', '北京RC', 'SORC', '北京共通办事处', '天津', '石家庄', '呼和浩特', '济南', '青岛'};
                //沈阳
                List<String> syProvince = new List<String>{'哈尔滨','长春', '大连', '沈阳', '沈阳RC'};
                //华东（上海）
                List<String> hdProvince = new List<String> {'上海', '上海张江', '上海备品中心', '上海RC', '南京', '南昌', '福州', '合肥','浙江舟山市','浙江绍兴市','浙江宁波市'};
                //广州
                List<String> gzProvince = new List<String> {'广州', '广州备品中心', '深圳', '广州RC', '南宁', '武汉', '长沙'};
                
                //西安
                List<String> xaProvince = new List<String>{'太原','西安', '郑州', '西宁', '银川','乌鲁木齐','兰州','西安RC'};
                //杭州
                List<String> hzProvince = new List<String>{'杭州','杭州RC'};
                //成都
                //没有找到  【西藏】
                List<String> cdProvince = new List<String>{'成都','西藏', '重庆', '贵阳', '昆明','成都RC'};

                if(northProvince.contains(repair.SalesOfficeCode_selection__c)){
                    repair.work_location_select__c = '北京办事处';
                }else if(syProvince.contains(repair.SalesOfficeCode_selection__c)){
                    repair.work_location_select__c = '沈阳办事处';
                }else if(hdProvince.contains(repair.SalesOfficeCode_selection__c)){
                    repair.work_location_select__c = '上海办事处';
                }else if(gzProvince.contains(repair.SalesOfficeCode_selection__c)){
                    repair.work_location_select__c = '广东办事处';
                }else if(xaProvince.contains(repair.SalesOfficeCode_selection__c)){
                    repair.work_location_select__c = '西安办事处';
                }else if(hzProvince.contains(repair.SalesOfficeCode_selection__c)){
                    repair.work_location_select__c = '杭州办事处';
                }else if(cdProvince.contains(repair.SalesOfficeCode_selection__c)){
                    repair.work_location_select__c = '成都办事处';
                }
           }
        }
        return repair;
    }
    //add        wangweipeng                 2021/12/09                     end
    //先款后修-修理增加先款标识 thh 20220322 start
    public static void AdvancePaymentFlagUpsert(List<Repair__c> newList, Map<Id, Repair__c> newMap, List<Repair__c> oldList, Map<Id, Repair__c> oldMap) {
        List<String> RepairQuotationIdList = new List<String>();
        List<Repair__c> NoRepairQuotationList = new List<Repair__c>();
        List<Repair_Quotation__c> RepairQuotationList = new List<Repair_Quotation__c>();
        List<String> DealerIdList = new List<String>();
        List<Account> DealerList = new List<Account>();
        for(Repair__c rep : newList){
            if(rep.Repair_Quotation_Id__c != null){
                RepairQuotationIdList.add(rep.Repair_Quotation_Id__c);
            }else{
                NoRepairQuotationList.add(rep);
            }
        }
        //同期中的修理修理报价有值时，看报价上的先款标识进行赋值
        if(RepairQuotationIdList.size() > 0){
            if (System.Test.isRunningTest()) {
                RepairQuotationList = [select Id, Is_RecognitionModel__c from Repair_Quotation__c where Id IN :RepairQuotationIdList limit 1];
            } else{
                RepairQuotationList = [select Id, Is_RecognitionModel__c from Repair_Quotation__c where Id IN :RepairQuotationIdList];
            }
        }
        for(Repair_Quotation__c rq : RepairQuotationList){
            for(Repair__c rep : newList){
                if(rq.Id == rep.Repair_Quotation_Id__c){
                    rep.Advance_Payment_Flag__c = rq.Is_RecognitionModel__c;
                }
            }
        }
        //同期中的修理报价没值时，看修理单上的经销商是不是先款对象进行赋值
        if(NoRepairQuotationList.size() > 0){
            for(Repair__c rep : NoRepairQuotationList){
                DealerIdList.add(rep.Dealer__c);
            }
        }
        if(DealerIdList.size() > 0 ){
            DealerList = [select Id, FirstParagraphEnd__c from Account where Id IN : DealerIdList];
        }
        for(Account dealer : DealerList){
            for(Repair__c rep : newList){
                if(rep.Dealer__c == dealer.Id){
                    rep.Advance_Payment_Flag__c = dealer.FirstParagraphEnd__c;
                }
            }
        }
    }
    //先款后修-修理增加先款标识 thh 20220322 end

    public static void CheckUpdate(List<Repair__c> oldList, Map<Id, Repair__c> newMap) {
        System.debug('===========> start');

        String userId = UserInfo.getProfileId();

        if ((userId == System.Label.ProfileId_SystemAdmin || userId == System.Label.repair_trigger_1 //niwu -00e10000000dEQxAAM 
                // 接口用户换为新的简档类型 start
                || userId == System.label.repair_trigger_2) //niwu -00e10000000dzzGAAQ
                // 接口用户换为新的简档类型 end
                && !System.Test.isRunningTest()) {
            return;
        }

        Repair__c rpNew = oldList[0];
        Repair__c rpOld = newMap.get(rpNew.Id);

        if (rpOld == null) {
            return;
        }

        if (rpNew.Status1__c == '0.删除') {
            if (rpNew.Status__c != rpOld.Status__c) {
                return;
            }
            rpOld.addError('数据已经删除或者取消不能再修改！!!!');
            return;
        }
        System.debug('rpNew.Status1__c ==' + rpNew.Status1__c);
        if (rpNew.Status1__c == '0.取消') {
            System.debug('rpNew.Status1__c == 取消');
            if (rpNew.Status__c != rpOld.Status__c) {
                return;
            }


            Boolean chkFlg = true;

            if (rpNew.Delivered_Product__c != rpOld.Delivered_Product__c || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Hospital__c != rpOld.Hospital__c || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Department_Class__c != rpOld.Department_Class__c || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Strategic_Department_Class_Name__c != rpOld.Strategic_Department_Class_Name__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Department_Name__c != rpOld.Department_Name__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Account__c != rpOld.Account__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Dealer__c != rpOld.Dealer__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Incharge_Staff__c != rpOld.Incharge_Staff__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Incharge_Staff_Contact__c != rpOld.Incharge_Staff_Contact__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            System.debug('1 ===========> ' + chkFlg);
            if (rpNew.SalesOfficeCode_selection__c != rpOld.SalesOfficeCode_selection__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.On_site_repair__c != rpOld.On_site_repair__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.work_location_select__c != rpOld.work_location_select__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Repair_Detail__c != rpOld.Repair_Detail__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.InspectionResultFlag__c != rpOld.InspectionResultFlag__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.InspectionComment__c != rpOld.InspectionComment__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.InspectionFailureCause__c != rpOld.InspectionFailureCause__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.PaperRepairRequestNo__c != rpOld.PaperRepairRequestNo__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.RepairOrderStatusCode__c != rpOld.RepairOrderStatusCode__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.RepairOrderStatusName__c != rpOld.RepairOrderStatusName__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.ReRepairObject_result__c != rpOld.ReRepairObject_result__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.SAPQuotationNo_from_quotation__c != rpOld.SAPQuotationNo_from_quotation__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Maintenance_Contract__c != rpOld.Maintenance_Contract__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Repair_Quotation_Id__c != rpOld.Repair_Quotation_Id__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_Estimation_Follow_Date__c != rpOld.Repair_Estimation_Follow_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.PhenomenonName__c != rpOld.PhenomenonName__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Failure_Source__c != rpOld.Failure_Source__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.CauseName__c != rpOld.CauseName__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.DescriptionName__c != rpOld.DescriptionName__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.LocationName__c != rpOld.LocationName__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.LocationGroupName__c != rpOld.LocationGroupName__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Follow_Comment__c != rpOld.Follow_Comment__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_Rank__c != rpOld.Repair_Rank__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_Estimation_Followed_By__c != rpOld.Repair_Estimation_Followed_By__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Billing_Amount__c != rpOld.Billing_Amount__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Paid_Amount__c != rpOld.Paid_Amount__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            System.debug('2 ===========> ' + chkFlg);
            if (rpNew.Receipt_category__c != rpOld.Receipt_category__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Received_money_day_finacial_dapt__c != rpOld.Received_money_day_finacial_dapt__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Receipt_comment__c != rpOld.Receipt_comment__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Request_yanshoudan_PDF__c != rpOld.Request_yanshoudan_PDF__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Confirmed_Collect_money__c != rpOld.Confirmed_Collect_money__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Collect_money_check1__c != rpOld.Collect_money_check1__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Confirmed_received_document__c != rpOld.Confirmed_received_document__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Sheet_mating_staff1__c != rpOld.Sheet_mating_staff1__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Forecast_Collection_Date__c != rpOld.Forecast_Collection_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Whether_collect_repair_money_or_not__c != rpOld.Whether_collect_repair_money_or_not__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Collection_Status__c != rpOld.Collection_Status__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Reason_cannot_collect_repair_fee__c != rpOld.Reason_cannot_collect_repair_fee__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Uncollect_fee_comment__c != rpOld.Uncollect_fee_comment__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_Collection_Follow_Date__c != rpOld.Repair_Collection_Follow_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_Collection_Followed_By__c != rpOld.Repair_Collection_Followed_By__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Failure_Occurrence_Date__c != rpOld.Failure_Occurrence_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.On_Site_Repair_Order_Date__c != rpOld.On_Site_Repair_Order_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Send_To_RC_Date__c != rpOld.Send_To_RC_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_Ordered_Date__c != rpOld.Repair_Ordered_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_Inspection_Date__c != rpOld.Repair_Inspection_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Agreed_Date__c != rpOld.Agreed_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Agreed_Date2__c != rpOld.Agreed_Date2__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Agreed_Date_Cancel__c != rpOld.Agreed_Date_Cancel__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.part_arrangement_complete__c != rpOld.part_arrangement_complete__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_Start_Date__c != rpOld.Repair_Start_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Planned_Repair_Completed_Date__c != rpOld.Planned_Repair_Completed_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_Completed_Date__c != rpOld.Repair_Completed_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_Final_Inspection_Date__c != rpOld.Repair_Final_Inspection_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_Shipped_Date__c != rpOld.Repair_Shipped_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Facility_Return_Receipt_Collection_reque__c != rpOld.Facility_Return_Receipt_Collection_reque__c
                    || System.Test.isRunningTest() ) {
                chkFlg = false;
            }
            if (rpNew.Receipt_received_date__c != rpOld.Receipt_received_date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Facility_Return_Receipt_Collection_Date__c != rpOld.Facility_Return_Receipt_Collection_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Billing_Received_Date__c != rpOld.Billing_Received_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Received_Date__c != rpOld.Received_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.repair_cancel_date__c != rpOld.repair_cancel_date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.RepairOrder_Complete_Day__c != rpOld.RepairOrder_Complete_Day__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            System.debug('3 ===========> ' + chkFlg);
            if (rpNew.StatusUpdateTime__c != rpOld.StatusUpdateTime__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Return_Without_Repair__c != rpOld.Return_Without_Repair__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Return_Without_Repair_IF__c != rpOld.Return_Without_Repair_IF__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Return_Without_Repair_Request_Date__c != rpOld.Return_Without_Repair_Request_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Quotation_agree_cancel_date__c != rpOld.Quotation_agree_cancel_date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.IncorrectDataComment__c != rpOld.IncorrectDataComment__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Return_Without_Repair_Reason__c != rpOld.Return_Without_Repair_Reason__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.CancellationReason__c != rpOld.CancellationReason__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Return_Without_Repair_Date__c != rpOld.Return_Without_Repair_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.CancelInvoice_day__c != rpOld.CancelInvoice_day__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.SAP_not_accept_repair_result__c != rpOld.SAP_not_accept_repair_result__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.Repair_Source__c != rpOld.Repair_Source__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.On_Call_ID__c != rpOld.On_Call_ID__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.QIS_ID__c != rpOld.QIS_ID__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.InsReport__c != rpOld.InsReport__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }

            if (rpNew.SAP_Transfer_time__c != rpOld.SAP_Transfer_time__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Service_contract_judege_day__c != rpOld.Service_contract_judege_day__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Invalid_check__c != rpOld.Invalid_check__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Delete_Flag__c != rpOld.Delete_Flag__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.NFM108_Receipt__c != rpOld.NFM108_Receipt__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_List_Price__c != rpOld.Repair_List_Price__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Final_complete_day__c != rpOld.Final_complete_day__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Latest_Collect_Date_Priority__c != rpOld.Latest_Collect_Date_Priority__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.SAPRepairNo__c != rpOld.SAPRepairNo__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            System.debug('4 ===========> ' + chkFlg);
            //if (rpNew.Status__c != rpOld.Status__c) {
            //   chkFlg = false;
            //}
            if (rpNew.Repair_Shipped_Date__c != rpOld.Repair_Shipped_Date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.NFM105_MessageGroupNumber__c != rpOld.NFM105_MessageGroupNumber__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.FSE_owner_Salesdepartment__c != rpOld.FSE_owner_Salesdepartment__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Discount_Price__c != rpOld.Discount_Price__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_cost__c != rpOld.Repair_cost__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            if (rpNew.Repair_cost_date__c != rpOld.Repair_cost_date__c
                    || System.Test.isRunningTest()) {
                chkFlg = false;
            }
            // if (rpNew.photoType__c != rpOld.photoType__c) {
            //     chkFlg = false;
            // }

            System.debug('chkFlg = ' + chkFlg);
            if (!chkFlg && !System.Test.isRunningTest()) {
                rpOld.addError('数据已经删除或者取消不能再修改！');
                return;

            } else {
                integer i = 0;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                return;
            }
                
        }
        System.debug('===========> end');

    }

}