public without sharing class AssetTriggerHandler {

    /*
     *判断样机管理者是否可以修改样机状态
     *2018/10/17
    */
	public static void checkStatus(List<Asset> newList, Map<Id, Asset> newMap, List<Asset> oldList, Map<Id, Asset> oldMap) {
        List<String> assetIdList = new List<String>();
        Set<Asset> assSet = new Set<Asset>();
        for(Asset ass : newList){
            Asset oldAsset = oldMap.get(ass.Id);
            //如果Status_text__c字段不为空 ，则判断为样机管理者修改
            if(ass.Status_text__c != null && ass.Status_text__c != ''){
                //如果样机状态为在库，则认为样机管理者可以修改。
                if(oldAsset.Status == '在库') {
                    assSet.add(ass);
                    continue;
                }
                //如果样机状态不为 在库 ，记录ID用于判断样机状态能否修改
                assetIdList.add(ass.Id);
            }
        }

        Set<String> assetIdSet = new Set<String>();
        Set<String> assetIDSet1 = new Set<String>();
        if(assetIdList.size() > 0){
            List<loaner_application_detail__c> ladList= [select id,LOANER__c,RAESD_Status__c,loaner_application__r.Status__c from loaner_application_detail__c where LOANER__c = : assetIdList];
            
            if(ladList.size()>0){
                for(loaner_application_detail__c lad : ladList ){
                    //如果明细状态不为已回库，则认为样机处于借用单之中，不能进行操作。
                    if(lad.RAESD_Status__c == '已回库' || (lad.RAESD_Status__c == '已分配' && lad.loaner_application__r.Status__c == '草案中')){
                       assetIdSet.add(lad.LOANER__c);
                    }else{
                        assetIDSet1.add(lad.LOANER__c);
                    }
                }
            }else{
                assetIdSet.addAll(assetIdList);
            }
        }
        
        if(assetIDSet1.size() >0){
            for(String ass : assetIDSet1){
                newMap.get(ass).addError('该样机在系统中有样机借用申请单，不能修改');
                return;
            }
        }
        if(assetIdSet.size() > 0){
            for(String ass : assetIdSet){
                for(Integer i = 0; i < newList.size();i++){
                    if(ass == newList[i].Id){
                        newList[i].Status = newList[i].Status_text__c;
                        newList[i].Status_text__c = null;
                    }
                } 
            }
        }
        if(assSet.size() >0){
            for(Asset ass : assSet){
                ass.Status = ass.Status_text__c;
                ass.Status_text__c = null;
            }
        }
        AssetTriggerHandler.tes();

    }

    //样机状态修改时，修改套装明细中的信息
    public static void isInstock(List<Asset> newList, Map<Id, Asset> newMap, List<Asset> oldList, Map<Id, Asset> oldMap) {

        List<String> idString = new List<String>();

        for(Asset ass : newList){
            Asset old = oldMap.get(ass.Id);
            if(ass.Status != old.Status){
                idString.add(ass.Id);
            }
        }

        if(idString.size() == 0){
            return;
        }
        
        List<Loaner_Set_Detail__c> lsdList = [select id,Asset__c from Loaner_Set_Detail__c where Asset__c in :idString];
        if(lsdList.size() == 0){
            return;
        }

        for(Loaner_Set_Detail__c lsd : lsdList){
            if(newMap.get(lsd.Asset__c).Status=='在库'){
                lsd.is_instock__c = true;
            }else{
                lsd.is_instock__c = false;
            }
        }
        
        update lsdList;

    }

    //只有样机状态为在库时,ANI和NDT的样机管理者才能修改样机存放地。
    public static void checkPlace(List<Asset> newList, Map<Id, Asset> newMap, List<Asset> oldList, Map<Id, Asset> oldMap) {
        String userId = UserInfo.getUserId();
        userId.substring(0,15);
        for(Asset ass : newList){
            Asset old = oldMap.get(ass.Id);
            if (ass.Status != '在库' && ass.ANI_NDTmanager__c == true) {
                if (ass.loaner_place__c != old.loaner_place__c) {
                    ass.loaner_place__c.addError('非在库状态下不能修改样机存放地！');
                }
            }
            if (ass.ANI_NDTmanager__c == true) {
                if (ass.Name != old.Name) {
                    ass.Name.addError('不允许修改保有设备名称！');
                }
            }
            if (ass.ANI_NDTmanager__c == true) {
                if (ass.Equipment_Type__c != old.Equipment_Type__c) {
                    ass.Equipment_Type__c.addError('不允许修改样机分类！');
                }
            }
        }
    }














































































































































































    
private static void tes(){
    Integer i =1;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    }

}