/**
     * @Author    lijinhuan
     * @DateTime  2022-12-14T17:01:14+0800
     * 测试类：FrameNumUploadControllerTest
*/
public without sharing class FrameNumManageHandler  extends Oly_TriggerHandler {

    private Map<Id, FrameNumManage__c > newMap;
    private Map<Id, FrameNumManage__c > oldMap;
    private List<FrameNumManage__c > newList;
    private List<FrameNumManage__c > oldList;
    public FrameNumManageHandler() {
        this.newMap = (Map<Id, FrameNumManage__c >) Trigger.newMap;
        this.oldMap = (Map<Id, FrameNumManage__c >) Trigger.oldMap;
        this.newList = (List<FrameNumManage__c >) Trigger.new;
        this.oldList = (List<FrameNumManage__c >) Trigger.old;
    }
    protected override void afterInsert() {
    }
    protected override void afterUpdate() {
        updateAsset();
    }

    protected override void beforeInsert() {
    }
    
    protected override void beforeUpdate() {
        IsChange();
    }
    private Void updateAsset(){
        Map<String,FrameNumManage__c> FrameNumManageMap = new Map<String,FrameNumManage__c>();
        for (FrameNumManage__c nObj : newList) {
            FrameNumManage__c oObj = oldMap.get(nObj.Id);
            if( String.isNotBlank(nObj.Asset__c) 
            && ( nObj.Asset__c != oObj.Asset__c 
                ||  nObj.ApplyList_Select__c != oObj.ApplyList_Select__c 
                ||  nObj.InspectionCard_Select__c != oObj.InspectionCard_Select__c
                ||  nObj.ApplyList__c != oObj.ApplyList__c
                ||  nObj.InspectionCard__c != oObj.InspectionCard__c
                ||  nObj.ScanDate__c != oObj.ScanDate__c
                ||  nObj.ScanDate_select__c  != oObj.ScanDate_select__c 
                ) 
            ){
                FrameNumManageMap.put(nObj.Asset__c,nObj);
            }
        }
        if(FrameNumManageMap.size() > 0 ){
            List<Asset> AssetList = [select id,ApplyList__c,InspectionCard__c from Asset where id in :FrameNumManageMap.keySet()];
            List<Asset> upAssetList = new List<Asset>();
            for(Asset ass:AssetList){
                if( (ass.ApplyList__c == null ||
                    (ass.ApplyList__c != null && ass.ApplyList__c != FrameNumManageMap.get(ass.Id).ApplyList_NoHave__c))
                || (ass.InspectionCard__c == null || 
                    (ass.InspectionCard__c != null && ass.InspectionCard__c != FrameNumManageMap.get(ass.Id).InspectionCard_NoHave__c))
                ){
                    Asset tempAsset = new Asset();
                    tempAsset.Id = ass.Id;
                    if(ass.ApplyList__c == null ||
                    (ass.ApplyList__c != null && ass.ApplyList__c != FrameNumManageMap.get(ass.Id).ApplyList_NoHave__c)){
                        tempAsset.ApplyList__c = FrameNumManageMap.get(ass.Id).ApplyList_NoHave__c;
                    }
                    if(ass.InspectionCard__c == null || 
                    (ass.InspectionCard__c != null && ass.InspectionCard__c != FrameNumManageMap.get(ass.Id).InspectionCard_NoHave__c)){
                        tempAsset.InspectionCard__c = FrameNumManageMap.get(ass.Id).InspectionCard_NoHave__c;   
                    }

                    upAssetList.add(tempAsset);
                }
            }
            if(upAssetList.size() > 0){
                update upAssetList;
            }
        }
    }
    private Void IsChange(){ 
        for (FrameNumManage__c nObj : newList) {
            FrameNumManage__c oObj = oldMap.get(nObj.Id);
            if((nObj.ApplyList__c != oObj.ApplyList__c)
            ||(nObj.InspectionCard__c != oObj.InspectionCard__c)
             ){
                nObj.IsChange__c = true;
            }
        }
    }
}