package com.palmnest.service.notice.impl;

import com.palmnest.application.CodeConstants;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.*;
import com.palmnest.application.dto.output.AuditDto;
import com.palmnest.application.dto.output.PushSendDto;
import com.palmnest.application.dto.output.PushUserDto;
import com.palmnest.dao.OrganizationDao;
import com.palmnest.dao.UserDao;
import com.palmnest.dao.estateserve.AppUserDao;
import com.palmnest.dao.estateserve.VirtualArchDao;
import com.palmnest.dao.notice.EmailSendDao;
import com.palmnest.model.User;
import com.palmnest.model.estateserve.AppUser;
import com.palmnest.model.notice.EmailConfigModel;
import com.palmnest.model.notice.EmailSendModel;
import com.palmnest.model.notice.NoticeApprovalTypeModel;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.model.property.VirtualArchitecture;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.notice.EmailConfigManager;
import com.palmnest.service.notice.EmailSendManager;
import com.palmnest.service.notice.NoticeApprovalsetManager;
import com.palmnest.service.notice.NoticeSubGroupManager;
import com.sun.mail.util.MailSSLSocketFactory;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import java.io.File;
import java.util.*;

@Slf4j
@Service("emailSendManager")
public class EmailSendManagerImpl extends GenericManagerImpl<EmailSendModel, Integer> implements EmailSendManager {

	private EmailSendDao emailSendDao;

	@Autowired
	public void setRoomInfoDao(EmailSendDao emailSendDao) {
		this.emailSendDao = emailSendDao;
		this.dao = emailSendDao;
	}

	@Autowired
	private UserDao            userDao;
	@Autowired
	private AppUserDao         appUserDao;
	@Autowired
	private OrganizationDao    organizationDao;
	@Autowired
	private VirtualArchDao     virtualArchDao;
	@Autowired
	private EmailConfigManager emailConfigManager;
	@Autowired
	private NoticeApprovalsetManager noticeApprovalsetManager;
	@Autowired
	private NoticeSubGroupManager noticeSubGroupManager;

	@Override
	public Page getDataList(Map<String, Object> map, int pageNo, int pageSize)
			throws Exception {
		Page page=emailSendDao.getDataList(map, pageNo, pageSize);
		List<Map<String, Object>> list=page.getItems();
		if(CollectionUtils.isEmpty(list)){
			return page;
		}
		String roleIds="";
		if(map.containsKey("roleIds") && null != map.get("roleIds") && StringUtils.isNotBlank(map.get("roleIds").toString())){
			roleIds=map.get("roleIds").toString();
		}
		if(StringUtils.isBlank(roleIds)){
			return page;
		}
		String [] roleArr=roleIds.split(",");
		if(null == roleArr || roleArr.length<=0 ){
			return page;
		}
		List<String> roleList = new ArrayList<>();
		roleList = Arrays.asList(roleArr);
		for(int i=0;i<list.size();i++){
			Map<String, Object> resMap=list.get(i);
			resMap.put("isPublish", 0);
			Integer id=null;
			if(resMap.containsKey("id") && null != resMap.get("id") && StringUtils.isNotBlank(resMap.get("id").toString())){
				id=Integer.parseInt(resMap.get("id").toString());
			}
			if(null == id){
				continue;
			}
			
			Integer orgId=null;
			if(resMap.containsKey("orgId") && null != resMap.get("orgId") && StringUtils.isNotBlank(resMap.get("orgId").toString())){
				orgId=Integer.parseInt(resMap.get("orgId").toString());
			}
			//项目为空时不执行多审批相关的数据
			if(null == orgId){
				continue;
			}
			
			Integer approvalTypeId=null;
			if(resMap.containsKey("approvalType") && null != resMap.get("approvalType") && StringUtils.isNotBlank(resMap.get("approvalType").toString())){
				approvalTypeId=Integer.parseInt(resMap.get("approvalType").toString());
			}
			
			Integer state=null;
			if(resMap.containsKey("state") && null != resMap.get("state") && StringUtils.isNotBlank(resMap.get("state").toString())){
				state=Integer.parseInt(resMap.get("state").toString());
			}
			//非未审核状态的直接返回，不需要判断
			if(null != state && 0 == state){
				//不为空说明该项目设置了多级审核权限,则需要判断当前登录的人是否有审核权限
				if(null != approvalTypeId && 0 != approvalTypeId.intValue()){
					Map<String,Object> roleMap=noticeApprovalsetManager.getRoleIdByOrderId(orgId, CodeConstants.APPROVAL_TYPE_NOTICE,CodeConstants.ORDER_TYPE_EMAIL, approvalTypeId, id);
					String roleId="";
					if(roleMap.containsKey("roleId") && null != roleMap.get("roleId") && StringUtils.isNotBlank(roleMap.get("roleId").toString())){
						roleId=roleMap.get("roleId").toString();
					}
					String roleName=roleMap == null ? "" : roleMap.get("roleName") == null ? "" : roleMap.get("roleName") .toString();
					resMap.put("roleId", roleId);
					resMap.put("roleName", roleName);
					//不为空则和当前登录的角色做比对,存在当前的角色中，订单状态为未审核，则代表有审核权限
					if(StringUtils.isNotBlank(roleId) && roleList.contains(roleId) && 0 == state){
						resMap.put("isApproval", 1);
					}
				}else{
					resMap.put("isPublish",1);
				}
			}
			
		}
		return page;
	}

	@Override
	public EmailSendModel saveOrUpdate(EmailSendModel emailSendModel) {
		if (emailSendModel.getId() == null) {
			emailSendModel = emailSendDao.save(emailSendModel);
		} else {
			emailSendDao.updateObj(emailSendModel);
		}
		return emailSendModel;
	}


	@Override
	public Page getEmailUser(String orgIds)
			throws Exception {

		return emailSendDao.getEmailUser(orgIds);
	}

	@Override
	public Integer getTotal(String orgIds) {
		return emailSendDao.getTotal(orgIds);
	}


	@Async
	public void sendMesseg(String id, String userId, String type, String state) {
		String title   = "";
		String content = "";
		if (StringUtils.isNotEmpty(userId)) {
			String code = "";
			if ("1".equals(type)) {//app公告
				code = CodeConstants.APP_NOTICE_PUSH_CODE;
				title = "您发起的app公告已" + state + "。";
				content = title;
			} else if ("2".equals(type)) {//微信
				code = CodeConstants.WEIXIN_PUSH_CODE;
				title = "您发起的微信消息已" + state + "。";
				content = title;
			} else if ("3".equals(type)) {//短信
				code = CodeConstants.MESSAGE_PUSH_CODE;
				title = "您发起的短信消息已" + state + "。";
				content = title;
			} else if ("4".equals(type)) {//邮箱
				code = CodeConstants.EMAIL_PUSH_CODE;
				title = "您发起的邮箱消息已" + state + "。";
				content = title;
			}

			List<User> userList = new ArrayList<User>();
			if (userDao.exists(Long.parseLong(userId))) {
				User user = userDao.get(Long.parseLong(userId));
				userList.add(user);
			}
			try {
				if (userList != null && userList.size() > 0) {
					PushMessageHelper helper=new PushMessageHelper();
					helper.pushMessage(Long.parseLong(id), code, userList, title, content);
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error(e.getMessage());
			}
		}
	}

	@Override
	public PushSendDto getDetailInfo(Integer id) {
		PushSendDto dto = new PushSendDto();
		try {
			if (emailSendDao.exists(id)) {
				EmailSendModel pmm = emailSendDao.get(id);
				if (pmm != null) {
					String         orgName = "";
					String         orgId   = "";
					TbOrganization orgs    = organizationDao.getOrganizationById(pmm.getOrgId());
					if (orgs != null) {
						orgId = pmm.getOrgId().toString();
						orgName = orgs.getName();
					}
					dto.setId(id);
					dto.setOrgId(orgId);
					dto.setApprovalType(pmm.getApprovalType());
					
					String approvalTypeName="";
					if(null != pmm.getApprovalType() && 0 != pmm.getApprovalType().intValue()){
						NoticeApprovalTypeModel typeModel=noticeApprovalsetManager.findById(pmm.getApprovalType());
						if(null != typeModel){
							approvalTypeName=typeModel.getName();
						}
					}
					
					dto.setApprovalTypeName(approvalTypeName);
					
					dto.setContent(pmm.getContentTxt());
					dto.setEmailCentent(pmm.getEmail_centent());
					if (pmm.getCreateTime() != null) {
						dto.setEditTime(DateHelper.formatDate(pmm.getCreateTime(), DateHelper.DATE_TIME_STRING));
					}
					//设置编辑人
					if (StringUtil.isNotEmpty(pmm.getCreator()) && NumberUtils.isInteger(pmm.getCreator()) && userDao.exists(Long.parseLong(pmm.getCreator()))) {
						User user = userDao.get(Long.parseLong(pmm.getCreator()));
						if (user != null) {
							dto.setEditUser(user.getUsername());
						}
					} else {
						dto.setEditUser(pmm.getCreator());
					}
					dto.setState(pmm.getState().toString());
					String auditor = "";
					if (StringUtil.isNotEmpty(pmm.getApproval_user()) && NumberUtils.isInteger(pmm.getApproval_user()) && userDao.exists(Long.parseLong(pmm.getApproval_user()))) {
						User user = userDao.get(Long.parseLong(pmm.getApproval_user()));
						if (user != null) {
							auditor = user.getUsername();
						}
					} else {
						auditor = pmm.getApproval_user();
					}
					dto.setAuditor(auditor);
					dto.setTitle(pmm.getEmail_theme());
					if (pmm.getApproval_time() != null) {
						dto.setAuditTime(DateHelper.formatDate(pmm.getApproval_time(), DateHelper.DATE_TIME_STRING));
					}
					dto.setAttachment(pmm.getAttach_ment());//附件路径
					dto.setIsPass(pmm.getIsPass());//（0不通过1通过）
					dto.setAuditOpinion(pmm.getAuditOpinion());
					if (pmm.getEmailaccount_id() != null) {
						dto.setAccountId(pmm.getEmailaccount_id().toString());
					}
					dto.setSendEmail(pmm.getSsender_email());
					dto.setType(2);
					List<AuditDto> auditList=new ArrayList<AuditDto>();
					if(null != pmm && null != pmm.getApprovalType() && 0 != pmm.getApprovalType().intValue()){
						auditList=noticeApprovalsetManager.getAuditList(pmm.getOrgId(), CodeConstants.ORDER_TYPE_EMAIL, pmm.getId());
					}else{
						if(StringUtils.isNotBlank(dto.getAuditTime())){
							AuditDto auditDto=new AuditDto();
							auditDto.setApprovalOpinion(dto.getAuditOpinion());
							if(null != dto.getIsPass() && 1 == dto.getIsPass().intValue()){
								auditDto.setApprovalResult(1);
							}else{
								auditDto.setApprovalResult(2);
							}
							auditDto.setApprovalTime(dto.getAuditTime());
							auditDto.setApprovalUser(auditor);
							auditList.add(auditDto);
						}
					}
					dto.setAuditList(auditList);
					//设置目标用户
					List<PushUserDto> userList = new ArrayList<PushUserDto>();
					if (StringUtil.isNotEmpty(pmm.getUserIds())) {
						String[] userArr = pmm.getUserIds().split(",");
						if (userArr != null && userArr.length > 0) {
							for (int i = 0; i < userArr.length; i++) {
								if (appUserDao.exists(Integer.parseInt(userArr[i]))) {
									AppUser user = appUserDao.get(Integer.parseInt(userArr[i]));
									if (user != null) {
										PushUserDto userDto = new PushUserDto();
										userDto.setId(userArr[i]);
										userDto.setName(user.getUserName());
										userDto.setContactWay(user.getEmail());
										System.out.println("项目：" + user.getOrganizationName() + "、企业:" + user.getVirtualArchName());
										//项目名
										if (user.getOrganizationId() != null) {
											TbOrganization org = organizationDao.getOrganizationById(user.getOrganizationId());
											if (org != null) {
												userDto.setOrgName(org.getName());
											}
										}
										//企业名
										if (user.getVirtualArchId() != null && virtualArchDao.exists(user.getVirtualArchId())) {
											VirtualArchitecture vir = virtualArchDao.get(user.getVirtualArchId());
											if (vir != null) {
												userDto.setVirName(vir.getName());
											}
										}
										userList.add(userDto);
									}
								}
							}
						}
					} else {//公告部分发布的
						if (StringUtil.isNotEmpty(pmm.getRecipient_email())) {
							String[] emailArr = pmm.getRecipient_email().split(";");
							for (int i = 0; i < emailArr.length; i++) {
								PushUserDto userDto = new PushUserDto();
								userDto.setContactWay(emailArr[i]);
								userDto.setOrgName(orgName);
								userList.add(userDto);
							}
						}
					}
					dto.setUserList(userList);
					if (userList != null) {
						dto.setCount(userList.size());
					}
					
					dto.setReceiveType(pmm.getReceiveType());
					//类型：1-公告;2-微信模板;3-短信;4-邮件;
					dto.setDeptGroupList(noticeSubGroupManager.getDeptGroupDto(4, pmm.getId()));
					
				}
			}
		} catch (Exception e) {
			log.error("获取邮箱消息详情失败(getDetailInfo):" + e.getMessage());
			e.printStackTrace();
		}
		return dto;
	}

	@Override
	public void sendMessage(List<AppUser> users, String msgContent) {

		try {

			for (AppUser user : users) {
				// 如果该用户邮箱账号为空或者格式不正确则跳过
				if (StringUtils.isBlank(user.getEmail()) || !RegularPattern.checkEmail(user.getEmail()))
					continue;

				// 获取项目邮箱配置
				EmailConfigModel emailConfigModel = emailConfigManager.getEmailByOrg(user.getOrganizationId() + "");

				// 发送邮件
				JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
				senderImpl.setHost(emailConfigModel.getSmtp_server());
				senderImpl.setPort(emailConfigModel.getStmp_port());
				senderImpl.setUsername(emailConfigModel.getEmail_address());
				senderImpl.setPassword(emailConfigModel.getPassword());
				senderImpl.setDefaultEncoding("UTF-8");
				Properties           prop = new Properties();
				MailSSLSocketFactory sf   = new MailSSLSocketFactory();
				sf.setTrustAllHosts(true);
				prop.put("mail.smtp.ssl.enable", "true");
				prop.put("mail.smtp.ssl.socketFactory", sf);
				prop.put("mail.smtp.auth", "true");
				prop.put("mail.smtp.timeout", "20000");
				senderImpl.setJavaMailProperties(prop);
				MimeMessage       mail   = senderImpl.createMimeMessage();
				MimeMessageHelper helper = new MimeMessageHelper(mail, true, "UTF-8");
				helper.setTo(user.getEmail()); // 收件人
				if (StringUtils.isNotEmpty(emailConfigModel.getSend_email_name())) {
					helper.setFrom(emailConfigModel.getEmail_address(), emailConfigModel.getSend_email_name()); // 发件人_发信名称
				} else {
					helper.setFrom(emailConfigModel.getEmail_address()); // 发件人
				}

				StringBuilder html = new StringBuilder();
				html.append("<html>");
				html.append("<body>");
				html.append(msgContent); // 邮件内容
				html.append("</body>");
				html.append("</html>");
				// 保存至收件箱start
				helper.setSubject("");
				helper.setText(html.toString(), true);
				senderImpl.send(mail);

				EmailSendModel emailSend = new EmailSendModel();
				emailSend.setState(0);
				emailSend.setCreateTime(new Date());
				emailSend.setSend_time(new Date());
				emailSend.setIsEnabled("1");
				emailSend.setOrgId(user.getOrganizationId());
				emailSend.setSsender_email(emailConfigModel.getEmail_address());// 发件账号
				emailSend.setEmail_theme("");// 邮件主题
				emailSend.setRecipient_email(user.getEmail());// 收件人邮箱
				emailSend.setAttach_ment("");// 附件
				emailSend.setEmailaccount_id(emailConfigModel.getId());
				emailSend.setEmail_centent(msgContent);// 邮件内容 带html标签格式的内容
				emailSend.setContentTxt(msgContent);// 纯文本内容
				emailSend = saveOrUpdate(emailSend);

			}

		} catch (Exception e) {
			log.error("EmailSendManagerImpl sendMessage =========>" + e.getMessage());
		}
	}

	@Async
	public void sendEmail(EmailSendModel emailSend) {
		try {
			EmailConfigModel   emailConfigModel = emailConfigManager.get(emailSend.getEmailaccount_id());
			JavaMailSenderImpl senderImpl       = new JavaMailSenderImpl();
			senderImpl.setHost(emailConfigModel.getSmtp_server());
			senderImpl.setPort(emailConfigModel.getStmp_port());
			senderImpl.setUsername(emailConfigModel.getEmail_address());
			senderImpl.setPassword(emailConfigModel.getPassword());
			senderImpl.setDefaultEncoding("UTF-8");
			Properties           prop = new Properties();
			MailSSLSocketFactory sf   = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);
			prop.put("mail.smtp.ssl.enable", "true");
			prop.put("mail.smtp.ssl.socketFactory", sf);
			prop.put("mail.smtp.auth", "true");
			prop.put("mail.smtp.timeout", "20000");
			senderImpl.setJavaMailProperties(prop);
			MimeMessage       mail   = senderImpl.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mail, true, "UTF-8");
			if (StringUtils.isNotEmpty(emailConfigModel.getSend_email_name())) {
				helper.setFrom(emailConfigModel.getEmail_address(), emailConfigModel.getSend_email_name()); // 发件人_发信名称
			} else {
				helper.setFrom(emailConfigModel.getEmail_address()); // 发件人
			}
			if (emailSend.getRecipient_email().indexOf(";") != -1) {
				helper.setTo(emailSend.getRecipient_email().split(";")); // 收件人
			} else {
				helper.setTo(emailSend.getRecipient_email()); // 收件人
			}

			StringBuilder html = new StringBuilder();
			html.append("<html>");
			html.append("<body>");
			html.append(emailSend.getEmail_centent()); // 邮件内容
			html.append("</body>");
			html.append("</html>");
			// 保存至收件箱start
			helper.setSubject(emailSend.getEmail_theme());
			helper.setText(html.toString(), true);
			//发送附件
			String attachMentstr = emailSend.getAttach_ment();
			String rootPath      = FileUploadUtils.getATTACHMENT_ROOT();
			if (StringUtils.isNotEmpty(attachMentstr)) {
				for (int i = 0; i < attachMentstr.split(",").length; i++) {
					String array_element = attachMentstr.split(",")[i];
					if (!array_element.equals("")) {
						String addr     = array_element.split(";")[0];
						String fileName = array_element.split(";")[1];
						fileName = MimeUtility.encodeWord(fileName, "GBK", null);
						helper.addAttachment(fileName, new File(rootPath + "/" + addr));
					}
				}
			}

			senderImpl.send(mail);
		} catch (Exception e) {
			log.error("发送邮件失败：" + e.getMessage());
			e.printStackTrace();
		}

	}

	@Async
	public void sendEmail(List<AppUser> users, String title, String content) {
		try {
			for (AppUser user : users) {
				// 如果该用户邮箱账号为空或者格式不正确则跳过
				if (StringUtils.isBlank(user.getEmail()) || !RegularPattern.checkEmail(user.getEmail())) {
					continue;
				}
				// 获取项目邮箱配置
				EmailConfigModel emailConfigModel = emailConfigManager.getEmailByOrg(user.getOrganizationId() + "");
				// 发送邮件
				JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
				senderImpl.setHost(emailConfigModel.getSmtp_server());
				senderImpl.setPort(emailConfigModel.getStmp_port());
				senderImpl.setUsername(emailConfigModel.getEmail_address());
				senderImpl.setPassword(emailConfigModel.getPassword());
				senderImpl.setDefaultEncoding("UTF-8");
				Properties           prop = new Properties();
				MailSSLSocketFactory sf   = new MailSSLSocketFactory();
				sf.setTrustAllHosts(true);
				prop.put("mail.smtp.ssl.enable", "true");
				prop.put("mail.smtp.ssl.socketFactory", sf);
				prop.put("mail.smtp.auth", "true");
				prop.put("mail.smtp.timeout", "20000");
				senderImpl.setJavaMailProperties(prop);
				MimeMessage       mail   = senderImpl.createMimeMessage();
				MimeMessageHelper helper = new MimeMessageHelper(mail, true, "UTF-8");
				helper.setTo(user.getEmail()); // 收件人
				if (StringUtils.isNotEmpty(emailConfigModel.getSend_email_name())) {
					helper.setFrom(emailConfigModel.getEmail_address(), emailConfigModel.getSend_email_name()); // 发件人_发信名称
				} else {
					helper.setFrom(emailConfigModel.getEmail_address()); // 发件人
				}

				StringBuilder html = new StringBuilder();
				html.append("<html>");
				html.append("<body>");
				html.append(content); // 邮件内容
				html.append("</body>");
				html.append("</html>");
				// 保存至收件箱start
				helper.setSubject(title);
				helper.setText(html.toString(), true);
				senderImpl.send(mail);
			}
		} catch (Exception e) {
			log.error("EmailSendManagerImpl sendEmail =========>" + e.getMessage());
			e.printStackTrace();
		}
	}


//	@Override
//	public Map<String, Object> saveImportPushBillData(InputStream ins) throws Exception {
//		
//		Map<String,Object> retMap=new HashMap<String,Object>();
//		List<Object> list=new ArrayList<Object>();
//		int bugCount=0;
//		StringBuffer bugStr=new StringBuffer("");
//		Sheet sheet=null;
//		try {
//			
//			Workbook workbook = WorkbookFactory.create(ins);  
//			sheet=workbook.getSheet("PushBill");
//		} catch (Exception e) {
//			//LOG.error(e.getMessage());
//			bugStr.append("模板格式有问题，请下载最新的模板");
//		}
//		if(sheet==null){
//			bugStr.append("模板格式有问题，请下载最新的模板");
//		}else{
//			//具体实现业务
//			int rows=sheet.getLastRowNum();
//			//如果大于2000行数据
//			if(rows>20000){
////				bugStr.append("数据量太大，不能大于2w行");
//			}
//			else if(rows<1){
//				bugStr.append("数据为空，请录入账单信息");
//			}
//			else{
//				for(int i=1;i<=rows;i++){
//					if(sheet.getRow(i)==null){
//						continue;
//					}
//					//单元格格式判断以及数据的准确性判断
//					//楼宇
//					//查询楼宇是否存在？
//					if(sheet.getRow(i).getCell(2)==null){
//						bugStr.append("第"+i+"行第3列数据有问题，请检查，问题：楼宇信息必填，请导出系统模板匹配录入<br/>");
//						bugCount++;
//						continue;
//					}
//					sheet.getRow(i).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
//					
//					if(StringUtils.isBlank(sheet.getRow(i).getCell(2).getStringCellValue())){
//						bugStr.append("第"+i+"行第3列数据有问题，请检查，问题：楼宇信息必填，请导出系统模板匹配录入<br/>");
//						bugCount++;
//						continue;
//					}
//					String orgName=sheet.getRow(i).getCell(2).getStringCellValue();
//					TbOrganization tbOrganization=organizationDao.getOrganizationInfoByName(orgName);
//					if(tbOrganization==null){
//						bugStr.append("第"+i+"行第3列数据有问题，请检查，问题：楼宇信息不存在，请导出系统模板匹配录入<br/>");
//						bugCount++;
//						continue;
//					}
//					
//					if(sheet.getRow(i).getCell(3)==null){
//						bugStr.append("第"+i+"行第4列数据有问题，请检查，问题：房间必填，请导出系统模板匹配录入<br/>");
//						bugCount++;
//						continue;
//					}
//					sheet.getRow(i).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
//					
//					if(StringUtils.isBlank(sheet.getRow(i).getCell(3).getStringCellValue())){
//						bugStr.append("第"+i+"行第4列数据有问题，请检查，问题：房间必填，请导出系统模板匹配录入<br/>");
//						bugCount++;
//						continue;
//					}
//					String roomName=sheet.getRow(i).getCell(3).getStringCellValue();
//					
//					//企业编码
//					if(sheet.getRow(i).getCell(4)==null){
//						bugStr.append("第"+i+"行第5列数据有问题，请检查，问题：企业编码必填，请导出系统模板匹配录入<br/>");
//						bugCount++;
//						continue;
//					}
//					sheet.getRow(i).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
//					if(StringUtils.isBlank(sheet.getRow(i).getCell(4).getStringCellValue())){
//						bugStr.append("第"+i+"行第5列数据有问题，请检查，问题：企业编码必填，请导出系统模板匹配录入<br/>");
//						bugCount++;
//						continue;
//					}
//					String code=sheet.getRow(i).getCell(4).getStringCellValue();
//					List<VirtualArchitecture> virList=virtualArchDao.getVirByCode(code);
//					if(virList==null||virList.size()<=0){
//						bugStr.append("第"+i+"行第5列数据有问题，请检查，问题：企业编码不存在，请导出系统模板匹配录入<br/>");
//						bugCount++;
//						continue;
//					}
//					PushBill pushBillInfo=new PushBill();
//					VirtualArchitecture virInfo=virList.get(0);
//					if(virInfo==null){
//						bugStr.append("第"+i+"行第5列数据有问题，请检查，问题：企业信息不存在，请导出系统模板匹配录入<br/>");
//						bugCount++;
//						continue;
//					}
//					if(sheet.getRow(i).getCell(5)==null){
//						bugStr.append("第"+i+"行第6列数据有问题，请检查，问题：企业名称必填，请导出系统模板匹配录入<br/>");
//						bugCount++;
//						continue;
//					}
//					sheet.getRow(i).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
//					if(StringUtils.isBlank(sheet.getRow(i).getCell(5).getStringCellValue())){
//						bugStr.append("第"+i+"行第6列数据有问题，请检查，问题：企业名称必填，请导出系统模板匹配录入<br/>");
//						bugCount++;
//						continue;
//					}
//					String virName=sheet.getRow(i).getCell(5).getStringCellValue();
//					//根据企业查出相关负责人，然后将负责人信息返回
//					List<AppUser> appUserList=virtualArchDao.getAppUserByVirAndIsRes(virInfo.getId());
//					AppUser user1=new AppUser();
//					AppUser user2=new AppUser();
//					if(appUserList.size()>0){
//						for(int j=0;j<appUserList.size();j++){
//							AppUser user=appUserList.get(j);
//							if(user.getIsPushPower()==1){
//								if(user.getReOrder()==1){
//									user1=user;
//								}else if(user.getReOrder()==2){
//									user2=user;
//								}
//							}
//							
//						}
//					}
//					pushBillInfo.setVirtId(Long.parseLong(virInfo.getId().toString()));
//					pushBillInfo.setCreateTime(new Date());
//					//账单的模板根据负责人的信息判断发送方式
//					pushBillInfo.setPushUser1(user1.getUserName());
//					pushBillInfo.setPushUser2(user2.getUserName());
//					pushBillInfo.setUserEmail1(user1.getEmail());
//					pushBillInfo.setUserEmail2(user2.getEmail());
//					pushBillInfo.setUserTel1(user1.getUserMobile());
//					pushBillInfo.setUserTel2(user2.getUserMobile());
//					//推送金额---Excel中获取
//					double pushAmount=sheet.getRow(i).getCell(23).getNumericCellValue();
//					pushBillInfo.setPushAmount(pushAmount);
//					pushBillInfo.setIsEmail(virInfo.getIsEmail());
//					pushBillInfo.setIsMessage(virInfo.getIsMessage());
//					pushBillInfo.setIsWechat(virInfo.getIsWechat());
//					
//					pushBillInfo.setRoomName(roomName);
////					pushBillInfo.setWechatData("");这些内容在发送的时候再设置
////					pushBillInfo.setEmailData("");
////					pushBillInfo.setMessageData("");
////					pushBillInfo.setBillUpDate(new Date());//-----需修改
//					
//					Map<String,Object> map=new HashMap<String, Object>();
//					map.put("pushBillInfo", pushBillInfo);
//					map.put("virInfo", virInfo);
//					map.put("orgName", orgName);
//					map.put("virName", virName);
//					map.put("floorNames", virInfo.getFloorNames());
//					map.put("roomName", roomName);
//					map.put("roomNames", virInfo.getRoomNames());
//					list.add(map);
//				}
//			}
//		}
//		retMap.put("bugCount", bugCount);
//		retMap.put("bugStr", bugStr.toString());
//		retMap.put("list", list);
//		retMap.put("okCount",list.size());
//		return retMap;
//	}
//
//	
}
