public without sharing class AgencyReportHandler extends Oly_TriggerHandler {
    private Map<Id, Agency_Report__c> newMap;
    private Map<Id, Agency_Report__c> oldMap;
    private List<Agency_Report__c> newList;
    private List<Agency_Report__c> oldList;
    public AgencyReportHandler() {
        this.newMap = (Map<Id, Agency_Report__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Agency_Report__c>) Trigger.oldMap;
        this.newList = (List<Agency_Report__c>) Trigger.new;
        this.oldList = (List<Agency_Report__c>) Trigger.old;
    }

    protected override void beforeInsert() {
        beforeExecute();
    }

    protected override void beforeUpdate() {
        beforeExecute();
    }

    protected override void afterInsert() {
        afterExecute();
        MonFirVisit();
        PerFirVisit();
    }

    protected override void afterUpdate() {
        afterExecute();
        MonFirVisit();
        PerFirVisit();
    }

    //Before処理
    private void beforeExecute() {
        for(Agency_Report__c a : this.newList) {
           a.Agency__c = a.Owner_Agency_ID__c;
           if(String.isNotBlank(a.Owner_Agency_ID__c))  //zhj 2023-01-30 测试类通过
                a.Agency_ID__c = String.valueOf(a.Owner_Agency_ID__c).substring(0,15);
        }
    }

    //After処理
    private void afterExecute() {
        Set<String> agency_Hospital_Set = new Set<String>();
        for (Agency_Report__c a : this.newList) {
            if (a.VistorKey__c != null) {
                if (Trigger.isUpdate) {
                    Agency_Report__c ar = this.oldMap.get(a.Id);
                    if (a.VistorKey__c != ar.VistorKey__c) {
                        Agency_Hospital_Set.add(ar.VistorKey__c);
                        Agency_Hospital_Set.add(a.VistorKey__c);
                    }
                } else {
                    Agency_Hospital_Set.add(a.VistorKey__c);
                }
            }
        }
        if (Agency_Hospital_Set.size() > 0) {
            List<Agency_Report__c> arList = [select Id,FirstVisitCount__c, VistorKey__c from Agency_Report__c where VistorKey__c in :agency_Hospital_Set order by VistorKey__c, CreatedDate];
        
            String oldKey = '';
            List<Agency_Report__c> updateList = new List<Agency_Report__c>();
            for (Agency_Report__c ar : arList) {
                if (oldKey != ar.VistorKey__c) {
                    oldKey = ar.VistorKey__c;
                    if (ar.FirstVisitCount__c != 1) {
                        ar.FirstVisitCount__c = 1;
                    } else {
                        continue;
                    }
                } else {
                    oldKey = ar.VistorKey__c;
                    if (ar.FirstVisitCount__c == 1) {
                        ar.FirstVisitCount__c = 0;
                    } else {
                        continue;
                    }
                }
                updateList.add(ar);
            }
            Database.update(updateList, false);
        }
    }

    //SWAG-C5XBY2  精琢科技  lt  2021-08-26  start
    //月首次访问
    private void MonFirVisit() {
        Set<String> agency_Hospital_Set1 = new Set<String>();
        for (Agency_Report__c a : this.newList) {
            if (a.MonVistorKey__c != null) {
                if (Trigger.isUpdate) {
                    Agency_Report__c ar = this.oldMap.get(a.Id);
                    if (a.MonVistorKey__c != ar.MonVistorKey__c || (System.Label.onlyupdate == '1' && UserInfo.getUserId() == System.Label.jingzhuo)) {//niwu-00510000005sEEMAA2-应改为 005C60000001iK1IAI 查找用户精琢技术
                        Agency_Hospital_Set1.add(ar.MonVistorKey__c);
                        Agency_Hospital_Set1.add(a.MonVistorKey__c);
                    }
                } else {
                    Agency_Hospital_Set1.add(a.MonVistorKey__c);
                }
            }
        }
        if (Agency_Hospital_Set1.size() > 0) {
            List<Agency_Report__c> arList = [select Id,FirstVisitCountMonthly__c, MonVistorKey__c from Agency_Report__c where MonVistorKey__c in :agency_Hospital_Set1 order by MonVistorKey__c, CreatedDate];
        
            String oldKey = '';
            List<Agency_Report__c> updateList = new List<Agency_Report__c>();
            for (Agency_Report__c ar : arList) {
                if (oldKey != ar.MonVistorKey__c) {
                    oldKey = ar.MonVistorKey__c;
                    if (ar.FirstVisitCountMonthly__c != 1) {
                        ar.FirstVisitCountMonthly__c = 1;
                    } else {
                        continue;
                    }
                } else {
                    oldKey = ar.MonVistorKey__c;
                    if (ar.FirstVisitCountMonthly__c == 1) {
                        ar.FirstVisitCountMonthly__c = 0;
                    } else {
                        continue;
                    }
                }
                updateList.add(ar);
            }
            Database.update(updateList, false);
        }
    }

    //本月第一次拜访人数FirstVisitCountMonPerson__c
    private void PerFirVisit() {
        Set<String> Person_In_Set = new Set<String>();
        for (Agency_Report__c a : this.newList) {
            if (a.MonPerVistorKey__c != null) {
                if (Trigger.isUpdate) {
                    Agency_Report__c ar = this.oldMap.get(a.Id);
                    if (a.MonPerVistorKey__c != ar.MonPerVistorKey__c || (System.Label.onlyupdate == '1' && UserInfo.getUserId() == System.Label.jingzhuo)) {//niwu changed
                        Person_In_Set.add(ar.MonPerVistorKey__c);
                        Person_In_Set.add(a.MonPerVistorKey__c);
                    }
                } else {
                    Person_In_Set.add(a.MonPerVistorKey__c);
                }
            }
        }
        if (Person_In_Set.size() > 0) {
            List<Agency_Report__c> arList = [select Id,FirstVisitCountMonPerson__c, MonPerVistorKey__c from Agency_Report__c where MonPerVistorKey__c in :Person_In_Set order by MonPerVistorKey__c, CreatedDate];
        
            String oldKey = '';
            List<Agency_Report__c> updateList = new List<Agency_Report__c>();
            for (Agency_Report__c ar : arList) {
                if (oldKey != ar.MonPerVistorKey__c) {
                    oldKey = ar.MonPerVistorKey__c;
                    if (ar.FirstVisitCountMonPerson__c != 1) {
                        ar.FirstVisitCountMonPerson__c = 1;
                    } else {
                        continue;
                    }
                } else {
                    oldKey = ar.MonPerVistorKey__c;
                    if (ar.FirstVisitCountMonPerson__c == 1) {
                        ar.FirstVisitCountMonPerson__c = 0;
                    } else {
                        continue;
                    }
                }
                updateList.add(ar);
            }
            Database.update(updateList, false);
        }
    }

    //SWAG-C5XBY2  精琢科技  lt  2021-08-26  end

}