?? auctionserivceimpl.java
字號:
package com.lixineng.serviceImpl;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import com.lixineng.dao.AuctionUser;
import com.lixineng.dao.AuctionUserDAO;
import com.lixineng.dao.Bid;
import com.lixineng.dao.BidDAO;
import com.lixineng.dao.Item;
import com.lixineng.dao.ItemDAO;
import com.lixineng.dao.Kind;
import com.lixineng.dao.KindDAO;
import com.lixineng.dao.StateDAO;
import com.lixineng.exception.AuctionException;
import com.lixineng.service.AuctionService;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class AuctionSerivceImpl implements AuctionService {
static Logger log = Logger.getLogger(AuctionSerivceImpl.class.getName());
private AuctionUserDAO userDao;
private BidDAO bidDao;
private ItemDAO itemDao;
private KindDAO kindDao;
private StateDAO stateDao;
private MailSender mailSender;
private SimpleMailMessage message;
public MailSender getMailSender() {
return mailSender;
}
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public SimpleMailMessage getMessage() {
return message;
}
public void setMessage(SimpleMailMessage message) {
this.message = message;
}
public void setUserDao(AuctionUserDAO userDao) {
this.userDao = userDao;
}
public void setBidDao(BidDAO bidDao) {
this.bidDao = bidDao;
}
public void setItemDao(ItemDAO itemDao) {
this.itemDao = itemDao;
}
public void setKindDao(KindDAO kindDao) {
this.kindDao = kindDao;
}
public void setStateDao(StateDAO stateDao) {
this.stateDao = stateDao;
}
public void addBid(int userId, int itemId, double bidPrice)
throws AuctionException {
AuctionUser au = userDao.findById(userId);
Item item = itemDao.findById(itemId);
if (bidPrice > item.getMaxPrice()) {
item.setMaxPrice(bidPrice);
itemDao.update(item);
}
Bid bid = new Bid();
bid.setAuctionUser(au);
bid.setBidDate(new Date());
bid.setItem(item);
bid.setBidPrice(bidPrice);
bidDao.save(bid);
SimpleMailMessage msg = new SimpleMailMessage(this.message);
msg.setTo(au.getEmail());
msg.setText(
"Dear "
+ au.getUsername()
+ ", 謝謝你參與競價,你的競價的物品的是: "
+ item.getItemName()
+ "你的競價的金額是: "
+ item.getMaxPrice());
try
{
mailSender.send(msg);
}
catch(MailException ex)
{
System.err.println(ex.getMessage());
}
}
public void addItem(String name, String desc, String remark,
double initPrice, int avail, int kind, int userId)
throws AuctionException {
try {
Kind k = kindDao.findById(kind);
// AuctionUser owner=userDao.findById(userId);
Item item = new Item();
item.setItemName(name);
item.setItemDesc(desc);
item.setItemRemark(remark);
item.setAddtime(new Date());
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, avail);
item.setEndtime(c.getTime());
item.setInitPrice(initPrice);
item.setMaxPrice(initPrice);
item.setState(stateDao.findById(1));
item.setKind(k);
item.setAuctionUserByOwnerId(userDao.findById(userId));
itemDao.save(item);
} catch (Exception e) {
log.debug(e.getMessage());
throw new AuctionException("添加物品出現異常,請重試");
}
}
public void addKind(String name, String desc) throws AuctionException {
try
{
Kind k = new Kind();
k.setKindName(name);
k.setKindDesc(desc);
kindDao.save(k);
}
catch (Exception e)
{
log.debug(e.getMessage());
throw new AuctionException("添加物品種類出現異常,請重試");
}
}
public List getAllKinds() throws AuctionException {
try
{
return kindDao.findAll();
}
catch (Exception e)
{
log.debug(e.getMessage());
throw new AuctionException("獲取所有的物品種類出現異常,請重試");
}
}
public List getBidsByUser(int userId) throws AuctionException {
try
{
List result = new ArrayList();
result=bidDao.findByUser(userId);
// List l = bidDao.findByUser(userId);
// for ( int i = 0 ; i < l.size() ; i++ )
// {
// Bid bid = (Bid)l.get(i);
// BidBean bb = new BidBean();
// initBid(bb, bid);
// result.add(bb);
// }
return result;
}
catch (Exception e)
{
log.debug(e.getMessage());
throw new AuctionException("查詢用戶對應所有的競價出現異常,請重試");
}
}
public List getFailItems() throws AuctionException {
try
{
List result = new ArrayList();
result=itemDao.findItemByState(3);
// List items = itemDao.findItemByState(3);
// for (Iterator it = items.iterator() ; it.hasNext() ; )
// {
// Item item = (Item)it.next();
// ItemBean ib = new ItemBean();
// initItem(ib,item);
// result.add(ib);
// }
return result;
}
catch (Exception e)
{
log.debug(e.getMessage());
throw new AuctionException("查詢流拍物品出現異常,請重試");
}
}
public Item getItem(int itemId) throws AuctionException {
{
Item item = itemDao.findById(itemId);
// ItemBean ib = new ItemBean();
// initItem(ib, item);
// return ib;
return item;
}
}
public List getItemsByKind(int kindId) throws AuctionException {
try
{
List result = new ArrayList();
result=itemDao.findItemByKind(kindId);
// List items = itemDao.findItemByKind(kindId);
// for (Iterator it = items.iterator() ; it.hasNext() ; )
// {
// Item item = (Item)it.next();
// ItemBean ib = new ItemBean();
// initItem(ib,item);
// result.add(ib);
// }
return result;
}
catch (Exception e)
{
log.debug(e.getMessage());
throw new AuctionException("獲取指定種類的物品出現異常,請重試");
}
}
public List getItemsByOwner(int userId) throws AuctionException {
try
{
List result = new ArrayList();
result=itemDao.findItemByOwer(userId);
// List items = itemDao.findItemByOwner(userId);
// for (Iterator it = items.iterator() ; it.hasNext() ; )
// {
// Item item = (Item)it.next();
// ItemBean ib = new ItemBean();
// initItem(ib,item);
// result.add(ib);
// }
return result;
}
catch (Exception e)
{
log.debug(e.getMessage());
throw new AuctionException("獲取用戶的所有拍賣物品出現異常,請重試");
}
}
public List getItemsByWinner(Integer WinerId) throws AuctionException {
try
{
List result = new ArrayList();
result=itemDao.findItemByWiner(WinerId);
// List items = itemDao.findItemByWiner(WinerId);
// for (Iterator it = items.iterator() ; it.hasNext() ; )
// {
// Item item = (Item)it.next();
// ItemBean ib = new ItemBean();
// initItem(ib,item);
// result.add(ib);
// }
return result;
}
catch (Exception e)
{
log.debug(e.getMessage());
throw new AuctionException("根據贏取者查詢物品出現異常,請重試");
}
}
public String getKind(int kindId) throws AuctionException {
{
Kind k = kindDao.findById(kindId);
return k.getKindName();
}
}
public void updateWiner() throws AuctionException {
List itemList = itemDao.findItemByState(1);
for (int i = 0; i < itemList.size(); i++) {
Item item = (Item) itemList.get(i);
if (!item.getEndtime().after(new Date())) {
AuctionUser au = bidDao.findUserByItemAndPrice(
item.getItemId(), item.getMaxPrice());
if (au != null) {
item.setAuctionUserByWinerId(au);
item.setState(stateDao.findById(2));
itemDao.save(item);
} else {
item.setState(stateDao.findById(3));
}
}
}
}
public Integer validLogin(String username, String pass)
throws AuctionException {
try
{
AuctionUser user = userDao.findUserByNameAndPass(username, pass);
if (user != null)
{
return user.getUserId();
}
return null;
}
catch (Exception e)
{
log.debug(e.getMessage());
throw new AuctionException("驗證用戶登陸出現異常,請重試");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -