public without sharing class ContactTriggerHandler {
    
    public static void setIsNew(List<Contact> newList, Map<Id, Contact> newMap, List<Contact> oldList, Map<Id, Contact> oldMap) {
        for (Contact newCon : newList) {
            // String new_profileId = UserInfo.getProfileId().subString(0,15);
            //calendarUtil.getMemberProfileID  这里用到的人员ID 和获取到的简档ID都是15位的
            String new_profileId = calendarUtil.getMemberProfileID(UserInfo.getUserId().subString(0,15));
            
            if (new_profileId.subString(0,15) == System.label.Market_Department || new_profileId.subString(0,15) == System.label.Market_Department1 || new_profileId.subString(0,15) == System.label.Service_Owner ||
                UserInfo.getUserType() == 'PowerPartner') {
                newCon.IsNew__c = true;
            } else {
                newCon.MobilePhoneD__c = newCon.MobilePhone;
                newCon.OtherPhoneD__c = newCon.OtherPhone;
                newCon.FaxD__c = newCon.Fax;
                newCon.EmailD__c = newCon.Email;
                newCon.PhoneD__c = newCon.Phone;
                newCon.TitleD__c = newCon.Title;
                newCon.Address1D__c = newCon.Address1__c;
                newCon.Address2D__c = newCon.Address2__c;
                newCon.Address3D__c = newCon.Address3__c;
                newCon.PostcodeD__c = newCon.Postcode__c;
                newCon.ContactStatusD__c = newCon.ContactStatus__c;
                newCon.CancelReasonD__c = newCon.CancelReason__c;
                if(new_profileId.subString(0,15) == '00e28000000YKLo'){
                    newCon.IsNew__c = true;
                }
            }
        }
    }
    
    public static void updateForDealer(List<Contact> newList, Map<Id, Contact> newMap, List<Contact> oldList, Map<Id, Contact> oldMap) {
        //王鹏伟 添加userId 服务部修改联系人重新审批 判断当前修改的登陆人是否是孙学洋  吴晓东
        String userId = UserInfo.getUserId(); 
        for (Contact newCon : newList) {
            Contact oldCon = oldMap.get(newCon.Id);
            
            if(newCon.AccountStaut__c != 'Pass' && newCon.StatusD__c != oldCon.StatusD__c && newCon.StatusD__c == 'Pass' ){
                newCon.addError('请先审批对应的客户');
            }

            if (newCon.MobilePhoneD__c != oldCon.MobilePhoneD__c ||
                newCon.OtherPhoneD__c != oldCon.OtherPhoneD__c ||
                newCon.FaxD__c != oldCon.FaxD__c ||
                newCon.EmailD__c != oldCon.EmailD__c ||
                newCon.PhoneD__c != oldCon.PhoneD__c ||
                newCon.TitleD__c != oldCon.TitleD__c ||
                newCon.Address1D__c != oldCon.Address1D__c ||
                newCon.Address2D__c != oldCon.Address2D__c ||
                newCon.Address3D__c != oldCon.Address3D__c ||
                newCon.PostcodeD__c != oldCon.PostcodeD__c ||
                newCon.ContactStatusD__c != oldCon.ContactStatusD__c ||
                newCon.CancelReasonD__c != oldCon.CancelReasonD__c) {
                // String new_profileId = UserInfo.getProfileId().subString(0,15);
                //新的获取简档ID  calendarUtil.getMemberProfileID  这里用到的人员ID 和获取到的简档ID都是15位的
                String new_profileId = calendarUtil.getMemberProfileID(UserInfo.getUserId().subString(0,15));
                //王鹏伟 添加 && !((oldCon.ProductSegment__c == 'BS' || oldCon.ProductSegment__c == 'NDT' || oldCon.ProductSegment__c = 'ANI') && oldCon.isServiceCreate__c == true)
                //服务部修改联系人信息 LS、NDT、ANI无需审批,跳过审批状态变草案
                if (( new_profileId.subString(0,15) == System.label.Service_Owner
                    || UserInfo.getUserType() == 'PowerPartner' || userId =='00528000000YWC6' || userId =='00528000000YWE7')
                    && !((oldCon.ProductSegment__c == 'BS' || oldCon.ProductSegment__c == 'NDT' || oldCon.ProductSegment__c == 'ANI') && oldCon.isServiceCreate__c == true)) {
                    newCon.StatusD__c = 'Draft';
                }
            }
            
            if (newCon.MobilePhone != oldCon.MobilePhone) {
                newCon.MobilePhoneD__c = newCon.MobilePhone;
            }
            if (newCon.OtherPhone != oldCon.OtherPhone) {
                newCon.OtherPhoneD__c = newCon.OtherPhone;
            }
            if (newCon.Fax != oldCon.Fax) {
                newCon.FaxD__c = newCon.Fax;
            }
            if (newCon.Email != oldCon.Email) {
                newCon.EmailD__c = newCon.Email;
            }
            if (newCon.Phone != oldCon.Phone) {
                newCon.PhoneD__c = newCon.Phone;
            }
            if (newCon.Title != oldCon.Title) {
                newCon.TitleD__c = newCon.Title;
            }
            if (newCon.Address1__c != oldCon.Address1__c) {
                newCon.Address1D__c = newCon.Address1__c;
            }
            if (newCon.Address2__c != oldCon.Address2__c) {
                newCon.Address2D__c = newCon.Address2__c;
            }
            if (newCon.Address3__c != oldCon.Address3__c) {
                newCon.Address3D__c = newCon.Address3__c;
            }
            if (newCon.Postcode__c != oldCon.Postcode__c) {
                newCon.PostcodeD__c = newCon.Postcode__c;
            }
                
            if (newCon.ContactStatus__c != oldCon.ContactStatus__c) {
                newCon.ContactStatusD__c = newCon.ContactStatus__c;
            }
            if (newCon.CancelReason__c != oldCon.CancelReason__c) {
                newCon.CancelReasonD__c = newCon.CancelReason__c;
            }
        }
    }
    
    public static void dealerContactApproval(List<Contact> newList, Map<Id, Contact> newMap, List<Contact> oldList, Map<Id, Contact> oldMap) {
        List<String> accIdList = new List<String>();
        for (Contact newCon : newList) {
            accIdList.add(newCon.AccountId);
        }
        // 现有客户小组
        Map<String, String> accsMap = new Map<String, String>();
        //List<AccountTeamMember> atmList = [select Id, AccountId, UserId from AccountTeamMember where AccountId in :accIdList];
        List<AccountShare> accsList = [select id, accountId, UserOrGroupId, AccountAccessLevel, RowCause from AccountShare where AccountId in :accIdList and RowCause = 'Team'];
        for (AccountShare accs : accsList) {
            accsMap.put(accs.AccountId + '' + accs.UserOrGroupId, accs.Id);
        }
        // 需要更新客户小组
//      List<AccountShare> updList = new List<AccountShare>();

        //新的获取简档ID calendarUtil.getMemberProfileID  这里用到的人员ID 和获取到的简档ID都是15位的  
        // String new_profileId = UserInfo.getProfileId().subString(0,15);
        String new_profileId = calendarUtil.getMemberProfileID(UserInfo.getUserId().subString(0,15));

        for (Contact newCon : newList) {
            Contact oldCon = oldMap.get(newCon.Id);

            if (new_profileId.subString(0,15) == System.label.Market_Department || new_profileId.subString(0,15) == System.label.Market_Department1){
                newCon.MobilePhone = newCon.MobilePhoneD__c;
                newCon.OtherPhone = newCon.OtherPhoneD__c;
                newCon.Fax = newCon.FaxD__c;
                newCon.Email = newCon.EmailD__c;
                newCon.Phone = newCon.PhoneD__c;
                newCon.Title = newCon.TitleD__c;
                newCon.Address1__c = newCon.Address1D__c;
                newCon.Address2__c = newCon.Address2D__c;
                newCon.Address3__c = newCon.Address3D__c;
                newCon.Postcode__c = newCon.PostcodeD__c;
                
                newCon.ContactStatus__c = newCon.ContactStatusD__c;
                newCon.CancelReason__c = newCon.CancelReasonD__c;
            } else if (newCon.StatusD__c != oldCon.StatusD__c) {
                if (newCon.StatusD__c == 'Submit') {
                    //newCon.DealerSelectOwner__c = newCon.Account.DealerSelectOwner__c;
                }
                if (newCon.StatusD__c == 'Pass') {
                    // 批准过程肯定是一条一条批准的，所以这里再循环中写了select文
                    
                    List<Account> accList = [select Id, IsNew__c, AccountStatus__c from Account where Id = :newCon.AccountId];
                    if (accList.size() > 0 ) {
                        Account acc = accList[0];
                        if (acc.IsNew__c == true || (acc.AccountStatus__c == 'Cancel' && newCon.ContactStatusD__c != 'Cancel')) {
                            newCon.addError('客户无效或未通过审批，不能批准当前联系人。');
                        }
                    }
                    
                    newCon.MobilePhone = newCon.MobilePhoneD__c;
                    newCon.OtherPhone = newCon.OtherPhoneD__c;
                    newCon.Fax = newCon.FaxD__c;
                    newCon.Email = newCon.EmailD__c;
                    newCon.Phone = newCon.PhoneD__c;
                    newCon.Title = newCon.TitleD__c;
                    newCon.Address1__c = newCon.Address1D__c;
                    newCon.Address2__c = newCon.Address2D__c;
                    newCon.Address3__c = newCon.Address3D__c;
                    newCon.Postcode__c = newCon.PostcodeD__c;
                    
                    newCon.ContactStatus__c = newCon.ContactStatusD__c;
                    newCon.CancelReason__c = newCon.CancelReasonD__c;
                    
//                  if (accsMap.containsKey(newCon.AccountId + '' + newCon.OwnerId) == true && newCon.IsNew__c == true) {
//                      AccountShare upd = new AccountShare(
//                          Id = accsMap.get(newCon.AccountId + '' + newCon.OwnerId),
//                          AccountAccessLevel = 'Edit'
//                      );
//                      updList.add(upd);
//                  }
                    
                    if (newCon.IsNew__c = true) {
                        newCon.IsNew__c = false;
                    }
                }
                if (newCon.StatusD__c == 'Reject' && newCon.IsNew__c == false) {
                    newCon.MobilePhoneD__c = newCon.MobilePhone;
                    newCon.OtherPhoneD__c = newCon.OtherPhone;
                    newCon.FaxD__c = newCon.Fax;
                    newCon.EmailD__c = newCon.Email;
                    newCon.PhoneD__c = newCon.Phone;
                    newCon.TitleD__c = newCon.Title;
                    newCon.Address1D__c = newCon.Address1__c;
                    newCon.Address2D__c = newCon.Address2__c;
                    newCon.Address3D__c = newCon.Address3__c;
                    newCon.PostcodeD__c = newCon.Postcode__c;
                    
                    newCon.ContactStatusD__c = newCon.ContactStatus__c;
                    newCon.CancelReasonD__c = newCon.CancelReason__c;
                }
            }
        }
        // 更新客户小组
//      if (updList!= null && updList.size() > 0) {
//          update updList;
//      }
    }
}