public without sharing class TransferApplySummaryHandler extends Oly_TriggerHandler {
    private Map<Id, TransferApplySummary__c> newMap;
    private Map<Id, TransferApplySummary__c> oldMap;
    private List<TransferApplySummary__c> newList;
    private List<TransferApplySummary__c> oldList;
    private static Map<Id, TransferApplySummary__c> raesdCMap = new Map<Id, TransferApplySummary__c>();

    public TransferApplySummaryHandler() {
        this.newMap = (Map<Id, TransferApplySummary__c>) Trigger.newMap;
        this.oldMap = (Map<Id, TransferApplySummary__c>) Trigger.oldMap;
        this.newList = (List<TransferApplySummary__c>) Trigger.new;
        this.oldList = (List<TransferApplySummary__c>) Trigger.old;
    }
    //20201109 ljh  start
    protected override void beforeInsert() {
        beforeSetValue();
    }
    //20201109 ljh add  end
    protected override void beforeUpdate() {
        DateTime now = System.Now();
        for (TransferApplySummary__c nObj : newList) {
            TransferApplySummary__c oObj = oldMap.get(nObj.Id);
            if (oObj.Cancel_Select__c == false && nObj.Cancel_Select__c == true) {
                nObj.Cancel_Mem__c = UserInfo.getUserId();
                nObj.Cancel_Time__c = now;
                raesdCMap.put(nObj.Id, nObj);
            }
        }
        beforeSetValue();//20201109 ljh add
    }
    protected override void afterUpdate() {
        cancelDetail(raesdCMap, true);
    }
    //明細更新メソッド    更新明細：一覧Cancel明細もCancel
    //                          一覧申请者收获NG明細更新Name
    //                          必須項目：Cancel_Reason__c
    // @param raesdCMap Cancel一覧Map
    // @param allOrNone  不允许部分保存
    // @return 保存结果
    private static Database.SaveResult[] cancelDetail(Map<Id, TransferApplySummary__c> raesdCMap, Boolean allOrNone) {
        // null.isEmpty の場合えラー
        if (raesdCMap == null) {
            raesdCMap = new Map<Id, TransferApplySummary__c>();
        }

        //更新明细
        Map<Id, TransferApplyDetail__c> raesdMap = new Map<Id, TransferApplyDetail__c>();

        //取消更新的明细
        if (!raesdCMap.isEmpty()) {
            List<TransferApplyDetail__c> raesds = [
                    Select Id, TransferApplySummary__c,TAESD_Status__c
                      From TransferApplyDetail__c
                     Where TransferApplySummary__c = :raesdCMap.keySet()
                       AND Cancel_Select__c = false
                       AND DeliverySlip__c = null];
            for (TransferApplyDetail__c raesd :raesds) {
                raesd.Cancel_Select__c = true;
                //raesd.Cancel_Mem__c = UserInfo.getUserId();
                //raesd.Cancel_Date__c = Date.today();
                //raesd.Cancel_Time__c = System.now();
                raesd.Cancel_Reason__c = raesdCMap.get(raesd.TransferApplySummary__c).Cancel_Reason__c;
                //raesd.Loaner_cancel_Remarks__c = raesdCMap.get(raesd.TransferApplySummary__c).Loaner_cancel_Remarks__c;
                raesdMap.put(raesd.Id, raesd);
            }
        }

        if (!raesdMap.isEmpty()) {
            return Database.update(raesdMap.values(), allOrNone);
        }
        return null;
    }
    //20201109 ljh add start
    private void beforeSetValue() {
        //20201109 ljh OCSM_BP5-44 调拨单设定对应_申请，一览，明细的命名规则 start
        for (TransferApplySummary__c nObj : newList) {
            if (nObj.First_TAD_Model_No_F__c != null  && nObj.IndexFromUniqueKey__c != null) {
                nObj.Name = nobj.Name_F__c + ':'+nObj.First_TAD_Model_No_F__c+':'+ ((Integer) Math.round(nObj.IndexFromUniqueKey__c)).format().leftpad(3, '0');
                //取消的时候名字加Canceled
                if (nObj.Cancel_Select__c) {
                    nObj.Name += ':Canceled';
                }
            }
        }
        //20201109 ljh OCSM_BP5-44 调拨单设定对应_申请，一览，明细的命名规则 end
    }
    //20201109 ljh add  end
}