feat[litemall-admin, litemall-admin-api, litemall-db]: 管理后台支持优惠券管理

This commit is contained in:
Junling Bu
2018-12-23 09:43:47 +08:00
parent d3ccabbf81
commit f6cd92a871
13 changed files with 1440 additions and 15 deletions

View File

@@ -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<LitemallCoupon> couponList = couponService.querySelective(name, type, status, page, limit, sort, order);
int total = couponService.countSelective(name, type, status, page, limit, sort, order);
Map<String, Object> 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<LitemallCouponUser> couponList = couponUserService.queryList(userId, couponId, status, page, limit, sort, order);
int total = couponUserService.countList(userId, couponId, status, page, limit, sort, order);
Map<String, Object> 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();
}
}

View File

@@ -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, "商品已经在购物车中,不能修改");
}
// 开启事务管理