public without sharing class AccountToTMSInfoTrigger {
    public static void createTms(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        if (Trigger.isInsert || Trigger.isUpdate) {
            Map<String,Dealer_ConsumableManage__c> dcList = new Map<String,Dealer_ConsumableManage__c>();
            Map<String,Dealer_ConsumableManage__c> dcList2 = new Map<String,Dealer_ConsumableManage__c>();
            List<String> keys = new List<String>();
            Integer year = Date.today().year();
            for (Account acc : newList) {
                keys.add(acc.Agent_Ref__c+'-'+acc.Contact_Type__c+'-'+'FY'+year+'-'+acc.OCM_man_province_HP__c);
            }
            
            List<Dealer_ConsumableManage__c> dcHas = [select id,TMS_Key__c from Dealer_ConsumableManage__c where TMS_Key__c in :keys];
            System.debug('dcHas='+dcHas);
            String myUserID = UserInfo.getUserId();
            User userinfo = [SELECT id,profileId FROM User WHERE id = :myUserID LIMIT 1];
            String userpid = userinfo.profileId;
            String OLYid = System.Label.OlympusSystem_ID;
            String ProfileId = System.Label.ProfileId_SystemAdmin;
            if ((myUserID.substring(0,15) == OLYid.substring(0,15) || (userpid == ProfileId))) {
                if (dcHas.size()==0) {
                    for (Account acc : newList) {
                        if (Trigger.isInsert || Trigger.isUpdate) {
                            if (acc.Acc_Record_Type__c == '契約' && acc.Is_Active_Formula__c == '有效' &&(acc.Contact_Type__c == 'ET' || acc.Contact_Type__c == 'ENG')) {
                                Dealer_ConsumableManage__c dc =  new Dealer_ConsumableManage__c();
                                Decimal amount = 0;
                                if (acc.Aim_Price__c != null) {
                                    amount = (acc.Aim_Price__c/12).setScale(0);
                                }
                                System.debug('acc.ET_SP_Dealer__c='+acc.ET_SP_Dealer__c);
                                if (acc.ET_SP_Dealer__c == true){
                                    dc.Category2__c = 'ET耗材';
                                    dc.ET_ENG__c = 'ET';
                                    dc.Dealer_Name__c = acc.Agent_Ref__c;
                                    dc.Dealer_Name_Account__c = acc.id;
                                    dc.FY__c = 'FY'+year;
                                    dc.FY_P_1__c = amount;
                                    dc.FY_P_10__c = amount;
                                    dc.FY_P_11__c = amount;
                                    dc.FY_P_12__c = amount;
                                    dc.FY_P_2__c = amount;
                                    dc.FY_P_3__c = amount;
                                    dc.FY_P_4__c = amount;
                                    dc.FY_P_5__c = amount;
                                    dc.FY_P_6__c = amount;
                                    dc.FY_P_7__c = amount;
                                    dc.FY_P_8__c = amount;
                                    dc.FY_P_9__c = amount;
                                    dc.TMS_Key__c = acc.Agent_Ref__c+'-'+'ET'+'-'+'FY'+year+'-'+acc.OCM_man_province_HP__c;
                                }else if (acc.ENG_Dealer__c == true){
                                    dc.Category2__c = '外科耗材';
                                    dc.ET_ENG__c = 'ENG';
                                    dc.Dealer_Name__c = acc.Agent_Ref__c;
                                    dc.Dealer_Name_Account__c = acc.id;
                                    dc.FY__c = 'FY'+year;
                                    dc.FY_P_1__c = amount;
                                    dc.FY_P_10__c = amount;
                                    dc.FY_P_11__c = amount;
                                    dc.FY_P_12__c = amount;
                                    dc.FY_P_2__c = amount;
                                    dc.FY_P_3__c = amount;
                                    dc.FY_P_4__c = amount;
                                    dc.FY_P_5__c = amount;
                                    dc.FY_P_6__c = amount;
                                    dc.FY_P_7__c = amount;
                                    dc.FY_P_8__c = amount;
                                    dc.FY_P_9__c = amount;
                                    dc.TMS_Key__c = acc.Agent_Ref__c+'-'+'ENG'+'-'+'FY'+year+'-'+acc.OCM_man_province_HP__c;
                                }
                                if (dc.TMS_Key__c !=null && !dcList.containskey(dc.id)) {
                                    dcList.put(dc.id,dc); 
                                }
                            } 
                        } 
                    }    
                }
                if (dcHas.size()!=0) {
                    for (Account acc : newList) {
                        if (acc.Acc_Record_Type__c == '契約' && acc.Is_Active_Formula__c == '有效'  &&(acc.Contact_Type__c == 'ET' || acc.Contact_Type__c == 'ENG')) {
                            String tmskey = acc.Agent_Ref__c+'-'+acc.Contact_Type__c+'-'+'FY'+year+'-'+acc.OCM_man_province_HP__c;
                            Decimal amount = 0;
                            if (acc.Aim_Price__c != null) {
                                amount = (acc.Aim_Price__c/12).setScale(0);
                            }
                            Dealer_ConsumableManage__c dc =  new Dealer_ConsumableManage__c();
                            if (Trigger.isInsert || Trigger.isUpdate) {
                                for (Dealer_ConsumableManage__c dcm : dcHas) {
                                    if (tmskey == dcm.TMS_Key__c) {
                                        dc.id = dcm.id;
                                        dc.FY_P_1__c = amount;
                                        dc.FY_P_10__c = amount;
                                        dc.FY_P_11__c = amount;
                                        dc.FY_P_12__c = amount;
                                        dc.FY_P_2__c = amount;
                                        dc.FY_P_3__c = amount;
                                        dc.FY_P_4__c = amount;
                                        dc.FY_P_5__c = amount;
                                        dc.FY_P_6__c = amount;
                                        dc.FY_P_7__c = amount;
                                        dc.FY_P_8__c = amount;
                                        dc.FY_P_9__c = amount;
                                        if (!dcList2.containsKey(dc.id)) {
                                            dcList2.put(dc.id,dc); 
                                        }
                                    }  
                                }
                            }
                        }
                    }
                }
            }
            
            system.debug('dcList=='+dcList.values());
            if (dcList.size()>0) {
                insert dcList.values();
            }
            if (dcList2.size()>0) {
                update dcList2.values();
            }
        }
    }
}