public without sharing class OpportunityBiddingHandler extends Oly_TriggerHandler {
    private Map<Id, Enquiry_No_Bidding_Apply__c> newMap;
    private Map<Id, Enquiry_No_Bidding_Apply__c> oldMap;
    private List<Enquiry_No_Bidding_Apply__c> newList;
    private List<Enquiry_No_Bidding_Apply__c> oldList;
    public OpportunityBiddingHandler() {
        this.newMap = (Map<Id, Enquiry_No_Bidding_Apply__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Enquiry_No_Bidding_Apply__c>) Trigger.oldMap;
        this.newList = (List<Enquiry_No_Bidding_Apply__c>) Trigger.new;
        this.oldList = (List<Enquiry_No_Bidding_Apply__c>) Trigger.old;
    }
    // protected override void beforeUpdate() {
    //  ();
    // }
    // @override
    //根据ocsm管理省的Gi助理为询价不应标申请中的主负责人（助理）赋值
    protected override void beforeInsert() {
        AssignValueToAssistant();
    }

    protected override void beforeUpdate() {
        AssignValueToAssistant();
    }
    protected override void afterUpdate() {
        addOpp();
        //add by rentx 20210726 start
        addShare();
        //add by rentx 20210726 end
    }
    protected override void afterInsert() {
        
        //add by rentx 20210726 start
        addShare();
        //add by rentx 20210726 end
    }

    //当审批流程走完时 更新 询价相关字段
    private void addOpp() {
        // List<Tender_information__c> TenInfoList = [select Id, IsReactionOpp__c from Tender_information__c];
        List<Opportunity> OppIdList = new List<Opportunity>();
        Map<Id, Opportunity> updateOppMap = new Map<Id, Opportunity>();
        List<Tender_information__c> updateTenList = new List<Tender_information__c>();
        // 遍历询价
        for (Enquiry_No_Bidding_Apply__c Enbas : newList) {
            // 如果新的状态！=更新之前的状态，现在的状态=批准 （将给询价中的是否应标赋值为否）
            if ((Enbas.NotBidApprovalStatus__c != oldMap.get(Enbas.Id).NotBidApprovalStatus__c) && Enbas.NotBidApprovalStatus__c == '批准') {

                String oppId = Enbas.Opportunity__c;
                if (String.isNotBlank(oppId)) {
                    Opportunity opp = new Opportunity();
                    opp.Id = oppId;
                    // System.debug('----1----' + opp.Bidding_Project_Name_Bid__c);
                    opp.Whether_Bidding__c = '否';
                    updateOppMap.put(oppId, opp);
                }
            }
        }

        if (updateOppMap.values().size() > 0) {
            update updateOppMap.values();
            List<Opportunity> oppInfoList = [select Id, Bidding_Project_Name_Bid__c from Opportunity where Id in :updateOppMap.keySet() and Bidding_Project_Name_Bid__c != null];
            for (Opportunity opp : oppInfoList) {
                Tender_information__c TenInfo = new Tender_information__c();
                TenInfo.Id = opp.Bidding_Project_Name_Bid__c;
                TenInfo.IsReactionOpp__c = true;
                updateTenList.add(TenInfo);
            }
        }
        if (updateTenList.size() > 0) {
            System.debug('----3----' + updateTenList.size());
            update updateTenList;
        }
    }
    //根据ocsm管理省的Gi助理为询价不应标申请中的主负责人（助理）赋值
    private void AssignValueToAssistant() {
        // 通过OCSM管理省，关联招投标 与 管理省
        Map<String, OCM_Management_Province__c> mpMap = new Map<String, OCM_Management_Province__c>();
        // 关联询价与询价不应标申请
        Map<Id, Enquiry_No_Bidding_Apply__c > oppMap = new Map<Id, Enquiry_No_Bidding_Apply__c >();
        // 查 询价不应标申请数据
        List<OCM_Management_Province__c> OCMinfoList = [SELECT id, Name, GI_assistant__c, GI_assistant__r.IsActive FROM OCM_Management_Province__c];

        for (OCM_Management_Province__c mp : OCMinfoList) {
            mpMap.put(mp.Name, mp);
        }

        //直接设置 信息负责人(助理) 为GI助理
        for (Enquiry_No_Bidding_Apply__c Enbainfos : newList) {
            oppMap.put(Enbainfos.Opportunity__c, Enbainfos);
        }
        // 关联 招投标 与 询价
        Map<Enquiry_No_Bidding_Apply__c, String> EnquiryMap = new Map<Enquiry_No_Bidding_Apply__c, String>();
        // 通过 与 询价 关联的 询价不应标申请 查对应的询价（多此一举，易理解）
        List<Opportunity> oppinfoList = [SELECT id, OCM_man_province__c, Bidding_Project_Name_Bid__c, Bidding_Project_Name_Bid__r.OCSMProvince__c FROM Opportunity Where Id In: oppMap.keySet()];
        for (Opportunity Oppinfos : oppinfoList) {
            EnquiryMap.put(oppMap.get(Oppinfos.Id), Oppinfos.OCM_man_province__c );
        }
        List<Enquiry_No_Bidding_Apply__c> EnquiryList = new List<Enquiry_No_Bidding_Apply__c>();
        for (Enquiry_No_Bidding_Apply__c Enbainfo : newList) {
            if (EnquiryMap.get(Enbainfo) != null && oppMap.keySet() != null) {
               // Enbainfo.leading_Cadre_Assistant__c = mpMap.get(EnquiryMap.get(Enbainfo)).GI_assistant__c;
                EnquiryList.add(Enbainfo);
            }

        }
        // if (EnquiryList.size() > 0) {
        //  upsert EnquiryList;
        // }
    }

    //add by rentx 20210726 start
    public void addShare(){
        //获取共享原因
        String rowCauseXinxi = Schema.Enquiry_No_Bidding_Apply__Share.RowCause.Xinxi__c;
        
        List<String> rowCauseList = new List<String>();
        rowCauseList.add(rowCauseXinxi);

        //设置共享
        Map<Id,Enquiry_No_Bidding_Apply__Share> sharemap = new Map<Id,Enquiry_No_Bidding_Apply__Share>();

        List<Enquiry_No_Bidding_Apply__Share> enqShareList = new List<Enquiry_No_Bidding_Apply__Share>();
        for (Enquiry_No_Bidding_Apply__c enq : newList) {
            //判断 如果信息负责人不是owner 则设置共享
            if (enq.leading_Cadre_Assistant__c != enq.OwnerId && enq.leading_Cadre_Assistant__c != null) {
                Enquiry_No_Bidding_Apply__Share aos = new Enquiry_No_Bidding_Apply__Share(
                    RowCause = rowCauseXinxi,
                    ParentId = enq.Id,
                    UserOrGroupId = enq.leading_Cadre_Assistant__c,
                    AccessLevel = 'Edit');
                sharemap.put(enq.leading_Cadre_Assistant__c, aos);
            }
        }

        //先删再加
        List<Enquiry_No_Bidding_Apply__Share> beforeShareList = 
            [select id from Enquiry_No_Bidding_Apply__Share 
             where ParentId in :newMap.keySet() 
             and RowCause in :rowCauseList];
        if (beforeShareList != null && beforeShareList.size() > 0) {
            delete beforeShareList;
        }

        if (sharemap != null && sharemap.size() > 0) {
            insert sharemap.values();
        }
    }
    //add by rentx 20210726 end
}