public with sharing class RepairAgainAnMBCHandler extends Oly_TriggerHandler{

	private Map<Id, RepairAgainAn_M_BC__c> newMap;
    private Map<Id, RepairAgainAn_M_BC__c> oldMap;
    private List<RepairAgainAn_M_BC__c> newList;
    private List<RepairAgainAn_M_BC__c> oldList;

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

	protected override void beforeInsert() {
		setName();
	}

	protected override void afterInsert() {
	}

	protected override void beforeUpdate() {	
	}

	protected override void afterUpdate(){
		//更新成功后，需要向相关RC人员、OSH-SH/OSH-GZ品质人员发送邮件通知
		sendEmail();
	}

	//before insert
    private void setName() {
    	//RepairAgainAn__c Set
    	Set<Id> repairAnSet= new Set<Id>();

    	for(RepairAgainAn_M_BC__c ram : newList){
    		repairAnSet.add(ram.RepairAgainAn_request_name__c);
    	}

    	Map<Id,RepairAgainAn__c> repairMbcMap= new Map<Id,RepairAgainAn__c>([SELECT Id,SORC__c,(SELECT Id from RepairAgainAn_M_BC__r) from RepairAgainAn__c where Id in :repairAnSet]);
        for (RepairAgainAn_M_BC__c qisMbc : newList){
        	if(repairMbcMap.containsKey(qisMbc.RepairAgainAn_request_name__c)){
        		//QIS-MBC name设定规则  SORC的编号 - M-BC的选项 - 财年 - 顺序编号
	            String tempName = null;
	            tempName = repairMbcMap.get(qisMbc.RepairAgainAn_request_name__c).SORC__c ;
				tempName += '-' + qisMbc.M_BC__c;
		        tempName += '-' + countFiscalyear();

	            //顺序编号计算
	            Integer repairSize = repairMbcMap.get(qisMbc.RepairAgainAn_request_name__c).RepairAgainAn_M_BC__r.size();
	            if( repairSize == 0 ){
 					tempName += '-' + '001';
	            } else {
	            	Integer temp = repairMbcMap.get(qisMbc.RepairAgainAn_request_name__c).RepairAgainAn_M_BC__r.size()+1 ;
	            	String repairAgainSizeString = '000' + temp ;
	            	String cutString = repairAgainSizeString.substring(repairAgainSizeString.length()-3,repairAgainSizeString.length());
	            	tempName += '-' + cutString;
	            }

	            qisMbc.Name = tempName;
        	}
        }
    }

    //after update
    private void sendEmail(){

    	EmailTemplate et=[Select id from EmailTemplate where name = '再修理分析M_BC回答内容更新' limit 1];

    	List<Id> neadUpdateList = new List<Id>();
    	for(RepairAgainAn_M_BC__c ram : newList){
			if(newMap.get(ram.Id).RepairAgainAn_M_BC_answer_detail__c != oldMap.get(ram.Id).RepairAgainAn_M_BC_answer_detail__c){
				neadUpdateList.add(ram.Id);
			}
		}

		String[] toAddresses = System.Label.RepairAgainAn_Sendmail.split(';');
		List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
		
		for(Id repairMbId: neadUpdateList){
			Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
			mail = Messaging.renderStoredEmailTemplate(et.Id, null , repairMbId);
			mail.setSaveAsActivity(false);

			mail.setToAddresses(toAddresses);
			emails.add(mail);
		}
		
		Messaging.sendEmail(emails);
    }

    //计算财年
    private String countFiscalyear(){
    	//财年
        Date dateNow = Date.today();
        Integer year = dateNow.year();
        Integer month = dateNow.month();
        String flag = '';
        if( month < 4 ){
        	year -= 1;
        }
        if( month < 4 || month > 9){
        	flag = '2H\'';
        }else{
        	flag = '1H\'';
        }
        Integer tempYear = year + 1;
        String currentPeriod = String.valueOf('FY' + tempYear + ' ' + flag);

        return currentPeriod;
    }
}