ef7977229997bd50e1baca8dec28615243f11bec.svn-base 33.1 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
package com.espeed.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.espeed.centre.pojo.YxyUserInfo;
import com.espeed.dao.CrmProductDao;
import com.espeed.dao.CrmSourcefromDao;
import com.espeed.dao.CrmStatusDao;
import com.espeed.dao.YxyCustomerEmailDao;
import com.espeed.dao.YxyCustomerTypeDao;
import com.espeed.dao.YxyMailCountDao;
import com.espeed.dao.YxyMailDayCountDao;
import com.espeed.dao.YxyMailFolderDao;
import com.espeed.dao.YxyMailMonthCountDao;
import com.espeed.dao.YxyMailStencilDao;
import com.espeed.dao.YxyMarketingPlanDao;
import com.espeed.dao.YxyMarketingWeekDao;
import com.espeed.dao.YxyOldcustomerCountsDao;
import com.espeed.dao.YxySendMailDetailDao;
import com.espeed.dao.YxyUserAddressDao;
import com.espeed.pojo.YxyMailCount;
import com.espeed.pojo.YxyMailDayCount;
import com.espeed.pojo.YxyMailFolder;
import com.espeed.pojo.YxyMailMonthCount;
import com.espeed.pojo.YxyMailStencil;
import com.espeed.pojo.YxyMarketingPlan;
import com.espeed.pojo.YxyMarketingWeek;
import com.espeed.pojo.YxyOldcustomerCounts;
import com.espeed.pojo.YxyUserAddress;
import com.espeed.service.YxyMarketingPlanService;
import com.espeed.tool.ConfigPath;
import com.espeed.tool.DateFormat;
import com.espeed.vo.PageBean;
import com.espeed.webmail.pojo.CrmProduct;
import com.espeed.webmail.pojo.CrmSourcefrom;
import com.espeed.webmail.pojo.CrmStatus;
import com.espeed.webmail.pojo.YxyCustomerType;

public class YxyMarketingPlanServiceImpl implements YxyMarketingPlanService{
	
	/**添加执行的营销计划前的发送量判断*/
	public Map<String,String> beforeAddMarketingPlan(String loginid,String domain,int commitnum,Map<String,String> usergraph,String planweekmodel)throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		//日发量限制(-1无限制)
		int daysendBig = Integer.parseInt(usergraph.get("yxy_day_send_mail_limit").toString());//日最大量	
		//月发量限制(-1无限制)
		int monthsendBig = Integer.parseInt(usergraph.get("yxy_month_send_mail_limit").toString());//月最大量	
		//总最大发量(-1无限制)
		int tolBig = Integer.parseInt(usergraph.get("yxy_sum_send").toString());
		if(daysendBig == -1 && monthsendBig == -1 && tolBig == -1){//日月总发量都是无限制则不需要判断
			map.put("result", "1");
			map.put("reason", "");
			return map;
		}else{//否则需要判断
			//循环判断每个日期
			String[] weekarry = planweekmodel.split("#");
			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 * weekarry.length >= tolBig){
					map.put("result", "-1");
					map.put("reason", "总发送量超标");
					return map;
				}
			}
			Map<String,Integer> monthMap = new HashMap<String,Integer>();
			//循环判断日份
			for(int i=0;i<weekarry.length;i++){
				String[] strarry = weekarry[i].split(",");
				String sendtime = strarry[1];//添加的计划日期
				if(daysendBig != -1){//日发送量不是无限制则需要判断
					//日已发量
					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='"+sendtime+"'";
					int daysendMax = yxymaildaycountdao.findByHqlCount(dayhql);
					//判断是否满了
					if(daysendMax+commitnum>=daysendBig){
						map.put("result", "-3");
						map.put("reason", "日发送量超标,日期:"+sendtime);
						return map;
					}
				}
				String year = sendtime.split("-")[0];//年
				String month = sendtime.split("-")[1];//月
				if(monthMap.containsKey(year+"-"+month)){//月发送量的map中有该值则累加
					monthMap.put(year+"-"+month,monthMap.get(year+"-"+month) + commitnum);
				}else{//否则直接加入
					monthMap.put(year+"-"+month,commitnum);
				}
			}
			//循环判断月份
			if(monthsendBig != -1){//月发送量不是无限制则需要判断
				for (Map.Entry<String, Integer> entry : monthMap.entrySet()) { 
				  	//月已发量
					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='"+entry.getKey()+"'";
					int monthsendMax = yxymailmonthcountdao.findByHqlCount(monthhql);//当月已发量
					//判断是否满了
					if(monthsendMax+entry.getValue()>=monthsendBig){
						map.put("result", "-2");
						map.put("reason", "月发送量超标,日期:"+entry.getKey());
						return map;
					}
				}
			}
			map.put("result", "1");
			map.put("reason", "");
			return map;
		}
	}
 
	/**增加营销计划与提醒计划*/
	public void addMarketingPlan(int user_id,YxyMarketingPlan plan, String weekmodelstr)throws Exception {
		String domain=plan.getDomain();
		String loginid=plan.getLoginid();
		//当前日期
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowdate=df.format(new Date());
		//添加计划信息
		int planid=marketingplandao.addPojo(plan);
		//添加计划周期信息
		List<YxyMarketingWeek> weeklist=new ArrayList<YxyMarketingWeek>();
		//日发量集合
		List<YxyMailDayCount> daycountlist=new ArrayList<YxyMailDayCount>();
		//月发量集合
		List<YxyMailMonthCount> monthcountlist =new ArrayList<YxyMailMonthCount>();
		//即将发送量
		int addressnum=0;
		//查询地址量
		if(plan.getPlan_differen() > 0){
			if(plan.getPlan_differen() == 9 || plan.getPlan_differen() == 10){//采购偏好
				addressnum=plan.getPlan_groupid();
			}else if(plan.getPlan_differen() == 11 || plan.getPlan_differen() == 12){//个性营销
				addressnum=plan.getPlan_groupid();
			}else if(plan.getPlan_differen() == 3 || plan.getPlan_differen() == 4){//客户状态
				String hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user_id+" " +
						"and delete_flag = 0 and current_status != 1 and customer_status = "+plan.getPlan_groupid();
				addressnum = yxycustomeremaildao.findByHqlCount(hql);
			}else if(plan.getPlan_differen() == 5 || plan.getPlan_differen() == 6){//客户来源
				String hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user_id+" " +
						"and delete_flag = 0 and current_status != 1 and source_from_id = "+plan.getPlan_groupid();
				addressnum = yxycustomeremaildao.findByHqlCount(hql);
			}else if(plan.getPlan_differen() == 7 || plan.getPlan_differen() == 8){//意向产品
				String hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user_id+" " +
						"and delete_flag = 0 and current_status != 1 and  find_in_set("+plan.getPlan_groupid()+",prductid) > 0";
				addressnum = yxycustomeremaildao.findByHqlCount(hql);
			}else{//客户分类
				String hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user_id+" " +
						"and delete_flag = 0 and current_status != 1 and classify_id = "+plan.getPlan_groupid();
				addressnum = yxycustomeremaildao.findByHqlCount(hql);
			}
		}else{
			String hql="select count(*) from YxyUserAddress where user_addr_type_id="+plan.getPlan_groupid();
			addressnum=yxyuseraddressdao.findByHqlCount(hql);
		}
		//现在发送量
		int nowsendnum=0;
		//月集合字符
		Map<String,Integer> monthArry=new HashMap<String,Integer>();
		//时间分割
		String[] weekarry=weekmodelstr.split("#");
		//循环处理时间段
		for(int i=0;i<weekarry.length;i++){
			nowsendnum+=addressnum;
			String[] strarry=weekarry[i].split(",");
			int weekid=Integer.parseInt(strarry[0]);
			int modelid=Integer.parseInt(strarry[2]);
			String sendtime=strarry[1];
			
			YxyMarketingWeek week=new YxyMarketingWeek();
			week.setMode_id(modelid);
			week.setPlan_id(planid);
			week.setWeek_value(weekid);
			week.setWeek_day(sendtime);
			week.setIsexecution(0);
			week.setDomain(domain);
			week.setLoginid(loginid);
			
			weeklist.add(week);
			//提醒计划不进行统计
			if(plan.getPlan_type()==1){
				//查询日发量
				String dayhql="from YxyMailDayCount where yxy_day_count_loiginid='"+loginid+"' and yxy_day_count_domain='"+domain+"' and yxy_day_count_date='"+sendtime+"'";
				List<YxyMailDayCount> dayoverlist=yxymaildaycountdao.findByHql(dayhql);
				if(dayoverlist.size()>0){//有日统计记录
					YxyMailDayCount dayinfo=new YxyMailDayCount();
					dayinfo.setYxy_day_count_id(dayoverlist.get(0).getYxy_day_count_id());
					dayinfo.setYxy_day_count_date(sendtime);
					dayinfo.setYxy_day_count_domain(domain);
					dayinfo.setYxy_day_count_loiginid(loginid);
					dayinfo.setYxy_day_count_address_num(0);
					dayinfo.setYxy_day_count_ci_num(dayoverlist.get(0).getYxy_day_count_ci_num()+1);
					dayinfo.setYxy_day_count_tol_num(dayoverlist.get(0).getYxy_day_count_tol_num()+addressnum);
					dayinfo.setIs_timer_count(0);
					//加入集合
					daycountlist.add(dayinfo);
				}else{//添加新的日统计记录
					YxyMailDayCount dayinfo=new YxyMailDayCount();
					dayinfo.setYxy_day_count_date(sendtime);
					dayinfo.setYxy_day_count_tol_num(addressnum);
					dayinfo.setYxy_day_count_ci_num(1);
					dayinfo.setYxy_day_count_domain(domain);
					dayinfo.setYxy_day_count_loiginid(loginid);
					dayinfo.setYxy_day_count_address_num(0);
					dayinfo.setIs_timer_count(0);
					//加入集合
					daycountlist.add(dayinfo);
				}
				
				//月份
				String countmonth=sendtime.substring(0,sendtime.length()-3);
				//遍历是否存在
			    Iterator<Entry<String, Integer>> iter = monthArry.entrySet().iterator();
			    boolean ishave=false;
				while(iter.hasNext()){
					Entry<String, Integer> entry =(Entry<String, Integer>)iter.next();
					String key = entry.getKey();//键
					int val = entry.getValue();//值
					//是不是存在
					if(key.equals(countmonth)){
						monthArry.put(key, val+addressnum);
						ishave=true;
						break;
					}
				}
				if(!ishave){
					monthArry.put(countmonth,addressnum);
				}
			}
		}
		//加入计划
		marketingweekdao.addPojoPi(weeklist);
		
		//提醒计划不进行统计
		if(plan.getPlan_type()==1){
			//查询月发量
			Iterator<Entry<String, Integer>> iter = monthArry.entrySet().iterator();
			while(iter.hasNext()){
				Entry<String, Integer> entry =(Entry<String, Integer>)iter.next();
				String key = entry.getKey();//键
				int val = entry.getValue();//值
				//分批查询
				String monthhql="from YxyMailMonthCount where yxy_month_count_loginid='"+loginid+"' and yxy_month_count_domain='"+domain+"' and yxy_month_count_date='"+key+"'";
				List<YxyMailMonthCount> monthoverlist = yxymailmonthcountdao.findByHql(monthhql);
				if(monthoverlist.size()>0){//有月统计
					YxyMailMonthCount monthinfo=new YxyMailMonthCount();//monthoverlist.get(0);
					monthinfo.setYxy_month_count_id(monthoverlist.get(0).getYxy_month_count_id());
					monthinfo.setYxy_month_count_ci_num(monthoverlist.get(0).getYxy_month_count_ci_num()+1);
					monthinfo.setYxy_month_count_tol_num(monthoverlist.get(0).getYxy_month_count_tol_num()+val);//monthinfo.setYxy_month_count_date(key);
					monthinfo.setYxy_month_count_domain(domain);
					monthinfo.setYxy_month_count_loginid(loginid);
					monthinfo.setYxy_month_count_date(key);
					//加入集合
					monthcountlist.add(monthinfo);
				}else{//无统计记录
					YxyMailMonthCount monthinfo =new YxyMailMonthCount();
					monthinfo.setYxy_month_count_ci_num(1);
					monthinfo.setYxy_month_count_date(key);
					monthinfo.setYxy_month_count_domain(domain);
					monthinfo.setYxy_month_count_loginid(loginid);
					monthinfo.setYxy_month_count_tol_num(val);
					//加入集合
					monthcountlist.add(monthinfo);
				}
			}
			//总
			String  counthql="from YxyMailCount where yxy_count_loginid='"+loginid+"' and yxy_count_domain='"+domain+"'";
			List<YxyMailCount> tolvoerlist =yxymailcountdao.findByHql(counthql);
			//实体
			YxyMailCount tolcount =new YxyMailCount();
			//是否存在
			if(tolvoerlist.size()>0){//有记录
				int countid=tolvoerlist.get(0).getYxy_count_id();
				int sendci=tolvoerlist.get(0).getYxy_count_ci_num()+weekarry.length;
				int sendtol=tolvoerlist.get(0).getYxy_count_tol_num()+nowsendnum;
				String updatehql="update YxyMailCount set yxy_count_ci_num="+sendci+",yxy_count_tol_num="+sendtol+",yxy_count_last_date='"+nowdate+"' where yxy_count_id="+countid;
				yxymailcountdao.updateByHql(updatehql);
			}else{//无记录
				tolcount.setYxy_count_ci_num(weekarry.length);
				tolcount.setYxy_count_domain(domain);
				tolcount.setYxy_count_loginid(loginid);
				tolcount.setYxy_count_last_date(nowdate);
				tolcount.setYxy_count_email_num(0);
				tolcount.setYxy_count_tol_num(nowsendnum);
				yxymailcountdao.addPojo(tolcount);
			}
			//分配量加入数据库中
			yxymaildaycountdao.addPojoPi(daycountlist);
			yxymailmonthcountdao.addPojoPi(monthcountlist);
			
			//执行计划且CRM库选择则需要统计数量
			if(plan.getPlan_differen() > 0){
				//循环判断每个日期
				Map<String,Integer> date_map = new HashMap<String,Integer>();
				String[] week_arr = weekmodelstr.split("#");
				for(String week : week_arr){
					String date = week.split(",")[1];
					date = date.substring(0,date.length()-3);
					if(date_map.containsKey(date)){//map中包含该日期
						int nums = date_map.get(date);
						nums = nums + 1;
						date_map.put(date, nums);
					}else{//不包含该日期
						date_map.put(date, 1);
					}
				}
				//循环每个年份-月份
				for(String date : date_map.keySet()){
					int count_year = Integer.parseInt(date.split("-")[0]);
					int count_month = Integer.parseInt(date.split("-")[1]);
					//当月已经发送的量
					String hql = "from YxyOldcustomerCounts where user_id = "+user_id+" and count_year = "+count_year+" and count_month = "+count_month;
					List<YxyOldcustomerCounts> crm_counts = yxyoldcustomercountsdao.findByHql(hql);
					if(crm_counts.size() > 0){//存在则更新
						YxyOldcustomerCounts crmcount = crm_counts.get(0);
						crmcount.setCounts(crmcount.getCounts() + addressnum * date_map.get(date));
						crmcount.setUpdate_time(DateFormat.getNowDate());
						yxyoldcustomercountsdao.updatePojo(crmcount);
					}else{//不存在则插入
						YxyOldcustomerCounts crmcount = new YxyOldcustomerCounts();
						crmcount.setUser_id(user_id);
						crmcount.setLoginid(loginid);
						crmcount.setDomain(domain);
						crmcount.setCount_year(count_year);
						crmcount.setCount_month(count_month);
						crmcount.setCounts(addressnum * date_map.get(date));
						crmcount.setUpdate_time(DateFormat.getNowDate());
						yxyoldcustomercountsdao.addPojo(crmcount);
					}
				}
			}
		}
	}
	
	/**列表查询营销计划*/
	public List<YxyMarketingPlan> findMarketingPlanTable(String loginid,String domain,int condition,String conditionvalue,PageBean pb)throws Exception{
		String hql="from YxyMarketingPlan where loginid='"+loginid+"' and domain='"+domain+"'"; 
		String hqlcount="select count(*) from YxyMarketingPlan where loginid='"+loginid+"' and domain='"+domain+"'"; 
		if(condition==1){//状态查询已执行
			hql+=" and plan_status=1";
			hqlcount+=" and plan_status=1";
		}else if(condition==2){//状态查询未执行
			hql+=" and plan_status=0";
			hqlcount+=" and plan_status=0";
		}else if(condition==3){//提醒计划
			hql+=" and plan_type=0";
			hqlcount+=" and plan_type=0";
		}else if(condition==4){//执行计划
			hql+=" and plan_type=1";
			hqlcount+=" and plan_type=1";
		}else if(condition==5){//状态查询正在执行
			hql+=" and plan_status=-1";
			hqlcount+=" and plan_status=-1";
		}
		hql+=" order by plan_createtime desc";
		hqlcount+=" order by plan_createtime desc";
		List<YxyMarketingPlan> plans = marketingplandao.findByHqlPage(hql,hqlcount,pb);
		for(YxyMarketingPlan plan : plans){
			if(plan.getPlan_differen() == 0){//本地库
				plan.setPlan_grouptype("本地库");
				String hql1 = "from YxyMailFolder where folder_id = "+plan.getPlan_groupid();
				List<YxyMailFolder> folders = yxymailfolderdao.findByHql(hql1);
				if(folders.size() > 0){
					plan.setPlan_groupname(folders.get(0).getFolder_name());
				}else{
					plan.setPlan_groupname("未知");
				}
			}else{//CRM库
				if(plan.getPlan_differen() == 1 || plan.getPlan_differen() == 2){//客户分类
					plan.setPlan_grouptype("CRM库-客户分类");
					String hql1 = "from YxyCustomerType where id = "+plan.getPlan_groupid();
					List<YxyCustomerType> types = customertypedao.findByHql(hql1);
					if(types.size() > 0){
						plan.setPlan_groupname(types.get(0).getName());
					}else{
						plan.setPlan_groupname("未知");
					}
				}else if(plan.getPlan_differen() == 3 || plan.getPlan_differen() == 4){//客户状态
					plan.setPlan_grouptype("CRM库-客户状态");
					String hql1 = "from CrmStatus where id = "+plan.getPlan_groupid();
					List<CrmStatus> status = crmstatusdao.findByHql(hql1);
					if(status.size() > 0){
						plan.setPlan_groupname(status.get(0).getName());
					}else{
						plan.setPlan_groupname("未知");
					}
				}else if(plan.getPlan_differen() == 5 || plan.getPlan_differen() == 6){//客户来源
					plan.setPlan_grouptype("CRM库-客户来源");
					String hql1 = "from CrmSourcefrom where id = "+plan.getPlan_groupid();
					List<CrmSourcefrom> froms = crmsourcefromdao.findByHql(hql1);
					if(froms.size() > 0){
						plan.setPlan_groupname(froms.get(0).getName());
					}else{
						plan.setPlan_groupname("未知");
					}
				}else if(plan.getPlan_differen() == 7 || plan.getPlan_differen() == 8){//意向产品
					plan.setPlan_grouptype("CRM库-意向产品");
					String hql1 = "from CrmProduct where id = "+plan.getPlan_groupid();
					List<CrmProduct> products = crmproductdao.findByHql(hql1);
					if(products.size() > 0){
						plan.setPlan_groupname(products.get(0).getName());
					}else{
						plan.setPlan_groupname("未知");
					}
				}else if(plan.getPlan_differen() == 9 || plan.getPlan_differen() == 10){//采购偏好
					plan.setPlan_grouptype("CRM库-采购偏好");
					plan.setPlan_groupname(plan.getOther_name());
				}else if(plan.getPlan_differen() == 11 || plan.getPlan_differen() == 12){//个性营销
					plan.setPlan_grouptype("CRM库-个性营销");
					plan.setPlan_groupname(plan.getOther_name());
				}
			}
		}
		return plans;
	}
	
	/**展示查询营销计划*/
	public List<YxyMarketingWeek> findMarketingPlan(String loginid,String domain,PageBean pb)throws Exception{
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		String nowday=df.format(new Date());
		String hql="from YxyMarketingWeek where loginid='"+loginid+"' and domain='"+domain+"' and week_day<='"+nowday+"' order by week_day desc";
		String hqlcount="select count(*) from YxyMarketingWeek where loginid='"+loginid+"' and domain='"+domain+"' and week_day<='"+nowday+"' order by week_day desc";
		return marketingweekdao.findByHqlPage(hql, hqlcount, pb);
	}
	
	/**根据地址分组id查询地址量*/
	public int findAddressNum(int typeid) throws Exception {
		String hql="select count(*) from YxyUserAddress where user_addr_type_id="+typeid;
		return yxyuseraddressdao.findByHqlCount(hql);
	}
	
	/**根据地址分组id查询地址*/
	public List<YxyUserAddress> findAddressByTypeid(int typeid) throws Exception {
		String hql="from YxyUserAddress where user_addr_type_id="+typeid;
		return yxyuseraddressdao.findByHql(hql);
	}
	
	/**删除营销计划(当天的)*/
	public void delMarketingPlanDay(int weekid)throws Exception {
		String hql="delete YxyMarketingWeek where week_id="+weekid;
		marketingweekdao.updateByHql(hql);
	}
	
	/**删除营销计划(整个)*/
	public void delMarketingPlan(int planid)throws Exception{
		String hql1="delete YxyMarketingPlan where plan_id="+planid;
		marketingplandao.updateByHql(hql1);
		
		//删除关系
		String hql2="delete YxyMarketingWeek where plan_id="+planid;
		marketingweekdao.updateByHql(hql2);
	}
	
	/**判断CRM库发送量是否超标,0超标,1不超标*/
	public Map<String,String> getMonthSendLimit(int user_id,int crmtype,int plangroupid,String planweekmodel)throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		int counts = 0;
		if(crmtype == 9 || crmtype == 11){//采购偏好或个性营销则数量由前端传递
			counts = plangroupid;
		}else{
			String hql = "";
			if(crmtype==3){//客户状态
				hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user_id+" " +
						"and delete_flag = 0 and current_status != 1 and customer_status = "+plangroupid;
			}else if(crmtype==5){//客户来源
				hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user_id+" " +
						"and delete_flag = 0 and current_status != 1 and source_from_id = "+plangroupid;
			}else if(crmtype==7){//意向产品
				hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user_id+" " +
						"and delete_flag = 0 and current_status != 1 and  find_in_set("+plangroupid+",prductid) > 0";
			}else{//客户分类
				hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user_id+" " +
						"and delete_flag = 0 and current_status != 1 and classify_id = "+plangroupid;
			}
			counts = yxycustomeremaildao.findByHqlCount(hql);
		}
		if(counts > 0){//有客户则进行判断
			if(counts > ConfigPath.getSendOnceLimit()){//单次量超标
				map.put("result", "-1");
				map.put("reason","单次发送量已经超标");
			}else{//单次量不超标
				//循环判断每个日期
				Map<String,Integer> date_map = new HashMap<String,Integer>();
				String[] week_arr = planweekmodel.split("#");
				for(String week : week_arr){
					String date = week.split(",")[1];
					date = date.substring(0,date.length()-3);
					if(date_map.containsKey(date)){//map中包含该日期
						int nums = date_map.get(date);
						nums = nums + 1;
						date_map.put(date, nums);
					}else{//不包含该日期
						date_map.put(date, 1);
					}
				}
				//循环每个年份-月份
				for(String date : date_map.keySet()){
					int count_year = Integer.parseInt(date.split("-")[0]);
					int count_month = Integer.parseInt(date.split("-")[1]);
					//当月已经发送的量
					String hql = "select counts from YxyOldcustomerCounts where user_id = "+user_id+" and count_year = "+count_year+" and count_month = "+count_month;
					int date_counts = yxyoldcustomercountsdao.findByHqlCount(hql);
					//已发量+当前发送量>系统设置的月最大量
					if(date_counts + counts * date_map.get(date) > ConfigPath.getSendMonthLimit()){
						map.put("result", "-2");
						map.put("reason",date+"发送量已经超标");
						return map;
					}
				}
				map.put("result", "1");
			}
		}else{//没客户则不超标
			map.put("result", "1");
		}
		return map;
	}
	
	/**查询营销计划信息*/
	public YxyMarketingPlan getMarketingPlan(int plan_id)throws Exception{
		String hql = "from YxyMarketingPlan where plan_id = "+plan_id;
		List<YxyMarketingPlan> plans = marketingplandao.findByHql(hql);
		if(plans.size() > 0){
			YxyMarketingPlan plan = plans.get(0);
			for(YxyMarketingWeek week : plan.getWeeklist()){
				int mode_id = week.getMode_id();
				hql = "from YxyMailStencil where stencil_id = "+mode_id;
				List<YxyMailStencil> modes = mailstencildao.findByHql(hql);
				if(modes.size() > 0){
					week.setMode_name(modes.get(0).getStencil_name());
				}else{
					week.setMode_name("未知");
				}
			}
			return plan;
		}else{
			return null;
		}
	}
	
	/**撤销营销计划(整个)*/
	public void editMarketingPlan(YxyUserInfo user,int plan_id)throws Exception{
		YxyMarketingPlan plan = getMarketingPlan(plan_id);
		if(plan != null){
			List<YxyMarketingWeek> weeks = plan.getWeeklist();
			int addressnum = 0;
			//获取该分类下的量
			if(plan.getPlan_differen() > 0){
				if(plan.getPlan_differen() == 9 || plan.getPlan_differen() == 10){//采购偏好
					addressnum = plan.getPlan_groupid();
				}else if(plan.getPlan_differen() == 11 || plan.getPlan_differen() == 12){//个性营销
					addressnum = plan.getPlan_groupid();
				}
				else if(plan.getPlan_differen() == 3 || plan.getPlan_differen() == 4){//客户状态
					String hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user.getUser_id()+" " +
							"and delete_flag = 0 and current_status != 1 and customer_status = "+plan.getPlan_groupid();
					addressnum = yxycustomeremaildao.findByHqlCount(hql);
				}else if(plan.getPlan_differen() == 5 || plan.getPlan_differen() == 6){//客户来源
					String hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user.getUser_id()+" " +
							"and delete_flag = 0 and current_status != 1 and source_from_id = "+plan.getPlan_groupid();
					addressnum = yxycustomeremaildao.findByHqlCount(hql);
				}else if(plan.getPlan_differen() == 7 || plan.getPlan_differen() == 8){//意向产品
					String hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user.getUser_id()+" " +
							"and delete_flag = 0 and current_status != 1 and  find_in_set("+plan.getPlan_groupid()+",prductid) > 0";
					addressnum = yxycustomeremaildao.findByHqlCount(hql);
				}else{//客户分类
					String hql = "select COUNT(customer_id) from YxyCustomerEmail where user_id = "+user.getUser_id()+" " +
							"and delete_flag = 0 and current_status != 1 and classify_id = "+plan.getPlan_groupid();
					addressnum = yxycustomeremaildao.findByHqlCount(hql);
				}
			}else{
				String hql="select count(*) from YxyUserAddress where user_addr_type_id="+plan.getPlan_groupid();
				addressnum=yxyuseraddressdao.findByHqlCount(hql);
			}
			//月统计Map
			Map<String,Integer> month_map = new HashMap<String,Integer>();
			//循环每个周期
			for(YxyMarketingWeek week : weeks){
				if(week.getIsexecution() == 0){//未执行才恢复量
					//查询日发量
					String dayhql="from YxyMailDayCount where yxy_day_count_loiginid='"+user.getLogin_id()+"' and yxy_day_count_domain='"+user.getDomain()+"' and yxy_day_count_date='"+week.getWeek_day()+"'";
					List<YxyMailDayCount> dayoverlist=yxymaildaycountdao.findByHql(dayhql);
					if(dayoverlist.size()>0){//有日统计记录
						int ci_num = dayoverlist.get(0).getYxy_day_count_ci_num() - 1;
						if(ci_num < 0){
							ci_num = 0;
						}
						dayoverlist.get(0).setYxy_day_count_ci_num(ci_num);
						int tol_num = dayoverlist.get(0).getYxy_day_count_tol_num() - addressnum;
						if(tol_num < 0){
							tol_num = 0;
						}
						dayoverlist.get(0).setYxy_day_count_tol_num(tol_num);
						yxymaildaycountdao.updatePojo(dayoverlist.get(0));
						String month = week.getWeek_day().substring(0,week.getWeek_day().length() - 3);
						if(month_map.containsKey(month)){
							month_map.put(month,month_map.get(month)+1);
						}else{
							month_map.put(month, 1);
						}
					}
				}
			}
			int month_nums = 0;
			//处理月发送量
			for(String month : month_map.keySet()){
				String monthhql="from YxyMailMonthCount where yxy_month_count_loginid='"+user.getLogin_id()+"' and yxy_month_count_domain='"+user.getDomain()+"' and yxy_month_count_date='"+month+"'";
				List<YxyMailMonthCount> monthoverlist = yxymailmonthcountdao.findByHql(monthhql);
				if(monthoverlist.size()>0){//有月统计
					int ci_num = monthoverlist.get(0).getYxy_month_count_ci_num() - month_map.get(month);
					if(ci_num < 0){
						ci_num = 0;
					}
					monthoverlist.get(0).setYxy_month_count_ci_num(ci_num);
					int tol_num = monthoverlist.get(0).getYxy_month_count_tol_num() - addressnum * month_map.get(month);
					if(tol_num < 0){
						tol_num = 0;
					}
					monthoverlist.get(0).setYxy_month_count_tol_num(tol_num);
					yxymailmonthcountdao.updatePojo(monthoverlist.get(0));
					month_nums = month_nums + month_map.get(month);
				}
				//CRM库营销计划则处理CRM库客户跟进发送量
				if(plan.getPlan_differen() > 0){
					int count_year = Integer.parseInt(month.split("-")[0]);
					int count_month = Integer.parseInt(month.split("-")[1]);
					//当月已经发送的量
					String hql = "from YxyOldcustomerCounts where user_id = "+user.getUser_id()+" and count_year = "+count_year+" and count_month = "+count_month;
					List<YxyOldcustomerCounts> crm_counts = yxyoldcustomercountsdao.findByHql(hql);
					if(crm_counts.size() > 0){//存在则更新
						YxyOldcustomerCounts crmcount = crm_counts.get(0);
						crmcount.setCounts(crmcount.getCounts() + addressnum * month_map.get(month));
						crmcount.setUpdate_time(DateFormat.getNowDate());
						yxyoldcustomercountsdao.updatePojo(crmcount);
					}
				}
			}
			//处理年发送量
			String  counthql="from YxyMailCount where yxy_count_loginid='"+user.getLogin_id()+"' and yxy_count_domain='"+user.getDomain()+"'";
			List<YxyMailCount> tolvoerlist =yxymailcountdao.findByHql(counthql);
			if(tolvoerlist.size()>0){//有记录
				int ci_num = tolvoerlist.get(0).getYxy_count_ci_num() - month_nums;
				if(ci_num < 0){
					ci_num = 0;
				}
				tolvoerlist.get(0).setYxy_count_ci_num(ci_num);
				int tol_num = tolvoerlist.get(0).getYxy_count_tol_num() - addressnum * month_nums;
				if(tol_num < 0){
					tol_num = 0;
				}
				tolvoerlist.get(0).setYxy_count_tol_num(tol_num);
				yxymailcountdao.updatePojo(tolvoerlist.get(0));
			}
			
			//处理以上的发送量后处理以下业务
			String hql = "delete from YxyMarketingWeek where plan_id = "+plan_id;
			marketingweekdao.updateByHql(hql);
			hql = "delete from YxyMarketingPlan where plan_id = "+plan_id;
			marketingplandao.updateByHql(hql);
		}
	}
	
	private YxyMarketingPlanDao marketingplandao;//营销计划dao
	private YxyMarketingWeekDao marketingweekdao;//营销计划周期dao
	private YxyUserAddressDao yxyuseraddressdao;//用户地址dao
	private YxySendMailDetailDao yxysendmaildetaildao;//待发地址dao
	private YxyMailDayCountDao yxymaildaycountdao;//日统计
	private YxyMailMonthCountDao yxymailmonthcountdao;//月统计
	private YxyMailCountDao yxymailcountdao;//总统计
	private YxyOldcustomerCountsDao yxyoldcustomercountsdao;//CRM库发信月统计dao
	private YxyCustomerEmailDao yxycustomeremaildao;//客户dao
	private YxyMailFolderDao yxymailfolderdao;//文件类别dao
	private YxyCustomerTypeDao customertypedao;//客户分类dao
	private CrmProductDao crmproductdao;//意向产品dao
	private CrmSourcefromDao crmsourcefromdao;//客户来源dao
	private CrmStatusDao crmstatusdao;//客户状态dao
	private YxyMailStencilDao mailstencildao;//模板dao
	public YxyMarketingPlanDao getMarketingplandao() {
		return marketingplandao;
	}
	public void setMarketingplandao(YxyMarketingPlanDao marketingplandao) {
		this.marketingplandao = marketingplandao;
	}
	public YxyMarketingWeekDao getMarketingweekdao() {
		return marketingweekdao;
	}
	public void setMarketingweekdao(YxyMarketingWeekDao marketingweekdao) {
		this.marketingweekdao = marketingweekdao;
	}
	public YxyUserAddressDao getYxyuseraddressdao() {
		return yxyuseraddressdao;
	}
	public void setYxyuseraddressdao(YxyUserAddressDao yxyuseraddressdao) {
		this.yxyuseraddressdao = yxyuseraddressdao;
	}
	public YxySendMailDetailDao getYxysendmaildetaildao() {
		return yxysendmaildetaildao;
	}
	public void setYxysendmaildetaildao(YxySendMailDetailDao yxysendmaildetaildao) {
		this.yxysendmaildetaildao = yxysendmaildetaildao;
	}
	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 YxyOldcustomerCountsDao getYxyoldcustomercountsdao() {
		return yxyoldcustomercountsdao;
	}
	public void setYxyoldcustomercountsdao(
			YxyOldcustomerCountsDao yxyoldcustomercountsdao) {
		this.yxyoldcustomercountsdao = yxyoldcustomercountsdao;
	}
	public YxyCustomerEmailDao getYxycustomeremaildao() {
		return yxycustomeremaildao;
	}
	public void setYxycustomeremaildao(YxyCustomerEmailDao yxycustomeremaildao) {
		this.yxycustomeremaildao = yxycustomeremaildao;
	}
	public YxyMailFolderDao getYxymailfolderdao() {
		return yxymailfolderdao;
	}
	public YxyCustomerTypeDao getCustomertypedao() {
		return customertypedao;
	}
	public void setCustomertypedao(YxyCustomerTypeDao customertypedao) {
		this.customertypedao = customertypedao;
	}
	public CrmProductDao getCrmproductdao() {
		return crmproductdao;
	}
	public void setCrmproductdao(CrmProductDao crmproductdao) {
		this.crmproductdao = crmproductdao;
	}
	public CrmSourcefromDao getCrmsourcefromdao() {
		return crmsourcefromdao;
	}
	public void setCrmsourcefromdao(CrmSourcefromDao crmsourcefromdao) {
		this.crmsourcefromdao = crmsourcefromdao;
	}
	public CrmStatusDao getCrmstatusdao() {
		return crmstatusdao;
	}
	public void setCrmstatusdao(CrmStatusDao crmstatusdao) {
		this.crmstatusdao = crmstatusdao;
	}
	public void setYxymailfolderdao(YxyMailFolderDao yxymailfolderdao) {
		this.yxymailfolderdao = yxymailfolderdao;
	}
	public YxyMailStencilDao getMailstencildao() {
		return mailstencildao;
	}
	public void setMailstencildao(YxyMailStencilDao mailstencildao) {
		this.mailstencildao = mailstencildao;
	}
}