public without sharing class QISMBCTrigger {
    // QISMBCのId
    public static Set<Id> QISMBS_Ids = new Set<Id>();
    
    // after insert, after update
    public static void setQISValues(List<QIS_M_BC__c> newList, List<QIS_M_BC__c> oldList, Map<Id, QIS_M_BC__c> newMap, Map<Id, QIS_M_BC__c> oldMap) {
        List<QIS_Report__c> updQisList = new LIst<QIS_Report__c>();
        for (QIS_M_BC__c qm : newList) {
            if (qm.QIS_request_name__c != null) {
                QIS_Report__c qr = new QIS_Report__c(Id = qm.QIS_request_name__c);
                // ①
                /*
                if (qm.QIS_submit_date_for_MBC__c != null) {
                    qr.Wether_publish_QIS_not__c = '发行';
                } else {
                    qr.Wether_publish_QIS_not__c = '不发行';
                }
                */
                // ②
                qr.QIS_no_for_m_BC__c = qm.Name;
                // ③
                qr.QIS_submit_date_for_MBC__c = qm.QIS_submit_date_for_MBC__c;
                qr.QIS_M_BC_submit_person__c = qm.OwnerAlias__c;
                // ④
                qr.QIS_M_BC_reception__c = qm.QIS_M_BC_reception__c;
                // ⑤
                qr.QIS_M_BC_reply_date__c = qm.QIS_M_BC_answer_day__c;
                // ⑥
                qr.Received_feiqi_date__c = qm.Received_feiqi_date__c;
                // ⑦
                qr.QIS_M_BC_answer__c = qm.QIS_M_BC_answer_detail__c;
                
                updQisList.add(qr);
            }
        }
        if (updQisList.size() > 0) update updQisList;
    }
    
    // before insert, before update
    public static void setMails(List<QIS_M_BC__c> newList, List<QIS_M_BC__c> oldList, Map<Id, QIS_M_BC__c> newMap, Map<Id, QIS_M_BC__c> oldMap) {
        // 使用的QISID
        Map<String, String> use_qisid = new Map<String, String>();
        for (QIS_M_BC__c local : newList) {
            if (use_qisid.containskey(local.QIS_request_name__c) == false) {
                use_qisid.put(local.QIS_request_name__c, local.id);
            }
        }
        // 检索QIS的mail信息
        Map<String, QIS_Report__c> qisMap = new Map<String, QIS_Report__c>();
        //LJPH-C5H8ST QIS判定邮件抄送添加 追加OCM_Repair_Mail1__c字段 liuyan
        List<QIS_Report__c> qisList = [select id, OCM_Manager_Mail__c, OCM_Member_Mail__c,OCM_Repair_Mail__c,OCM_Repair_Mail1__c 
                                         from QIS_Report__c 
                                        where id in :use_qisid.keyset()];
        for (QIS_Report__c qis : qisList) {
            if (qisMap.containskey(qis.Id) == false) {
                qisMap.put(qis.Id, qis);
            }
        }
        // repair，Member，Manager的mail拷贝到QISMBC
        for (QIS_M_BC__c local : newList) {
            QIS_Report__c qis = qisMap.get(local.QIS_request_name__c);
            local.OCM_Manager_Mail__c = qis == null ? null : qis.OCM_Manager_Mail__c;
            local.OCM_Member_Mail__c = qis == null ? null : qis.OCM_Member_Mail__c;
            local.OCM_Repair_Mail__c = qis == null ? null : qis.OCM_Repair_Mail__c;
            //LJPH-C5H8ST QIS判定邮件抄送添加 追加OCM_Member_Mail1__c字段 liuyan START
            local.OCM_Repair_Mail1__c = qis == null ? null : qis.OCM_Repair_Mail1__c;
            //END
        }
    }
    
    // after insert, after update, after delete
    public static Boolean checkAndSetNo(List<QIS_M_BC__c> newList, List<QIS_M_BC__c> oldList, Map<Id, QIS_M_BC__c> newMap, Map<Id, QIS_M_BC__c> oldMap) {
        // 新规或更新后
        if (Trigger.isInsert || Trigger.isUpdate) {
            // 使用的QISID
            Map<String, String> use_qisid = new Map<String, String>();
            for (QIS_M_BC__c local : newList) {
                if (use_qisid.containskey(local.QIS_request_name__c) == false) {
                    use_qisid.put(local.QIS_request_name__c, local.id);
                }
            }
            // 所有使用这些QISID的QISMBC
            Map<String, String> all_qismbc = new Map<String, String>();
            List<QIS_M_BC__c> qismbcList = [select id,name,QIS_request_name__c 
                                              from QIS_M_BC__c 
                                             where QIS_request_name__c in :use_qisid.keyset()];
            for (QIS_M_BC__c qismbc : qismbcList) {
                if (qismbc.QIS_request_name__c != null && qismbc.id != use_qisid.get(qismbc.QIS_request_name__c)) {
                    QIS_M_BC__c q = newMap.get(use_qisid.get(qismbc.QIS_request_name__c));
                    q.addError('一个QIS只能创建一个QISMBC');
                    return false;
                }
            }
        }
        
        // 更新QIS的QIS No(M-BC)
        List<QIS_Report__c> qisList = new List<QIS_Report__c>();
        List<QIS_Report__c> oldqisList = new List<QIS_Report__c>();
        if (Trigger.isInsert) {
            // 新建QIS_M_BC全部更新
            for (QIS_M_BC__c local : newList) {
                QIS_Report__c qis = new QIS_Report__c(id=local.QIS_request_name__c, QIS_no_for_m_BC__c=local.id);
                qisList.add(qis);
            }
        }
        if (Trigger.isUpdate) {
            for (QIS_M_BC__c local : newList) {
                QIS_M_BC__c old = oldMap.get(local.Id);
                // QIS号码变更时，需要更新
                if (old.QIS_request_name__c != local.QIS_request_name__c) {
                    QIS_Report__c qis_old = new QIS_Report__c(id=old.QIS_request_name__c, QIS_no_for_m_BC__c='');
                    oldqisList.add(qis_old);
                    QIS_Report__c qis_new = new QIS_Report__c(id=local.QIS_request_name__c, QIS_no_for_m_BC__c=local.Name);
                    qisList.add(qis_new);
                }
            }
        }
        if (Trigger.isDelete) {
            // 删除QIS_M_BC全部更新
            for (QIS_M_BC__c local : oldList) {
                QIS_Report__c qis = new QIS_Report__c(id=local.QIS_request_name__c, QIS_no_for_m_BC__c='');
                oldqisList.add(qis);
            }
        }
        // 更新QIS
        if (oldqisList.size() > 0) {
            update oldqisList;
        }
        if (qisList.size() > 0) {
            update qisList;
        }
        
        return true;
    }
}