force-app/main/default/classes/ApplicationForCAHandler.cls
@@ -21,6 +21,8 @@
    }
    protected override void beforeUpdate() {
        // 更新时判断是否更新了绑定的学会,如果绑定多个学会且不是赞助会时,清空绑定关系
        clean_binding();
        clean_campaign();
        update_campaign();
    }
@@ -30,18 +32,40 @@
        update_status_on_opd();
    }
    // 更新时判断是否更新了绑定的学会,如果绑定多个学会且不是赞助会时,清空绑定关系
    private void clean_binding() {
        Map<String, String> app_cam_binds = new Map<String, String>();
        for (Application_for_Conference_Adjudication__c app : this.newList) {
            Application_for_Conference_Adjudication__c old_app = this.oldMap.get(app.Id);
            if (String.isNotBlank(old_app.Num__c) && !old_app.Num__c.equals(app.Num__c) && app.MeetingType__c != '赞助会') {
                app_cam_binds.put(old_app.Id, old_app.Num__c);
            }
        }
        if (app_cam_binds.size() > 0) {
            List<Campaign> cams = [select Id, MeetingApprovedNo__c, Meeting_Approved_No__c, RecordTypeId, Meeting_Type__c from Campaign where Meeting_Approved_No__c in :app_cam_binds.keySet() and Num__c in :app_cam_binds.values()];
            if (cams != null && cams.size() > 0) {
                for (Campaign cam : cams) {
                    cam.MeetingApprovedNo__c = null;
                    cam.Meeting_Approved_No__c = null;
                }
                update cams;
            }
        }
    }
    // 审批不通过的时候 清空会议和OPD计划上的决裁信息
    private void clean_campaign() {
        List<String> approved_nos = new List<String>();
        for (Application_for_Conference_Adjudication__c app : this.newList) {
            if (app.ProcessState__c == '驳回' || 
                app.ProcessState__c == '终止申请' || 
                app.ProcessState__c == '取消') {
                app.ProcessState__c == '取消' ||
                app.ProcessState__c == '删除') {
                    approved_nos.add(app.Id);
            }
        }
        if (approved_nos.size() > 0) {
            List<Campaign> campaigns = [select Id, MeetingApprovedNo__c, Meeting_Approved_No__c from Campaign where Meeting_Approved_No__c in :approved_nos];
            List<Campaign> campaigns = [select Id, MeetingApprovedNo__c, Meeting_Approved_No__c , RecordTypeId, Meeting_Type__c from Campaign where Meeting_Approved_No__c in :approved_nos];
            if (campaigns != null && campaigns.size() > 0) {
                for (Campaign cam : campaigns) {
                    cam.MeetingApprovedNo__c = null;
@@ -65,7 +89,7 @@
        List<String> app_ids = new List<String>();
        Map<String, Application_for_Conference_Adjudication__c> app_nos = new Map<String, Application_for_Conference_Adjudication__c>();
        for (Application_for_Conference_Adjudication__c app : this.newList) { 
            if (app.ProcessState__c == '审批成功') {
            if (app.ProcessState__c != oldMap.get(app.Id).ProcessState__c && app.ProcessState__c == '审批成功') {  //20221206 lt DB202212060256 update
                app.Approved_Date__c = Date.today();
                app_ids.add(app.Id);
                // 补充决裁
@@ -76,7 +100,7 @@
        }
        Map<String, Campaign> wait_upd_campaigns = new Map<String, Campaign>();
        if (app_ids.size() > 0) {
            List<Campaign> campaigns = [select Id, Num__c, MeetingApprovedNo__c, Meeting_Approved_No__c from Campaign where Meeting_Approved_No__c in :app_ids];
            List<Campaign> campaigns = [select Id, Num__c, MeetingApprovedNo__c, Meeting_Approved_No__c, WorkshopPlace__c, State_Master__c, City_Master__c, RecordTypeId, Meeting_Type__c from Campaign where Meeting_Approved_No__c in :app_ids and Meeting_Type__c != '赞助会'];
            if (campaigns.size() > 0) {
                for (Campaign cam : campaigns) {
                    Application_for_Conference_Adjudication__c app = this.newMap.get(cam.Meeting_Approved_No__c);
@@ -88,7 +112,7 @@
            }
        }
        if (app_nos.size() > 0) {
            List<Campaign> campaigns = [select Id, Num__c, MeetingApprovedNo__c, Meeting_Approved_No__c from Campaign where MeetingApprovedNo__c in :app_nos.keySet()];
            List<Campaign> campaigns = [select Id, Num__c, MeetingApprovedNo__c, Meeting_Approved_No__c, WorkshopPlace__c, State_Master__c, City_Master__c, RecordTypeId, Meeting_Type__c from Campaign where MeetingApprovedNo__c in :app_nos.keySet()];
            if (campaigns.size() > 0) {
                for (Campaign cam : campaigns) {
                    Application_for_Conference_Adjudication__c app = app_nos.get(cam.MeetingApprovedNo__c);
@@ -128,15 +152,33 @@
    // 更新决裁的信息到学会上
    private Campaign update_info_from_app(Campaign cam, Application_for_Conference_Adjudication__c app) {
        init_state_city();
        cam.StartDate                        = app.StartDate__c;
        cam.EndDate                          = app.EndDate__c;
        cam.Budget_Type__c                   = app.BudgetType__c;
        //20221206 lt DB202212060256 start
        Id RtId = Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Society').getRecordTypeId();
        Id RtId2 = Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('BusinessTraining').getRecordTypeId();
        System.debug('lt123-------Cam------记录类型----'+cam.RecordTypeId);
        System.debug('lt123-------Cam------会议类型----'+cam.Meeting_Type__c);
        System.debug('lt123-------Cam------记录类型RtId----'+RtId);
        if(cam.RecordTypeId == RtId || cam.RecordTypeId == RtId2){
            System.debug('lt123-------App------1----');
            if (cam.Meeting_Type__c != '主办会'){
                System.debug('lt123-------App------2----');
                cam.StartDate                        = app.StartDate__c;
                cam.EndDate                          = app.EndDate__c;
            }
        }
        if(cam.RecordTypeId != RtId && cam.RecordTypeId != RtId2){
            System.debug('lt123-------App------3----');
            cam.StartDate                        = app.StartDate__c;
            cam.EndDate                          = app.EndDate__c;
        }
        // cam.StartDate                        = app.StartDate__c;
        // cam.EndDate                          = app.EndDate__c;
        //20221206 lt DB202212060256 end
        cam.Implementation_Purpose__c                   = app.Implementation_Purpose1__c;
        cam.OfficeCategory__c                = app.OfficeCategory__c;
        cam.Meeting_Type__c                  = app.MeetingType__c;
        cam.Activity_Type_Name__c            = app.ActivityTypeName__c;
        cam.WorkshopPlace__c                 = app.WorkshopPlace__c;
        cam.WorkshopPlace__c                 = String.isNotBlank(app.WorkshopPlace__c) ? app.WorkshopPlace__c : cam.WorkshopPlace__c;
        cam.HostName__c                      = app.HostName__c;
        cam.cooperatorCompany__c             = app.CooperatorCompany__c;
        cam.Convening_Participants_Num__c    = app.ConveningParticipantsNum__c;
@@ -146,65 +188,12 @@
        cam.Expected_Hcp_Attendance__c       = app.ExpectedHcpAttendance__c;
        cam.SponsorshipCategory__c           = app.SponsorshipCategory__c;
        cam.IsPaidToSponsor__c               = app.IsPaidToSponsor__c;
        cam.Activity_Type_Name__c            = app.ActivityTypeName__c;
        // cam.Activity_Type_Name__c            = app.ActivityTypeName__c;
        cam.Activity_Type_Name__c            = app.Type_of_adjudication_meeting__c;
        // 这里需要额外加操作
        cam.State_Master__c                  = get_state(app.StateMaster__c);
        cam.City_Master__c                   = get_city(app.CityMaster__c);
        cam.State_Master__c                  = String.isNotBlank(app.StateMaster__c) ? StateCityUtil.get_state(app.StateMaster__c) : cam.State_Master__c;
        cam.City_Master__c                   = String.isNotBlank(app.CityMaster__c)  ? StateCityUtil.get_city(app.CityMaster__c) : cam.City_Master__c;
        return cam;
    }
    // 初始化省市信息
    public static void init_state_city() {
        if (states == null || states.size() <= 0) {
            states = new Map<String, String>();
            List<Address_Level__c> lst_state = [select Id, Name from Address_Level__c];
            for (Address_Level__c level1 : lst_state) {
                states.put(level1.Name, level1.Id);
            }
        }
        if (cities == null || cities.size() <= 0) {
            cities = new Map<String, String>();
            List<Address_Level2__c> lst_city = [select Id, Name from Address_Level2__c];
            for (Address_Level2__c level2 : lst_city) {
                cities.put(level2.Name, level2.Id);
            }
        }
    }
    // 设置会议上的省
    public static String get_state(String state_name) {
        if (String.isBlank(state_name)) {
            return null;
        }
        if (states == null || states.size() <= 0) {
            return null;
        }
        // 处理空格 或许后续还有别的处理
        state_name = state_name.trim().replace(' ', '');
        for(String state : states.keySet()) {
            if (state_name.contains(state) || state.contains(state_name)) {
                return states.get(state);
            }
        }
        return null;
    }
    // 设置会议上的市
    public static String get_city(String city_name) {
        if (String.isBlank(city_name)) {
            return null;
        }
        if (cities == null || cities.size() <= 0) {
            return null;
        }
        // 处理空格 或许后续还有别的处理
        city_name = city_name.trim().replace(' ', '');
        for(String city : cities.keySet()) {
            if (city_name.contains(city) || city.contains(city_name)) {
                return cities.get(city);
            }
        }
        return null;
    }
}