⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customerdaoimpl.java

📁 基于Sturts+Spring+Hibernate的一个高级销售管理系统。内容丰富
💻 JAVA
字号:
package com.yuanchung.sales.dao.customer.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yuanchung.sales.dao.customer.CustomerDAO;
import com.yuanchung.sales.exception.SystemException;
import com.yuanchung.sales.model.businessOpportunity.BusinessOpportunity;
import com.yuanchung.sales.model.customer.Customer;
import com.yuanchung.sales.model.customer.CustomerContact;

import com.yuanchung.sales.model.service.CustAccount;
import com.yuanchung.sales.model.service.ServiceOrder;
import com.yuanchung.sales.model.user.User;
import com.yuanchung.sales.model.userDefined.UserDefined;
import com.yuanchung.sales.model.userDefined.UserField;
import com.yuanchung.sales.model.userDefined.UserFilter;

public class CustomerDAOImpl extends HibernateDaoSupport implements CustomerDAO {
	// 根据用户取得所有的客户
	public List getByUser(User user, int flag) throws DataAccessException {
		try {
			return getHibernateTemplate()
					.find(
							"from Customer as c where c.user=? and c.flag=? order by c.lastModifyTime desc ",
							new Object[] { user, flag });
		} catch (RuntimeException e) {
			logger.error(e);
			throw e;
		}
	}

	public List getByUser(String userIds, int flag) {
		StringBuffer hql = new StringBuffer("");
		hql.append("from Customer as c where c.user in (" + userIds + ") ");
		hql.append("and c.flag=" + flag +  "order by c.lastModifyTime desc");
		return getHibernateTemplate().find(hql.toString());
	}

	// 查找相关的客户
	public List getCustomerByName(final String nameLike)
			throws DataAccessException {
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createSQLQuery(
						"select * from customer c where 1=1 and c.flag=1 and customer_name like '%"
								+ nameLike + "%';").addEntity(Customer.class);// 注意:必须转化成对象来映射;
				List list = query.list();
				return list;
			}
		});
	}

	// 保存客户
	public void save(Customer customer) throws DataAccessException {
		try {
			getHibernateTemplate().saveOrUpdate(customer);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 保存用户自定义选项
	public void saveUserDefined(UserDefined userDefined)
			throws DataAccessException {
		try {
			getHibernateTemplate().save(userDefined);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 保存自定义选项过滤条件
	public void saveUserFilter(UserFilter userFilter)
			throws DataAccessException {
		try {
			getHibernateTemplate().save(userFilter);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 保存用户显示字段
	public void saveUserField(UserField userField) throws DataAccessException {
		try {
			getHibernateTemplate().save(userField);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// /根据用户和类型查找选项
	public List getUserDefinedByUserAndType(User user, int type)
			throws DataAccessException {
		return getHibernateTemplate().find(
				"from UserDefined as ud where ud.user=? and ud.type=?",
				new Object[] { user, type });
	}

	// 根据用户和自定义选项搜索客户
	public List<Customer> getByUserAndUserDefined(User user,
			UserDefined userDefined) throws DataAccessException {
		return getHibernateTemplate().find(
				"from Customer as c where c.user=? and c.flag=1", user);
	}

	// 根据id搜索选项
	public UserDefined getUserDefinedById(int userDefinedId)
			throws DataAccessException {
		try {
			return (UserDefined) getHibernateTemplate().get(UserDefined.class,
					userDefinedId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 根据用户自定义查询语句查找客户
	public List<Customer> getCustomerByUserHql(String hql)
			throws DataAccessException {
		try {
			return getHibernateTemplate().find(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 根据选项搜索显示字段
	public UserField getUserFieldByOption(UserDefined userDefined)
			throws DataAccessException {
		return (UserField) getHibernateTemplate().find(
				"from UserField as uf where uf.userDefined=?", userDefined)
				.get(0);
	}

	// 根据id查找客户
	public Customer getById(int customerId) throws DataAccessException {
		try {
			return (Customer) getHibernateTemplate().get(Customer.class,
					customerId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 修改客户
	public void update(Customer customer) throws DataAccessException {
		try {
			getHibernateTemplate().saveOrUpdate(customer);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 删除用户
	public void delete(Customer customer) throws DataAccessException {
		try {
			getHibernateTemplate().delete(customer);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 根据选项查找过滤条件
	public List getUserFilterByOption(UserDefined userDefined)
			throws DataAccessException {
		return getHibernateTemplate().find(
				"from UserFilter as uf where uf.userDefined=?", userDefined);
	}

	// 更新用户选项
	public void updateUserDefined(UserDefined userDefined)
			throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(userDefined);
	}

	// 根据id查询过滤条件
	public UserFilter getUserFilter(int id) throws DataAccessException {
		return (UserFilter) getHibernateTemplate().get(UserFilter.class, id);
	}

	// 更新过滤条件
	public void updateUserFilter(UserFilter userFilter)
			throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(userFilter);
	}

	// 根据选项搜索显示字段
	public UserField getUserFieldByUserDefined(UserDefined userDefined)
			throws DataAccessException {
		return (UserField) getHibernateTemplate().find(
				"from UserField as uf where uf.userDefined=?", userDefined)
				.get(0);
	}

	// 修改显示字段
	public void updateUserField(UserField userField) throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(userField);
	}

	// 删除过滤条件
	public void deleteFilter(UserFilter userFilter) throws DataAccessException {
		getHibernateTemplate().delete(userFilter);
	}

	/**
	 * 获取最新头几个客户;
	 */
	public List<Customer> getTopCustomer() throws DataAccessException {
		return this
				.getHibernateTemplate()
				.find(
						"FROM Customer c where 1=1 and c.flag=1 order by createTime desc limit 0,8");
	}

	/**
	 * 冻结该客户;
	 */
	public void clearCustomer(final Integer id) throws DataAccessException {
		this.getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sql = new StringBuffer(
						"update Customer set flag = 1 where id =" + id);
				session.createQuery(sql.toString()).executeUpdate();
				return null;
			}
		});
	}

	/**
	 * 搜索冻结客户列表;
	 */
	public List getCustomerByDelete(User user, int flag)
			throws DataAccessException {
		try {
			return getHibernateTemplate()
					.find(
							"from Customer c where c.user=? and c.flag=? order by c.lastModifyTime desc",
							new Object[] { user, flag });
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("find data exception");
			throw new SystemException("find data exception");
		}
	}

	/**
	 * 彻底删除客户以及相关信息;
	 */
	public boolean deleteCustomerAndSome(Integer id) throws DataAccessException {
		try {
			Customer customer = (Customer) this.getHibernateTemplate().get(
					Customer.class, id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 根据客户搜索联系人;
	 */
	public List getCusConByCus(Customer customer) throws DataAccessException {
		try {
			return this.getHibernateTemplate().find(
					"from CustomerContact cc where 1=1 and cc.customer=?",
					customer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据客户搜索业务机会列表;
	 */
	public List getOpportByCus(Customer customer) throws DataAccessException {
		try {
			return getHibernateTemplate().find(
					"from BusinessOpportunity bo where 1=1 and bo.customer=?",
					customer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据业务机会搜索业务活动列表;
	 */
	public List getPlanByOpport(BusinessOpportunity businessOpportunity)
			throws DataAccessException {
		try {
			return this
					.getHibernateTemplate()
					.find(
							"from BusinessActivityPlan bap where 1=1 and bap.businessopportunity=?",
							businessOpportunity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 删除该业务机会
	 */
	public boolean deleteOpport(BusinessOpportunity bo)
			throws DataAccessException {
		try {
			this.getHibernateTemplate().delete(bo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/*
	 * public boolean deleteCusCon(List listCusCon) throws DataAccessException {
	 * try { this.getHibernateTemplate().deleteAll(listCusCon); return true; }
	 * catch (Exception e) { e.printStackTrace(); } return false; }
	 */

	/**
	 * 搜索该客户的用户客户联系表;
	 */
	public List getUserConListByCusCon(CustomerContact cusCon)
			throws DataAccessException {
		try {
			return this.getHibernateTemplate().find(
					"from UserContact uc where uc.customerContact=?", cusCon);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 删除该客户的用户联系人关联表;
	 */
	public boolean deleteListUserCon(List listUserCon)
			throws DataAccessException {
		try {
			this.getHibernateTemplate().deleteAll(listUserCon);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 删除该用户联系人;
	 */
	public boolean deleteCusCon(CustomerContact cusCon)
			throws DataAccessException {
		try {
			this.getHibernateTemplate().delete(cusCon);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 删除该客户的联系人;
	 */
	public boolean updateCusCon(CustomerContact cusCon)
			throws DataAccessException {
		try {
			this.getHibernateTemplate().update(cusCon);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 修改业务机会;
	 */
	public boolean updateOpport(BusinessOpportunity businessOpportunity)
			throws DataAccessException {
		try {
			this.getHibernateTemplate().update(businessOpportunity);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	// 根据客户ID搜索联系人
	public List findContactByCustomerId(int customerId)
			throws DataAccessException {
		try {
			return getHibernateTemplate()
					.find(
							"from CustomerContact as cc where cc.flag=1 and cc.customer.id=?",
							customerId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 根据客户ID搜索业务机会
	public List findBusiOpportByCustomerId(int customerId)
			throws DataAccessException {
		try {
			return getHibernateTemplate()
					.find(
							"from BusinessOpportunity as bo where bo.flag=1 and bo.customer.id=?",
							customerId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 根据用户和相关项取得未处理的任务
	public List findTaskByFunctionIdAndRecordId(User user, int functionId,
			int recordaid) throws DataAccessException {
		try {
			return getHibernateTemplate()
					.find(
							"from ActivityRask as ar where ar.user=? and ar.functionId=? and ar.recordId=?",
							new Object[] { user, functionId, recordaid });
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// ---------------------------------------------更新后--------------------------------------------------------//
	// 查找最新保存的客户
	public Customer getCustomerLastest() throws DataAccessException {
		try {
			return (Customer) getHibernateTemplate()
					.find(
							"from Customer as c where c.id >=(select max(cc.id) from Customer as cc)")
					.get(0);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 查找上次修改人
	public User getUserById(int userId) throws DataAccessException {
		try {
			return (User) getHibernateTemplate().get(User.class, userId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 更新客户的状态
	public void updateCustomers(int modifyManId, String modifiTime,
			int customerId, int flag) throws DataAccessException {
		try {
			getHibernateTemplate()
					.bulkUpdate(
							"update Customer as c set c.flag=?, c.modifyManId=?, c.lastModifyTime=? where c.id=?",
							new Object[] { flag, modifyManId, modifiTime,
									customerId });
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("update customer error");
			throw new SystemException("update customer error");
		}
	}

	// 根据名称模糊查找所有的客户
	public List getCustoemrsByNameLike(int flag, User user, String name)
			throws DataAccessException {
		try {
			return getHibernateTemplate().find(
					"from Customer as c where c.flag=? and c.user=? and c.customerName like '%"
							+ name + "%'", new Object[] { flag, user });
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("find customer error");
			throw new SystemException("find customer error");
		}
	}
	//************小洪2009-2-28**************//
	//取得ID最大的客户
	public Customer getCustomerByMaxId(){
		Customer customer = null;
		try {
			String queryString1 = "from Customer c";
			String queryString2 = "from Customer c where c.id = (select max(id) from Customer where 1=1)";
			List<Customer> solist1 = super.getSession().createQuery(
					queryString1).list();
			if (solist1.size()>0) {
				List<Customer> solist2 = super.getSession().createQuery(
						queryString2).list();
				customer = solist2.get(0);
			}
		} catch (RuntimeException re) {
			re.getMessage();
			throw re;
		}
		return customer;
	}
	//保存客户账号
	public void saveCustAccount(CustAccount custAccount){
		try {
			getHibernateTemplate().save(custAccount);
		} catch (RuntimeException re) {
			re.getMessage();
			throw re;
		}
	}
	//*************小洪2009-2-28end****************//
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -