优惠券制作和分配
一、制作:
import java.math.BigDecimal;
import java.util.Date;
/**
* 优惠券详细信息
* @author hzh
*
*/
public class CouponDetail {
//优惠券类型ID
private int typeId;
//优惠劵面值
private BigDecimal faceValue ;
//优惠券名称
private String couponName ;
//数据库时间歘
private Date timestamp ;
public int getTypeId() {
return typeId;
}
public void setTypeId(int typeId) {
this.typeId = typeId;
}
public BigDecimal getFaceValue() {
return faceValue;
}
public void setFaceValue(BigDecimal faceValue) {
this.faceValue = faceValue;
}
public String getCouponName() {
return couponName;
}
public void setCouponName(String couponName) {
this.couponName = couponName;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
}
/**
* 优惠券
* @author hzh
*
*/
public class Coupon {
//优惠劵ID
private Long couponId;
//优惠劵面值
private BigDecimal faceValue ;
//制作人ID
private Long founderId ;
//分配人ID
private Long assignerId ;
//用户ID
private Long userId ;
//优惠劵类型
private int couponType ;
//制作时间
private Date createDate ;
//分配时间
private Date assignDate ;
//状态
private int status ;
//使用此优惠劵的交易ID
private long employId ;
//使用时间
private Date employDate ;
//数据库时间戳
private Date timestamp ;
/**
* 后台到页面
*
*/
//制作人用户名
private String createUserName ;
//分配人用户名
private String assignUserName;
//被分配用户名
private String userName ;
//优惠劵名称
private String couponName ;
//数量
private long number ;
//可抵消总金额
private BigDecimal totalFee ;
public BigDecimal getTotalFee() {
return faceValue.multiply(BigDecimal.valueOf(number));
}
public void setTotalFee(BigDecimal totalFee) {
this.totalFee = totalFee;
}
public Long getCouponId() {
return couponId;
}
public void setCouponId(Long couponId) {
this.couponId = couponId;
}
public BigDecimal getFaceValue() {
return faceValue;
}
public void setFaceValue(BigDecimal faceValue) {
this.faceValue = faceValue;
}
public Long getFounderId() {
return founderId;
}
public void setFounderId(Long founderId) {
this.founderId = founderId;
}
public Long getAssignerId() {
return assignerId;
}
public void setAssignerId(Long assignerId) {
this.assignerId = assignerId;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public int getCouponType() {
return couponType;
}
public void setCouponType(int couponType) {
this.couponType = couponType;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getAssignDate() {
return assignDate;
}
public void setAssignDate(Date assignDate) {
this.assignDate = assignDate;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public String getCreateUserName() {
return createUserName;
}
public void setCreateUserName(String createUserName) {
this.createUserName = createUserName;
}
public String getAssignUserName() {
return assignUserName;
}
public void setAssignUserName(String assignUserName) {
this.assignUserName = assignUserName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getCouponName() {
return couponName;
}
public void setCouponName(String couponName) {
this.couponName = couponName;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
public long getEmployId() {
return employId;
}
public void setEmployId(long employId) {
this.employId = employId;
}
public Date getEmployDate() {
return employDate;
}
public void setEmployDate(Date employDate) {
this.employDate = employDate;
}
}
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.company.bbd.account.model.Coupon;
import cn.company.bbd.account.model.CouponDetail;
import cn.company.bbd.account.model.BasicUser;
@Controller("CreateCouponBean")
@Scope("session")
public class CreateCouponController implements Serializable {
/**
*
*/
private static final Logger log = Logger
.getLogger(CreateCouponController.class);
//管理员
private BasicUser user ;
// 优惠劵所有类型
private List<CouponDetail> list;
// 选择的优惠劵
private Coupon coupon = new Coupon();
//需制作的优惠劵
private List<Coupon> couponList = new ArrayList<Coupon>();
// 制作张数
private int number;
@Autowired(required = true)
@Qualifier("UserAuthenticationService")
private UserAuthenticationService UserAuthenticationService;
@Autowired(required = true)
@Qualifier("AccountService")
private AccountService accountService;
@Autowired(required = true)
@Qualifier("IdService")
private IdService idService;
public CreateCouponController() {
log.debug("CreateCouponController constructed!");
}
public String submit() {
if(validate()){
if(null != getList()){
for(CouponDetail cd : list){
if(cd.getTypeId() == coupon.getCouponType()){
for (int i = 0; i < number; i++) {
Coupon c = new Coupon();
c.setCouponId(IdKit.generateCouponId());
c.setFaceValue(cd.getFaceValue());
c.setFounderId(getUser().getUserId());
c.setCouponType(coupon.getCouponType());
c.setCreateDate(new Date());
c.setStatus(AccountConstant.COUPON_STATUS_CREATED);
couponList.add(c);
}
}
}
accountService.createCoupon(couponList);
number = 0 ;
couponList = new ArrayList<Coupon>();
return PageConstant.PG_COUPON_MANAGE;
}
}
return "";
}
private boolean validate() {
if (0 == number) {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, MessageKit
.getMessage("error_coupon_numberNull"), ""));
return false;
}else if(!RegexKit.patternTest(SecurityConstant.REGEX_PROFILE_NUMBER, String.valueOf(number))){
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, MessageKit
.getMessage("error_coupon_number"), ""));
return false;
}
return true ;
}
public List<CouponDetail> getList() {
list = null ;
if(null == list){
list = accountService.queryAllCouponDetail();
}
return list ;
}
public BasicUser getUser() {
if(null == user){
user = UserAuthenticationService.getCurrentUser();
}
return user;
}
public void setCoupon(Coupon coupon) {
this.coupon = coupon;
}
public Coupon getCoupon() {
return coupon;
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
}