Java tutorial
/* * Copyright 2005-2015 shopxx.net. All rights reserved. * Support: http://3936242.01p.com/ * License: http://3936242.01p.com/license */ package net.shopxx.entity; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.persistence.AttributeConverter; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Convert; import javax.persistence.Converter; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.OrderBy; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.persistence.Transient; import javax.validation.Valid; import javax.validation.constraints.Digits; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; import javax.validation.groups.Default; import net.shopxx.BaseAttributeConverter; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.StringUtils; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; @Entity @Table(name = "xx_order") @SequenceGenerator(name = "sequenceGenerator", sequenceName = "seq_order") public class Order extends BaseEntity<Long> { private static final long serialVersionUID = 2253558998320786386L; public static final int LOCK_EXPIRE = 60; public interface Delivery extends Default { } public enum Type { general, exchange } public enum Status { pendingPayment, pendingReview, pendingShipment, shipped, received, completed, failed, canceled, denied } private String sn; private Order.Type type; private Order.Status status; private BigDecimal price; private BigDecimal fee; private BigDecimal freight; private BigDecimal tax; private BigDecimal promotionDiscount; private BigDecimal couponDiscount; private BigDecimal offsetAmount; private BigDecimal amount; private BigDecimal amountPaid; private BigDecimal refundAmount; private Long rewardPoint; private Long exchangePoint; private Integer weight; private Integer quantity; private Integer shippedQuantity; private Integer returnedQuantity; private String consignee; private String areaName; private String address; private String zipCode; private String phone; private String memo; private Date expire; private Boolean isUseCouponCode; private Boolean isExchangePoint; private Boolean isAllocatedStock; private String paymentMethodName; private PaymentMethod.Type paymentMethodType; private String shippingMethodName; private String lockKey; private Date lockExpire; private Date completeDate; private Invoice invoice; private Area area; private PaymentMethod paymentMethod; private ShippingMethod shippingMethod; private Member member; private CouponCode couponCode; private List<String> promotionNames = new ArrayList<String>(); private List<Coupon> coupons = new ArrayList<Coupon>(); private List<OrderItem> orderItems = new ArrayList<OrderItem>(); private Set<PaymentLog> paymentLogs = new HashSet<PaymentLog>(); private Set<Payment> payments = new HashSet<Payment>(); private Set<Refunds> refunds = new HashSet<Refunds>(); private Set<Shipping> shippings = new HashSet<Shipping>(); private Set<Returns> returns = new HashSet<Returns>(); private Set<OrderLog> orderLogs = new HashSet<OrderLog>(); @Column(nullable = false, updatable = false, unique = true) public String getSn() { return sn; } public void setSn(String sn) { this.sn = sn; } @Column(nullable = false, updatable = false) public Order.Type getType() { return type; } public void setType(Order.Type type) { this.type = type; } @Column(nullable = false) public Order.Status getStatus() { return status; } public void setStatus(Order.Status status) { this.status = status; } @Column(nullable = false, updatable = false, precision = 21, scale = 6) public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } @Column(nullable = false, precision = 21, scale = 6) public BigDecimal getFee() { return fee; } public void setFee(BigDecimal fee) { this.fee = fee; } @NotNull(groups = Delivery.class) @Min(0) @Digits(integer = 12, fraction = 3) @Column(nullable = false, precision = 21, scale = 6) public BigDecimal getFreight() { return freight; } public void setFreight(BigDecimal freight) { this.freight = freight; } @Min(0) @Digits(integer = 12, fraction = 3) @Column(nullable = false, precision = 21, scale = 6) public BigDecimal getTax() { return tax; } public void setTax(BigDecimal tax) { this.tax = tax; } @Column(nullable = false, updatable = false, precision = 21, scale = 6) public BigDecimal getPromotionDiscount() { return promotionDiscount; } public void setPromotionDiscount(BigDecimal promotionDiscount) { this.promotionDiscount = promotionDiscount; } @Column(nullable = false, updatable = false, precision = 21, scale = 6) public BigDecimal getCouponDiscount() { return couponDiscount; } public void setCouponDiscount(BigDecimal couponDiscount) { this.couponDiscount = couponDiscount; } @NotNull @Digits(integer = 12, fraction = 3) @Column(nullable = false, precision = 21, scale = 6) public BigDecimal getOffsetAmount() { return offsetAmount; } public void setOffsetAmount(BigDecimal offsetAmount) { this.offsetAmount = offsetAmount; } @Column(nullable = false, precision = 21, scale = 6) public BigDecimal getAmount() { return amount; } public void setAmount(BigDecimal amount) { this.amount = amount; } @Column(nullable = false, precision = 21, scale = 6) public BigDecimal getAmountPaid() { return amountPaid; } public void setAmountPaid(BigDecimal amountPaid) { this.amountPaid = amountPaid; } @Column(nullable = false, precision = 21, scale = 6) public BigDecimal getRefundAmount() { return refundAmount; } public void setRefundAmount(BigDecimal refundAmount) { this.refundAmount = refundAmount; } @Min(0) @Column(nullable = false) public Long getRewardPoint() { return rewardPoint; } public void setRewardPoint(Long rewardPoint) { this.rewardPoint = rewardPoint; } @Column(nullable = false, updatable = false) public Long getExchangePoint() { return exchangePoint; } public void setExchangePoint(Long exchangePoint) { this.exchangePoint = exchangePoint; } @Column(nullable = false, updatable = false) public Integer getWeight() { return weight; } public void setWeight(Integer weight) { this.weight = weight; } @Column(nullable = false, updatable = false) public Integer getQuantity() { return quantity; } public void setQuantity(Integer quantity) { this.quantity = quantity; } @Column(nullable = false) public Integer getShippedQuantity() { return shippedQuantity; } public void setShippedQuantity(Integer shippedQuantity) { this.shippedQuantity = shippedQuantity; } @Column(nullable = false) public Integer getReturnedQuantity() { return returnedQuantity; } public void setReturnedQuantity(Integer returnedQuantity) { this.returnedQuantity = returnedQuantity; } @NotEmpty(groups = Delivery.class) @Length(max = 200) public String getConsignee() { return consignee; } public void setConsignee(String consignee) { this.consignee = consignee; } public String getAreaName() { return areaName; } public void setAreaName(String areaName) { this.areaName = areaName; } @NotEmpty(groups = Delivery.class) @Length(max = 200) public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @NotEmpty(groups = Delivery.class) @Length(max = 200) @Pattern(regexp = "^\\d{6}$") public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } @NotEmpty(groups = Delivery.class) @Length(max = 200) @Pattern(regexp = "^\\d{3,4}-?\\d{7,9}$") public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Length(max = 200) public String getMemo() { return memo; } public void setMemo(String memo) { this.memo = memo; } public Date getExpire() { return expire; } public void setExpire(Date expire) { this.expire = expire; } @Column(nullable = false) public Boolean getIsUseCouponCode() { return isUseCouponCode; } public void setIsUseCouponCode(Boolean isUseCouponCode) { this.isUseCouponCode = isUseCouponCode; } @Column(nullable = false) public Boolean getIsExchangePoint() { return isExchangePoint; } public void setIsExchangePoint(Boolean isExchangePoint) { this.isExchangePoint = isExchangePoint; } @Column(nullable = false) public Boolean getIsAllocatedStock() { return isAllocatedStock; } public void setIsAllocatedStock(Boolean isAllocatedStock) { this.isAllocatedStock = isAllocatedStock; } public String getPaymentMethodName() { return paymentMethodName; } public void setPaymentMethodName(String paymentMethodName) { this.paymentMethodName = paymentMethodName; } public PaymentMethod.Type getPaymentMethodType() { return paymentMethodType; } public void setPaymentMethodType(PaymentMethod.Type paymentMethodType) { this.paymentMethodType = paymentMethodType; } public String getShippingMethodName() { return shippingMethodName; } public void setShippingMethodName(String shippingMethodName) { this.shippingMethodName = shippingMethodName; } public String getLockKey() { return lockKey; } public void setLockKey(String lockKey) { this.lockKey = lockKey; } public Date getLockExpire() { return lockExpire; } public void setLockExpire(Date lockExpire) { this.lockExpire = lockExpire; } public Date getCompleteDate() { return completeDate; } public void setCompleteDate(Date completeDate) { this.completeDate = completeDate; } @Valid @Embedded public Invoice getInvoice() { return invoice; } public void setInvoice(Invoice invoice) { this.invoice = invoice; } @NotNull(groups = Delivery.class) @ManyToOne(fetch = FetchType.LAZY) public Area getArea() { return area; } public void setArea(Area area) { this.area = area; } @ManyToOne(fetch = FetchType.LAZY) public PaymentMethod getPaymentMethod() { return paymentMethod; } public void setPaymentMethod(PaymentMethod paymentMethod) { this.paymentMethod = paymentMethod; } @ManyToOne(fetch = FetchType.LAZY) public ShippingMethod getShippingMethod() { return shippingMethod; } public void setShippingMethod(ShippingMethod shippingMethod) { this.shippingMethod = shippingMethod; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(nullable = false, updatable = false) public Member getMember() { return member; } public void setMember(Member member) { this.member = member; } @OneToOne(fetch = FetchType.LAZY) public CouponCode getCouponCode() { return couponCode; } public void setCouponCode(CouponCode couponCode) { this.couponCode = couponCode; } @Column(updatable = false, length = 4000) @Convert(converter = PromotionNameConverter.class) public List<String> getPromotionNames() { return promotionNames; } public void setPromotionNames(List<String> promotionNames) { this.promotionNames = promotionNames; } @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "xx_order_coupon") public List<Coupon> getCoupons() { return coupons; } public void setCoupons(List<Coupon> coupons) { this.coupons = coupons; } @OneToMany(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.ALL) @OrderBy("type asc") public List<OrderItem> getOrderItems() { return orderItems; } public void setOrderItems(List<OrderItem> orderItems) { this.orderItems = orderItems; } @OneToMany(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) @OrderBy("createDate asc") public Set<PaymentLog> getPaymentLogs() { return paymentLogs; } public void setPaymentLogs(Set<PaymentLog> paymentLogs) { this.paymentLogs = paymentLogs; } @OneToMany(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) @OrderBy("createDate asc") public Set<Payment> getPayments() { return payments; } public void setPayments(Set<Payment> payments) { this.payments = payments; } @OneToMany(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) @OrderBy("createDate asc") public Set<Refunds> getRefunds() { return refunds; } public void setRefunds(Set<Refunds> refunds) { this.refunds = refunds; } @OneToMany(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) @OrderBy("createDate asc") public Set<Shipping> getShippings() { return shippings; } public void setShippings(Set<Shipping> shippings) { this.shippings = shippings; } @OneToMany(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) @OrderBy("createDate asc") public Set<Returns> getReturns() { return returns; } public void setReturns(Set<Returns> returns) { this.returns = returns; } @OneToMany(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) @OrderBy("createDate asc") public Set<OrderLog> getOrderLogs() { return orderLogs; } public void setOrderLogs(Set<OrderLog> orderLogs) { this.orderLogs = orderLogs; } @Transient public boolean getIsDelivery() { return CollectionUtils.exists(getOrderItems(), new Predicate() { @Override public boolean evaluate(Object object) { OrderItem orderItem = (OrderItem) object; return orderItem != null && BooleanUtils.isTrue(orderItem.getIsDelivery()); } }); } @Transient public BigDecimal getAmountPayable() { if (!hasExpired() && !Order.Status.completed.equals(getStatus()) && !Order.Status.failed.equals(getStatus()) && !Order.Status.canceled.equals(getStatus()) && !Order.Status.denied.equals(getStatus())) { BigDecimal amountPayable = getAmount().subtract(getAmountPaid()); return amountPayable.compareTo(BigDecimal.ZERO) >= 0 ? amountPayable : BigDecimal.ZERO; } return BigDecimal.ZERO; } @Transient public BigDecimal getAmountReceivable() { if (!hasExpired() && PaymentMethod.Type.cashOnDelivery.equals(getPaymentMethodType()) && !Order.Status.completed.equals(getStatus()) && !Order.Status.failed.equals(getStatus()) && !Order.Status.canceled.equals(getStatus()) && !Order.Status.denied.equals(getStatus())) { BigDecimal amountReceivable = getAmount().subtract(getAmountPaid()); return amountReceivable.compareTo(BigDecimal.ZERO) >= 0 ? amountReceivable : BigDecimal.ZERO; } return BigDecimal.ZERO; } @Transient public BigDecimal getRefundableAmount() { if (hasExpired() || Order.Status.failed.equals(getStatus()) || Order.Status.canceled.equals(getStatus()) || Order.Status.denied.equals(getStatus())) { BigDecimal refundableAmount = getAmountPaid(); return refundableAmount.compareTo(BigDecimal.ZERO) >= 0 ? refundableAmount : BigDecimal.ZERO; } if (Order.Status.completed.equals(getStatus())) { BigDecimal refundableAmount = getAmountPaid().subtract(getAmount()); return refundableAmount.compareTo(BigDecimal.ZERO) >= 0 ? refundableAmount : BigDecimal.ZERO; } return BigDecimal.ZERO; } @Transient public int getShippableQuantity() { if (!hasExpired() && Order.Status.pendingShipment.equals(getStatus())) { int shippableQuantity = getQuantity() - getShippedQuantity(); return shippableQuantity >= 0 ? shippableQuantity : 0; } return 0; } @Transient public int getReturnableQuantity() { if (!hasExpired() && Order.Status.failed.equals(getStatus())) { int returnableQuantity = getShippedQuantity() - getReturnedQuantity(); return returnableQuantity >= 0 ? returnableQuantity : 0; } return 0; } @Transient public boolean hasExpired() { return getExpire() != null && !getExpire().after(new Date()); } @Transient public OrderItem getOrderItem(String sn) { if (StringUtils.isEmpty(sn) || CollectionUtils.isEmpty(getOrderItems())) { return null; } for (OrderItem orderItem : getOrderItems()) { if (orderItem != null && StringUtils.equalsIgnoreCase(orderItem.getSn(), sn)) { return orderItem; } } return null; } @PrePersist public void prePersist() { if (getArea() != null) { setAreaName(getArea().getFullName()); } if (getPaymentMethod() != null) { setPaymentMethodName(getPaymentMethod().getName()); setPaymentMethodType(getPaymentMethod().getType()); } if (getShippingMethod() != null) { setShippingMethodName(getShippingMethod().getName()); } } @PreUpdate public void preUpdate() { if (getArea() != null) { setAreaName(getArea().getFullName()); } if (getPaymentMethod() != null) { setPaymentMethodName(getPaymentMethod().getName()); setPaymentMethodType(getPaymentMethod().getType()); } if (getShippingMethod() != null) { setShippingMethodName(getShippingMethod().getName()); } } @Converter public static class PromotionNameConverter extends BaseAttributeConverter<List<String>> implements AttributeConverter<Object, String> { } }