global class AssetHandlerCheckQueuable implements Queueable  {
    String recIds;
    public AssetHandlerCheckQueuable(String recIds){
        this.recIds = recIds;
    }

    global void execute(QueueableContext context) {
        checkAsset2(this.recIds);
    }
    private static void checkAsset2(String ids) {
        List<Asset> newList = [SELECT Id
            , Ji_Zhong_Guan_Li_Ku_Cun__c
            , Out_of_wh__c
            , Out_of_wh_Sys__c
            , Rental_Count__c
            , Rental_Count_Sys__c
            , LastModifiedBy.Name
            , LastModifiedDate
            , Name
            FROM Asset WHERE Id IN:ids.split(',')
         ];
         System.debug(newList);
        List<String> rentalErrors = new List<String>();
        List<String> jizhongErrors = new List<String>();

        for (Asset nObj : newList) {
            if(intValueOf(nObj.Out_of_wh__c) != intValueOf(nObj.Out_of_wh_Sys__c)
                || intValueOf(nObj.Rental_Count_Sys__c) != intValueOf(nObj.Rental_Count__c)) {
                String s = '';
                s += '名称:'+nObj.Name + '\n';
                s += 'Id:'+nObj.Id + '\n';
                s += '借出_分配_数:'+nObj.Out_of_wh__c + '\n';
                s += '借出_分配_数(Sys):'+nObj.Out_of_wh_Sys__c + '\n';
                s += '已借出数:'+nObj.Rental_Count__c + '\n';
                s += '已借出数(Sys):'+nObj.Rental_Count_Sys__c + '\n';
                s += '修改人:'+nObj.LastModifiedBy.Name + '\n';
                s += '修改时间:'+ String.valueOf(nObj.LastModifiedDate) + '\n';
                rentalErrors.add(s);
            }
            if(nObj.Ji_Zhong_Guan_Li_Ku_Cun__c < 0) {
                String s = '';
                s += '名称:'+nObj.Name + '\n';
                s += 'Id:'+nObj.Id + '\n';
                s += '集中管理库存:'+nObj.Ji_Zhong_Guan_Li_Ku_Cun__c + '\n';
                s += '修改人:'+nObj.LastModifiedBy.Name + '\n';
                s += '修改时间:'+ String.valueOf(nObj.LastModifiedDate) + '\n';
                jizhongErrors.add(s);
            }

        }
        System.debug(rentalErrors);
        System.debug(jizhongErrors);

        if(!rentalErrors.isEmpty()) {
            sendEmail('已分配数或已借出数异常', String.join(rentalErrors,'\n'));
        }
        if(!jizhongErrors.isEmpty()) {
            sendEmail('集中管理库存异常', String.join(jizhongErrors,'\n'));
        }

    }
    private static Integer intValueOf(Decimal d) {
        if(d == null) {
            return 0;
        }
        return Integer.valueOf(d);
    }
    private static void sendEmail(String title , String bodyText) {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            String[] toAddresses = System.Label.AssetCountErrorMailAddress.split(',');

            mail.setToAddresses(toAddresses);
            mail.setReplyTo(UserInfo.getUserEmail());
            mail.setSubject(title);
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setPlainTextBody(bodyText);

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}