From f6cd92a8716ea9faef74236e2f5927653aadf68e Mon Sep 17 00:00:00 2001 From: Junling Bu Date: Sun, 23 Dec 2018 09:43:47 +0800 Subject: [PATCH] =?UTF-8?q?feat[litemall-admin,=20litemall-admin-api?= =?UTF-8?q?=EF=BC=8C=20litemall-db]:=20=E7=AE=A1=E7=90=86=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E6=94=AF=E6=8C=81=E4=BC=98=E6=83=A0=E5=88=B8=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/web/AdminCouponController.java | 139 ++++++ .../admin/web/AdminGoodsController.java | 4 +- litemall-admin/src/api/coupon.js | 49 ++ litemall-admin/src/router/index.js | 15 +- litemall-admin/src/views/promotion/coupon.vue | 453 ++++++++++++++++++ .../src/views/promotion/couponDetail.vue | 248 ++++++++++ .../litemall/db/domain/LitemallCoupon.java | 74 +++ .../db/domain/LitemallCouponExample.java | 274 +++++++++++ .../db/service/CouponVerifyService.java | 10 +- .../db/service/LitemallCouponService.java | 105 ++++ .../db/service/LitemallCouponUserService.java | 26 + .../litemall/db/util/CouponConstant.java | 4 + .../litemall/db/dao/LitemallCouponMapper.xml | 54 ++- 13 files changed, 1440 insertions(+), 15 deletions(-) create mode 100644 litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminCouponController.java create mode 100644 litemall-admin/src/api/coupon.js create mode 100644 litemall-admin/src/views/promotion/coupon.vue create mode 100644 litemall-admin/src/views/promotion/couponDetail.vue diff --git a/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminCouponController.java b/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminCouponController.java new file mode 100644 index 00000000..f978c42e --- /dev/null +++ b/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminCouponController.java @@ -0,0 +1,139 @@ +package org.linlinjava.litemall.admin.web; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.linlinjava.litemall.admin.annotation.LoginAdmin; +import org.linlinjava.litemall.core.util.ResponseUtil; +import org.linlinjava.litemall.core.validator.Order; +import org.linlinjava.litemall.core.validator.Sort; +import org.linlinjava.litemall.db.domain.LitemallCoupon; +import org.linlinjava.litemall.db.domain.LitemallCouponUser; +import org.linlinjava.litemall.db.domain.LitemallTopic; +import org.linlinjava.litemall.db.service.LitemallCouponService; +import org.linlinjava.litemall.db.service.LitemallCouponUserService; +import org.linlinjava.litemall.db.service.LitemallTopicService; +import org.linlinjava.litemall.db.util.CouponConstant; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.StringUtils; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.validation.constraints.NotNull; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/admin/coupon") +@Validated +public class AdminCouponController { + private final Log logger = LogFactory.getLog(AdminCouponController.class); + + @Autowired + private LitemallCouponService couponService; + @Autowired + private LitemallCouponUserService couponUserService; + + @GetMapping("/list") + public Object list(@LoginAdmin Integer adminId, + String name, Short type, Short status, + @RequestParam(defaultValue = "1") Integer page, + @RequestParam(defaultValue = "10") Integer limit, + @Sort @RequestParam(defaultValue = "add_time") String sort, + @Order @RequestParam(defaultValue = "desc") String order) { + if (adminId == null) { + return ResponseUtil.unlogin(); + } + + List couponList = couponService.querySelective(name, type, status, page, limit, sort, order); + int total = couponService.countSelective(name, type, status, page, limit, sort, order); + Map data = new HashMap<>(); + data.put("total", total); + data.put("items", couponList); + + return ResponseUtil.ok(data); + } + + @GetMapping("/listuser") + public Object listuser(@LoginAdmin Integer adminId, + Integer userId, Integer couponId, Short status, + @RequestParam(defaultValue = "1") Integer page, + @RequestParam(defaultValue = "10") Integer limit, + @Sort @RequestParam(defaultValue = "add_time") String sort, + @Order @RequestParam(defaultValue = "desc") String order) { + if (adminId == null) { + return ResponseUtil.unlogin(); + } + + List couponList = couponUserService.queryList(userId, couponId, status, page, limit, sort, order); + int total = couponUserService.countList(userId, couponId, status, page, limit, sort, order); + Map data = new HashMap<>(); + data.put("total", total); + data.put("items", couponList); + + return ResponseUtil.ok(data); + } + + private Object validate(LitemallCoupon coupon) { + String name = coupon.getName(); + if(StringUtils.isEmpty(name)){ + return ResponseUtil.badArgument(); + } + return null; + } + + @PostMapping("/create") + public Object create(@LoginAdmin Integer adminId, @RequestBody LitemallCoupon coupon) { + if (adminId == null) { + return ResponseUtil.unlogin(); + } + Object error = validate(coupon); + if (error != null) { + return error; + } + + // 如果是兑换码类型,则这里需要生存一个兑换码 + if (coupon.getType().equals(CouponConstant.TYPE_CODE)){ + String code = couponService.generateCode(); + coupon.setCode(code); + } + + couponService.add(coupon); + return ResponseUtil.ok(coupon); + } + + @GetMapping("/read") + public Object read(@LoginAdmin Integer adminId, @NotNull Integer id) { + if (adminId == null) { + return ResponseUtil.unlogin(); + } + + LitemallCoupon coupon = couponService.findById(id); + return ResponseUtil.ok(coupon); + } + + @PostMapping("/update") + public Object update(@LoginAdmin Integer adminId, @RequestBody LitemallCoupon coupon) { + if (adminId == null) { + return ResponseUtil.unlogin(); + } + Object error = validate(coupon); + if (error != null) { + return error; + } + if (couponService.updateById(coupon) == 0) { + return ResponseUtil.updatedDataFailed(); + } + return ResponseUtil.ok(coupon); + } + + @PostMapping("/delete") + public Object delete(@LoginAdmin Integer adminId, @RequestBody LitemallCoupon coupon) { + if (adminId == null) { + return ResponseUtil.unlogin(); + } + couponService.deleteById(coupon.getId()); + return ResponseUtil.ok(); + } + +} diff --git a/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminGoodsController.java b/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminGoodsController.java index 8aa75d52..1e5ccbb3 100644 --- a/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminGoodsController.java +++ b/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminGoodsController.java @@ -185,10 +185,10 @@ public class AdminGoodsController { // 检查是否存在购物车商品或者订单商品 // 如果存在则拒绝修改商品。 if(orderGoodsService.checkExist(id)){ - return ResponseUtil.fail(GOODS_UPDATE_NOT_ALLOWED, "商品已经在购物车中,不能修改"); + return ResponseUtil.fail(GOODS_UPDATE_NOT_ALLOWED, "商品已经在订单中,不能修改"); } if(cartService.checkExist(id)){ - return ResponseUtil.fail(GOODS_UPDATE_NOT_ALLOWED, "商品已经在订单中,不能修改"); + return ResponseUtil.fail(GOODS_UPDATE_NOT_ALLOWED, "商品已经在购物车中,不能修改"); } // 开启事务管理 diff --git a/litemall-admin/src/api/coupon.js b/litemall-admin/src/api/coupon.js new file mode 100644 index 00000000..656bcd08 --- /dev/null +++ b/litemall-admin/src/api/coupon.js @@ -0,0 +1,49 @@ +import request from '@/utils/request' + +export function listCoupon(query) { + return request({ + url: '/coupon/list', + method: 'get', + params: query + }) +} + +export function createCoupon(data) { + return request({ + url: '/coupon/create', + method: 'post', + data + }) +} + +export function readCoupon(id) { + return request({ + url: '/coupon/read', + method: 'get', + params: { id } + }) +} + +export function updateCoupon(data) { + return request({ + url: '/coupon/update', + method: 'post', + data + }) +} + +export function deleteCoupon(data) { + return request({ + url: '/coupon/delete', + method: 'post', + data + }) +} + +export function listCouponUser(query) { + return request({ + url: '/coupon/listuser', + method: 'get', + params: query + }) +} diff --git a/litemall-admin/src/router/index.js b/litemall-admin/src/router/index.js index 17c4a056..379b5410 100644 --- a/litemall-admin/src/router/index.js +++ b/litemall-admin/src/router/index.js @@ -227,7 +227,20 @@ export const asyncRouterMap = [ path: 'ad', component: () => import('@/views/promotion/ad'), name: 'ad', - meta: { title: '广告列表', noCache: true } + meta: { title: '广告管理', noCache: true } + }, + { + path: 'coupon', + component: () => import('@/views/promotion/coupon'), + name: 'coupon', + meta: { title: '优惠券管理', noCache: true } + }, + { + path: 'couponDetail', + component: () => import('@/views/promotion/couponDetail'), + name: 'couponDetail', + meta: { title: '优惠券详情', noCache: true }, + hidden: true }, { path: 'topic', diff --git a/litemall-admin/src/views/promotion/coupon.vue b/litemall-admin/src/views/promotion/coupon.vue new file mode 100644 index 00000000..81f9450e --- /dev/null +++ b/litemall-admin/src/views/promotion/coupon.vue @@ -0,0 +1,453 @@ + + + + + diff --git a/litemall-admin/src/views/promotion/couponDetail.vue b/litemall-admin/src/views/promotion/couponDetail.vue new file mode 100644 index 00000000..1f12e7a4 --- /dev/null +++ b/litemall-admin/src/views/promotion/couponDetail.vue @@ -0,0 +1,248 @@ + + + + diff --git a/litemall-db/src/main/java/org/linlinjava/litemall/db/domain/LitemallCoupon.java b/litemall-db/src/main/java/org/linlinjava/litemall/db/domain/LitemallCoupon.java index 85f0d78b..ba895c04 100644 --- a/litemall-db/src/main/java/org/linlinjava/litemall/db/domain/LitemallCoupon.java +++ b/litemall-db/src/main/java/org/linlinjava/litemall/db/domain/LitemallCoupon.java @@ -132,6 +132,24 @@ public class LitemallCoupon { */ private Integer[] goodsValue; + /** + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column litemall_coupon.code + * + * @mbg.generated + */ + private String code; + + /** + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column litemall_coupon.time_type + * + * @mbg.generated + */ + private Short timeType; + /** * * This field was generated by MyBatis Generator. @@ -474,6 +492,54 @@ public class LitemallCoupon { this.goodsValue = goodsValue; } + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column litemall_coupon.code + * + * @return the value of litemall_coupon.code + * + * @mbg.generated + */ + public String getCode() { + return code; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column litemall_coupon.code + * + * @param code the value for litemall_coupon.code + * + * @mbg.generated + */ + public void setCode(String code) { + this.code = code; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column litemall_coupon.time_type + * + * @return the value of litemall_coupon.time_type + * + * @mbg.generated + */ + public Short getTimeType() { + return timeType; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column litemall_coupon.time_type + * + * @param timeType the value for litemall_coupon.time_type + * + * @mbg.generated + */ + public void setTimeType(Short timeType) { + this.timeType = timeType; + } + /** * This method was generated by MyBatis Generator. * This method returns the value of the database column litemall_coupon.days @@ -642,6 +708,8 @@ public class LitemallCoupon { sb.append(", status=").append(status); sb.append(", goodsType=").append(goodsType); sb.append(", goodsValue=").append(goodsValue); + sb.append(", code=").append(code); + sb.append(", timeType=").append(timeType); sb.append(", days=").append(days); sb.append(", startTime=").append(startTime); sb.append(", endTime=").append(endTime); @@ -682,6 +750,8 @@ public class LitemallCoupon { && (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus())) && (this.getGoodsType() == null ? other.getGoodsType() == null : this.getGoodsType().equals(other.getGoodsType())) && (Arrays.equals(this.getGoodsValue(), other.getGoodsValue())) + && (this.getCode() == null ? other.getCode() == null : this.getCode().equals(other.getCode())) + && (this.getTimeType() == null ? other.getTimeType() == null : this.getTimeType().equals(other.getTimeType())) && (this.getDays() == null ? other.getDays() == null : this.getDays().equals(other.getDays())) && (this.getStartTime() == null ? other.getStartTime() == null : this.getStartTime().equals(other.getStartTime())) && (this.getEndTime() == null ? other.getEndTime() == null : this.getEndTime().equals(other.getEndTime())) @@ -712,6 +782,8 @@ public class LitemallCoupon { result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode()); result = prime * result + ((getGoodsType() == null) ? 0 : getGoodsType().hashCode()); result = prime * result + (Arrays.hashCode(getGoodsValue())); + result = prime * result + ((getCode() == null) ? 0 : getCode().hashCode()); + result = prime * result + ((getTimeType() == null) ? 0 : getTimeType().hashCode()); result = prime * result + ((getDays() == null) ? 0 : getDays().hashCode()); result = prime * result + ((getStartTime() == null) ? 0 : getStartTime().hashCode()); result = prime * result + ((getEndTime() == null) ? 0 : getEndTime().hashCode()); @@ -752,6 +824,8 @@ public class LitemallCoupon { status("status", "status", "SMALLINT", true), goodsType("goods_type", "goodsType", "SMALLINT", false), goodsValue("goods_value", "goodsValue", "VARCHAR", false), + code("code", "code", "VARCHAR", false), + timeType("time_type", "timeType", "SMALLINT", false), days("days", "days", "SMALLINT", true), startTime("start_time", "startTime", "TIMESTAMP", false), endTime("end_time", "endTime", "TIMESTAMP", false), diff --git a/litemall-db/src/main/java/org/linlinjava/litemall/db/domain/LitemallCouponExample.java b/litemall-db/src/main/java/org/linlinjava/litemall/db/domain/LitemallCouponExample.java index 78ade52d..79332cb5 100644 --- a/litemall-db/src/main/java/org/linlinjava/litemall/db/domain/LitemallCouponExample.java +++ b/litemall-db/src/main/java/org/linlinjava/litemall/db/domain/LitemallCouponExample.java @@ -1897,6 +1897,280 @@ public class LitemallCouponExample { return (Criteria) this; } + public Criteria andCodeIsNull() { + addCriterion("code is null"); + return (Criteria) this; + } + + public Criteria andCodeIsNotNull() { + addCriterion("code is not null"); + return (Criteria) this; + } + + public Criteria andCodeEqualTo(String value) { + addCriterion("code =", value, "code"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andCodeEqualToColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("code = ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andCodeNotEqualTo(String value) { + addCriterion("code <>", value, "code"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andCodeNotEqualToColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("code <> ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andCodeGreaterThan(String value) { + addCriterion("code >", value, "code"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andCodeGreaterThanColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("code > ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andCodeGreaterThanOrEqualTo(String value) { + addCriterion("code >=", value, "code"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andCodeGreaterThanOrEqualToColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("code >= ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andCodeLessThan(String value) { + addCriterion("code <", value, "code"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andCodeLessThanColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("code < ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andCodeLessThanOrEqualTo(String value) { + addCriterion("code <=", value, "code"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andCodeLessThanOrEqualToColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("code <= ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andCodeLike(String value) { + addCriterion("code like", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeNotLike(String value) { + addCriterion("code not like", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeIn(List values) { + addCriterion("code in", values, "code"); + return (Criteria) this; + } + + public Criteria andCodeNotIn(List values) { + addCriterion("code not in", values, "code"); + return (Criteria) this; + } + + public Criteria andCodeBetween(String value1, String value2) { + addCriterion("code between", value1, value2, "code"); + return (Criteria) this; + } + + public Criteria andCodeNotBetween(String value1, String value2) { + addCriterion("code not between", value1, value2, "code"); + return (Criteria) this; + } + + public Criteria andTimeTypeIsNull() { + addCriterion("time_type is null"); + return (Criteria) this; + } + + public Criteria andTimeTypeIsNotNull() { + addCriterion("time_type is not null"); + return (Criteria) this; + } + + public Criteria andTimeTypeEqualTo(Short value) { + addCriterion("time_type =", value, "timeType"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andTimeTypeEqualToColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("time_type = ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andTimeTypeNotEqualTo(Short value) { + addCriterion("time_type <>", value, "timeType"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andTimeTypeNotEqualToColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("time_type <> ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andTimeTypeGreaterThan(Short value) { + addCriterion("time_type >", value, "timeType"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andTimeTypeGreaterThanColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("time_type > ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andTimeTypeGreaterThanOrEqualTo(Short value) { + addCriterion("time_type >=", value, "timeType"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andTimeTypeGreaterThanOrEqualToColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("time_type >= ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andTimeTypeLessThan(Short value) { + addCriterion("time_type <", value, "timeType"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andTimeTypeLessThanColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("time_type < ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andTimeTypeLessThanOrEqualTo(Short value) { + addCriterion("time_type <=", value, "timeType"); + return (Criteria) this; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table litemall_coupon + * + * @mbg.generated + * @project https://github.com/itfsw/mybatis-generator-plugin + */ + public Criteria andTimeTypeLessThanOrEqualToColumn(LitemallCoupon.Column column) { + addCriterion(new StringBuilder("time_type <= ").append(column.getEscapedColumnName()).toString()); + return (Criteria) this; + } + + public Criteria andTimeTypeIn(List values) { + addCriterion("time_type in", values, "timeType"); + return (Criteria) this; + } + + public Criteria andTimeTypeNotIn(List values) { + addCriterion("time_type not in", values, "timeType"); + return (Criteria) this; + } + + public Criteria andTimeTypeBetween(Short value1, Short value2) { + addCriterion("time_type between", value1, value2, "timeType"); + return (Criteria) this; + } + + public Criteria andTimeTypeNotBetween(Short value1, Short value2) { + addCriterion("time_type not between", value1, value2, "timeType"); + return (Criteria) this; + } + public Criteria andDaysIsNull() { addCriterion("`days` is null"); return (Criteria) this; diff --git a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/CouponVerifyService.java b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/CouponVerifyService.java index 3644d977..7ae4f7d7 100644 --- a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/CouponVerifyService.java +++ b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/CouponVerifyService.java @@ -33,18 +33,24 @@ public class CouponVerifyService { } // 检查是否超期 + Short timeType = coupon.getTimeType(); Short days = coupon.getDays(); LocalDateTime now = LocalDateTime.now(); - if (days == 0) { + if (timeType.equals(CouponConstant.TIME_TYPE_TIME)) { if (now.isBefore(coupon.getStartTime()) || now.isAfter(coupon.getEndTime())) { return null; } - } else { + } + else if(timeType.equals(CouponConstant.TIME_TYPE_DAYS)) { LocalDateTime expired = couponUser.getAddTime().plusDays(days); if (now.isAfter(expired)) { return null; } } + else { + return null; + } + // 检测商品是否符合 // TODO 目前仅支持全平台商品,所以不需要检测 Short goodType = coupon.getGoodsType(); diff --git a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCouponService.java b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCouponService.java index a3201d57..c8f47f88 100644 --- a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCouponService.java +++ b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCouponService.java @@ -1,5 +1,6 @@ package org.linlinjava.litemall.db.service; +import com.alibaba.druid.util.StringUtils; import com.github.pagehelper.PageHelper; import org.linlinjava.litemall.db.dao.LitemallCouponMapper; import org.linlinjava.litemall.db.domain.LitemallCoupon; @@ -10,7 +11,9 @@ import org.linlinjava.litemall.db.util.CouponConstant; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.time.LocalDateTime; import java.util.List; +import java.util.Random; @Service public class LitemallCouponService { @@ -52,6 +55,22 @@ public class LitemallCouponService { return couponMapper.selectByPrimaryKey(id); } + + public LitemallCoupon findByCode(String code) { + LitemallCouponExample example = new LitemallCouponExample(); + example.or().andCodeEqualTo(code).andTypeEqualTo(CouponConstant.TYPE_CODE).andStatusEqualTo(CouponConstant.STATUS_NORMAL).andDeletedEqualTo(false); + List couponList = couponMapper.selectByExample(example); + if(couponList.size() > 1){ + throw new RuntimeException(""); + } + else if(couponList.size() == 0){ + return null; + } + else { + return couponList.get(0); + } + } + /** * 查询新用户注册优惠券 * @@ -62,4 +81,90 @@ public class LitemallCouponService { example.or().andTypeEqualTo(CouponConstant.TYPE_REGISTER).andStatusEqualTo(CouponConstant.STATUS_NORMAL).andDeletedEqualTo(false); return couponMapper.selectByExample(example); } + + public List querySelective(String name, Short type, Short status, Integer page, Integer limit, String sort, String order) { + LitemallCouponExample example = new LitemallCouponExample(); + LitemallCouponExample.Criteria criteria = example.createCriteria(); + + if (!StringUtils.isEmpty(name)) { + criteria.andNameLike("%" + name + "%"); + } + if (type != null) { + criteria.andTypeEqualTo(type); + } + if (status != null) { + criteria.andStatusEqualTo(status); + } + criteria.andDeletedEqualTo(false); + + if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) { + example.setOrderByClause(sort + " " + order); + } + + PageHelper.startPage(page, limit); + return couponMapper.selectByExample(example); + } + + public int countSelective(String name, Short type, Short status, Integer page, Integer limit, String sort, String order) { + LitemallCouponExample example = new LitemallCouponExample(); + LitemallCouponExample.Criteria criteria = example.createCriteria(); + + if (!StringUtils.isEmpty(name)) { + criteria.andNameLike("%" + name + "%"); + } + if (type != null) { + criteria.andTypeEqualTo(type); + } + if (status != null) { + criteria.andStatusEqualTo(status); + } + criteria.andDeletedEqualTo(false); + + if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) { + example.setOrderByClause(sort + " " + order); + } + + return (int)couponMapper.countByExample(example); + } + + public void add(LitemallCoupon coupon) { + coupon.setAddTime(LocalDateTime.now()); + coupon.setUpdateTime(LocalDateTime.now()); + couponMapper.insertSelective(coupon); + } + + public int updateById(LitemallCoupon coupon) { + coupon.setUpdateTime(LocalDateTime.now()); + return couponMapper.updateByPrimaryKeySelective(coupon); + } + + public void deleteById(Integer id) { + couponMapper.logicalDeleteByPrimaryKey(id); + } + + private String getRandomNum(Integer num) { + String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + base += "0123456789"; + + Random random = new Random(); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < num; i++) { + int number = random.nextInt(base.length()); + sb.append(base.charAt(number)); + } + return sb.toString(); + } + + /** + * 生成优惠码 + * + * @return 可使用优惠码 + */ + public String generateCode() { + String code = getRandomNum(8); + while(findByCode(code) != null){ + code = getRandomNum(8); + } + return code; + } } diff --git a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCouponUserService.java b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCouponUserService.java index 179d33b8..f5d21846 100644 --- a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCouponUserService.java +++ b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCouponUserService.java @@ -60,6 +60,32 @@ public class LitemallCouponUserService { return couponUserMapper.selectByExample(example); } + + public int countList(Integer userId, Integer couponId, Short status, Integer page, Integer size, String sort, String order) { + LitemallCouponUserExample example = new LitemallCouponUserExample(); + LitemallCouponUserExample.Criteria criteria = example.createCriteria(); + if (userId != null) { + criteria.andUserIdEqualTo(userId); + } + if(couponId != null){ + criteria.andCouponIdEqualTo(couponId); + } + if (status != null) { + criteria.andStatusEqualTo(status); + } + criteria.andDeletedEqualTo(false); + + if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) { + example.setOrderByClause(sort + " " + order); + } + + if (!StringUtils.isEmpty(page) && !StringUtils.isEmpty(size)) { + PageHelper.startPage(page, size); + } + + return (int)couponUserMapper.countByExample(example); + } + public List queryAll(Integer userId, Integer couponId) { return queryList(userId, couponId, CouponUserConstant.STATUS_USABLE, null, null, "add_time", "desc"); } diff --git a/litemall-db/src/main/java/org/linlinjava/litemall/db/util/CouponConstant.java b/litemall-db/src/main/java/org/linlinjava/litemall/db/util/CouponConstant.java index e481abe4..99f088e2 100644 --- a/litemall-db/src/main/java/org/linlinjava/litemall/db/util/CouponConstant.java +++ b/litemall-db/src/main/java/org/linlinjava/litemall/db/util/CouponConstant.java @@ -3,6 +3,7 @@ package org.linlinjava.litemall.db.util; public class CouponConstant { public static final Short TYPE_COMMON = 0; public static final Short TYPE_REGISTER = 1; + public static final Short TYPE_CODE = 2; public static final Short GOODS_TYPE_ALL = 0; public static final Short GOODS_TYPE_CATEGORY = 1; @@ -11,4 +12,7 @@ public class CouponConstant { public static final Short STATUS_NORMAL = 0; public static final Short STATUS_EXPIRED = 1; public static final Short STATUS_OUT = 2; + + public static final Short TIME_TYPE_DAYS = 0; + public static final Short TIME_TYPE_TIME = 1; } diff --git a/litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/LitemallCouponMapper.xml b/litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/LitemallCouponMapper.xml index ace6e052..82a7e5b1 100644 --- a/litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/LitemallCouponMapper.xml +++ b/litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/LitemallCouponMapper.xml @@ -18,6 +18,8 @@ + + @@ -135,7 +137,8 @@ This element is automatically generated by MyBatis Generator, do not modify. --> id, `name`, `desc`, tag, total, discount, `min`, `limit`, `type`, `status`, goods_type, - goods_value, `days`, start_time, end_time, add_time, update_time, deleted + goods_value, code, time_type, `days`, start_time, end_time, add_time, update_time, + deleted