public without sharing class CampaignMemberController {
    public String campaignId { get; set; }
    public String campaignName { get; set; }
    public boolean hasError { get; set; }
    public Campaign cam { get; set; }
    public List<LineInfo> lineInfoList { get; set; }
    public User localuser { get; set; }

    public Integer lineNo { get; set; }
    public String saveflg { get; set; }

    public String errorStr { get; set; }
    // 20220216 PI改造 by 徐亮

    public CampaignMemberController() {
        campaignId = ApexPages.currentPage().getParameters().get('id');
    }

    public Integer getLineInfoListSize() {
        return lineInfoList == null ? 0 : lineInfoList.size();
    }

    public void init() {
        hasError = false;
        // SWAG-B9J9YR 2-19-02-19 start 额外加入检索 recordType.developerName
        List<Campaign> camList = [select id, Name, recordType.developerName from Campaign where id = :campaignId];
        // SWAG-B9J9YR 2-19-02-19 end
        if (camList.size() == 0) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '学会不存在。'));
            hasError = true;
            return;
        }

        cam = camList[0];
        campaignName = cam.Name;

        localuser = [select id, State_Hospital__c from User where id = :UserInfo.getUserId()];

        // 20220216 PI改造 by 徐亮
        //deloitte-zhj 20231115 PIPL还原
        List<CampaignMember__c> cmList = [
                                             select id, Name, Campaign__c, Department__c, Department_ID__c, Opportunity__c, Opportunity_ID__c,
                                             Contact__c, Contact_ID__c, Type__c, Contact_ID__r.Strategic_dept_Class__r.Name
                                             from CampaignMember__c
                                             where Campaign__c = :campaignId];

        lineInfoList = new List<LineInfo>();
        Integer line = 0;
        for (CampaignMember__c cm : cmList) {
            line += 1;
            LineInfo info = new LineInfo(line, cm);
            lineInfoList.add(info);
        }

        if (cmList.size() < 5) {
            for (Integer i = cmList.size(); i < 5; i++) {
                line += 1;
                LineInfo temp = new LineInfo(line);
                temp.cm.Campaign__c = cam.Id;
                lineInfoList.add(temp);
            }
        }

        line += 1;
        LineInfo temp = new LineInfo(line);
        temp.cm.Campaign__c = cam.Id;
        lineInfoList.add(temp);
    }

    public PageReference addLine() {
        Integer nowLine = getLineInfoListSize();
        system.debug('=====before addline:' + lineInfoList.size());
        LineInfo newopi = new LineInfo(nowLine + 1);
        newopi.cm.Campaign__c = cam.Id;

        List<LineInfo> temp = new List<LineInfo>();
        for (LineInfo li : lineInfoList) {
            temp.add(li);
        }
        temp.add(newopi);
        lineInfoList = temp.clone();
        system.debug('=====after addline:' + lineInfoList.size());
        return null;
    }

    public PageReference checkLine() {
        List<LineInfo> temp = new List<LineInfo>();
        for (LineInfo li : lineInfoList) {
            temp.add(li);
        }
        lineInfoList = temp.clone();
        system.debug('=====after checkLine:' + lineInfoList.size());

        if (saveflg == '1') {
            if (errorStr != '') {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, errorStr));
            } else {
                // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '保存完了。'));
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM, '保存完了。'));//20231103 李文涛 lihgtning升级
            }
            hasError = true;
        }
        return null;
    }

    public PageReference deleteLine() {
        Integer rownum = 0;
        List<LineInfo> temp = new List<LineInfo>();
        for (LineInfo li : lineInfoList) {
            if (li.line != lineNo) {
                rownum += 1;
                LineInfo tmp = new LineInfo(rownum, li.cm);
                temp.add(tmp);
            }
        }
        lineInfoList = temp.clone();

        return null;
    }

    public PageReference saveLine() {
        List<CampaignMember__c> cmList = [
                                             select id
                                             from CampaignMember__c
                                             where Campaign__c = :campaignId];

        List<CampaignMember__c> insertList = new List<CampaignMember__c>();
        for (LineInfo li : lineInfoList) {
            system.debug('=====department:' + li.cm.Department__c + '|DepartmentID:' + li.cm.Department_ID__c + '|Contact:' + li.cm.Contact__c + '|ContactID:' + li.cm.Contact_ID__c);
            if (li.cm.Department__c != null && li.cm.Department_ID__c != null && li.cm.Contact__c != null && li.cm.Contact_ID__c != null) {
                li.cm.Id = null;
                insertList.add(li.cm);
            }
        }

        errorStr = '';
        Savepoint sp = Database.setSavepoint();
        try {
            delete cmList;
            system.debug('=====insertList.size:' + insertList.size());
            if (insertList.size() > 0) {
                insert insertList;
            }
        } catch (System.Exception e) {
            Database.rollback(sp);
            //20231103 李文涛 lihgtning升级 start
            String eMsg;
            if(e.getMessage().contains(':')){
                String eMessage =e.getMessage();
                Integer left = eMessage.indexOf(',')+1;
                Integer right= eMessage.lastIndexOf('。')+1;
                if(right>eMessage.length()||right<=left){
                    right=eMessage.length();
                }
                String mes=eMessage.substring(left,right);
                eMsg =mes;
            }else{
                eMsg = e.getMessage() + '';
            }
            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
            // errorStr = e.getMessage();
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, eMsg));
            errorStr = eMsg;
            //20231103 李文涛 lihgtning升级 end
            hasError = true;
            return null;
        }
        // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '保存完了。'));
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM, '保存完了。'));//20231103 李文涛 lihgtning升级
        hasError = true;
        init();//Add by Li Jun 20231102
        return null;
    }

    class LineInfo {
        public Integer line { get; set; }
        public CampaignMember__c cm { get; set; }

        public LineInfo(Integer in_line) {
            line = in_line;
            cm = new CampaignMember__c();
        }

        public LineInfo(Integer in_line, CampaignMember__c in_cm) {
            line = in_line;
            cm = in_cm;
        }
    }
}