public with sharing class PersonalEvaluationCommentListController {
    public String userId { get; private set; }
    public String province { get; private set; }          // 省（複数）
    public String tabName { get; private set; }
    public String rptName { get; private set; }

    public PersonalEvaluationCommentListController() {
        userId = System.currentPageReference().getParameters().get('user');
        province = System.currentPageReference().getParameters().get('province');
        // tab
        String tabParam = System.currentPageReference().getParameters().get('tab');
        if (String.isBlank(tabParam)) {
            // Controllerの値をそのままつかう
        } else {
            tabName = tabParam;
        }
        // report
        String rptParam = System.currentPageReference().getParameters().get('rpt');
        if (String.isBlank(rptParam)) {
            // Controllerの値をそのままつかう
        } else {
            rptName = rptParam;
        }
        // INCLUDESを使うため、provinceは必ず 'xxx','yyy' の形式です
        if (String.isBlank(province) == false) {
            String tmpP1 = province.replace('\'', '');
            tmpP1 = tmpP1.replace(';', ',');
            String tmpP2 = '';
            for (String str : tmpP1.split(',')) {
                tmpP2 += '\'' + str + '\',';
            }
            province = tmpP2.substring(0, tmpP2.length() - 1);
        }
    }

    public List<PersonalEvaluation__c> getCommentList() {
        List<PersonalEvaluation__c> rtn = new List<PersonalEvaluation__c>();
        if (String.isBlank(tabName)) {
System.debug('rptName=' + rptName);
            if (String.isBlank(province)) {
                rtn = [Select CreatedById, CreatedDate, Comment__c, province__c from PersonalEvaluation__c where OwnerId = :userId and ReportName__c <> null and ReportName__c = :rptName order by CreatedDate desc limit 5];
            } else {
                // INCLUDESのBINDができません、必ずこう使う
                String soql = 'Select CreatedById, CreatedDate, Comment__c, province__c from PersonalEvaluation__c'
                            + ' where province__c INCLUDES (' + province + ')'
                            + ' and ReportName__c <> null and ReportName__c = :rptName order by CreatedDate desc limit 5';
                rtn = Database.query(soql);
                //rtn = [Select CreatedById, CreatedDate, Comment__c, province__c from PersonalEvaluation__c where province__c INCLUDES (:province) and ReportName__c <> null and ReportName__c = :rptName order by CreatedDate desc limit 5];
            }
        } else {
System.debug('tabName=' + tabName);
            if (String.isBlank(province)) {
                rtn = [Select CreatedById, CreatedDate, Comment__c, province__c from PersonalEvaluation__c where OwnerId = :userId and TabName__c <> null and TabName__c = :tabName order by CreatedDate desc limit 5];
            } else {
                // INCLUDESのBINDができません、必ずこう使う
                String soql = 'Select CreatedById, CreatedDate, Comment__c, province__c from PersonalEvaluation__c'
                            + ' where province__c INCLUDES (' + province + ')'
                            + ' and TabName__c <> null and TabName__c = :tabName order by CreatedDate desc limit 5';
                rtn = Database.query(soql);
                //rtn = [Select CreatedById, CreatedDate, Comment__c, province__c from PersonalEvaluation__c where province__c INCLUDES pstr and TabName__c <> null and TabName__c = :tabName order by CreatedDate desc limit 5];
            }
        }
        return rtn;
    }
}