YxySeoSetServiceImpl.java 35.2 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
package com.espeed.service.impl;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.espeed.centre.pojo.CentreSnsSet;
import com.espeed.centre.pojo.CentreUserContact;
import com.espeed.centre.pojo.CentreYxyedmAlibaba;
import com.espeed.centre.pojo.CentreYxyedmContact;
import com.espeed.centre.pojo.CentreYxyedmSeo;
import com.espeed.centre.pojo.YxyCompanyInfo;
import com.espeed.centre.pojo.YxyUserInfo;
import com.espeed.dao.CentreSnsSetDao;
import com.espeed.dao.CentreUserContactDao;
import com.espeed.dao.CentreYxyedmAlibabaDao;
import com.espeed.dao.CentreYxyedmContactDao;
import com.espeed.dao.CentreYxyedmSeoDao;
import com.espeed.dao.YxyMailStencilHeaderDao;
import com.espeed.dao.YxyMailStencilHtmlDao;
import com.espeed.dao.YxyMailStencilKeywordDao;
import com.espeed.pojo.YxyMailStencilHeader;
import com.espeed.pojo.YxyMailStencilHtml;
import com.espeed.pojo.YxyMailStencilKeyword;
import com.espeed.pojo.YxyStencilHtmlVo;
import com.espeed.service.YxySeoSetService;
import com.espeed.tool.ConfigPath;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

/***
 * @author 陈南巧
 * seo配置service实现类
 */
public class YxySeoSetServiceImpl implements YxySeoSetService {
	
	//添加编辑关键词配置
	public void addEditKeyword(YxyMailStencilKeyword keyword)throws Exception{
		if(keyword.getId() > 0){//编辑
			yxymailstencilkeyworddao.updatePojo(keyword);
		}else{//添加
			yxymailstencilkeyworddao.addPojo(keyword);
		}
	}
		
	//查询关键词配置
	public YxyMailStencilKeyword getKeyword(int stencil_id)throws Exception{
		String hql = "from YxyMailStencilKeyword where stencil_id = "+stencil_id;
		List<YxyMailStencilKeyword> keywords = yxymailstencilkeyworddao.findByHql(hql);
		if(keywords.size() > 0)
			return keywords.get(0);
		return null;
	}
	
	//查询seo配置
	public CentreYxyedmSeo getEdmSeoSet(int company_id)throws Exception{
		String hql = "from CentreYxyedmSeo where company_id = "+company_id;
		List<CentreYxyedmSeo> seos = centreyxyedmseodao.findByHql(hql);
		if(seos.size() > 0)
			return seos.get(0);
		return null;	
	}
	
	//查询阿里认证配置
	public CentreYxyedmAlibaba getXEdmAlibaba(int company_id)throws Exception{
		String hql = "from CentreYxyedmAlibaba where company_id = "+company_id;
		List<CentreYxyedmAlibaba> alibabas = centreyxyedmalibabadao.findByHql(hql);
		if(alibabas.size() > 0){
			return alibabas.get(0);
		}else{
			return null;
		}
	}
		
	//查询社交链接配置
	public CentreYxyedmContact getXEdmContact(int company_id)throws Exception{
		String hql = "from CentreYxyedmContact where company_id = "+company_id;
		List<CentreYxyedmContact> contacts = centreyxyedmcontactdao.findByHql(hql);
		if(contacts.size() > 0){
			return contacts.get(0);
		}else{
			return null;
		}
	}
	
	//查询企业信息
	public YxyCompanyInfo getCompanyInfo(int company_id)throws Exception{
		YxyCompanyInfo company = new YxyCompanyInfo();
		String sql = "select company_name,website,address,img_url,telephone from centre_company where company_id = "+company_id;
		List<Object> objects = centreyxyedmseodao.findBySqlFind(sql);
		Object[] obj_arr = (Object[])objects.get(0);
		company.setCompany_id(company_id);
		company.setCompany_name(obj_arr[0].toString());
		if(null!=obj_arr[1]&&!"".equals(obj_arr[1])){
			company.setWebsite(obj_arr[1].toString());
		}
		if(null!=obj_arr[2]&&!"".equals(obj_arr[2])){
			company.setAddress(obj_arr[2].toString());
		}
		if(null!=obj_arr[3]&&!"".equals(obj_arr[3])){
			company.setImg_url(obj_arr[3].toString());
		}
		if(null!=obj_arr[4]&&!"".equals(obj_arr[4])){
			company.setTelephone(obj_arr[4].toString());
		}
		return company;
	}

	//模板转网页
	public void updateStencilToHtml(YxyUserInfo user,YxyStencilHtmlVo vo)throws Exception{
		//获取配置文件中的存储路径
		String savePath = ConfigPath.getStencilHtmlUrl();
		//获取配置文件中的网页访问路径
		String urlPath = ConfigPath.getStencilDomain();
		//数据库保存的文件存储路径
		String dbSavePath = "";
		//数据库保存的网页访问路径
		String dbUrlPath = "";
		//品牌关键词转化为拼音
		String newBrandKeyword = vo.getBrand_keyword().replace(" ", "");
		newBrandKeyword = PinyinHelper.convertToPinyinString(newBrandKeyword, "", PinyinFormat.WITHOUT_TONE);
		//获取域名
		String newDomain = "";
		if(user.getDomain().split("\\.").length>2){
			String[] domain_arr = user.getDomain().split("\\.");
			newDomain = domain_arr[1];
		}else{
			newDomain = user.getDomain().replace(".", "");
		}
		newDomain = newDomain.replace("-", "");
		newDomain = newBrandKeyword + "-" + newDomain;
		//0行业推广,存储路径:savePath/行业/品牌关键词-域名/模板id.html,网页路径:urlPath/行业/品牌关键词-域名/模板id.html
		if(vo.getType() == 0){
			savePath = savePath + "/" + vo.getHangye() + "/" + newDomain;
			urlPath = urlPath + "/" + vo.getHangye() + "/" + newDomain;
			dbSavePath = "/" + vo.getHangye() + "/" + newDomain;
			dbUrlPath = "/" + vo.getHangye() + "/" + newDomain;
		}else{//1国家推广,存储路径:savePath/行业/国家/域名/模板id.html,网页路径:urlPath/国家/域名/模板id.html
			savePath = savePath + "/" + vo.getHangye() + "/" + vo.getCountry() + "/" + newDomain;
			urlPath = urlPath + "/" + vo.getCountry() + "/" + newDomain;
			dbSavePath = "/" + vo.getHangye() + "/" + vo.getCountry() + "/" + newDomain;
			dbUrlPath = "/" + vo.getHangye() + "/" + vo.getCountry() + "/" + newDomain;
		}
		//存储文件名
		String modul_title = vo.getModul_title();
		modul_title = PinyinHelper.convertToPinyinString(modul_title, "", PinyinFormat.WITHOUT_TONE);
		StringBuffer newModulTitle = new StringBuffer();
		String[] modul_title_arr = modul_title.split(" ");
		for(String str : modul_title_arr){
			newModulTitle.append(str + "-");
		}
		String fileName = newModulTitle.toString() + vo.getStencil_id() + ".html";
		//生成存储目录
		File file = new File(savePath);
		if(!file.exists()){
			file.mkdirs();
		}
		//获取模板网页路径
		String modulFilePath = this.getClass().getResource("/").getPath();
		modulFilePath = modulFilePath.substring(1, modulFilePath.length());
		modulFilePath = modulFilePath.replace("/WEB-INF/classes/", "");
		modulFilePath = modulFilePath + "/modulhtml/index.html";
		//根据模板网页路径生成buffer
		BufferedReader bufferReader = new BufferedReader(new InputStreamReader(new FileInputStream(modulFilePath),"UTF-8"));
		StringBuffer buffer = new StringBuffer();
		String msg = null;
		while((msg = bufferReader.readLine()) != null){
			buffer.append(msg);
			buffer.append("\n");
		}
		bufferReader.close();
		String htmlInfo = buffer.toString();
		//获取个人社交信息
		CentreUserContact userContact = getUserContact(user.getUser_id());
		//替换script
		htmlInfo=htmlInfo.replace("<script src=\"/tracker.jsp\" charset=\"UTF-8\">","<script src=\""+ConfigPath.getTrackUrl()+"/tracker.jsp\" charset=\"UTF-8\">");
		//替换description
		htmlInfo=htmlInfo.replace("<meta name=\"description\" content=\"\">", "<meta name=\"description\" content=\""+vo.getInput_description()+"\">");
		//替换keywords,关键词配置-关键词1,关键词2,主关键词,品牌关键词
		htmlInfo=htmlInfo.replace("<meta name=\"keywords\" content=\"\">", "<meta name=\"keywords\" content=\""+vo.getInput_keyword1()+","+vo.getInput_keyword2()+","+vo.getMain_keyword()+","+vo.getBrand_keyword()+"\">");
		//替换title,关键词配置-关键词1,关键词2|品牌关键词|模板标题
		htmlInfo=htmlInfo.replace("<title></title>", "<title>"+vo.getInput_keyword1()+","+vo.getInput_keyword2()+"|"+vo.getBrand_keyword()+"|"+vo.getModul_title()+"</title>");
		//替换og:title
		htmlInfo=htmlInfo.replace("<meta property=\"og:title\" content=\"\">", "<meta property=\"og:title\" content=\""+vo.getInput_keyword1()+","+vo.getInput_keyword2()+"|"+vo.getBrand_keyword()+"|"+vo.getModul_title()+"\">");
		//替换og:description
		htmlInfo=htmlInfo.replace("<meta property=\"og:description\" content=\"\">", "<meta property=\"og:description\" content=\""+vo.getInput_description()+"\">");
		//替换og:url
		htmlInfo=htmlInfo.replace("<meta property=\"og:url\" content=\"\">", "<meta property=\"og:url\" content=\""+urlPath + "/" + fileName+"\">");
		//替换og:img
		String modul_img = vo.getModul_img();
		if(modul_img!=null&&!"".equals(modul_img)){
			if(modul_img.contains("model/") || modul_img.contains("mailtempfile/")){
				modul_img = ConfigPath.getLocalWebUrl() + "/" + modul_img;
			}
			htmlInfo=htmlInfo.replace("<meta property=\"og:image\" content=\"\">", "<meta property=\"og:image\" content=\""+modul_img+"\">");
		}
		//只有生成头部时才执行下面的替换
		if(vo.getHas_header()==1){
			htmlInfo=htmlInfo.replace("header dn", "header");
			//替换头像
			if(vo.getImg_url()!=null&&!"".equals(vo.getImg_url())){
				htmlInfo=htmlInfo.replace("<img class=\"companyLogo\" src=\"https://edm1422.yiwaixiao.com/modulhtml/img/header.jpg\" />", "<img class=\"companyLogo\" src=\""+vo.getImg_url()+"\" />");
			}
			//替换company_name
			htmlInfo=htmlInfo.replace("<p class=\"hdCompony\"></p>", "<p class=\"hdCompony\">"+vo.getCompany_name()+"</p>");
			//替换website
			if(vo.getWebsite()!=null&&!vo.getWebsite().equals("")){
				String webUrl = vo.getWebsite();
				if(!webUrl.startsWith("http://") && !webUrl.startsWith("https://")){
					webUrl = "http://" + webUrl;
				}
				htmlInfo=htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"companyLink dn\" style=\"border: 1px solid #e5e5e5;padding: 5px 5px;\">", "<a href=\""+webUrl+"\" class=\"companyLink\" target=\"_blank\" style=\"border: 1px solid #e5e5e5;padding: 5px 5px;\">");
			}
			//替换底部的网址
			else if(vo.getCompany_website()!=null&&!"".equals(vo.getCompany_website())){ 
				String webUrl = vo.getCompany_website();
				if(!webUrl.startsWith("http://") && !webUrl.startsWith("https://")){
					webUrl = "http://" + webUrl;
				}
				htmlInfo=htmlInfo.replace("<span class=\"website dn\">Website:<a href=\"javascript:void(0)\" class=\"websiteLink\"></a>", "<span class=\"website\">Website:<a href=\""+webUrl+"\" class=\"websiteLink\" target=\"_blank\">"+webUrl+"</a>");
			}
			//信保认证
			if(vo.getIs_xinbao()==1){
				htmlInfo=htmlInfo.replace("iconCreditInsurance dn", "iconCreditInsurance");
			}
			//SGS认证
			if(vo.getIs_sgs()==1){
				htmlInfo=htmlInfo.replace("iconSgs dn", "iconSgs");
			}
			//诚信认证
			if(vo.getIs_trust()==1){
				htmlInfo=htmlInfo.replace("iconCredit dn", "iconCredit");
			}
			//视频认证
			if(vo.getIs_video()==1){
				htmlInfo=htmlInfo.replace("iconVideo dn", "iconVideo");
			}
			//经营年份
			if(vo.getAlibaba_year() > 0){
				htmlInfo=htmlInfo.replace("iconYearBox dn", "iconYearBox");
				htmlInfo=htmlInfo.replace("iconYears", "iconYears"+vo.getAlibaba_year());
			}
			//Amazon
			if(vo.getAmazon()!=null&&!vo.getAmazon().equals("")){
				htmlInfo=htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"hdcSpan amazon dn\">", "<a href=\""+vo.getAmazon()+"\" target=\"_blank\" class=\"hdcSpan amazon\">");
			}
			//Ebay
			if(vo.getEbay()!=null&&!vo.getEbay().equals("")){
				htmlInfo=htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"hdcSpan ebay dn\">", "<a href=\""+vo.getEbay()+"\" target=\"_blank\" class=\"hdcSpan ebay\">");
			}
			//Aliexpress
			if(vo.getAlieexpress()!=null&&!vo.getAlieexpress().equals("")){
				htmlInfo=htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"hdcSpan aliexpress dn\">", "<a href=\""+vo.getAlieexpress()+"\" target=\"_blank\" class=\"hdcSpan aliexpress\">");
			}
			//Wish
			if(vo.getWish()!=null&&!vo.getWish().equals("")){
				htmlInfo=htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"hdcSpan wish dn\">", "<a href=\""+vo.getWish()+"\" target=\"_blank\" class=\"hdcSpan wish\">");
			}
			//Dhgate
			if(vo.getDhgate()!=null&&!vo.getDhgate().equals("")){
				htmlInfo=htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"hdcSpan dhgate dn\">", "<a href=\""+vo.getDhgate()+"\" target=\"_blank\" class=\"hdcSpan dhgate\">");
			}
		}
		//替换Address
		if(vo.getAddress()!=null&&!"".equals(vo.getAddress())){
			htmlInfo=htmlInfo.replace("reachMain address dn", "reachMain address");
			htmlInfo=htmlInfo.replace("<span class=\"reachAddress\"></span>", "<span class=\"reachAddress\">"+vo.getAddress()+"</span>");
		}
		//替换Telephone
		//企业有电话
		if(vo.getTelephone()!=null && !"".equals(vo.getTelephone())){
			//个人有手机
			if(user.getMobile() != null && !"".equals(user.getMobile())){
				htmlInfo=htmlInfo.replace("reachMain phone dn", "reachMain phone");
				htmlInfo=htmlInfo.replace("<span class=\"reachPhone\"></span>", "<span class=\"reachPhone\">"+vo.getTelephone()+" "+user.getMobile()+"</span>");
			}
			else{//个人没有手机
				htmlInfo=htmlInfo.replace("reachMain phone dn", "reachMain phone");
				htmlInfo=htmlInfo.replace("<span class=\"reachPhone\"></span>", "<span class=\"reachPhone\">"+vo.getTelephone()+"</span>");
			}
		}else{//企业没有电话
			//个人有手机
			if(user.getMobile() != null && !"".equals(user.getMobile())){
				htmlInfo=htmlInfo.replace("reachMain phone dn", "reachMain phone");
				htmlInfo=htmlInfo.replace("<span class=\"reachPhone\"></span>", "<span class=\"reachPhone\">"+user.getMobile()+"</span>");
			}
		}
		//替换Email
		if(user.getMain_account()!=null&&!"".equals(user.getMain_account())){
			htmlInfo=htmlInfo.replace("reachMain email dn", "reachMain email");
			htmlInfo=htmlInfo.replace("<a class=\"reachEmail\" href=\"mailto:\"></a>", "<a class=\"reachEmail\" href=\"mailto:"+user.getMain_account()+"\">"+user.getMain_account()+"</a>");
		}
		//替换Skype
		if(userContact!=null&&userContact.getSkype()!=null&&!"".equals(userContact.getSkype())){
			htmlInfo=htmlInfo.replace("reachMain skype dn", "reachMain skype");
			htmlInfo=htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"reachSkype\"></a>", "<a href=\"https://www.addtoany.com/add_to/skype?linkurl="+URLEncoder.encode(userContact.getSkype(), "utf-8")+"\" class=\"reachSkype\" target=\"_blank\">"+userContact.getSkype()+"</a>");
		}
		//替换Whatsapp
		if(userContact!=null&&userContact.getWhatsapp()!=null&&!"".equals(userContact.getWhatsapp())){
			htmlInfo=htmlInfo.replace("reachMain whatsapp dn", "reachMain whatsapp");
			htmlInfo=htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"reachWhatsApp\"></a>", "<a href=\"https://www.addtoany.com/add_to/whatsapp?linkurl="+URLEncoder.encode(userContact.getWhatsapp(), "utf-8")+"\" class=\"reachWhatsApp\" target=\"_blank\">"+userContact.getWhatsapp()+"</a>");
		}
		//替换Website
		if(vo.getCompany_website()!=null&&!"".equals(vo.getCompany_website())){
			String webUrl = vo.getCompany_website();
			if(!webUrl.startsWith("http://") && !webUrl.startsWith("https://")){
				webUrl = "http://" + webUrl;
			}
			htmlInfo=htmlInfo.replace("reachMain website dn", "reachMain website");
			htmlInfo=htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"reachWebsite\"></a>", "<a href=\""+webUrl+"\" class=\"reachWebsite\" target=\"_blank\">"+webUrl+"</a>");
		}
		//处理各社交分享
		String urlFileName = urlPath + "/" + fileName;
		urlFileName = URLEncoder.encode(urlFileName, "utf-8");
		//facebook
		htmlInfo = htmlInfo.replace("<a class=\"Facebook\" href=\"javascript:void(0)\" title=\"Share on Facebook\">", "<a class=\"Facebook\" href=\"https://www.facebook.com/sharer.php?u="+urlFileName+"&t="+URLEncoder.encode(vo.getModul_title(), "utf-8")+"\" title=\"Share on Facebook\" target=\"_blank\">");
		//twitter
		htmlInfo = htmlInfo.replace("<a class=\"Twitter\" href=\"javascript:void(0)\" title=\"Share on Twitter\">", "<a class=\"Twitter\" href=\"https://twitter.com/share?text="+URLEncoder.encode(vo.getModul_title(), "utf-8")+"&url="+urlFileName+"\" title=\"Share on Twitter\" target=\"_blank\">");
		//google+
		htmlInfo = htmlInfo.replace("<a class=\"Google\" href=\"javascript:void(0)\" title=\"Share on Google\">", "<a class=\"Google\" href=\"https://plus.google.com/share?url="+urlFileName+"\" title=\"Share on Google\" target=\"_blank\">");
		//pinterest
		if(modul_img!=null&&!"".equals(modul_img)){
			htmlInfo = htmlInfo.replace("<a class=\"Pinterest\" href=\"javascript:void(0)\" title=\"Share on Pinterest\">", "<a class=\"Pinterest\" href=\"http://pinterest.com/pin/create/button/?url="+urlFileName+"&description="+URLEncoder.encode(vo.getModul_title(), "utf-8")+"&media="+URLEncoder.encode(modul_img, "utf-8")+"\" title=\"Share on Pinterest\" target=\"_blank\">");
		}
		//linkedin
		htmlInfo = htmlInfo.replace("<a class=\"Linkedin\" href=\"javascript:void(0)\" title=\"Share on Linkedin\">", "<a class=\"Linkedin\" href=\"http://linkedin.com/shareArticle?mini=true&url="+urlFileName+"\" title=\"Share on Linkedin\" target=\"_blank\">");
		//tumblr
		htmlInfo = htmlInfo.replace("<a class=\"Tumblr\" href=\"javascript:void(0)\" title=\"Share on Tumblr\">", "<a class=\"Tumblr\" href=\"http://www.tumblr.com/share/link?url="+urlFileName+"&name="+URLEncoder.encode(vo.getModul_title(), "utf-8")+"&description="+URLEncoder.encode(vo.getModul_title(), "utf-8")+"\" title=\"Share on Tumblr\" target=\"_blank\">");
		//vk
		htmlInfo = htmlInfo.replace("<a class=\"Vk\" href=\"javascript:void(0)\" title=\"Share on Vk\">", "<a class=\"Vk\" href=\"http://vk.com/share.php?url="+urlFileName+"\" title=\"Share on Vk\" target=\"_blank\">");
		//reddit
		htmlInfo = htmlInfo.replace("<a class=\"Reddit\" href=\"javascript:void(0)\" title=\"Share on Reddit\">", "<a class=\"Reddit\" href=\"http://reddit.com/submit?url="+urlFileName+"&title="+URLEncoder.encode(vo.getModul_title(), "utf-8")+"\" title=\"Share on Reddit\" target=\"_blank\">");
		//mail
		//htmlInfo = htmlInfo.replace("<a class=\"Mail\" href=\"javascript:void(0)\" title=\"Share on Mail\">", "<a class=\"Mail\" href=\"mailto:?subject="+URLEncoder.encode(vo.getModul_title(), "utf-8")+"&body="+URLDecoder.decode(urlFileName, "utf-8")+"\" title=\"Share on Mail\" target=\"_blank\">");
		
		//获取企业社交信息
		CentreYxyedmContact companyContact = getCompanyContact(user.getCompany_id());
		if(null!=companyContact && companyContact.getUser_first()==1){//个人社交信息优先
			if(null!=userContact){//有个人社交信息
				//处理facebook
				if(userContact.getFacebook()!=null&&!"".equals(userContact.getFacebook())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerFacebook dn\" title=\"Share on Facebook\">", "<a href=\""+userContact.getFacebook()+"\" class=\"iconLink footerFacebook\" title=\"Share on Facebook\" target=\"_blank\">");
				}
				//处理twitter
				if(userContact.getTwitter()!=null&&!"".equals(userContact.getTwitter())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerTwitter dn\" title=\"Share on Twitter\">", "<a href=\""+userContact.getTwitter()+"\" class=\"iconLink footerTwitter\" title=\"Share on Twitter\" target=\"_blank\">");
				}
				//处理google
				if(userContact.getGoogle()!=null&&!"".equals(userContact.getGoogle())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerGoogle dn\" title=\"Share on Google\">", "<a href=\""+userContact.getGoogle()+"\" class=\"iconLink footerGoogle\" title=\"Share on Google\" target=\"_blank\">");
				}
				//处理pinterest
				if(userContact.getPinterest()!=null&&!"".equals(userContact.getPinterest())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerPinterest dn\" title=\"Share on Pinterest\">", "<a href=\""+userContact.getPinterest()+"\" class=\"iconLink footerPinterest\" title=\"Share on Pinterest\" target=\"_blank\">");
				}
				//处理linkedin
				if(userContact.getLinkedin()!=null&&!"".equals(userContact.getLinkedin())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerLinkedin dn\" title=\"Share on Linkedin\">", "<a href=\""+userContact.getLinkedin()+"\" class=\"iconLink footerLinkedin\" title=\"Share on Linkedin\" target=\"_blank\">");
				}
				//处理vk
				if(userContact.getVk()!=null&&!"".equals(userContact.getVk())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerVk dn\" title=\"Share on Vk\">", "<a href=\""+userContact.getVk()+"\" class=\"iconLink footerVk\" title=\"Share on Vk\" target=\"_blank\">");
				}
				//处理reddit
				if(userContact.getReddit()!=null&&!"".equals(userContact.getReddit())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerReddit dn\" title=\"Share on Reddit\">", "<a href=\""+userContact.getReddit()+"\" class=\"iconLink footerReddit\" title=\"Share on Reddit\" target=\"_blank\">");
				}
				//处理tumblr
				if(userContact.getTumblr()!=null&&!"".equals(userContact.getTumblr())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerTumblr dn\" title=\"Share on Tumblr\">", "<a href=\""+userContact.getTumblr()+"\" class=\"iconLink footerTumblr\" title=\"Share on Tumblr\" target=\"_blank\">");
				}
				//处理instagram
				if(userContact.getInstragram()!=null&&!"".equals(userContact.getInstragram())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerInstagram dn\" title=\"Share on Instagram\">", "<a href=\""+userContact.getInstragram()+"\" class=\"iconLink footerInstagram\" title=\"Share on Instagram\" target=\"_blank\">");
				}
				//处理youtube
				if(userContact.getYoutube()!=null&&!"".equals(userContact.getYoutube())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerYoutube dn\" title=\"Share on Youtube\">", "<a href=\""+userContact.getYoutube()+"\" class=\"iconLink footerYoutube\" title=\"Share on Youtube\" target=\"_blank\">");
				}
			}
		}else{//企业社交信息优先或者无企业社交信息
			if(null!=companyContact){//有企业社交信息
				//处理facebook
				if(companyContact.getFacebook()!=null&&!"".equals(companyContact.getFacebook())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerFacebook dn\" title=\"Share on Facebook\">", "<a href=\""+companyContact.getFacebook()+"\" class=\"iconLink footerFacebook\" title=\"Share on Facebook\" target=\"_blank\">");
				}
				//处理twitter
				if(companyContact.getTwitter()!=null&&!"".equals(companyContact.getTwitter())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerTwitter dn\" title=\"Share on Twitter\">", "<a href=\""+companyContact.getTwitter()+"\" class=\"iconLink footerTwitter\" title=\"Share on Twitter\" target=\"_blank\">");
				}
				//处理google
				if(companyContact.getGoogle()!=null&&!"".equals(companyContact.getGoogle())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerGoogle dn\" title=\"Share on Google\">", "<a href=\""+companyContact.getGoogle()+"\" class=\"iconLink footerGoogle\" title=\"Share on Google\" target=\"_blank\">");
				}
				//处理pinterest
				if(companyContact.getPinterest()!=null&&!"".equals(companyContact.getPinterest())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerPinterest dn\" title=\"Share on Pinterest\">", "<a href=\""+companyContact.getPinterest()+"\" class=\"iconLink footerPinterest\" title=\"Share on Pinterest\" target=\"_blank\">");
				}
				//处理linkedin
				if(companyContact.getLinkedin()!=null&&!"".equals(companyContact.getLinkedin())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerLinkedin dn\" title=\"Share on Linkedin\">", "<a href=\""+companyContact.getLinkedin()+"\" class=\"iconLink footerLinkedin\" title=\"Share on Linkedin\" target=\"_blank\">");
				}
				//处理vk
				if(companyContact.getVk()!=null&&!"".equals(companyContact.getVk())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerVk dn\" title=\"Share on Vk\">", "<a href=\""+companyContact.getVk()+"\" class=\"iconLink footerVk\" title=\"Share on Vk\" target=\"_blank\">");
				}
				//处理reddit
				if(companyContact.getReddit()!=null&&!"".equals(companyContact.getReddit())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerReddit dn\" title=\"Share on Reddit\">", "<a href=\""+companyContact.getReddit()+"\" class=\"iconLink footerReddit\" title=\"Share on Reddit\" target=\"_blank\">");
				}
				//处理tumblr
				if(companyContact.getTumblr()!=null&&!"".equals(companyContact.getTumblr())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerTumblr dn\" title=\"Share on Tumblr\">", "<a href=\""+companyContact.getTumblr()+"\" class=\"iconLink footerTumblr\" title=\"Share on Tumblr\" target=\"_blank\">");
				}
				//处理instagram
				if(companyContact.getInstagram()!=null&&!"".equals(companyContact.getInstagram())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerInstagram dn\" title=\"Share on Instagram\">", "<a href=\""+companyContact.getInstagram()+"\" class=\"iconLink footerInstagram\" title=\"Share on Instagram\" target=\"_blank\">");
				}
				//处理youtube
				if(companyContact.getYoutube()!=null&&!"".equals(companyContact.getYoutube())){
					htmlInfo = htmlInfo.replace("<a href=\"javascript:void(0)\" class=\"iconLink footerYoutube dn\" title=\"Share on Youtube\">", "<a href=\""+companyContact.getYoutube()+"\" class=\"iconLink footerYoutube\" title=\"Share on Youtube\" target=\"_blank\">");
				}
			}
		}
		
		//替换底部的公司名
		htmlInfo = htmlInfo.replace("<span class=\"footerCompany\"></span>", "<span class=\"footerCompany\">Copyright "+Calendar.getInstance().get(Calendar.YEAR)+" "+vo.getCompany_name()+" | All Rights Reserved</span>");
		//替换底部的网址
		if(vo.getCompany_website()!=null&&!"".equals(vo.getCompany_website())){ 
			String webUrl = vo.getCompany_website();
			if(!webUrl.startsWith("http://") && !webUrl.startsWith("https://")){
				webUrl = "http://" + webUrl;
			}
			htmlInfo = htmlInfo.replace("<a class=\"footerWebsite\" href=\"javascript:void(0)\"></a>", " | Web site:<a class=\"footerWebsite\" href=\""+webUrl+"\" target=\"_blank\">"+webUrl+"</a>");
		}
		 
		//替换form提交的url
		htmlInfo = htmlInfo.replace("<form action=\"\" method=\"post\" id=\"fromData\">", "<form action=\""+ConfigPath.getSpreadUrl()+"\" method=\"post\" id=\"fromData\">");
		//替换form提交的subject
		htmlInfo = htmlInfo.replace("<input type=\"text\" name=\"subject\" id=\"subject_id\" value=\"\"", "<input type=\"text\" name=\"subject\" id=\"subject_id\" value=\""+vo.getModul_title()+"\"");
		//替换form提交的user_id、company_id、stencil_id
		htmlInfo = htmlInfo.replace("<input type=\"hidden\" name=\"user_id\" class=\"submitUser\" value=\"0\">", "<input type=\"hidden\" name=\"user_id\" class=\"submitUser\" value=\""+user.getUser_id()+"\">");
		htmlInfo = htmlInfo.replace("<input type=\"hidden\" name=\"company_id\" class=\"submitCompany\" value=\"0\">", "<input type=\"hidden\" name=\"company_id\" class=\"submitCompany\" value=\""+user.getCompany_id()+"\">");
		htmlInfo = htmlInfo.replace("<input type=\"hidden\" name=\"stencil_id\" class=\"submitStencil\" value=\"0\">", "<input type=\"hidden\" name=\"stencil_id\" class=\"submitStencil\" value=\""+vo.getStencil_id()+"\">");
		
		//替换模板内容中的图片路径
		String body = vo.getModul_body().replace("src=\"/mailtempfile/", "src=\""+ConfigPath.getLocalWebUrl()+"/mailtempfile/");
		//替换模板内容
		htmlInfo = htmlInfo.replace("<div class=\"pageMain\"></div>", "<div class=\"pageMain\">"+body+"</div>");
		
		//替换推广链接信息
		htmlInfo = replaceSpreadInfo(htmlInfo);
		
		//替换跟踪统计信息
		htmlInfo = rebuildHtmlMail(htmlInfo,vo.getStencil_id(),user.getUser_id(),user.getCompany_id());
		
		//替换跟踪图片的链接信息
		htmlInfo = htmlInfo.replace("/track_recordFromHTML.action?", ConfigPath.getTrackUrl()+"/track_recordFromHTML.action?");
		
		//获取该模板对应的hmtl信息
		String hql = "from YxyMailStencilHtml where stencil_id = "+vo.getStencil_id();
		List<YxyMailStencilHtml> htmls = yxymailstencilhtmldao.findByHql(hql);
		if(htmls.size() > 0){//编辑
			YxyMailStencilHtml html = htmls.get(0);
			html.setHtml_url(dbSavePath + "/" + fileName);//文件存储路径
			html.setWeb_url(dbUrlPath + "/" + fileName);//网页访问路径
			yxymailstencilhtmldao.updatePojo(html);
		}else{//添加
			YxyMailStencilHtml html = new YxyMailStencilHtml();
			html.setStencil_id(vo.getStencil_id());
			html.setHtml_url(dbSavePath + "/" + fileName);//文件存储路径
			html.setWeb_url(dbUrlPath + "/" + fileName);//网页访问路径
			yxymailstencilhtmldao.addPojo(html);
		}
		
		//修改该模板的sns分享图片路径
		hql = "update YxyMailStencilHeader set sns_img_url = '"+modul_img+"' where stencil_id = "+vo.getStencil_id();
		yxymailstencilheaderdao.updateByHql(hql);
		
		//写入新的html
		Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savePath+ "/" + fileName),"UTF-8"));
		writer.write(htmlInfo);
		writer.close();
	}
	
	//查询模板生成网页的访问路径
	public String getHtmlWebUrl(int stencil_id)throws Exception{
		String hql = "from YxyMailStencilHtml where stencil_id = "+stencil_id;
		List<YxyMailStencilHtml> htmls = yxymailstencilhtmldao.findByHql(hql);
		if(htmls.size() > 0){
			return ConfigPath.getStencilDomain()+htmls.get(0).getWeb_url();
		}
		return null;
	}
	
	//获取个人社交信息
	public CentreUserContact getUserContact(int user_id)throws Exception{
		String hql = "from CentreUserContact where user_id = "+user_id;
		List<CentreUserContact> contacts = centreusercontactdao.findByHql(hql);
		if(contacts.size() > 0){
			return contacts.get(0);
		}
		return null;
	}
	
	//获取企业社交信息
	public CentreYxyedmContact getCompanyContact(int company_id)throws Exception{
		String hql = "from CentreYxyedmContact where company_id = "+company_id;
		List<CentreYxyedmContact> contacts = centreyxyedmcontactdao.findByHql(hql);
		if(contacts.size() > 0){
			return contacts.get(0);
		}
		return null;
	}
	
	//替换推广链接
	private String replaceSpreadInfo(String body){
		Document doc = Jsoup.parse(body);
		Elements elements = doc.select("a");
		if (elements.size() > 0) {
			for (Element element : elements) {
				if(element.attr("href").indexOf(ConfigPath.getStencilDomain())!=-1){
					body = body.replace("<a href=\""+element.attr("href")+"\"", "<a href=\""+element.attr("href")+"?edmtitle="+"\"");
				}
			}
		}
		return body;
	}
	
	//替换跟踪链接
	private String rebuildHtmlMail(String body,int stencil_id, int user_id, int company_id)throws Exception {
		Document doc = Jsoup.parse(body);
		Elements elements = doc.select("a[ywx-mark-track=true]");
		if (elements.size() > 0) {
			Set<String> set = new HashSet<String>();
			String href = null;
			String url = null;
			for (Element element : elements) {
				href = element.attr("href");
				if (href != null && !href.trim().isEmpty()) {
					set.add(href.trim() + "@espeed@" + element.text());
				}
			}
			if (set.size() > 0) {
				String[] hres = null;
				for (String hre : set) {
					hres = hre.split("@espeed@");
					url = ConfigPath.getTrackUrl() + "/chain.jsp?ywxType=4&ywxUid="
							+ user_id + "&ywxCid=" + company_id + "&ywxMid="
							+ stencil_id + "&ywxTo="
							+ URLEncoder.encode(hres[0], "UTF-8");
					if (hres.length > 1) {
						url += "&ywxTitle=" + URLEncoder.encode(hres[1], "UTF-8");
					}
					body = body.replace("href=\""+hres[0]+"\"", "href=\""+url+"\"");
				}
			}
		}
		return body;
	}
	
	//获取模板推广头部生成策略
	public YxyMailStencilHeader getHtmlHeader(int stencil_id)throws Exception{
		String hql = "from YxyMailStencilHeader where stencil_id = "+stencil_id;
		List<YxyMailStencilHeader> headers = yxymailstencilheaderdao.findByHql(hql);
		if(headers.size() > 0){
			return headers.get(0);
		}else{
			return null;
		}
	}
	
	//更改模板推广头部生成策略
	public void addEditHtmlHeader(int stencil_id,int has_header,String sns_img_url)throws Exception{
		String hql = "from YxyMailStencilHeader where stencil_id = "+stencil_id;
		List<YxyMailStencilHeader> headers = yxymailstencilheaderdao.findByHql(hql);
		if(headers.size() > 0){//编辑
			headers.get(0).setHas_header(has_header);
			headers.get(0).setSns_img_url(sns_img_url);
			yxymailstencilheaderdao.updatePojo(headers.get(0));
		}else{//添加
			YxyMailStencilHeader header = new YxyMailStencilHeader();
			header.setHas_header(has_header);
			header.setSns_img_url(sns_img_url);
			header.setStencil_id(stencil_id);
			yxymailstencilheaderdao.addPojo(header);
		}
	}
	
	/**查询sns配置*/
	public CentreSnsSet getSnsSet(int company_id)throws Exception{
		String hql = "from CentreSnsSet where company_id = "+company_id;
		List<CentreSnsSet> sets = centresnssetdao.findByHql(hql);
		if(sets.size() > 0)
			return sets.get(0);
		return null;
	}

	private YxyMailStencilKeywordDao yxymailstencilkeyworddao;//关键词dao
	private YxyMailStencilHtmlDao yxymailstencilhtmldao;//html存储dao
	private CentreYxyedmSeoDao centreyxyedmseodao;//seo配置dao
	private CentreYxyedmAlibabaDao centreyxyedmalibabadao;//阿里认证dao
	private CentreYxyedmContactDao centreyxyedmcontactdao;//社交资料dao
	private CentreUserContactDao centreusercontactdao;//个人社交dao
	private YxyMailStencilHeaderDao yxymailstencilheaderdao;//网页头部生成策略dao
	private CentreSnsSetDao centresnssetdao;//sns配置dao
	public YxyMailStencilKeywordDao getYxymailstencilkeyworddao() {
		return yxymailstencilkeyworddao;
	}
	public void setYxymailstencilkeyworddao(
			YxyMailStencilKeywordDao yxymailstencilkeyworddao) {
		this.yxymailstencilkeyworddao = yxymailstencilkeyworddao;
	}
	public YxyMailStencilHtmlDao getYxymailstencilhtmldao() {
		return yxymailstencilhtmldao;
	}
	public void setYxymailstencilhtmldao(YxyMailStencilHtmlDao yxymailstencilhtmldao) {
		this.yxymailstencilhtmldao = yxymailstencilhtmldao;
	}
	public CentreYxyedmSeoDao getCentreyxyedmseodao() {
		return centreyxyedmseodao;
	}
	public void setCentreyxyedmseodao(CentreYxyedmSeoDao centreyxyedmseodao) {
		this.centreyxyedmseodao = centreyxyedmseodao;
	}
	public CentreYxyedmAlibabaDao getCentreyxyedmalibabadao() {
		return centreyxyedmalibabadao;
	}
	public void setCentreyxyedmalibabadao(
			CentreYxyedmAlibabaDao centreyxyedmalibabadao) {
		this.centreyxyedmalibabadao = centreyxyedmalibabadao;
	}
	public CentreYxyedmContactDao getCentreyxyedmcontactdao() {
		return centreyxyedmcontactdao;
	}
	public void setCentreyxyedmcontactdao(
			CentreYxyedmContactDao centreyxyedmcontactdao) {
		this.centreyxyedmcontactdao = centreyxyedmcontactdao;
	}
	public CentreUserContactDao getCentreusercontactdao() {
		return centreusercontactdao;
	}
	public void setCentreusercontactdao(CentreUserContactDao centreusercontactdao) {
		this.centreusercontactdao = centreusercontactdao;
	}
	public YxyMailStencilHeaderDao getYxymailstencilheaderdao() {
		return yxymailstencilheaderdao;
	}
	public void setYxymailstencilheaderdao(
			YxyMailStencilHeaderDao yxymailstencilheaderdao) {
		this.yxymailstencilheaderdao = yxymailstencilheaderdao;
	}
	public CentreSnsSetDao getCentresnssetdao() {
		return centresnssetdao;
	}
	public void setCentresnssetdao(CentreSnsSetDao centresnssetdao) {
		this.centresnssetdao = centresnssetdao;
	}
}