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

📄 usermanagerimpl.java

📁 拍卖系统
💻 JAVA
字号:
package com.dagang.bidsys.manager.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

import com.dagang.bidsys.dao.BidDao;
import com.dagang.bidsys.dao.ItemDao;
import com.dagang.bidsys.dao.KindDao;
import com.dagang.bidsys.dao.StateDao;
import com.dagang.bidsys.dao.UserDao;
import com.dagang.bidsys.exception.BidException;
import com.dagang.bidsys.manager.UserManager;
import com.dagang.bidsys.model.Bid;
import com.dagang.bidsys.model.Item;
import com.dagang.bidsys.model.Kind;
import com.dagang.bidsys.model.User;

public class UserManagerImpl implements UserManager {

	static Logger log = Logger.getLogger(UserManagerImpl.class.getName());

	private KindDao kindDao;

	private StateDao stateDao;

	private ItemDao itemDao;

	private UserDao userDao;

	private BidDao bidDao;

	private MailSender mailSender;

	private SimpleMailMessage message;

	public void addBid(int userId, int itemId, BigDecimal maxPrice)
			throws BidException {
		
			User user = userDao.get(userId);
			Item item = itemDao.get(itemId);

			if (maxPrice.compareTo(item.getPresentPrice()) == 1) {
				item.setPresentPrice(maxPrice);
				itemDao.update(item);
			}

			Bid bid = new Bid();
			bid.setUser(user);
			bid.setItem(item);
			bid.setPrice(maxPrice);
			bid.setTime(new Date());

			bidDao.save(bid);

			SimpleMailMessage msg = new SimpleMailMessage(this.message);
			msg.setTo(user.getEmail());
			msg.setText("你好" + user.getUsername() + ",\n谢谢您参与竞价\n您竞价的物品是:"
					+ item.getName() + "\n您竞价的价格是:" + maxPrice);
		try {
			mailSender.send(msg);
		} catch (MailException e) {
			System.err.println(e.getMessage());
		}

	}

	public void addItem(String name, String desc, BigDecimal initPrice,
			int avail, int kindId, int ownerId) throws BidException {
		try {
			Kind kind = kindDao.get(kindId);
			User owner = userDao.get(ownerId);

			Item item = new Item();
			item.setName(name);
			item.setDesc(desc);
			item.setInitPrice(initPrice);
			item.setPresentPrice(initPrice);
			item.setAddTime(new Date());
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE, avail + 1);
			item.setEndTime(c.getTime());

			item.setKind(kind);
			item.setOwner(owner);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BidException("添加项目异常,请重试");
		}
	}

	public void addKind(String name, String desc) throws BidException {
		try {
			Kind kind = new Kind();
			kind.setName(name);
			kind.setDesc(desc);

			kindDao.save(kind);

		} catch (Exception e) {
			e.printStackTrace();
			throw new BidException("添加种类异常,请重试");
		}

	}

	public void addUser(String username, String password, String email)
			throws BidException {
		try {
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			user.setEmail(email);

			userDao.save(user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BidException("添加用户失败,请重试");
		}

	}

	public List getAllBids() throws BidException {
		try {
			return bidDao.findAll();
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("查询所有竞价记录异常,请重试");
		}
	}

	public List getAllItems() throws BidException {
		try {
			return itemDao.findAll();
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("查询所有项目记录异常,请重试");
		}
	}

	public List getAllKinds() throws BidException {

		try {
			return kindDao.findAll();
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("查找种类失败,请重试");
		}
	}

	public List getAllState() throws BidException {
		try {
			return stateDao.findAll();
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("查找所有种类异常,请重试");
		}
	}

	public List getAllUser() throws BidException {
		try {
			return userDao.findAll();
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("查找所有用户异常,请重试");
		}
	}

	public List getBiddingItems() throws BidException {
		try {
			return itemDao.findByState(1);
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("查找处于竞拍状态的项目异常,请重试");
		}

	}

	public List getBidsByItem(int itemId) throws BidException {
		try {
			return itemDao.findByState(1);
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("查找处于竞拍状态的项目异常,请重试");
		}

	}

	public List getBidsByUser(int userId) throws BidException {
		try {
			return bidDao.findByUser(userId);
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("根据用户查找竞价记录异常,请重试");
		}

	}

	public List getFailItems() throws BidException {
		try {
			return itemDao.findByState(3);
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("查找流拍记录异常,请重试");
		}

	}

	public List getItemsByKind(int kindId) throws BidException {
		try {
			return itemDao.findByKind(kindId);
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("根据种类查找项目异常,请重试");
		}
	}

	public List getItemsByOwner(int ownerId) throws BidException {
		try {
			return itemDao.findByOwner(ownerId);
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("根据用户查询项目异常,请重试");
		}
	}

	public List getItemsByState(int stateId) throws BidException {
		try {
			return itemDao.findByState(stateId);
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("根据状态查询记录异常,请重试");
		}
	}

	public List getItemsByWinner(int winnerId) throws BidException {
		try {
			return itemDao.findByWinner(winnerId);
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("根据竞拍获胜者查询记录异常,请重试");
		}
	}

	public List getSucceedItems() throws BidException {
		try {
			return itemDao.findByState(2);
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("查找成功竞拍记录异常,请重试");
		}
	}

	public int validLogin(String username, String password) throws BidException {
		try {
			User user = userDao.findUserByNameAndPass(username, password);
			if (user != null) {
				return user.getId();
			} else {
				return 0;
			}
		} catch (Exception e) {
			log.debug(e.getMessage());
			throw new BidException("用户名或密码不正确,请重试");
		}

	}

	public void setKindDao(KindDao kindDao) throws BidException {
		this.kindDao = kindDao;
	}

	public void setStateDao(StateDao stateDao) throws BidException {
		this.stateDao = stateDao;
	}

	public void setItemDao(ItemDao itemDao) throws BidException {
		this.itemDao = itemDao;
	}

	public void setUserDao(UserDao userDao) throws BidException {
		this.userDao = userDao;
	}

	public void setBidDao(BidDao bidDao) throws BidException {
		this.bidDao = bidDao;
	}

	public void setMailSender(MailSender mailSender) throws BidException {
		this.mailSender = mailSender;
	}

	public void setMessage(SimpleMailMessage message) throws BidException {
		this.message = message;
	}

}

⌨️ 快捷键说明

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