www.pudn.com > Community_open20061104.rar > AuctionOrderDb.java~5~


package com.redmoon.forum.plugin.auction; 
 
import java.sql.*; 
import java.util.*; 
 
import javax.servlet.http.*; 
 
import cn.js.fan.base.*; 
import cn.js.fan.db.*; 
import cn.js.fan.util.*; 
import cn.js.fan.web.*; 
import com.redmoon.forum.plugin.*; 
import com.redmoon.forum.message.MessageDb; 
import cn.js.fan.mail.SendMail; 
import com.redmoon.forum.person.UserDb; 
import com.redmoon.forum.SequenceMgr; 
 
/** 
 * 

Title:

* *

Description:

* *

Copyright: Copyright (c) 2005

* *

Company:

* * @author not attributable * @version 1.0 */ public class AuctionOrderDb extends ObjectDb { public static final int SCORE_NONE = -100; public AuctionOrderDb() { super(); } private int buyerScore = SCORE_NONE; public static final int STATE_NONE = 0; public static final int STATE_PAY = 1; // 已付款 public static final int STATE_DELIVERY = 2; // 已交货 public static final int STATE_COMPLETE = 3; // 订单已付款交货 public static int JUDGE_GOOD = 10; // 好评 public static int JUDGE_COMMON = 0; // 中评 public static int JUDGE_BAD = -10; // 差评 static { AuctionConfig ac = new AuctionConfig(); JUDGE_GOOD = Integer.parseInt(ac.getProperty("judge.good")); JUDGE_COMMON = Integer.parseInt(ac.getProperty("judge.common")); JUDGE_BAD = Integer.parseInt(ac.getProperty("judge.bad")); } public AuctionOrderDb(int id) { this.id = id; init(); load(); } public String getJudgeDesc(HttpServletRequest request, int judge) { String str = ""; if (judge==JUDGE_GOOD) str = AuctionSkin.LoadString(request, "judge_good"); else if (judge==JUDGE_COMMON) str = AuctionSkin.LoadString(request, "judge_common"); else if (judge==JUDGE_BAD) str = AuctionSkin.LoadString(request, "judge_bad"); return str; } public static String getStateDesc(HttpServletRequest request, int state) { String str = ""; if (state==STATE_NONE) str = AuctionSkin.LoadString(request, "order_state_none"); if (state==STATE_PAY) str = AuctionSkin.LoadString(request, "order_state_pay"); else if (state==STATE_DELIVERY) str = AuctionSkin.LoadString(request, "order_state_delivery"); else if (state==STATE_COMPLETE) str = AuctionSkin.LoadString(request, "order_state_complete"); return str; } public boolean del() throws ErrMsgException { int rowcount = 0; Conn conn = null; try { conn = new Conn(connname); PreparedStatement ps = conn.prepareStatement(this.QUERY_DEL); ps.setInt(1, id); rowcount = conn.executePreUpdate(); } catch (SQLException e) { logger.error(e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } if (rowcount > 0) { AuctionOrderCache cc = new AuctionOrderCache(this); primaryKey.setValue(new Integer(id)); cc.refreshDel(primaryKey); } return rowcount>0? true:false; } public ObjectDb getObjectRaw(PrimaryKey pk) { return new AuctionOrderDb(pk.getIntValue()); } public boolean create() throws ErrMsgException, ResKeyException { int rowcount = 0; Conn conn = null; id = (int)SequenceMgr.nextID(SequenceMgr.AuctionOrderId); try { conn = new Conn(connname); PreparedStatement ps = conn.prepareStatement(this.QUERY_CREATE); ps.setLong(1, auctionId); ps.setInt(2, auctionSellType); ps.setString(3, seller); ps.setString(4, buyer); ps.setDouble(5, price); ps.setString(6, moneyCode); ps.setDouble(7, totalPrice); ps.setInt(8, amount); ps.setInt(9, sellerScore); ps.setInt(10, buyerScore); ps.setInt(11, state); ps.setInt(12, id); if (payTime==null) ps.setString(13, null); else ps.setString(13, DateUtil.toLongString(payTime)); if (deliverTime==null) ps.setString(14, null); else ps.setString(14, DateUtil.toLongString(deliverTime)); ps.setString(15, commodityName); ps.setString(16, "" + System.currentTimeMillis()); rowcount = conn.executePreUpdate(); if (rowcount>0) { if (auctionSellType==AuctionDb.SELL_TYPE_AUCTION) { AuctionDb ad = new AuctionDb(); ad = ad.getAuctionDb(auctionId); ad.setOrderId(id); // logger.info("msgRootId=" + ad.getMsgRootId()); // logger.info("orderId=" + ad.getOrderId()); ad.save(); } } } catch (SQLException e) { logger.error("create:" + e.getMessage()); throw new ResKeyException(new SkinUtil(), SkinUtil.ERR_DB); } finally { if (conn != null) { conn.close(); conn = null; } } if (rowcount>0) { AuctionConfig ac = new AuctionConfig(); // 发短消息 String subject = ac.getOrderNoticeSubject(); String body = ac.getOrderNoticeBody(); body = body.replaceFirst("\\$buyer", buyer); body = body.replaceFirst("\\$seller", seller); body = body.replaceAll("\\$orderId", "" + id); String rootPath = Global.getRootPath(); rootPath = rootPath.replaceFirst("http", "hhttttpp"); body = body.replaceAll("\\$rootPath", rootPath); String ubbBody = ac.getUbbBody(); ubbBody = ubbBody.replaceFirst("\\$buyer", buyer); ubbBody = ubbBody.replaceFirst("\\$seller", seller); ubbBody = ubbBody.replaceAll("\\$orderId", "" + id); ubbBody = ubbBody.replaceAll("\\$rootPath", rootPath); MessageDb msd = new MessageDb(); msd.title = subject; msd.content = ubbBody; msd.sender = msd.USER_SYSTEM; msd.type = msd.TYPE_SYSTEM; msd.ip = "0.0.0.0"; // 至卖家 msd.receiver = seller; msd.create(); // 至买家 msd.receiver = buyer; msd.create(); // 发邮件 String from = StrUtil.GBToUnicode(ac.getEmailAlias()); from += "<" + ac.getEmail() + ">"; // 至卖家 SendMail sm = new SendMail(); UserDb user = new UserDb(); user = user.getUser(seller); sm.send(Global.smtpServer, Global.smtpPort, Global.smtpUser, Global.smtpPwd, user.getEmail(), from, subject, body); // 至买家 user = user.getUser(buyer); sm.send(Global.smtpServer, Global.smtpPort, Global.smtpUser, Global.smtpPwd, user.getEmail(), from, subject, body); } return rowcount>0? true:false; } public boolean save() { int rowcount = 0; Conn conn = null; try { conn = new Conn(connname); PreparedStatement ps = conn.prepareStatement(this.QUERY_SAVE); ps.setLong(1, auctionId); ps.setInt(2, auctionSellType); ps.setString(3, seller); ps.setString(4, buyer); ps.setDouble(5, price); ps.setString(6, moneyCode); ps.setDouble(7, totalPrice); ps.setInt(8, amount); ps.setInt(9, sellerScore); ps.setInt(10, buyerScore); ps.setInt(11, state); if (payTime==null) ps.setString(12, null); else ps.setString(12, DateUtil.toLongString(payTime)); if (deliverTime==null) ps.setString(13, null); else ps.setString(13, DateUtil.toLongString(deliverTime)); ps.setString(14, commodityName); ps.setInt(15, id); rowcount = conn.executePreUpdate(); if (rowcount>0) { AuctionOrderCache uc = new AuctionOrderCache(this); primaryKey.setValue(new Integer(id)); uc.refreshSave(primaryKey); } } catch (SQLException e) { logger.error("save:" + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return rowcount>0? true:false; } public void load() { ResultSet rs = null; Conn conn = new Conn(connname); try { PreparedStatement ps = conn.prepareStatement(this.QUERY_LOAD); ps.setInt(1, id); primaryKey.setValue(new Integer(id)); rs = conn.executePreQuery(); if (rs.next()) { auctionId = rs.getInt(1); auctionSellType = rs.getInt(2); seller = rs.getString(3); buyer = rs.getString(4); price = rs.getDouble(5); moneyCode = rs.getString(6); totalPrice = rs.getDouble(7); amount = rs.getInt(8); sellerScore = rs.getInt(9); buyerScore = rs.getInt(10); state = rs.getInt(11); orderDate = DateUtil.parse(rs.getString(12)); String str = rs.getString(13); if (str!=null) { payTime = DateUtil.parse(str); } str = rs.getString(14); if (str != null) { deliverTime = DateUtil.parse(rs.getString(14)); } commodityName = rs.getString(15); loaded = true; } } catch (SQLException e) { logger.error("load:" + e.getMessage()); } finally { if (conn!=null) { conn.close(); conn = null; } } } public void setPrice(double price) { this.price = price; } public void setId(int id) { this.id = id; } public void setAuctionId(int auctionId) { this.auctionId = auctionId; } public void setAuctionSellType(int auctionSellType) { this.auctionSellType = auctionSellType; } public void setSeller(String seller) { this.seller = seller; } public void setBuyer(String buyer) { this.buyer = buyer; } public void setMoneyCode(String moneyCode) { this.moneyCode = moneyCode; } public void setTotalPrice(double totalPrice) { this.totalPrice = totalPrice; } public void setAmount(int amount) { this.amount = amount; } public void setSellerScore(int sellerScore) { this.sellerScore = sellerScore; } public void setBuyerScore(int buyerScore) { this.buyerScore = buyerScore; } public void setState(int state) { this.state = state; } public void setOrderDate(java.util.Date orderDate) { this.orderDate = orderDate; } public void setPayTime(java.util.Date payTime) { this.payTime = payTime; } public void setDeliverTime(java.util.Date deliverTime) { this.deliverTime = deliverTime; } public void setCommodityName(String commodityName) { this.commodityName = commodityName; } public double getPrice() { return price; } public int getId() { return id; } public int getAuctionId() { return auctionId; } public int getAuctionSellType() { return auctionSellType; } public String getSeller() { return seller; } public String getBuyer() { return buyer; } public String getMoneyCode() { return moneyCode; } public double getTotalPrice() { return totalPrice; } public int getAmount() { return amount; } public int getSellerScore() { return sellerScore; } public int getBuyerScore() { return buyerScore; } public int getState() { return state; } public java.util.Date getOrderDate() { return orderDate; } public java.util.Date getPayTime() { return payTime; } public java.util.Date getDeliverTime() { return deliverTime; } public String getCommodityName() { return commodityName; } public AuctionOrderDb getAuctionOrderDb(int id) { return (AuctionOrderDb)getObjectDb(new Integer(id)); } /** * 查看对应于商品的所有订单 * @param auctionId int * @return Vector */ public Vector list(int auctionId) { Vector v = new Vector(); Conn conn = new Conn(connname); ResultSet rs = null; String sql = "select id from " + tableName + " where auctionId=? order by price desc"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, auctionId); rs = conn.executePreQuery(); if (rs!=null) { while (rs.next()) { int id = rs.getInt(1); v.addElement(getAuctionOrderDb(id)); } } } catch (SQLException e) { logger.error("list: " + e.getMessage()); } finally { if (conn!=null) { conn.close(); conn = null; } } return v; } public boolean makeOrderForAuction(AuctionDb ad) throws ErrMsgException, ResKeyException { auctionId = ad.getMsgRootId(); if (ad.getCurBidPrice()==0) return false; auctionSellType = ad.getSellType(); seller = ad.getUserName(); AuctionBidDb ab = new AuctionBidDb(); ab = ab.getLastBid(auctionId); buyer = ab.getName(); price = ab.getPrice(); AuctionWorthDb aw = new AuctionWorthDb(); long[] aids = aw.getWorthOfAuction(auctionId); aw = aw.getAuctionWorthDb((int)aids[0]); moneyCode = aw.getMoneyCode(); totalPrice = price*ad.getCount(); amount = ad.getCount(); state = 0; ScoreMgr sm = new ScoreMgr(); ScoreUnit su = sm.getScoreUnit(moneyCode); // 如果是虚拟币,则直接将其支付 if (!su.isReal()) { if (su.getScore().pay(buyer, seller, totalPrice)) { state = STATE_PAY; payTime = new java.util.Date(); payTime.setTime(System.currentTimeMillis()); } } commodityName = ad.getName(); boolean re = create(); // logger.info("makeOrderForAuction re=" + re); if (re) { // 更改商品的状态为已售完 ad.setState(ad.STATE_SELLOUT); ad.save(); logger.info("state=" + ad.getState()); } return re; } // 为一口价出售方式生成订单 public boolean makeOrderForSell(AuctionWorthDb aw, String buyer, int count) throws ErrMsgException, ResKeyException { AuctionDb ad = new AuctionDb(); ad = ad.getAuctionDb(aw.getMsgRootId()); auctionId = ad.getMsgRootId(); commodityName = ad.getName(); auctionSellType = ad.getSellType(); seller = ad.getUserName(); this.buyer = buyer; price = aw.getPrice(); moneyCode = aw.getMoneyCode(); totalPrice = price*count; amount = count; state = 0; ScoreMgr sm = new ScoreMgr(); ScoreUnit su = sm.getScoreUnit(moneyCode); // 如果是虚拟币,则直接将其支付 if (!su.isReal()) { if (su.getScore().pay(buyer, seller, totalPrice)) { state = STATE_PAY; payTime = new java.util.Date(); payTime.setTime(System.currentTimeMillis()); } } boolean re = create(); if (re) { // 将商品数量减去购买的数量,如果数量为0则置为已售完 ad.setCount(ad.getCount() - count); if (ad.getCount()<=0) ad.setState(ad.STATE_SELLOUT); ad.save(); } return re; } private double price; private int id; private long auctionId; private int auctionSellType; private String seller; private String buyer; private String moneyCode; private double totalPrice; private int amount; private int sellerScore = SCORE_NONE; private int state; private java.util.Date orderDate; private java.util.Date payTime; private java.util.Date deliverTime; private String commodityName; }