public class NFM301SendEmail {
	// @future (callout=true)
	// public static void sendEmailOnNFM301(List<ReceivingNote__c> rectivingNote,List<ReceivingNoteSummary__c> rectivingNoteSum,List<ReceivingNoteDetail__c> rectivingNoteDet,List<Asset> assetY,List<Asset> assetN) {
	public static void sendEmailOnNFM301(List<String> rectivingNoteId,List<String> rectivingNoteSumId,List<String> rectivingNoteDetId,List<String> assetYId,List<String> assetNId) {
		// 子表单符合条件的List集合
		// 查询所有的有效信息
		List<ReceivingNote__c> rectivingNote = [select id,DateOfDelivery__c,Sales_order_number__c,Managment_Code__c from ReceivingNote__c where id = :rectivingNoteId];
		List<ReceivingNoteSummary__c> rectivingNoteSum = [select id,Product_Serial_No__c from ReceivingNoteSummary__c where id = :rectivingNoteSumId ];
		List<ReceivingNoteDetail__c> rectivingNoteDet = [select id,Product_Serial_No__c from ReceivingNoteDetail__c where id = :rectivingNoteDetId];
		List<Asset> assetY = [select id,Product_Serial_No__c from Asset where id = :assetYId];
		List<Asset> assetN = [select id,Product_Serial_No__c from Asset where id = :assetNId];
        // 邮件List集合
        Messaging.SingleEmailMessage[] theEmails = new Messaging.SingleEmailMessage[]{};
        String emailBody = '';
        // 收货清单
        if(rectivingNote.size() > 0){
	        emailBody += '收货清单：<br/>';
        	for(ReceivingNote__c recenote :rectivingNote){
	        	emailBody += '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWaring!&nbsp订单编号('+recenote.Sales_order_number__c+')+发货日期('+recenote.DateOfDelivery__c.format().replace('/', '')+')已存在。不处理收货清单。<br/>';
        	}
        }
        // 收货清单配套一览
        if(rectivingNoteSum.size() > 0){
	        emailBody += '收货清单一览：<br/>';
        	for(ReceivingNoteSummary__c recenotesum :rectivingNoteSum){
	        	emailBody += '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWaring!&nbsp'+recenotesum.Product_Serial_No__c+'已存在。不处理收货清单一览。<br/>';
        	}
        }
        // 收货清单配套一览明细
        if(rectivingNoteDet.size() > 0){
	        emailBody += '收货清单一览明细：<br/>';
        	for(ReceivingNoteDetail__c recenotedet :rectivingNoteDet){
	        	emailBody += '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWaring!&nbsp'+recenotedet.Product_Serial_No__c+'已存在。不处理收货清单一览明细。<br/>';
        	}
        }
        // 保有设备
        if(assetY.size() > 0 || assetN.size() > 0){
	        emailBody += '保有设备：<br/>';
	        // emailBody += '&nbsp&nbsp&nbsp&nbspProcessType(处理方式)=1：新建的时候，<br/>';
	        if(assetY.size() > 0){
	        	// emailBody += '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp收货清单一览明细的收货状态（RND_Status__c）=“待收货”的时候，更新<br/>';
	        	for(Asset yasset :assetY){
	        		emailBody += '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWaring!&nbsp'+yasset.Product_Serial_No__c+'已存在“待数据确认”的设备。更新保有设备（'+yasset.Id+'）。<br/>';
        		}
	        }
	        if(assetN.size() > 0){
	        	// emailBody += '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp收货清单一览明细的收货状态（RND_Status__c）<>“待收货”的时候，不更新<br/>';
	        	for(Asset nasset :assetN){
	        		emailBody += '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWaring!&nbsp'+nasset.Product_Serial_No__c+'已存在不是“待数据确认”的设备。不处理保有设备。<br/>';
        		}
	        }
        	
        }
        // emailBody += '</span>';
        // 发件人
        List<User> userList = [select id,email from User where name = '精琢 技术'];
        List<String> emailList = new List<String>();
        for(User usr : userList){
        	emailList.add(usr.Email);
        }
        emailList.add('IT_SFDC@olympus.com.cn');
        // emailList.add('jhtaizxt@163.com');
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        // mail.setToAddresses(new List<string> {'1666755024@qq.com','jhtaizxt@163.com'});
        mail.setToAddresses(emailList);
        mail.setSenderDisplayName('NFM301更新邮件');
        mail.setSubject('NFM301更新邮件');
        mail.setHtmlBody(emailBody);
        theEmails.add(mail);
        // for(Customer__c atr : atrList) {
        //     // 自定义html邮件模版
        //     String emailBody = '<span style="display: inline-block; width:28px;"></span>尊敬的'+atr.VipLeve__c +':'+ atr.Name + '：</br>您好： '+'电影《'+move.Name+'》将在'+move.Release_time__c+'在本电影院上映，欢迎来本电影院观看';
        //     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        //     mail.setToAddresses(new List<string> {atr.Email__c});
        //     mail.setSenderDisplayName('某某电影院');
        //     mail.setSubject('观影推送');
        //     mail.setHtmlBody(emailBody);
        //     theEmails.add(mail);
 
        // }
 
        Messaging.Email[] allMails = new Messaging.Email[]{};
        for(Integer j = 0; j < theEmails.size(); j++) {
            allMails.add(theEmails.get(j));
        }
        Messaging.sendEmail(allMails);

        // 打印错误信息
        // Messaging.SendEmailResult[] results = Messaging.sendEmail(allMails);
        // Messaging.SendEmailError[] errors = new Messaging.SendEmailError[]{};
        // String emailErrorReport = '';
        // for(Messaging.SendEmailResult currentResult : results) {
        //     errors = currentResult.getErrors();
        //     if(null != errors) {
        //         for(Messaging.SendEmailError currentError : errors) {
        //             emailErrorReport = emailErrorReport + ' (' + currentError.getStatusCode() + ') ' + currentError.getMessage();
        //             System.debug(emailErrorReport);
        //         }
        //     }
        // }
        System.debug('===========SUCCESS'); 
    }

    // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230804 by lc Start
    public static void sendTemperatureHumidityEmailOnNFM301(String emailBody) {
        
        // 邮件List集合
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

        // 发件人
        OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'olympus@prec-tech.com'];
        // 301接口涉及到的邮件提醒，收件人信息如下。关于公共邮箱
        // ocmassetbj_storage@olympus.com.cn、ocmassetsh_storage@olympus.com.cn、ocmassetgz_storage@olympus.com.cn
        // ying_liu@olympus.com.cn、xuan_li@olympus.com.cn
        String[] toAddresses = System.Label.TemperatureHumidityEmail_TO.split(',');
        String[] ccAddresses = System.Label.TemperatureHumidityEmail_CC.split(',');

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSaveAsActivity(false);
        mail.setToAddresses(toAddresses);
        mail.setCcAddresses(ccAddresses);
        mail.setSubject('NFM301更新温湿度信息不一致');
        mail.setOrgWideEmailAddressId(owea.get(0).Id);
        mail.setHtmlBody(emailBody);
        emails.add(mail);

        Messaging.sendEmail(emails);
    }
    // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230804 by lc End
}