public without sharing class Solution_ProgrammeTrigger extends Oly_TriggerHandler{
    private Map<Id,  Solution_Programme__c> newMap;
    private Map<Id, Solution_Programme__c> oldMap;
    private List<Solution_Programme__c> newList;
    private List<Solution_Programme__c> oldList;
    public Set<id> solProjectId = new Set<id>();

    public Solution_ProgrammeTrigger() {
        this.newMap = (Map<Id, Solution_Programme__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Solution_Programme__c>) Trigger.oldMap;
        this.newList = (List<Solution_Programme__c>) Trigger.new;
        this.oldList = (List<Solution_Programme__c>) Trigger.old;
    }

    protected override void beforeInsert() {

        // getSolutionProjectId();
        // Map<Id,SolutionProjectRequirements__c> solutionProJectMap = new Map<Id,SolutionProjectRequirements__c>();
        // List<SolutionProjectRequirements__c> solutionProJectList = [select id,Status__c,First_Proposal_Time__c from SolutionProjectRequirements__c where id = :solProjectId];
        // for(SolutionProjectRequirements__c solProject : solutionProJectList){
        //     solutionProJectMap.put(solProject.Id, solProject);
        // }
        // List<SolutionProjectRequirements__c> solProjectUpdateList = new List<SolutionProjectRequirements__c>();
        for(Solution_Programme__c solutionPro : newList){
            if(solutionPro.Scheme_Type__c == '初次'){
                solutionPro.RecordTypeId = '01210000000gUGGAA2';
                // solutionPro.RecordTypeId = '012p0000000Rr3KAAS';
            }
            if(solutionPro.Scheme_Type__c == '结案'){
                solutionPro.RecordTypeId = '01210000000gUGFAA2';
                // solutionPro.RecordTypeId = '012p0000000Rr3PAAS';
            }
        }
        // if(solProjectUpdateList.size() > 0){
        //     update solProjectUpdateList;
        // }

    }

    protected override void beforeUpdate() {
    	// 提交solution方案审批时，点击审批之后，更新solution方案之前，更新solution方案的状态以及solution项目需求的状态
    	// 获取solution项目需求的id值
    	getSolutionProjectId();
    	updateSolutionProgrammeAndProjectStatus();
    }

    protected override void afterUpdate() {
    	// 为solution项目需求的第一次提交日期赋值。
    	getSolutionProjectId();
    	updateSolutionProjectFirstSubDate();
    }

    protected override void afterInsert() {
    }

    protected override void afterDelete() {
    }
    public void getSolutionProjectId(){
    	for(Solution_Programme__c sol : newList){
			solProjectId.add(sol.SolutionProjectRequirements__c);
	    }
    }

    public void updateSolutionProjectFirstSubDate(){
    	System.debug('进入after trigger ');
	    Map<Id,SolutionProjectRequirements__c> solutionProJectMap = new Map<Id,SolutionProjectRequirements__c>();
	    List<SolutionProjectRequirements__c> solutionProJectList = [select id,Status__c,First_Proposal_Time__c,Confirm_Result__c from SolutionProjectRequirements__c where id = :solProjectId];
	    

        // 更新项目需求的确认结果 start
        List<SolutionProjectRequirements__c> solProjectUpdateResultList = new List<SolutionProjectRequirements__c>();
        List<Solution_Programme__c> solutionProgrammeList = [select id,SolutionProjectRequirements__c,Confirmation_Result__c from Solution_Programme__c where SolutionProjectRequirements__c = :solProjectId and Confirmation_Result__c != '' order by CreatedDate];
        Map<String,String> confirmResultMap = new Map<String,String>();
        for(Solution_Programme__c soll : solutionProgrammeList){
            confirmResultMap.put(soll.SolutionProjectRequirements__c, soll.Confirmation_Result__c);
        }


        for(SolutionProjectRequirements__c solProject : solutionProJectList){
            solutionProJectMap.put(solProject.Id, solProject);
            if(confirmResultMap.containsKey(solProject.Id)){
                if(solProject.Confirm_Result__c != confirmResultMap.get(solProject.Id)){
                    solProject.Confirm_Result__c = confirmResultMap.get(solProject.Id);
                    solProjectUpdateResultList.add(solProject);
                }
            }
        }
        // 更新项目需求的确认结果 end
    	List<SolutionProjectRequirements__c> solProjectUpdateList = new List<SolutionProjectRequirements__c>();
        
    	for(Solution_Programme__c sol : newList){
			if(sol.Proposal_Date__c != null || sol.Submint_TF__c == true){
				System.debug('solution方案的提交日期不为空');
				if(solutionProJectMap.get(sol.SolutionProjectRequirements__c).First_Proposal_Time__c == null){
					System.debug('solution项目需求的第一次提交日期为空');
					solutionProJectMap.get(sol.SolutionProjectRequirements__c).First_Proposal_Time__c = Datetime.now();
					// solProjectUpdateList.add(solutionProJectMap.get(sol.SolutionProjectRequirements__c));
				}
                if(solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c == '03方案制作中'){
                    solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c = '04方案提出';
                    
                }
                // 自动结案
                if(sol.ApprovalClosingProgramme__c == true && solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c != '07结案'){
                    solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c = '07结案';
                }
                // 2020-09-25 mzy  新增 CHAN-BTJCPQ  方案类型为初案时,调回以后,项目需求的状态变回 start
               if(sol.Submint_TF__c == false && sol.Scheme_Type__c == '初次'){
                    if(solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c == '04方案提出'){
                    solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c = '03方案制作中';
                  }
                }
                // 2020-09-25 mzy 新增 CHAN-BTJCPQ  方案类型为初案时,调回以后,项目需求的状态变回 end
                solProjectUpdateList.add(solutionProJectMap.get(sol.SolutionProjectRequirements__c));
                        
			}

            // if(sol.Confirmation_Result__c != '' && sol.Confirmation_Result__c != null){
            //     if(){

            //     }
            // }
            // if(sol.Submint_TF__c == true ){
                // if(solutionProJectMap.containsKey(sol.SolutionProjectRequirements__c)){
                //     if(solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c == '03方案制作中')
                //         solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c = '04方案提出';
                //         solProjectUpdateList.add(solutionProJectMap.get(sol.SolutionProjectRequirements__c));
                // }
            // }
    	}

    	if(solProjectUpdateList.size() > 0){
    		update solProjectUpdateList;
    	}
        if(solProjectUpdateResultList.size() > 0 ){
            update solProjectUpdateResultList;
        }
    }

    public void updateSolutionProgrammeAndProjectStatus(){
    	Map<Id,SolutionProjectRequirements__c> solutionProJectMap = new Map<Id,SolutionProjectRequirements__c>();
	    List<SolutionProjectRequirements__c> solutionProJectList = [select id,Status__c,First_Proposal_Time__c from SolutionProjectRequirements__c where id = :solProjectId];
	    for(SolutionProjectRequirements__c solProject : solutionProJectList){
	    	solutionProJectMap.put(solProject.Id, solProject);
	    }
    	List<SolutionProjectRequirements__c> solProjectUpdateList = new List<SolutionProjectRequirements__c>();
        for(Solution_Programme__c solutionPro : newList){
            // 驳回无效
            if(solutionPro.RejectedInvalid__c == true){
                solutionPro.addError('请填写反馈结果，并按审批按钮');
            }
    		if(solutionPro.ProcessOfApproval__c == true && (solutionPro.Confirmation_Result__c == null || solutionPro.Confirmation_Result__c == '')){
    			solutionPro.addError('批准之前必须填写反馈结果！');
    		}
    		if(solutionPro.ProcessOfApproval__c == true && solutionPro.Confirmation_Result__c != null && solutionPro.Confirmation_Result__c != ''){
    			if(solutionPro.Confirmation_Result__c == '01采纳/部分采纳'){
    				solutionPro.Status__c = '03采纳/部分采纳';
    				// solutionPro.Scheme_Type__c = '结案';
    				solutionPro.ProcessOfApproval__c = false;
    				if(solutionProJectMap.containsKey(solutionPro.SolutionProjectRequirements__c)){
    					solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c).Status__c = '06已反馈';
    					solProjectUpdateList.add(solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c));
    				}
    			}
    			if(solutionPro.Confirmation_Result__c == '02未采纳'){
    				solutionPro.Status__c = '04未采纳';
    				// solutionPro.Scheme_Type__c = '结案';
    				solutionPro.ProcessOfApproval__c = false;
    				if(solutionProJectMap.containsKey(solutionPro.SolutionProjectRequirements__c)){
    					solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c).Status__c = '06已反馈';
    					solProjectUpdateList.add(solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c));
    				}
    			}
    			if(solutionPro.Confirmation_Result__c == '04其他'){
    				solutionPro.Status__c = '06其他';
    				// solutionPro.Scheme_Type__c = '结案';
    				solutionPro.ProcessOfApproval__c = false;
    				if(solutionProJectMap.containsKey(solutionPro.SolutionProjectRequirements__c)){
    					solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c).Status__c = '06已反馈';
    					solProjectUpdateList.add(solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c));
    				}
    			}
    			if(solutionPro.Confirmation_Result__c == '03跟进中'){
    				solutionPro.Status__c = '05营业跟进中';
    				// solutionPro.Scheme_Type__c = '初次';
    				solutionPro.ProcessOfApproval__c = false;
    				if(solutionProJectMap.containsKey(solutionPro.SolutionProjectRequirements__c)){
    					solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c).Status__c = '05方案修改中';
    					solProjectUpdateList.add(solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c));
    				}
    			}
                
    		}
    	}
    	if(solProjectUpdateList.size() > 0){
    		update solProjectUpdateList;
    	}
    }
}