7e7eb7c892dfd2c01507082466e5ce3d19ec5948.svn-base 6.7 KB
package com.espeed.service.impl;
 
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.espeed.dao.YxyMailStencilDao;
import com.espeed.dao.YxyMailStencilHtmlDao;
import com.espeed.dao.YxyMailStencilTypeDao;
import com.espeed.pojo.YxyMailStencil;
import com.espeed.pojo.YxyMailStencilHtml;
import com.espeed.pojo.YxyMailStencilType;
import com.espeed.service.YxyMailStencilService;
import com.espeed.tool.ConfigPath;
import com.espeed.tool.createAndDeleteFile;
import com.espeed.vo.PageBean;

public class YxyMailStencilServiceImpl implements YxyMailStencilService {

	/**条件查询模版*/
	public List<YxyMailStencil> findByCondition(String loginid, String domain, PageBean pb,int condition, String conditionvalue) throws Exception {
		String hql="from YxyMailStencil where 1=1";
		String hqlcount="select count(*) from YxyMailStencil where 1=1";
		if(condition==1){//系统模版
			hql+=" and stencil_level=1";
			hqlcount+=" and stencil_level=1";
		}else if(condition==2){//企业模版
			hql+=" and stencil_level=2 and stencil_domain='"+domain+"'";
			hqlcount+=" and stencil_level=2 and stencil_domain='"+domain+"'";
		}else if(condition==3){//个人模版
			hql+=" and stencil_level=3 and stencil_domain='"+domain+"' and stencil_loginid='"+loginid+"'";
			hqlcount+=" and stencil_level=3 and stencil_domain='"+domain+"' and stencil_loginid='"+loginid+"'";
		}else if(condition==4){//系统模版分类查看
			hql+=" and stencil_level=1 and stencil_type="+conditionvalue;
			hqlcount+=" and stencil_level=1 and stencil_type="+conditionvalue;
		}else if(condition==5){//企业模版分类查看
			hql+=" and stencil_level=2 and stencil_domain='"+domain+"' and stencil_type="+conditionvalue;
			hqlcount+=" and stencil_level=2 and stencil_domain='"+domain+"' and stencil_type="+conditionvalue;
		}else if(condition==6){//个人模版分类查看
			hql+=" and stencil_level=3 and stencil_domain='"+domain+"' and stencil_loginid='"+loginid+"' and stencil_type="+conditionvalue;
			hqlcount+=" and stencil_level=3 and stencil_domain='"+domain+"' and stencil_loginid='"+loginid+"' and stencil_type="+conditionvalue;
		}else{
			return new ArrayList<YxyMailStencil>();
		}
		hql+=" order by stencil_time desc";
		hqlcount+=" order by stencil_time desc";
		return mailstencildao.findByHqlPage(hql, hqlcount, pb);
	}
	
	/**添加编辑模版*/
	public void addEditStencil(YxyMailStencil o) throws Exception {
		if(o.getStencil_id()>0){//编辑
			mailstencildao.updatePojo(o);
		}else{//添加
			mailstencildao.addPojo(o);
		}
	}
	
	/**删除模版*/
	public void delStencil(String stencilidstr) throws Exception {
		String hql="delete from YxyMailStencil where stencil_id in("+stencilidstr+")";
		mailstencildao.updateByHql(hql);
	}
	
	/**详细信息*/
	public YxyMailStencil findinfo(int stencilid) throws Exception {
		String hql="from YxyMailStencil where stencil_id="+stencilid;
		List<YxyMailStencil> list=mailstencildao.findByHql(hql);
		if(list.size()>0){
			YxyMailStencil stencil = list.get(0);
			hql = "from YxyMailStencilHtml where stencil_id = "+stencilid;
			List<YxyMailStencilHtml> htmls = yxymailstencilhtmldao.findByHql(hql);
			if(htmls.size() > 0){
				stencil.setWeb_url(ConfigPath.getStencilDomain()+htmls.get(0).getWeb_url());
			}
			return stencil;
		}else{
			return null;
		}
	}

	/**条件查询模版分类*/
	public List<YxyMailStencilType> findByConditionType(String loginid,String domain,int condition)throws Exception{
		String hql="from YxyMailStencilType where 1=1";
		if(condition==1){//系统级分类
			hql+=" and level=1";
		}else if(condition==2){//企业级分类
			hql+=" and level=2 and domain='"+domain+"'";
		}else if(condition==3){//个人级分类
			hql+=" and level=3 and domain='"+domain+"' and loginid='"+loginid+"'";
		}
		return mailstenciltypedao.findByHql(hql);
	}
	
	/**添加模版分类*/
	public void addStencilType(YxyMailStencilType o)throws Exception{
		if(o.getTypeid()>0){
			String hql="update YxyMailStencilType set typename='"+o.getTypename()+"' where typeid="+o.getTypeid();
			mailstenciltypedao.updateByHql(hql);
		}else{
			mailstenciltypedao.addPojo(o);
		}
	}
	
	/**删除编辑模版分类*/
	public void delStencilType(int typeid,int condition)throws Exception{
		if(condition==1){//删除模版
			String hql="delete YxyMailStencil where stencil_type="+typeid;
			mailstencildao.updateByHql(hql);
		}
		String hql="delete YxyMailStencilType where typeid="+typeid;
		mailstenciltypedao.updateByHql(hql);
	}
	
	/**验证模版分类是否重复*/
	public int findIsExits(String typename,int level,String domain,String loginid)throws Exception{
		String hql="select count(*) from YxyMailStencilType where";
		if(level==1){//系统
			hql+=" typename='"+typename+"'";
		}else if(level==2){//企业
			hql+=" typename='"+typename+"' and domain='"+domain+"'";
		}else if(level==3){//个人
			hql+=" typename='"+typename+"' and domain='"+domain+"' and loginid='"+loginid+"'";
		}
		int count=mailstenciltypedao.findByHqlCount(hql);
		if(count>0){
			return 0;
		}else{
			return 1;
		}
	}
	
	/**上传模板缩略图*/ 
	public String uploadStencilThumbnail(int user_id,String filePath,String fileName)throws Exception{
		File f = new File(filePath);
		//项目路径
		String pathew = this.getClass().getResource("/").getPath();
		pathew = pathew.substring(1, pathew.length());
		pathew = pathew.replace("/", "//");
		pathew = pathew.replace("//WEB-INF//classes//", "");
		String type = fileName.substring(fileName.lastIndexOf("."),fileName.length());
		String picname = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
		String newsave = user_id+picname+type;
		File create = new File(pathew+"//thumbnail");
		if(!create.exists()){ 
			create.mkdirs();
		}
		File newf = new File(pathew+"//thumbnail//"+newsave);//新文件
		createAndDeleteFile.filecopy(f, newf);
		newsave = newsave.replace("//", "/");
		newsave = "thumbnail/" + newsave;
		return newsave;
	}
	
	private YxyMailStencilDao mailstencildao;//模版dao
	private YxyMailStencilTypeDao mailstenciltypedao;//模版分类dao
	private YxyMailStencilHtmlDao yxymailstencilhtmldao;//模板对应的网页dao
	public YxyMailStencilDao getMailstencildao() {
		return mailstencildao;
	}
	public void setMailstencildao(YxyMailStencilDao mailstencildao) {
		this.mailstencildao = mailstencildao;
	}
	public YxyMailStencilTypeDao getMailstenciltypedao() {
		return mailstenciltypedao;
	}
	public void setMailstenciltypedao(YxyMailStencilTypeDao mailstenciltypedao) {
		this.mailstenciltypedao = mailstenciltypedao;
	}
	public YxyMailStencilHtmlDao getYxymailstencilhtmldao() {
		return yxymailstencilhtmldao;
	}
	public void setYxymailstencilhtmldao(YxyMailStencilHtmlDao yxymailstencilhtmldao) {
		this.yxymailstencilhtmldao = yxymailstencilhtmldao;
	}
}