YxySendMailServiceImpl.java 35.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
package com.espeed.service.impl;
  
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.BeanUtils;

import com.espeed.centre.pojo.CentreYxySenderDomain;
import com.espeed.centre.pojo.CentreYxyedmNewcustomer;
import com.espeed.centre.pojo.CentreYxyedmOldcustomer;
import com.espeed.dao.CentreYxySenderDomainDao;
import com.espeed.dao.CentreYxyedmNewcustomerDao;
import com.espeed.dao.CentreYxyedmOldcustomerDao;
import com.espeed.dao.CrmCustomerFollowupDao;
import com.espeed.dao.YxyCompanyWebInfoDao;
import com.espeed.dao.YxyCustomerEmailDao;
import com.espeed.dao.YxyMailCountDao;
import com.espeed.dao.YxyMailDayCountDao;
import com.espeed.dao.YxyMailManyDao;
import com.espeed.dao.YxyMailMonthCountDao;
import com.espeed.dao.YxyNewcustomerCountsDao;
import com.espeed.dao.YxyOldcustomerCountsDao;
import com.espeed.dao.YxySendCountDao;
import com.espeed.dao.YxySendMailDetailDao;
import com.espeed.dao.YxySendMailMasterBaseDao;
import com.espeed.dao.YxySendMailMasterDao;
import com.espeed.dao.YxySendSmtpInfoDao;
import com.espeed.dao.YxySysParamatersValueDao;
import com.espeed.dao.YxyUserAddressDao;
import com.espeed.dao.YxyUserSetDao;
import com.espeed.log.LogClass;
import com.espeed.pojo.YxyMailCount;
import com.espeed.pojo.YxyMailDayCount;
import com.espeed.pojo.YxyMailMonthCount;
import com.espeed.pojo.YxyNewcustomerCounts;
import com.espeed.pojo.YxyOldcustomerCounts;
import com.espeed.pojo.YxySendCount;
import com.espeed.pojo.YxySendMailDetail;
import com.espeed.pojo.YxySendMailMaster;
import com.espeed.pojo.YxySendMailMasterBase;
import com.espeed.pojo.YxySendSmtpInfo;
import com.espeed.pojo.YxyUserAddress;
import com.espeed.pojo.YxyUserSet;
import com.espeed.service.YxySendMailService;
import com.espeed.tool.ConfigPath;
import com.espeed.tool.DateFormat;
import com.espeed.tool.GreateEmlFile;
import com.espeed.tool.WebmailHttpUtil;
import com.espeed.webmail.pojo.CrmCustomerFollowup;

/**
 * 程序名称:    	EspeedMail_时速邮箱
 * 程序版本:    	V1.0
 * 作    者:    	深圳市科飞时速网络技术有限公司(0755-88843776)
 * 版权所有:    	深圳市科飞时速网络技术有限公司
 * 技术支持:    	Tech@21gmail.com
 * 单元名称:     邮件营销待发业务层实现类
 * 开始时间:    	2013.12.24
 * 程 序 员:    	谢勇
 * 最后修改:    
 * 备    注:		如需修改请通知程序员    
 */
public class YxySendMailServiceImpl implements YxySendMailService{
	
	/**查询用户设置信息*/
	public List<YxyUserSet> findUserSet(String loginid,String domain) throws Exception {
		String hql="from YxyUserSet where user_set_loginid='"+loginid+"' and user_companydomain='"+domain+"'";
		return yxyusersetdao.findByHql(hql);
	}
	
	/**判断用户控制参数(发送量的限制)*/
	public int findSendNum(String loginid,String domain,int commitnum,Map usergraph) throws Exception {
		//时间定义
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		String nowdate=df.format(new Date());
		//日发量限制
		int daysendBig=Integer.parseInt(usergraph.get("yxy_day_send_mail_limit").toString());//日最大量	
		//月发量限制
		int monthsendBig=Integer.parseInt(usergraph.get("yxy_month_send_mail_limit").toString());//月最大量	
		//单次最大发送量限制
		int onesendBig=Integer.parseInt(usergraph.get("yxy_single_send_num").toString());//单次最大量	
		//总最大发量(-1无限制)
		int tolBig=Integer.parseInt(usergraph.get("yxy_sum_send").toString());
		//日已发量
		String dayhql="select yxy_day_count_tol_num from YxyMailDayCount where yxy_day_count_loiginid='"+loginid+"' and yxy_day_count_domain='"+domain+"' and yxy_day_count_date='"+nowdate+"'";
		int daysendnum=yxymaildaycountdao.findByHqlCount(dayhql);//当天已发量
		//判断是否满了
		if(daysendBig!=-1&&daysendnum+commitnum>daysendBig){
			return -2;
		}
		//月已发量
		String monthdate=nowdate.substring(0,nowdate.length()-3);
		String monthhql="select yxy_month_count_tol_num from YxyMailMonthCount where yxy_month_count_loginid='"+loginid+"' and yxy_month_count_domain='"+domain+"' and yxy_month_count_date='"+monthdate+"'";
		int monthsendMax=yxymailmonthcountdao.findByHqlCount(monthhql);//当月已发量
		//判断是否满了
		if(monthsendBig!=-1&&monthsendMax+commitnum>monthsendBig){
			return -3;
		}
		//总量
		if(tolBig!=-1){
			String tolhql="select yxy_count_tol_num from YxyMailCount where yxy_count_loginid='"+loginid+"' and yxy_count_domain='"+domain+"'";
			int tolNum=yxymailcountdao.findByHqlCount(tolhql);
			//判断是否满了
			if(tolNum+commitnum>tolBig){
				return -4;                                                                                                                                                                                               
			}
		}
		//单次最大发送量
		if(commitnum>onesendBig){
			return -5; 
		}
		return 1;
		
	}
	
	/**判断用户控制参数(发送量的限制,不是当天的)*/
	public int findSendNumTime(String loginid, String domain, int commitnum,Map usergraph,String timer) throws Exception {	
		timer = timer.split(" ")[0];
		String month = timer.split("-")[1];
		//String month = timer.substring(0,timer.length()-3);
		//日发量限制
		int daysendBig=Integer.parseInt(usergraph.get("yxy_day_send_mail_limit").toString());//日最大量	
		//月发量限制
		int monthsendBig=Integer.parseInt(usergraph.get("yxy_month_send_mail_limit").toString());//月最大量	
		//单次最大发送量限制
		int onesendBig=Integer.parseInt(usergraph.get("yxy_single_send_num").toString());//单次最大量	
		//总最大发量(-1无限制)
		int tolBig=Integer.parseInt(usergraph.get("yxy_sum_send").toString());
		//日已发量
		String dayhql="select yxy_day_count_tol_num from YxyMailDayCount where yxy_day_count_loiginid='"+loginid+"' and yxy_day_count_domain='"+domain+"' and yxy_day_count_date='"+timer+"'";
		int daysendMax=yxymaildaycountdao.findByHqlCount(dayhql);//当天已发量
		//判断是否满了
		if(daysendBig!=-1&&daysendMax+commitnum>daysendBig){
			return -2;
		}
		//月已发量
		String monthsql="select yxy_month_count_tol_num from YxyMailMonthCount where yxy_month_count_loginid='"+loginid+"' and yxy_month_count_domain='"+domain+"' and yxy_month_count_date='"+month+"'";
		int monthsendMax=yxymailmonthcountdao.findByHqlCount(monthsql);//当月已发量
		//判断是否满了
		if(monthsendBig!=-1&&monthsendMax+commitnum>monthsendBig){
			return -3;
		}
		//总量
		if(tolBig!=-1){
			String tolhql="select yxy_count_tol_num from YxyMailCount where yxy_count_loginid='"+loginid+"' and yxy_count_domain='"+domain+"'";
			int tolNum=yxymailcountdao.findByHqlCount(tolhql);
			//判断是否满了
			if(tolNum+commitnum>tolBig){
				return -4;                                                                                                                                                                                               
			}
		}
		//单次最大发送量
		if(commitnum>onesendBig){
			return -5; 
		}
		return 1;
		
	}
	
	/**添加待发邮件与地址*/
	public void addMasterDetail(int user_type,int company_id,int user_id,YxySendMailMaster master,List<YxyUserAddress> address,String uploadattrName,String usersetpath,String setEmlPath,int mailid,int sendci,int uploadtype,String customerid,int jinchengid) throws Exception {
		int masterid=0;
		String loginid=master.getUser_loginid();
		String domain=master.getUser_domain();
		//当前时间
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowDate=df.format(new Date());
		String nowday=nowDate.split(" ")[0];
		//定义邮件基本信息实体
		YxySendMailMasterBase masterbase=new YxySendMailMasterBase();
		//复制对象
		BeanUtils.copyProperties(master,masterbase);
		//地址条数
		int addressNum=address.size();
		//赋值
		masterbase.setSend_num(addressNum);
		//将地址生成excel文件(存放路径:域名+用户名+当前时间)
		if(address!=null){
			//项目路进	
			String pathew = this.getClass().getClassLoader().getResource("/").getPath();
			pathew = pathew.substring(1, pathew.length());
			pathew = pathew.replace("/WEB-INF/classes/","");
			//文件路径
			String savepath=pathew+"\\userSaveFile\\"+domain+"\\"+loginid;
			//生成文件
			File file=new File(savepath);
			file.mkdirs();
			
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("已发邮件地址信息");//在Excel工作簿中建一工作表
			HSSFCell cell = null;//创建列
			HSSFRow row =null;//创建行
			row = sheet.createRow((short) 0);
			//加入文件头
			String [] str={"E-mail","姓名","公司名称","自定义变量"};
			for(int begin=0;begin<str.length;begin++){
				cell = row.createCell((short) begin);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);//如果内容有中文件,则要设置这里
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);// 定义单元格为字符串类型
				cell.setCellValue(str[begin]);
			}
			//加入详细信息
			for(int i=0;i<address.size();i++){
				row = sheet.createRow((short) i+1);
				for(int j = 0; j < 4; j++) {
				  cell = row.createCell((short) j);//创建列
				  cell.setEncoding(HSSFCell.ENCODING_UTF_16);//如果内容有中文件,则要设置这里
				  cell.setCellType(HSSFCell.CELL_TYPE_STRING);// 定义单元格为字符串类型
				  //在单元格中输入一些内容
				  if(j==0){//email
				      cell.setCellValue(address.get(i).getUser_addr_email());
				  }else if(j==1){//姓名
					  cell.setCellValue(address.get(i).getUser_addr_name());
				  }else if(j==2){//公司名称
					  cell.setCellValue(address.get(i).getCompanyname());
				  }else if(j==3){//自定义变量
					  cell.setCellValue(address.get(i).getUser_addr_variable());
				  }
				}  
			}
			SimpleDateFormat dfs=new SimpleDateFormat("yyyyMMddHHmmssSSS");
			String nowFilename=dfs.format(new Date());
			String filename=nowFilename+".xls";
			
			// 新建一输出文件流
			FileOutputStream fOut = new FileOutputStream(savepath+"\\"+filename);		
			workbook.write(fOut);
			fOut.flush();
			fOut.close();
//			writestr.append(masterbase.getCreate_time()+"发送的邮件地址\r\n");
//			for(int i=0;i<address.size();i++){
//				writestr.append(address.get(i).getUser_addr_email()+"\r\n");
//			}
//			//项目路进	
//			String pathew = this.getClass().getClassLoader().getResource("/").getPath();
//			pathew = pathew.substring(1, pathew.length());
//			pathew = pathew.replace("/WEB-INF/classes/","");
//			
//			SimpleDateFormat dfs=new SimpleDateFormat("yyyyMMddHHmmssSSS");
//			String nowFilename=dfs.format(new Date());
//			String filename=nowFilename+".txt";
//			String txtsavepath=pathew+"\\userSaveFile\\"+domain+"\\"+loginid+"\\"+filename;
//			File f=new File(pathew+"\\userSaveFile\\"+domain+"\\"+loginid);
//			if(!f.exists()){
//				f.mkdirs();
//			}
//			createAndDeleteFile.createFile(txtsavepath);
//			//写入文件 
//			FileWriter w = new FileWriter(txtsavepath, true);
//			w.write(writestr.toString());
//			w.close();
			
			masterbase.setTxtaddresspath(filename);
		}
		//插入待发表中
		masterid=yxysendmailmasterdao.addMaster(master);
		 
		//插入邮件信息
		int masterbaseid=yxysendmailmasterbasedao.addPojo(masterbase);
		
		//将此地址库转换为待发地址集合
		List<YxySendMailDetail> detailAddress=new ArrayList<YxySendMailDetail>();
		String suffix="";//地址后缀
		String customeridstr="";
		for(int i=0;i<address.size();i++){
			customeridstr+=address.get(i).getCustomerid()+",";
			YxySendMailDetail o=new YxySendMailDetail();
			o.setUid(master.getUid());//用户id
			o.setBase_mail_id(masterbaseid);//邮件基本信息ID
			o.setSend_mail_id(masterid);//邮件ID
			o.setStatus(0);//发送状态
			o.setEmail(address.get(i).getUser_addr_email());//地址
			o.setReceiver(address.get(i).getUser_addr_name());//收件人姓名
			suffix=address.get(i).getUser_addr_email().split("@")[1];
			o.setEmail_suffix(suffix);//后缀
			o.setVariable(address.get(i).getUser_addr_variable());//主题变量名
			o.setCompany_variable(address.get(i).getCompanyname());//企业名称
			o.setLoginid(master.getUser_loginid());//帐号
			o.setDomain(master.getUser_domain());//域名
			detailAddress.add(o);
		}
		yxysendmaildetaildao.addPojoPi(detailAddress);
		
		
		//更新地址的发送时间与量
		int addressendnum=0;
		int addressid=0;
		for(int a=0;a<address.size();a++){
			addressid=address.get(a).getUser_addr_id();
			if(addressid>0){   
				addressendnum=address.get(a).getUser_send_num()+1;
				String updatehql="update YxyUserAddress set user_send_time='"+nowDate+"',user_send_num="+addressendnum+" where user_addr_id="+addressid;
				//更新
				yxyuseraddressdao.updateByHql(updatehql);
			}
		}
		//生成本地eml文件
		List resulteml=GreateEmlFile.GreateMail(master,loginid,domain,uploadattrName,usersetpath);
		
		int emlsize=Integer.parseInt(resulteml.get(0).toString());//eml文件大小以K存储
		String emlpath=((String)resulteml.get(1));//eml文件存放路径
		String emlkey=(String)resulteml.get(2);//emlKEY
		int isAttr=Integer.parseInt(resulteml.get(3).toString());//是否包含附件
		String picpath=resulteml.get(4).toString();//图片路径	
		//加入本地eml文件信息(待发表)
		master.setEml_encode_key(emlkey);
		master.setEml_file_path(emlpath.replace(setEmlPath, "#####"));
		master.setMail_size(emlsize);
		master.setAttachment_count(isAttr);
		master.setImage_path(picpath);
		//加入本地eml文件信息(邮件基本信息表)
		masterbase.setEml_encode_key(emlkey);
		masterbase.setEml_file_path(emlpath.replace(setEmlPath, "#####"));
		masterbase.setMail_size(emlsize);
		masterbase.setAttachment_count(isAttr);
		masterbase.setImage_path(picpath);
		
		
		//===========日统计=================
		//定时邮件的日期
		if(master.getStatus() == 2){//定时邮件
			nowday=master.getPlans_send_time().split(" ")[0];
		}
		//查询此时间的发送量(日)
		String dayhql="from YxyMailDayCount where yxy_day_count_loiginid='"+loginid+"' and yxy_day_count_domain='"+domain+"' and yxy_day_count_date='"+nowday+"'";
		List<YxyMailDayCount> daycountlist=yxymaildaycountdao.findByHql(dayhql);
		if(daycountlist.size()>0){//有记录则更新
			//历史统计数据
			YxyMailDayCount dayinfo=daycountlist.get(0);
			int tolnum=dayinfo.getYxy_day_count_tol_num();//当天发送总数
			int notcountnum=dayinfo.getYxy_day_count_nocount();//不计数量
			int cinum=dayinfo.getYxy_day_count_ci_num()+1;//当天发送次数
			//如果是再发一次且是第1次和第2次则不记数
			if(sendci==1||sendci==2){
				notcountnum+=detailAddress.size();
			}else{
				tolnum+=detailAddress.size();//当天发送总数
			}
			dayinfo.setYxy_day_count_date(nowday);
			dayinfo.setYxy_day_count_tol_num(tolnum);
			dayinfo.setYxy_day_count_ci_num(cinum);
			dayinfo.setYxy_day_count_nocount(notcountnum);
			dayinfo.setIs_timer_count(0);
			//更新记录
			yxymaildaycountdao.updatePojo(dayinfo);
		}else{//如果没记录则添加
			YxyMailDayCount dayinfo=new YxyMailDayCount();
			dayinfo.setYxy_day_count_date(nowday);
			dayinfo.setYxy_day_count_ci_num(1);
			dayinfo.setYxy_day_count_domain(domain);
			dayinfo.setYxy_day_count_loiginid(loginid);
			//如果是再发一次且是第1次和第2次则不记数
			if(sendci==1||sendci==2){
				dayinfo.setYxy_day_count_tol_num(0);
				dayinfo.setYxy_day_count_nocount(detailAddress.size());
			}else{
				dayinfo.setYxy_day_count_tol_num(detailAddress.size());
				dayinfo.setYxy_day_count_nocount(0);
			}
			dayinfo.setIs_timer_count(0);
			//添加新记录
			yxymaildaycountdao.addPojo(dayinfo);
		}
		
		//===========月统计=================
		String countmonth=nowday.substring(0,nowday.length()-3);
		if(master.getStatus() == 2){//定时邮件
			countmonth=master.getPlans_send_time().split(" ")[0].substring(0,nowday.length()-3);
		}
		//查询是否有当月统计信息
		String monthhql="from YxyMailMonthCount where yxy_month_count_loginid='"+loginid+"' and yxy_month_count_domain='"+domain+"' and yxy_month_count_date='"+countmonth+"'";
		List<YxyMailMonthCount> monthcount = yxymailmonthcountdao.findByHql(monthhql);
		//判断是否有新记录
		if(monthcount.size()>0){//当月有记录
			YxyMailMonthCount monthinfo = monthcount.get(0);
			int tolnum = monthinfo.getYxy_month_count_tol_num();//月总数
			int cinum = monthinfo.getYxy_month_count_ci_num()+1;//次数
			int notcountnum=monthinfo.getYxy_month_nocount();//不计数量
			//如果是再发一次且是第1次和第2次则不记数
			if(sendci==1||sendci==2){
				notcountnum+=detailAddress.size();
			}else{
				tolnum+=detailAddress.size();
			}
			monthinfo.setYxy_month_count_tol_num(tolnum);
			monthinfo.setYxy_month_nocount(notcountnum);
			monthinfo.setYxy_month_count_ci_num(cinum);
			yxymailmonthcountdao.updatePojo(monthinfo);
		}else{//添加
			YxyMailMonthCount monthinfo =new YxyMailMonthCount();
			monthinfo.setYxy_month_count_ci_num(1);
			monthinfo.setYxy_month_count_date(countmonth);
			monthinfo.setYxy_month_count_domain(domain);
			monthinfo.setYxy_month_count_loginid(loginid);
			//如果是再发一次且是第1次和第2次则不记数
			if(sendci==1||sendci==2){
				monthinfo.setYxy_month_nocount(detailAddress.size());
				monthinfo.setYxy_month_count_tol_num(0);
			}else{
				monthinfo.setYxy_month_nocount(0);
				monthinfo.setYxy_month_count_tol_num(detailAddress.size());
			}
			yxymailmonthcountdao.addPojo(monthinfo);
		}
		//===========总统计=================
		//查询是否有记录
		String  counthql="from YxyMailCount where yxy_count_loginid='"+loginid+"' and yxy_count_domain='"+domain+"'";
		List<YxyMailCount> tolcount =yxymailcountdao.findByHql(counthql);
		if(tolcount.size()==0){//无记录
			//插入新记录
			YxyMailCount tolcountinfo=new YxyMailCount();
			tolcountinfo.setYxy_count_ci_num(1);//次数
			tolcountinfo.setYxy_count_last_date(nowDate);//更新时间
			tolcountinfo.setYxy_count_tol_num(detailAddress.size());//总数
			tolcountinfo.setYxy_count_loginid(loginid);//用户账号
			tolcountinfo.setYxy_count_domain(domain);
			//如果是再发一次且是第1次和第2次则不记数
			if(sendci==1||sendci==2){
				tolcountinfo.setYxy_count_email_num(0);
				tolcountinfo.setYxy_count_nocount(detailAddress.size());
			}else{
				tolcountinfo.setYxy_count_email_num(detailAddress.size());
				tolcountinfo.setYxy_count_nocount(0);
			}
			yxymailcountdao.addPojo(tolcountinfo);
		}else{//更新记录
			YxyMailCount tolcountinfo=new YxyMailCount();
			tolcountinfo=tolcount.get(0);
			int sendtol=tolcount.get(0).getYxy_count_tol_num();
			int sendnocount=tolcount.get(0).getYxy_count_nocount();
			tolcountinfo.setYxy_count_ci_num(tolcount.get(0).getYxy_count_ci_num()+1);
			tolcountinfo.setYxy_count_last_date(nowDate);
			//如果是再发一次且是第1次和第2次则不记数
			if(sendci==1||sendci==2){
				tolcountinfo.setYxy_count_tol_num(sendtol);
				tolcountinfo.setYxy_count_nocount(sendnocount+detailAddress.size());
			}else{
				tolcountinfo.setYxy_count_tol_num(sendtol+detailAddress.size());
				tolcountinfo.setYxy_count_nocount(sendnocount);
			}
			yxymailcountdao.updatePojo(tolcountinfo);
		}
		
		//更新邮件次数
		if(mailid>0){
			String updateci="update YxySendMailMasterBase set sendci="+(sendci+1)+" where send_mail_id="+mailid;
			yxysendmailmasterbasedao.updateByHql(updateci);
		}
		
		//如果是导入地址或粘贴地址则进行数量统计,前提不是个人版营销邮
		if(user_type == 0 && (uploadtype == 1 || uploadtype == 2)){
			if(master.getStatus() == 2){//定时邮件
				nowday=master.getPlans_send_time().split(" ")[0];
			}
			int count_year = Integer.parseInt(nowday.split("-")[0]);
			int count_month = Integer.parseInt(nowday.split("-")[1]);
			//查询是否有记录
			String crmhql = "from YxyNewcustomerCounts where user_id = "+user_id+" and count_year = "+count_year+" and count_month = "+count_month;
			List<YxyNewcustomerCounts> newcount = yxynewcustomercountsdao.findByHql(crmhql);
			if(newcount.size()==0){//无记录
				//插入新记录
				YxyNewcustomerCounts count = new YxyNewcustomerCounts();
				count.setUser_id(user_id);
				count.setLoginid(loginid);
				count.setDomain(domain);
				count.setCount_year(count_year);
				count.setCount_month(count_month);
				count.setUpdate_time(DateFormat.getNowDate());
				count.setCounts(detailAddress.size());
				yxynewcustomercountsdao.addPojo(count);
			}else{//更新记录
				YxyNewcustomerCounts count = newcount.get(0);
				count.setCounts(count.getCounts() + detailAddress.size());
				count.setUpdate_time(DateFormat.getNowDate());
				yxynewcustomercountsdao.updatePojo(count);
			}
		}
		
		//更新客户信息
		if((uploadtype == 4 || uploadtype == 7) && !"".equals(customeridstr)){//选择CRM库或从客户跳转过来
			//CRM库选择发送或CRM库跳转发送统计
			if(master.getStatus() == 2){//定时邮件
				nowday=master.getPlans_send_time().split(" ")[0];
			}
			int count_year = Integer.parseInt(nowday.split("-")[0]);
			int count_month = Integer.parseInt(nowday.split("-")[1]);
			//查询是否有记录
			String crmhql = "from YxyOldcustomerCounts where user_id = "+user_id+" and count_year = "+count_year+" and count_month = "+count_month;
			List<YxyOldcustomerCounts> crmcount = yxyoldcustomercountsdao.findByHql(crmhql);
			if(crmcount.size()==0){//无记录
				//插入新记录
				YxyOldcustomerCounts count = new YxyOldcustomerCounts();
				count.setUser_id(user_id);
				count.setLoginid(loginid);
				count.setDomain(domain);
				count.setCount_year(count_year);
				count.setCount_month(count_month);
				count.setUpdate_time(DateFormat.getNowDate());
				count.setCounts(detailAddress.size());
				yxyoldcustomercountsdao.addPojo(count);
			}else{//更新记录
				YxyOldcustomerCounts count = crmcount.get(0);
				count.setCounts(count.getCounts() + detailAddress.size());
				count.setUpdate_time(DateFormat.getNowDate());
				yxyoldcustomercountsdao.updatePojo(count);
			}
		}
	}
	
	/**查询邮件txt文件*/
	public String findMailTxtPath(int mailid)throws Exception{
		String sql="select txtaddresspath from yxy_send_mail_master_base where send_mail_id="+mailid;
		List<Object> list=yxysendmailmasterbasedao.findBySqlQuery(sql);
		if(list!=null&&list.size()>0){
			return list.get(0).toString();
		}else{
			return "";
		}
	}
	
	/**查询最后一封邮件的时间是否在90天之内*/
	public String findLastMail(int mailid)throws Exception{
		String sql="select create_time from yxy_send_mail_master_base where send_mail_id="+mailid +" or mainid="+mailid+" order by create_time desc limit 1";
		List<Object> list=yxysendmailmasterbasedao.findBySqlQuery(sql);
		if(list!=null&&list.size()>0){
			return list.get(0).toString();
		}else{
			return "";
		}
	}
	
	/**判断当前用户是否有新客户营销权限:0没有,1有*/
	public int getNewCusPower(String domain,int company_id,int user_id)throws Exception{
		String hql = "from CentreYxySenderDomain where allow_domain = '"+domain+"'";
		List<CentreYxySenderDomain> domains = centreyxysenderdomaindao.findByHql(hql);
		if(domains.size() > 0){//域名在白名单里
			return 1;
		}else{//域名不在白名单里
			hql = "from CentreYxyedmNewcustomer where company_id = "+company_id;
			List<CentreYxyedmNewcustomer> news = centreyxyedmnewcustomerdao.findByHql(hql);
			if(news.size() > 0){
				CentreYxyedmNewcustomer newcustomer = news.get(0);
				String[] user_arr = newcustomer.getAllow_user().split(",");
				int result = 0;
				for(String user : user_arr){//当前用户在指定的用户里
					if(Integer.parseInt(user) == user_id){
						result = 1;
						break;
					}
				}
				return result;
			}else{//无权限
				return 0;
			}
		}
	}
	
	/**获取EDM配置-开发客户配置信息*/
	public CentreYxyedmNewcustomer getNewCusSet(int company_id)throws Exception{
		String hql= "from CentreYxyedmNewcustomer where company_id = "+company_id;
		List<CentreYxyedmNewcustomer> list = centreyxyedmnewcustomerdao.findByHql(hql);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	/**获取EDM配置-盘活客户配置信息*/
	public CentreYxyedmOldcustomer getOldCusSet(int company_id)throws Exception{
		String hql= "from CentreYxyedmOldcustomer where company_id = "+company_id;
		List<CentreYxyedmOldcustomer> list = centreyxyedmoldcustomerdao.findByHql(hql);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	/**获取SMTP信息*/
	public List<YxySendSmtpInfo> findSmtpByHql(String hql)throws Exception{
		return yxysendsmtpinfodao.findByHql(hql);
	}
	
	/**判断CRM库发信本月是否超标,0超标,1不超标*/
	public int getThisMonthSendLimit(int user_id,String customer_ids,String send_time)throws Exception{
		String[] date_arr = send_time.split("-");
		int count_year = Integer.parseInt(date_arr[0]);
		int count_month = Integer.parseInt(date_arr[1]);
		//当月已经发送的量
		String hql = "select counts from YxyOldcustomerCounts where user_id = "+user_id+" and count_year = "+count_year+" and count_month = "+count_month;
		int counts = yxyoldcustomercountsdao.findByHqlCount(hql);
		//已发量+当前发送量>系统设置的月最大量
		if(counts + customer_ids.split(",").length > ConfigPath.getSendMonthLimit()){
			return 0;//超标
		}else{
			return 1;//没超标
		}
	}
	
	/**判断新客户营销发信本月是否超标,-1单次超标,-2月发超标,1不超标*/
	public int getThisMonthSendLimit2(int company_id,int user_id,int send_nums,String send_time)throws Exception{
		//开发客户的代发模式不用判断数量的超标
		String hql = "from CentreYxyedmNewcustomer where company_id = "+company_id+" and type = 0 and status = 1";
		List<CentreYxyedmNewcustomer> list = centreyxyedmnewcustomerdao.findByHql(hql);
		if(list.size() > 0){//配置了开发客户的自有域名则进行数量的判断
			if(send_nums > ConfigPath.getSendOnceLimitNew()){
				return -1;//单次量发送超标
			}else{
				String[] date_arr = send_time.split("-");
				int count_year = Integer.parseInt(date_arr[0]);
				int count_month = Integer.parseInt(date_arr[1]);
				//当月已经发送的量
				hql = "select counts from YxyNewcustomerCounts where user_id = "+user_id+" and count_year = "+count_year+" and count_month = "+count_month;
				int counts = yxynewcustomercountsdao.findByHqlCount(hql);
				//已发量+当前发送量>系统设置的月最大量
				if(counts + send_nums > ConfigPath.getSendMonthLimitNew()){
					return -2;//月发送量超标
				}else{
					return 1;//没超标
				}
			}
		}else{//没配置开发客户的自有域名则无限
			return 1;
		}
	}
	
	/**若发送的是客户则处理发送发送后的业务*/
	public void updateCustomerService(String domain,String login_id,int company_id,int user_id,String send_time,int uploadtype,String customerid,int jinchengid,String subject)throws Exception{
		if((uploadtype == 4 || uploadtype == 7) && null != customerid && !"".equals(customerid)){//选择CRM库或从客户跳转过来
			String[] customer_id_arr = customerid.split(",");
			try{
				//更新最后联系时间
				String hql = "update YxyCustomerEmail set last_contact_date = '"+DateFormat.getNowDate()+"',sale_process = "+jinchengid+" where customer_id in("+customerid+")";
				yxycustomeremaildao.updateByHql(hql);
				List<CrmCustomerFollowup> followuplist = new ArrayList<CrmCustomerFollowup>();
				for(String customer_id : customer_id_arr)
				{
					CrmCustomerFollowup followup = new CrmCustomerFollowup();
					followup.setCustomer_id(Integer.parseInt(customer_id));//客户id
					followup.setCreate_user_id(user_id);//用户id
					followup.setFollowup_time(DateFormat.getNowDate());//跟进时间
					followup.setFollowup_classify(5);//跟进记录,营销邮发信
					followup.setFollowup_way(0);//跟进方式
					followup.setRemind_time_classify(0);//提醒类型
					followup.setPurpose_process(jinchengid);//进程id
					followup.setFollowup_content(subject);//主题
					followup.setFocus_product("");//关注产品
					followup.setIs_msg_remind(0);//短信提醒
					followup.setIs_email_remind(0);//邮件提醒
					followup.setRemind_time("");//提醒时间
					followup.setMail_id(0);//营销邮的邮件id
					followup.setCreate_date(DateFormat.getNowDate());//创建时间
					followuplist.add(followup);
				}
				crmcustomerfollowupdao.addPojoPI(followuplist);
			}catch (Exception e) {
				StringWriter sw = new StringWriter();
				e.printStackTrace(new PrintWriter(sw, true));
		        String str = sw.toString();
		        LogClass.errolog(str,"erro@erro");
			}
			String nowday=DateFormat.getNowDate().split(" ")[0];
			if(send_time != null && !"".equals(send_time) && !"no".equals(send_time)){
				nowday=send_time.split(" ")[0];  
			}
			for(String s : customer_id_arr){
				String hql = "from YxySendCount where user_id = "+user_id+" and customer_id = "+s+" and send_date = '"+nowday+"'";
				List<YxySendCount> counts = yxysendcountdao.findByHql(hql);
				if(counts.size() > 0){
					YxySendCount count = counts.get(0);
					count.setSend_counts(count.getSend_counts()+1);
					count.setSend_time(DateFormat.getNowDate());
					yxysendcountdao.updatePojo(count);
				}else{
					YxySendCount count = new YxySendCount();
					count.setUser_id(user_id);
					count.setCompany_id(company_id);
					count.setSend_counts(1);
					count.setSend_date(nowday);
					count.setSend_time(DateFormat.getNowDate());
					count.setCustomer_id(Integer.parseInt(s));
					yxysendcountdao.addPojo(count);
				}
			}
			//更改客户跟进次数
			WebmailHttpUtil.updateFollowCount(company_id, domain, user_id, login_id, customerid);
		}		
	}
	
	private YxyUserSetDao yxyusersetdao;//用户设置
	private YxySendMailMasterBaseDao yxysendmailmasterbasedao;//邮件基本信息dao
	private YxySendMailMasterDao yxysendmailmasterdao;//待发邮件dao
	private YxyUserAddressDao yxyuseraddressdao;//用户地址库dao
	private YxyMailDayCountDao yxymaildaycountdao;//日统计
	private YxyMailMonthCountDao yxymailmonthcountdao;//月统计
	private YxyMailCountDao yxymailcountdao;//总统计
	private YxySendMailDetailDao yxysendmaildetaildao;//待发地址dao
	private YxyMailManyDao mailmanydao;//邮件多次dao
	private YxyCompanyWebInfoDao yxycompanywebinfodao;//webmail访问dao
	private YxySysParamatersValueDao yxysysparamatersvaluedao;//参数值dao
	private YxySendSmtpInfoDao yxysendsmtpinfodao;//邮箱dao
	private CentreYxySenderDomainDao centreyxysenderdomaindao;//营销邮发件人配置白名单dao
	private CentreYxyedmNewcustomerDao centreyxyedmnewcustomerdao;//营销开发客户配置dao
	private CentreYxyedmOldcustomerDao centreyxyedmoldcustomerdao;//营销盘活客户配置dao
	private CrmCustomerFollowupDao crmcustomerfollowupdao;//跟进dao
	private YxyCustomerEmailDao yxycustomeremaildao;//客户dao
	private YxyOldcustomerCountsDao yxyoldcustomercountsdao;//CRM库发信月统计dao
	private YxyNewcustomerCountsDao yxynewcustomercountsdao;//新客户营销发信月统计dao
	private YxySendCountDao yxysendcountdao;//客户发送邮件的统计dao
	public YxyCompanyWebInfoDao getYxycompanywebinfodao() {
		return yxycompanywebinfodao;
	}
	public void setYxycompanywebinfodao(YxyCompanyWebInfoDao yxycompanywebinfodao) {
		this.yxycompanywebinfodao = yxycompanywebinfodao;
	}
	public YxySysParamatersValueDao getYxysysparamatersvaluedao() {
		return yxysysparamatersvaluedao;
	}
	public void setYxysysparamatersvaluedao(
			YxySysParamatersValueDao yxysysparamatersvaluedao) {
		this.yxysysparamatersvaluedao = yxysysparamatersvaluedao;
	}
	public YxyUserSetDao getYxyusersetdao() {
		return yxyusersetdao;
	}
	public void setYxyusersetdao(YxyUserSetDao yxyusersetdao) {
		this.yxyusersetdao = yxyusersetdao;
	}
	public YxySendMailMasterBaseDao getYxysendmailmasterbasedao() {
		return yxysendmailmasterbasedao;
	}
	public void setYxysendmailmasterbasedao(
			YxySendMailMasterBaseDao yxysendmailmasterbasedao) {
		this.yxysendmailmasterbasedao = yxysendmailmasterbasedao;
	}
	public YxyUserAddressDao getYxyuseraddressdao() {
		return yxyuseraddressdao;
	}
	public void setYxyuseraddressdao(YxyUserAddressDao yxyuseraddressdao) {
		this.yxyuseraddressdao = yxyuseraddressdao;
	}
	public YxyMailDayCountDao getYxymaildaycountdao() {
		return yxymaildaycountdao;
	}
	public void setYxymaildaycountdao(YxyMailDayCountDao yxymaildaycountdao) {
		this.yxymaildaycountdao = yxymaildaycountdao;
	}
	public YxyMailMonthCountDao getYxymailmonthcountdao() {
		return yxymailmonthcountdao;
	}
	public void setYxymailmonthcountdao(YxyMailMonthCountDao yxymailmonthcountdao) {
		this.yxymailmonthcountdao = yxymailmonthcountdao;
	}
	public YxyMailCountDao getYxymailcountdao() {
		return yxymailcountdao;
	}
	public void setYxymailcountdao(YxyMailCountDao yxymailcountdao) {
		this.yxymailcountdao = yxymailcountdao;
	}
	public YxySendMailMasterDao getYxysendmailmasterdao() {
		return yxysendmailmasterdao;
	}
	public void setYxysendmailmasterdao(YxySendMailMasterDao yxysendmailmasterdao) {
		this.yxysendmailmasterdao = yxysendmailmasterdao;
	}
	public YxySendMailDetailDao getYxysendmaildetaildao() {
		return yxysendmaildetaildao;
	}
	public void setYxysendmaildetaildao(YxySendMailDetailDao yxysendmaildetaildao) {
		this.yxysendmaildetaildao = yxysendmaildetaildao;
	}
	public YxyMailManyDao getMailmanydao() {
		return mailmanydao;
	}
	public void setMailmanydao(YxyMailManyDao mailmanydao) {
		this.mailmanydao = mailmanydao;
	}
	public YxySendSmtpInfoDao getYxysendsmtpinfodao() {
		return yxysendsmtpinfodao;
	}
	public void setYxysendsmtpinfodao(YxySendSmtpInfoDao yxysendsmtpinfodao) {
		this.yxysendsmtpinfodao = yxysendsmtpinfodao;
	}
	public CentreYxySenderDomainDao getCentreyxysenderdomaindao() {
		return centreyxysenderdomaindao;
	}
	public void setCentreyxysenderdomaindao(
			CentreYxySenderDomainDao centreyxysenderdomaindao) {
		this.centreyxysenderdomaindao = centreyxysenderdomaindao;
	}
	public CentreYxyedmNewcustomerDao getCentreyxyedmnewcustomerdao() {
		return centreyxyedmnewcustomerdao;
	}
	public void setCentreyxyedmnewcustomerdao(
			CentreYxyedmNewcustomerDao centreyxyedmnewcustomerdao) {
		this.centreyxyedmnewcustomerdao = centreyxyedmnewcustomerdao;
	}
	public CentreYxyedmOldcustomerDao getCentreyxyedmoldcustomerdao() {
		return centreyxyedmoldcustomerdao;
	}
	public void setCentreyxyedmoldcustomerdao(
			CentreYxyedmOldcustomerDao centreyxyedmoldcustomerdao) {
		this.centreyxyedmoldcustomerdao = centreyxyedmoldcustomerdao;
	}
	public CrmCustomerFollowupDao getCrmcustomerfollowupdao() {
		return crmcustomerfollowupdao;
	}
	public void setCrmcustomerfollowupdao(
			CrmCustomerFollowupDao crmcustomerfollowupdao) {
		this.crmcustomerfollowupdao = crmcustomerfollowupdao;
	}
	public YxyCustomerEmailDao getYxycustomeremaildao() {
		return yxycustomeremaildao;
	}
	public void setYxycustomeremaildao(YxyCustomerEmailDao yxycustomeremaildao) {
		this.yxycustomeremaildao = yxycustomeremaildao;
	}
	public YxyOldcustomerCountsDao getYxyoldcustomercountsdao() {
		return yxyoldcustomercountsdao;
	}
	public void setYxyoldcustomercountsdao(
			YxyOldcustomerCountsDao yxyoldcustomercountsdao) {
		this.yxyoldcustomercountsdao = yxyoldcustomercountsdao;
	}
	public YxyNewcustomerCountsDao getYxynewcustomercountsdao() {
		return yxynewcustomercountsdao;
	}
	public void setYxynewcustomercountsdao(
			YxyNewcustomerCountsDao yxynewcustomercountsdao) {
		this.yxynewcustomercountsdao = yxynewcustomercountsdao;
	}
	public YxySendCountDao getYxysendcountdao() {
		return yxysendcountdao;
	}
	public void setYxysendcountdao(YxySendCountDao yxysendcountdao) {
		this.yxysendcountdao = yxysendcountdao;
	}
}