public without sharing class AssetMaintainHeaderHandler extends Oly_TriggerHandler {
    private Map<Id, AssetMaintainHeader__c> newMap;
    private Map<Id, AssetMaintainHeader__c> oldMap;
    private List<AssetMaintainHeader__c> newList;
    private List<AssetMaintainHeader__c> oldList;
    public AssetMaintainHeaderHandler() {
        this.newMap = (Map<Id, AssetMaintainHeader__c>) Trigger.newMap;
        this.oldMap = (Map<Id, AssetMaintainHeader__c>) Trigger.oldMap;
        this.newList = (List<AssetMaintainHeader__c>) Trigger.new;
        this.oldList = (List<AssetMaintainHeader__c>) Trigger.old;
    }
    protected override void beforeUpdate() {
        beforeSetValue();
    }
    protected override void beforeInsert() {
        beforeSetValue();
    }
    protected override void afterUpdate() {
        afterSetValue();
    }
    private void beforeSetValue(){
        for (AssetMaintainHeader__c nObj : newList) {
            AssetMaintainHeader__c oObj;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
            }
            if (Trigger.isInsert
                || (oObj != null
                        && nObj.Status__c == '填写完毕'
                        && nObj.Status__c != oObj.Status__c)) {
                nObj.Buzhang__c = nObj.Buzhang_ID_F__c;
                nObj.Zongjian__c = nObj.Zongjian_ID_F__c;
            }
            if(Trigger.isInsert){
                if(nObj.MaintainType__c == '实物报废'){
                    nObj.RecordTypeId = [SELECT Id FROM RecordType WHERE DeveloperName = 'AssetMaintain_Scrapped'].Id;
                }
                else{
                    nObj.RecordTypeId = [SELECT Id FROM RecordType WHERE DeveloperName = 'AssetMaintain_NotScrapped'].Id;
                }
            }

        }
    }
    private void afterSetValue() {
        for (AssetMaintainHeader__c nObj : newList) {
            AssetMaintainHeader__c oObj = oldMap.get(nObj.Id);
            if(oObj.Status__c == '申请中' && nObj.Status__c == '已批准'){
                AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(nObj.Id, true);
                Database.executeBatch(amBatch);
            }
            else if (oObj.Status__c == '申请中' && nObj.Status__c == '草案中'){
                AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(nObj.Id, false, AssetMaintainManualBatch.Operation.UNFROZEN);
                Database.executeBatch(amBatch);
            }
        }
    }
}