Java tutorial
/* * Copyright 2013-2015 cetvision.com. All rights reserved. * Support: http://www.cetvision.com * License: http://www.cetvision.com/license */ package com.dp2345.entity; import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Lob; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.PreRemove; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.persistence.Transient; import javax.validation.constraints.Digits; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import org.apache.commons.lang.StringUtils; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; import com.dp2345.util.FreemarkerUtils; import freemarker.template.TemplateException; /** * Entity - * * @author CETVISION CORP * @version 2.0.3 */ @Entity @Table(name = "xx_coupon") @SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_coupon_sequence") public class Coupon extends BaseEntity { private static final long serialVersionUID = -7907808728349149722L; /** ?? */ private String name; /** ? */ private String prefix; /** */ private Date beginDate; /** ? */ private Date endDate; /** ??? */ private Integer minimumQuantity; /** ?? */ private Integer maximumQuantity; /** ?? */ private BigDecimal minimumPrice; /** ? */ private BigDecimal maximumPrice; /** ?? */ private String priceExpression; /** ?? */ private Boolean isEnabled; /** ??? */ private Boolean isExchange; /** ? */ private Long point; /** ? */ private String introduction; /** ? */ private Set<CouponCode> couponCodes = new HashSet<CouponCode>(); /** */ private Set<Promotion> promotions = new HashSet<Promotion>(); /** ? */ private List<Order> orders = new ArrayList<Order>(); /** * ??? * * @return ?? */ @NotEmpty @Length(max = 200) @Column(nullable = false) public String getName() { return name; } /** * ?? * * @param name * ?? */ public void setName(String name) { this.name = name; } /** * ?? * * @return ? */ @NotEmpty @Length(max = 200) @Column(nullable = false) public String getPrefix() { return prefix; } /** * ? * * @param prefix * ? */ public void setPrefix(String prefix) { this.prefix = prefix; } /** * ? * * @return */ public Date getBeginDate() { return beginDate; } /** * * * @param beginDate * */ public void setBeginDate(Date beginDate) { this.beginDate = beginDate; } /** * ?? * * @return ? */ public Date getEndDate() { return endDate; } /** * ? * * @param endDate * ? */ public void setEndDate(Date endDate) { this.endDate = endDate; } /** * ???? * * @return ??? */ @Min(0) public Integer getMinimumQuantity() { return minimumQuantity; } /** * ??? * * @param minimumQuantity * ??? */ public void setMinimumQuantity(Integer minimumQuantity) { this.minimumQuantity = minimumQuantity; } /** * ??? * * @return ?? */ @Min(0) public Integer getMaximumQuantity() { return maximumQuantity; } /** * ?? * * @param maximumQuantity * ?? */ public void setMaximumQuantity(Integer maximumQuantity) { this.maximumQuantity = maximumQuantity; } /** * ??? * * @return ?? */ @Min(0) @Digits(integer = 12, fraction = 3) @Column(precision = 21, scale = 6) public BigDecimal getMinimumPrice() { return minimumPrice; } /** * ?? * * @param minimumPrice * ?? */ public void setMinimumPrice(BigDecimal minimumPrice) { this.minimumPrice = minimumPrice; } /** * ?? * * @return ? */ @Min(0) @Digits(integer = 12, fraction = 3) @Column(precision = 21, scale = 6) public BigDecimal getMaximumPrice() { return maximumPrice; } /** * ? * * @param maximumPrice * ? */ public void setMaximumPrice(BigDecimal maximumPrice) { this.maximumPrice = maximumPrice; } /** * ??? * * @return ?? */ public String getPriceExpression() { return priceExpression; } /** * ?? * * @param priceExpression * ?? */ public void setPriceExpression(String priceExpression) { this.priceExpression = priceExpression; } /** * ??? * * @return ?? */ @NotNull @Column(nullable = false) public Boolean getIsEnabled() { return isEnabled; } /** * ?? * * @param isEnabled * ?? */ public void setIsEnabled(Boolean isEnabled) { this.isEnabled = isEnabled; } /** * ???? * * @return ??? */ @NotNull @Column(nullable = false) public Boolean getIsExchange() { return isExchange; } /** * ??? * * @param isExchange * ??? */ public void setIsExchange(Boolean isExchange) { this.isExchange = isExchange; } /** * ?? * * @return ? */ @Min(0) public Long getPoint() { return point; } /** * ? * * @param point * ? */ public void setPoint(Long point) { this.point = point; } /** * ?? * * @return ? */ @Lob public String getIntroduction() { return introduction; } /** * ? * * @param introduction * ? */ public void setIntroduction(String introduction) { this.introduction = introduction; } /** * ?? * * @return ? */ @OneToMany(mappedBy = "coupon", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) public Set<CouponCode> getCouponCodes() { return couponCodes; } /** * ? * * @param couponCodes * ? */ public void setCouponCodes(Set<CouponCode> couponCodes) { this.couponCodes = couponCodes; } /** * ? * * @return */ @ManyToMany(mappedBy = "coupons", fetch = FetchType.LAZY) public Set<Promotion> getPromotions() { return promotions; } /** * * * @param promotions * */ public void setPromotions(Set<Promotion> promotions) { this.promotions = promotions; } /** * ?? * * @return ? */ @ManyToMany(mappedBy = "coupons", fetch = FetchType.LAZY) public List<Order> getOrders() { return orders; } /** * ? * * @param orders * ? */ public void setOrders(List<Order> orders) { this.orders = orders; } /** * ? * * @return ? */ @Transient public boolean hasBegun() { return getBeginDate() == null || new Date().after(getBeginDate()); } /** * ? * * @return ? */ @Transient public boolean hasExpired() { return getEndDate() != null && new Date().after(getEndDate()); } /** * * * @param quantity * ?? * @param price * ? * @return */ @Transient public BigDecimal calculatePrice(Integer quantity, BigDecimal price) { if (price == null || StringUtils.isEmpty(getPriceExpression())) { return price; } BigDecimal result = new BigDecimal(0); try { Map<String, Object> model = new HashMap<String, Object>(); model.put("quantity", quantity); model.put("price", price); result = new BigDecimal(FreemarkerUtils.process("#{(" + getPriceExpression() + ");M50}", model)); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } if (result.compareTo(price) > 0) { return price; } return result.compareTo(new BigDecimal(0)) > 0 ? result : new BigDecimal(0); } /** * ?? */ @PreRemove public void preRemove() { Set<Promotion> promotions = getPromotions(); if (promotions != null) { for (Promotion promotion : promotions) { promotion.getCoupons().remove(this); } } List<Order> orders = getOrders(); if (orders != null) { for (Order order : orders) { order.getCoupons().remove(this); } } } }