| // 日付を更新するため、更新をbeforeにする | 
| // SAP送信はWF値を送信しないので、before更新でも問題ない | 
| trigger NFM001 on Account (before insert, before update, after insert) { | 
|     if (ControllerUtil.EscapeNFM001Trigger) { | 
| System.debug('TriggerEscape、EscapeNFM001Trigger:::::' + ControllerUtil.EscapeNFM001Trigger); | 
|         return; | 
|     } | 
|      | 
|     // WFなど設定により、2回目呼び出される場合もある、2回目をここに入らないように NFM001Controller.isRunning を判断する | 
|     // LHJ Start | 
|     //if (NFM001Controller.isRunning) { | 
|     if (NFM001Controller.isRunning || NFM201Controller.isRunning) { | 
|     // LHJ End | 
|         return; | 
|     }  | 
|     // 送信対象のレコードタイプ | 
|     String[] needSendTypes = new String[] {'病院', '診療科 その他', '診療科 呼吸科', '診療科 婦人科', '診療科 普外科', '診療科 泌尿科', '診療科 消化科', '診療科 耳鼻喉科'}; | 
|     //  LD 20200807 优化account record type ID 读取方法 Start | 
|   | 
|     // 原逻辑 | 
|     // List<RecordType> rects = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN :needSendTypes]; | 
|     // Map<String, String> needSendRectMap = new Map<String, String>(); | 
|     // for (RecordType rect : rects) { | 
|     //     needSendRectMap.put(rect.Id, rect.Name); | 
|     // } | 
|   | 
|   | 
|     // 新逻辑 | 
|     String [] RecordTypeId = new String[] {System.Label.HP , System.Label.Department_OTH , System.Label.Department_BF , System.Label.Department_GYN , System.Label.Department_GS , System.Label.Department_URO , System.Label.Department_GI , System.Label.Department_ENT }; | 
|     Map<String, String> needSendRectMap = new Map<String, String>(); | 
|     needSendRectMap.put(RecordTypeId[0], needSendTypes[0]); | 
|     needSendRectMap.put(RecordTypeId[1], needSendTypes[1]); | 
|     needSendRectMap.put(RecordTypeId[2], needSendTypes[2]); | 
|     needSendRectMap.put(RecordTypeId[3], needSendTypes[3]); | 
|     needSendRectMap.put(RecordTypeId[4], needSendTypes[4]); | 
|     needSendRectMap.put(RecordTypeId[5], needSendTypes[5]); | 
|     needSendRectMap.put(RecordTypeId[6], needSendTypes[6]); | 
|     needSendRectMap.put(RecordTypeId[7], needSendTypes[7]); | 
|   | 
|     //  LD 20200807 优化account record type ID 读取方法 End | 
|   | 
|     // 送信対象 Id をセット | 
|     List<String> accIds = new List<String>(); | 
|     Map<String, String> purposeOfAdviceMap = new Map<String, String>();    // 1:Delete 2:Add(Insert) 3:Change(Update) | 
|     // 病院ID:{Name(変更後)、OCM_Category__c(変更後)、State_Text__c(変更後)}, isUpdate のところ値を設定 | 
|     Map<Id, Account> hpAfterAccMap = new Map<Id, Account>(); | 
|     // 病院ID:{Name(変更後)}, isUpdate のところ値を設定 | 
|     Map<Id, Account> hpNameAfterAccMap = new Map<Id, Account>(); | 
|     // 病院の省、変更したIDを集める | 
|     List<String> stateIds = new List<String>(); | 
|   | 
|     // LHJ CBPR 20190321 Start | 
|     Map<String, String> needQuolifiedMap = new Map<String, String>(); | 
|     // LHJ CBPR 20190321 End | 
|   | 
|     if (Trigger.isInsert) { | 
|         if (Trigger.isBefore) { | 
|             for(Account a : Trigger.new) { | 
|                 // I/F と関係ない | 
|                 if (needSendRectMap.get(a.RecordTypeId) == '病院' && String.isBlank(a.State_Master__c) == false) { | 
|                     stateIds.add(a.State_Master__c); | 
|                 } | 
|             } | 
|         } else if (Trigger.isAfter){ | 
|             // isAfter | 
|             for(Account a : Trigger.new) { | 
|                 if (needSendRectMap.get(a.RecordTypeId) == null) { | 
|                     continue; | 
|                 } | 
|                 //未能审批通过的,不上传sap | 
|                 if(a.Is_Active__c == '草案中' || a.Is_Active__c == '申请中'){ | 
|                     continue; | 
|                 } | 
|                 NFM001Controller.isRunning = true; | 
|                 // LHJ Start | 
|                 NFM201Controller.isRunning = true; | 
|                 // LHJ End | 
|                 accIds.add(a.Id); | 
|                 purposeOfAdviceMap.put(a.Id, '2'); | 
|                 NFM001Controller.debug_msg = 'NFM001_callout_insert_' + a.Name; | 
|             } | 
|         } | 
|     } | 
|     else if (Trigger.isUpdate) { | 
|         List<String> hospitalIds = new List<String>(); | 
|         List<String> hospitalIdsNeedUpdateAccountIsActive = new List<String>(); | 
|         Map<Id, String> hpDateToMap = new Map<Id, String>();         // 変更後のDepartmentEffectiveDateTo__c | 
|         Map<Id, String> hpIsActiveMap = new Map<Id, String>();       // 変更後の有効/無効 | 
|         for(Account a : Trigger.new) { | 
|             if (needSendRectMap.get(a.RecordTypeId) == null) { | 
|                 continue; | 
|             } | 
|             //未能审批通过的,不上传sap | 
|             if(a.Is_Active__c == '草案中' || a.Is_Active__c == '申请中'){ | 
|                 continue; | 
|             } | 
|             // I/F と関係ない | 
|             if (needSendRectMap.get(a.RecordTypeId) == '病院' && Trigger.oldMap.get(a.Id).get('State_Master__c') != a.State_Master__c) { | 
|                 stateIds.add(a.State_Master__c); | 
|             } | 
|             // NFM001の送信項目として、isChanged かを確認 | 
|             Boolean isChanged = false; | 
|             Boolean needChild = false; | 
|             // 論理削除/復活の判断 | 
|             Boolean isLogicDeleteFlg = false; | 
|             // Is_Active__c変更する度に、callout必要 | 
|             if (Trigger.oldMap.get(a.Id).get('Is_Active__c') != a.Is_Active__c) { | 
|                 isLogicDeleteFlg = true; | 
|             } | 
|             if (Trigger.oldMap.get(a.Id).get('Name') != a.Name && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'Name_' + a.Name; | 
|                 if (needSendRectMap.get(a.RecordTypeId) == '病院') { | 
|                     needChild = true; | 
|                     hpAfterAccMap.put(a.Id, a); | 
|                     hpNameAfterAccMap.put(a.Id, a); | 
|                 } | 
|             } | 
|             if (Trigger.oldMap.get(a.Id).get('Site') != a.Site && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'Site'; | 
|                 if (needSendRectMap.get(a.RecordTypeId) == '病院') { | 
|                     needChild = true; | 
|                 } | 
|             } | 
|             if (Trigger.oldMap.get(a.Id).get('Postal_Code__c') != a.Postal_Code__c && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'Postal_Code__c'; | 
|                 if (needSendRectMap.get(a.RecordTypeId) == '病院') { | 
|                     needChild = true; | 
|                 } | 
|             } | 
|             if (Trigger.oldMap.get(a.Id).get('State_Master__c') != a.State_Master__c && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'State_Master__c'; | 
|                 if (needSendRectMap.get(a.RecordTypeId) == '病院') { | 
|                     needChild = true; | 
|                 } | 
|             } | 
|             if (Trigger.oldMap.get(a.Id).get('City_Master__c') != a.City_Master__c && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'City_Master__c'; | 
|                 if (needSendRectMap.get(a.RecordTypeId) == '病院') { | 
|                     needChild = true; | 
|                 } | 
|             } | 
|             if (Trigger.oldMap.get(a.Id).get('Town__c') != a.Town__c && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'Town__c'; | 
|                 if (needSendRectMap.get(a.RecordTypeId) == '病院') { | 
|                     needChild = true; | 
|                 } | 
|             } | 
|             if (Trigger.oldMap.get(a.Id).get('Street__c') != a.Street__c && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'Street__c'; | 
|                 if (needSendRectMap.get(a.RecordTypeId) == '病院') { | 
|                     needChild = true; | 
|                 } | 
|             } | 
|             // OCM_Category__cについて病院しかつかってないため、診療科より変更はありえない | 
|             if (Trigger.oldMap.get(a.Id).get('OCM_Category__c') != a.OCM_Category__c && needSendRectMap.get(a.RecordTypeId) == '病院' && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'OCM_Category__c'; | 
|                 needChild = true; | 
|                 hpAfterAccMap.put(a.Id, a); | 
|             } | 
|             // needChildいらない項目の変更チェック | 
|             if (Trigger.oldMap.get(a.Id).get('Phone') != a.Phone && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'Phone'; | 
|             } | 
|             if (Trigger.oldMap.get(a.Id).get('Fax') != a.Fax && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'Fax'; | 
|             } | 
|   | 
|             // LHJ Start | 
|             if (Trigger.oldMap.get(a.Id).get('Alias_Name2__c') != a.Alias_Name2__c  | 
|                     || Trigger.oldMap.get(a.Id).get('Attribute_Type__c') != a.Attribute_Type__c | 
|                     || Trigger.oldMap.get(a.Id).get('Grade__c') != a.Grade__c | 
|                     || Trigger.oldMap.get(a.Id).get('If_Need_Quolified__c') == false && a.If_Need_Quolified__c == true | 
|                     || Trigger.oldMap.get(a.Id).get('Speciality_Type__c') != a.Speciality_Type__c | 
|                     || Trigger.oldMap.get(a.Id).get('Goverment_key_customer__c') != a.Goverment_key_customer__c) { | 
|   | 
|                 isChanged = true; | 
|                 needChild = true; | 
|             } | 
|             if (a.If_Need_Quolified__c) { | 
|                 needQuolifiedMap.put(a.Id, '1'); | 
|             } | 
|             // LHJ End | 
|   | 
|             // 論理削除/復活の判断は、isChangedより優先 | 
|             if (isLogicDeleteFlg) { | 
|                 NFM001Controller.isRunning = true; | 
|                 // LHJ Start | 
|                 NFM201Controller.isRunning = true; | 
|                 // LHJ End | 
|                 accIds.add(a.Id); | 
|                 if (needSendRectMap.get(a.RecordTypeId) == '病院') { | 
|                     hospitalIdsNeedUpdateAccountIsActive.add(a.Id); | 
|                 } | 
|                 if (a.Is_Active__c == '無効') { | 
|                     // 無効→論理削除 | 
|                     a.DepartmentEffectiveDateTo__c = System.now().format('yyyy/MM/dd_') + a.Management_Code__c; | 
|                     hpDateToMap.put(a.Id, a.DepartmentEffectiveDateTo__c); | 
|                     hpIsActiveMap.put(a.Id, a.Is_Active__c); | 
|                     purposeOfAdviceMap.put(a.Id, '1'); | 
|                     NFM001Controller.debug_msg = 'NFM001_callout_logic_delete_' + a.DepartmentEffectiveDateTo__c; | 
| System.debug(Logginglevel.DEBUG, 'NFM001Controller.debug_msg=' + NFM001Controller.debug_msg); | 
|                 } else { | 
|                     // 有効→論理復活 | 
|                     a.DepartmentEffectiveDateTo__c = null; | 
|                     hpDateToMap.put(a.Id, a.DepartmentEffectiveDateTo__c); | 
|                     hpIsActiveMap.put(a.Id, a.Is_Active__c); | 
|                     purposeOfAdviceMap.put(a.Id, '3'); | 
|                     NFM001Controller.debug_msg = 'NFM001_callout_logic_undelete'; | 
|                 } | 
|             } else if (isChanged) { | 
|                 NFM001Controller.isRunning = true; | 
|                 // LHJ Start | 
|                 NFM201Controller.isRunning = true; | 
|                 NFM201Controller.debug_msg += '_NFMq01_callout_update'; | 
|                 // LHJ End | 
|                 accIds.add(a.Id); | 
|                 purposeOfAdviceMap.put(a.Id, '3'); | 
|                 NFM001Controller.debug_msg += '_NFM001_callout_update'; | 
|                 if (needChild) { | 
|                     hospitalIds.add(a.Id); | 
|                 } | 
|             } | 
|         } | 
| System.debug(Logginglevel.DEBUG, 'hospitalIdsNeedUpdateAccountIsActive.size()=' + hospitalIdsNeedUpdateAccountIsActive.size()); | 
|         ControllerUtil.updateDeptIsActive(accIds, purposeOfAdviceMap, hospitalIdsNeedUpdateAccountIsActive, hpIsActiveMap, hpDateToMap); | 
|         ControllerUtil.collectDeptId(accIds, purposeOfAdviceMap, hospitalIds); | 
|          | 
|         // LHJ 20190319 CBPR 发送医院资质审批后,将标志置为False | 
|         for(Account a : Trigger.new) {         | 
|             if (Trigger.oldMap.get(a.Id).get('If_Need_Quolified__c') == false && a.If_Need_Quolified__c == true) { | 
|                 a.If_Need_Quolified__c = false; | 
|             } | 
|         } | 
|     } | 
|      | 
|     if (stateIds.size() > 0) { | 
|         // 病院しかないはず、しかも beforeのはず | 
|         Map<Id, Address_Level__c> stateMasterMap = new Map<Id, Address_Level__c>([Select Id, Name from Address_Level__c where Id IN :stateIds]); | 
|         for(Account a : Trigger.new) { | 
|             // I/F と関係ない、下記のifのチェックはいらないはずが、念のためifにてチェックしています | 
|             if (needSendRectMap.get(a.RecordTypeId) == '病院' && stateMasterMap.get(a.State_Master__c) != null) { | 
|                 // 病院 before insert, before update | 
|                 a.State_Text__c = stateMasterMap.get(a.State_Master__c).Name; | 
| System.debug(Logginglevel.DEBUG, 'a.State_Text__c=' + a.State_Text__c); | 
|                 if (Trigger.isUpdate) { | 
| System.debug(Logginglevel.DEBUG, 'Trigger.isUpdate'); | 
|                     hpAfterAccMap.put(a.Id, a); | 
|                 } | 
|             } | 
|         } | 
|     } | 
|     if (hpAfterAccMap.size() > 0) { | 
|         ControllerUtil.updateDeptName(hpAfterAccMap); | 
|     } | 
|     if (hpNameAfterAccMap.size() > 0) { | 
|         ControllerUtil.updateAgencyName(hpNameAfterAccMap); | 
|     } | 
| //        // 更新の場合、配下の診療科も更新 | 
| //        if (Trigger.isUpdate) { | 
| //            List<Account> accUpdList = new List<Account>(); | 
| //            // 戦略課室と診療科の省(Text)を更新 | 
| //            Set<Id> accHpStateKeys = hpStateNameMap.keySet(); | 
| //System.debug(Logginglevel.DEBUG, 'Trigger.isUpdate accHpStateKeys.size()' + accHpStateKeys.size()); | 
| //            Map<Id, Account> accUpdStateMap = new Map<Id, Account>([Select Id, Name, Parent.Id, Parent.RecordType.DeveloperName, Parent.Parent.Id from Account where (Parent.Id IN :accHpStateKeys or Parent.Parent.Id IN :accHpStateKeys)]); | 
| //System.debug(Logginglevel.DEBUG, 'accUpdStateMap.size()' + accUpdStateMap.size()); | 
| //            for (String accId: accUpdStateMap.keySet()) { | 
| //                Account acc = accUpdStateMap.get(accId); | 
| //                if (acc.Parent.RecordType.DeveloperName == 'HP') { | 
| //                    acc.State_Text__c = hpStateNameMap.get(acc.Parent.Id); | 
| //                    accUpdList.add(acc); | 
| //                } | 
| //                else { | 
| //                    acc.State_Text__c = hpStateNameMap.get(acc.Parent.Parent.Id); | 
| //                    accUpdList.add(acc); | 
| //                } | 
| //            } | 
| //            if (accUpdList.size() > 0) update accUpdList; | 
| //        } | 
| System.debug('*****************'+accIds.size()); | 
|     if (accIds.size() > 0) { | 
|         NFM001Controller.debug_msg = accIds.size() + ',' + NFM001Controller.debug_msg; | 
|         // MessageGroupNumber の採番 | 
|         BatchIF_Log__c iflog = new BatchIF_Log__c(); | 
|         iflog.Type__c = 'NFM001'; | 
|         iflog.Log__c  = 'callout start\n'+accIds+'\n'; | 
|         insert iflog; | 
|         iflog = [Select Id, Name from BatchIF_Log__c where Id = :iflog.Id]; | 
|         System.debug(Logginglevel.DEBUG, 'NFM001_' + iflog.Name + ' start');                  // callout の中 end のlogを出します | 
|         NFM001Controller.callout(iflog.Id, accIds, purposeOfAdviceMap, needSendRectMap); | 
|          | 
|         // LHJ 向SPO传NFM201 Start | 
|         NFM201Controller.debug_msg = accIds.size() + ',' + NFM201Controller.debug_msg; | 
|         BatchIF_Log__c iflog201 = new BatchIF_Log__c(); | 
|         iflog201.Type__c = 'NFM201'; | 
|         iflog201.Log__c  = 'callout start\n'+accIds+'\n'; | 
|         insert iflog201; | 
|         iflog201 = [Select Id, Name from BatchIF_Log__c where Id = :iflog201.Id]; | 
|         System.debug(Logginglevel.DEBUG, 'NFM201_' + iflog201.Name + ' start');                  // callout の中 end のlogを出します | 
|         //NFM201Controller.callout(iflog201.Id, accIds, purposeOfAdviceMap, needSendRectMap); | 
|         if(!System.Test.isRunningTest()){ | 
|         NFM201Controller.callout(iflog201.Id, accIds, purposeOfAdviceMap, needSendRectMap, needQuolifiedMap); | 
|         } | 
|         // LHJ End | 
|         //NFM201Controller.debug_msg = accIds.size() + ',' + NFM201Controller.debug_msg; | 
|         // BatchIF_Log__c iflog601 = new BatchIF_Log__c(); | 
|         // iflog601.Type__c = 'NFM601'; | 
|         // iflog601.Log__c  = 'callout start\n'+accIds+'\n'; | 
|         // insert iflog601; | 
|         // if(!System.Test.isRunningTest()){ | 
|         //     NFM601Controller.callout(iflog601.Id, accIds); | 
|         // } | 
|     } | 
|     // LHJ 20190321 CBPR 发送医院资质审批后,将标志置为False | 
|     if (Trigger.isUpdate) { | 
|         for (Account a : Trigger.new) {      | 
|             if (Trigger.oldMap.get(a.Id).get('If_Need_Quolified__c') == false && a.If_Need_Quolified__c == true) { | 
|                 a.If_Need_Quolified__c = false; | 
|             } | 
|         } | 
|     } | 
|     // LHJ 20190321 End | 
| } |