public without sharing class AssetMaintainDetailHandler extends Oly_TriggerHandler {
    private Map<Id, AssetMaintainDetail__c> newMap;
    private Map<Id, AssetMaintainDetail__c> oldMap;
    private List<AssetMaintainDetail__c> newList;
    private List<AssetMaintainDetail__c> oldList;
    private Datetime today = System.now();
    private Map<String,String> MAINTYPE_ABBR_MAP = new Map<String, String> {
            '断念找回(断念取消)'=> 'DNQX',
            '断念找回(重新登录)'=>'DNDL',
            '冻结'=>'DJ',
            '解冻'=>'JD',
            '实物报废'=>'BFSW',
            '废弃(丢失)'=>'BFDS',
            '废弃(盘亏)'=>'BFPK',
            '盘盈(数量管理)'=>'PY',
            '耗材已消耗数'=>'XH'
        } ;
    public AssetMaintainDetailHandler() {
        this.newMap = (Map<Id, AssetMaintainDetail__c>) Trigger.newMap;
        this.oldMap = (Map<Id, AssetMaintainDetail__c>) Trigger.oldMap;
        this.newList = (List<AssetMaintainDetail__c>) Trigger.new;
        this.oldList = (List<AssetMaintainDetail__c>) Trigger.old;
    }
    protected override void beforeInsert() {
        beforeSetValue();
    }
    protected override void beforeUpdate() {
        beforeSetValue();
    }

    protected override void afterInsert() {
        changeDesperateRetrieveDate();
    }

    protected override void afterUpdate() {
        changeDesperateRetrieveDate();
    }

    private void beforeSetValue() {
        for (AssetMaintainDetail__c nObj : newList) {
            if(Trigger.isInsert || Trigger.isUpdate){
                nObj.Name = 'WHD-' + this.MAINTYPE_ABBR_MAP.get(nObj.MaintainType__c) + this.today.format('-YYYYMM-') + String.valueOf(nObj.OrderNumber__c).leftpad(4, '0');
            }
        }
    }

    private void changeDesperateRetrieveDate(){
        Set<Id> ids = new Set<Id>();
        List<AssetMaintainDetail__c> amdList = new List<AssetMaintainDetail__c>();
        for (AssetMaintainDetail__c amd : newList) {
            AssetMaintainDetail__c oldAMD = null;
            if (Trigger.isUpdate) {
                oldAMD = oldMap.get(amd.Id);
            }
            if ((oldAMD == null || (oldAMD != null && oldAMD.MaintainType__c != amd.MaintainType__c))
                && (amd.MaintainType__c == '断念找回(断念取消)' || amd.MaintainType__c == '断念找回(重新登录)')) {
                ids.add(amd.AssetMaintainHeader__c);
                amdList.add(amd);
            }
        }

        Map<Id, AssetMaintainHeader__c> amhMap = new Map<Id, AssetMaintainHeader__c>([
                SELECT Id, Date__c
                FROM AssetMaintainHeader__c
                WHERE Id IN: ids]);

        List<Asset> updList = new List<Asset>();
        for (AssetMaintainDetail__c amd : amdList) {
            if (!amhMap.isEmpty() && amhMap.containsKey(amd.AssetMaintainHeader__c)) {
                Asset ass = new Asset();
                ass.Id = amd.Asset__c;
                ass.DesperateRetrieveDate__c = amhMap.get(amd.AssetMaintainHeader__c).Date__c;
                updList.add(ass);
            }
        }

        if (!updList.isEmpty()) {
            update updList;
        }

    }
}