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

📄 contactdaoimpl.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.hibernate.Transaction;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yuanchung.sales.dao.customer.ContactDAO;
import com.yuanchung.sales.exception.SystemException;
import com.yuanchung.sales.model.customer.Customer;
import com.yuanchung.sales.model.customer.CustomerContact;
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 ContactDAOImpl extends HibernateDaoSupport implements ContactDAO {
	// 保存联系人
	public void save(CustomerContact contact) throws DataAccessException {
		try {
			getHibernateTemplate().save(contact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 根据用户查找客户
	public List getCustomerByUser(User user, int flag)
			throws DataAccessException {
		return getHibernateTemplate().find(
				"from Customer as c where c.user=? and c.flag=?",
				new Object[] { user, flag });
	}

	// 查找所有的客户
	public List getAllCusomer() throws DataAccessException {
		try {
			return getHibernateTemplate()
					.find("from Customer c where c.flag=1");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 根据名称模糊查询客户
	public List getCustomerByNamelike(User user, String customerName)
			throws DataAccessException {
		return getHibernateTemplate()
				.find(
						"from Customer as c where 1=1 and c.flag=1 and c.user=? and c.customerName like '%"
								+ customerName + "%'", user);
	}

	// 根据名称查找客户
	public Customer getCustomerByName(String name) throws DataAccessException {
		try {
			return (Customer) getHibernateTemplate()
					.find(
							"from Customer as c where 1=1 and c.flag=1 and c.customerName=?",
							name).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 根据用户查找联系人
	// from CustomerContact cc left join cc.userContacts uc where 1=1 and
	// cc.flag=1 uc.user=?
	public List getContactByUser(User user) throws DataAccessException {
		// try {
		return getHibernateTemplate()
				.find(
						"from CustomerContact cc where cc.flag=1 and cc.user=? order by  cc.lastModifyTime desc",
						user);
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// return null;
	}

	public List getContactByUser(String userIds) {
		StringBuffer hql = new StringBuffer("from CustomerContact cc where cc.flag=1 and ");
		hql.append(" cc.user in (" + userIds + ") order by  cc.lastModifyTime desc");
		return getHibernateTemplate()
				.find(hql.toString());
	}

	// 根据查找联系人
	public List getContact() throws DataAccessException {
		try {
			return getHibernateTemplate().find(
					"from CustomerContact cc where cc.flag=1");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 保存用户自定义选项
	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 getContactByUserDefined(String hql) throws DataAccessException {
		return getHibernateTemplate().find(hql);
	}

	// 根据id搜索选项
	public UserDefined getUserDefinedById(int userDefinedId)
			throws DataAccessException {
		try {
			return (UserDefined) getHibernateTemplate().get(UserDefined.class,
					userDefinedId);
		} 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 CustomerContact getById(int contactId) throws DataAccessException {
		try {
			return (CustomerContact) getHibernateTemplate().get(
					CustomerContact.class, contactId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 删除联系人
	public void delete(CustomerContact customerContact)
			throws DataAccessException {
		try {
			getHibernateTemplate().delete(customerContact);
		} 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 void updateUserField(UserField userField) throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(userField);
	}

	// 更新联系人
	public void update(CustomerContact contact) throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(contact);
	}

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

	/**
	 * 根据名称搜索联系人;
	 */
	public List getContactByName(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_contact where 1=1 and flag=1 and Name like '%"
								+ nameLike + "%'").addEntity(
						CustomerContact.class);
				List list = query.list();
				return list;
			}

		});

	}

	/**
	 * 搜索左边页面的头几条联系人;
	 */
	public List<CustomerContact> getTopContact() throws DataAccessException {
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// 内连接两张表;根据关系表排列时间来搜索联系人记录;
				Query query = session.createSQLQuery(
						"SELECT c.* FROM customer_contact c where c.flag=1;")
						.addEntity(CustomerContact.class);
				List<CustomerContact> list = query.list();
				return list;
			}

		});
	}

	/**
	 * 显示被冻结的联系人列表;
	 */
	public List getContactByDelete(User user, int flag)
			throws DataAccessException {
		try {
			return getHibernateTemplate()
					.find(
							"from CustomerContact as cc where cc.user=? and cc.flag=? order by cc.lastModifyTime desc",
							new Object[] { user, flag });
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("find delete data error");
			throw new SystemException("find delete data error");
		}
	}

	/**
	 * 添加联系人和联系人关联表;
	 */
	public void addContact(CustomerContact contact) throws DataAccessException {
		try {
			this.getHibernateTemplate().save(contact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取客户;
	 */
	public Customer getCustomerById(Integer id) throws DataAccessException {
		try {
			return (Customer) this.getHibernateTemplate().get(Customer.class,
					id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据联系人搜索用户-联系人;
	 */
	public List getUserContactByCon(CustomerContact contact)
			throws DataAccessException {
		try {
			return this.getHibernateTemplate().find(
					"from UserContact uc where uc.customerContact=?", contact);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void delete(String sql) throws DataAccessException {
		try {
			getHibernateTemplate().bulkUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 查找最新联系人
	public CustomerContact findContactLastest() throws DataAccessException {
		return (CustomerContact) getHibernateTemplate()
				.find(
						"from CustomerContact as cc where cc.id >= (select max(c.id) from CustomerContact as c)")
				.get(0);
	}

	// 找出上次修改人
	public User findLastModifyMan(int userId) throws DataAccessException {
		return (User) getHibernateTemplate().find(
				"from User as u where u.id=?", userId).get(0);
	}

	// 根据联系人ID查找业务机会
	public List getBusiOpportsByContactId(int contactId)
			throws DataAccessException {
		try {
			return getHibernateTemplate()
					.find(
							"select bo from BusinessOpportunity as bo join bo.contactBusiopports as cbs where cbs.customerContact.id=?",
							contactId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 根据姓名模糊查找联系人
	public List getContactsByName(User user, String name, int flag)
			throws DataAccessException {
		try {
			return getHibernateTemplate().find(
					"from CustomerContact as cc where cc.user=? and cc.name like '%"
							+ name + "%' " + " and cc.flag=?",
					new Object[] { user, flag });
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 更新联系人
	public void updateContacts(int modifyManId, String modifyTime,
			int customerId, int flag) throws DataAccessException {
		try {
			getHibernateTemplate()
					.bulkUpdate(
							"update CustomerContact as cc set cc.flag=?, cc.modifyManId=?, cc.lastModifyTime=?  where cc.customer.id=?",
							new Object[] { flag, modifyManId, modifyTime,
									customerId });
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 根据联系ID查找
	public void updateContactById(int modifyManId, String modifyTime,
			int contactId, int flag) throws DataAccessException {
		try {
			getHibernateTemplate()
					.bulkUpdate(
							"update CustomerContact as cc set cc.flag=?, cc.modifyManId=?, cc.lastModifyTime=?  where cc.id=?",
							new Object[] { flag, modifyManId, modifyTime,
									contactId });
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("find cotact exception!");
			throw new SystemException("find cotact exception!");
		}
	}

	// 根据ID查找用户
	public User getUserById(int userId) throws DataAccessException {
		try {
			return (User) getHibernateTemplate().get(User.class, userId);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("find user exception!");
			throw new SystemException("find user exception!");
		}
	}

	// 根据客户ID删除业务机会
	public void deleteByCustomerId(int customerId, int flag)
			throws DataAccessException {
		try {
			getHibernateTemplate()
					.bulkUpdate(
							"delete from BusinessOpportunity as bo where bo.customer.id=? and bo.flag=?",
							new Object[] { customerId, flag });
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			throw new SystemException(e.getMessage());
		}
	}
}

⌨️ 快捷键说明

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