package com.peanut.modules.common.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.peanut.common.utils.R; import com.peanut.modules.book.service.ShopProductService; import com.peanut.modules.common.entity.*; import com.peanut.modules.common.service.CouponHistoryService; import com.peanut.modules.common.service.CouponService; import com.peanut.modules.common.service.CouponToProductService; import com.peanut.modules.master.service.CourseService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; import java.util.Set; @Slf4j @RestController("commonCoupon") @RequestMapping("common/coupon") public class CouponController { @Autowired private CouponService couponService; @Autowired private CouponHistoryService couponHistoryService; @Autowired private CourseService courseService; @Autowired private CouponToProductService couponToProductService; @Autowired private ShopProductService shopProductService; //优惠卷列表 @RequestMapping("/getCouponList") public R getCouponList(@RequestBody Map params){ Integer limit = Integer.valueOf(params.get("limit").toString()); Integer page = Integer.valueOf(params.get("page").toString()); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); if (StringUtils.isNotEmpty(params.get("couponType").toString())){ wrapper.eq(CouponEntity::getCouponType,params.get("couponType")); } if (StringUtils.isNotEmpty(params.get("couponRange").toString())){ wrapper.eq(CouponEntity::getCouponRange,params.get("couponRange")); } if (StringUtils.isNotEmpty(params.get("couponName").toString())){ wrapper.like(CouponEntity::getCouponName,params.get("couponName")); } if (StringUtils.isNotEmpty(params.get("currentState").toString())){ wrapper.eq(CouponEntity::getCurrentState,params.get("currentState")); } wrapper.orderByDesc(CouponEntity::getCreateTime); Page couponPage = couponService.page(new Page<>(page, limit), wrapper); for (CouponEntity couponEntity : couponPage.getRecords()) { couponService.setRangeList(couponEntity); couponEntity.setGrantCount(couponHistoryService.count(new LambdaQueryWrapper() .eq(CouponHistory::getCouponId,couponEntity.getId()))); } return R.ok().put("couponPage",couponPage); } //用户优惠卷列表 @RequestMapping("/getCouponHistoryList") public R getCouponHistoryList(@RequestBody Map params){ Integer limit = Integer.valueOf(params.get("limit").toString()); Integer page = Integer.valueOf(params.get("page").toString()); MPJLambdaWrapper wrapper = new MPJLambdaWrapper<>(); wrapper.leftJoin(CouponEntity.class,CouponEntity::getId,CouponHistory::getCouponId); wrapper.leftJoin(MyUserEntity.class,MyUserEntity::getId,CouponHistory::getUserId); wrapper.selectAll(CouponHistory.class); if (StringUtils.isNotEmpty(params.get("getType").toString())){ wrapper.eq(CouponHistory::getGetType,params.get("getType")); } if (StringUtils.isNotEmpty(params.get("status").toString())){ wrapper.eq(CouponHistory::getStatus,params.get("status")); } if (StringUtils.isNotEmpty(params.get("userInfo").toString())) { wrapper.and(t->t.like(MyUserEntity::getName,params.get("userInfo")).or().like(MyUserEntity::getNickname,params.get("userInfo")) .or().like(MyUserEntity::getTel,params.get("userInfo")).or().like(MyUserEntity::getEmail,params.get("userInfo"))); } if (StringUtils.isNotEmpty(params.get("userId").toString())){ wrapper.eq(MyUserEntity::getId,params.get("userId")); } wrapper.orderByAsc(CouponHistory::getStatus); wrapper.orderByDesc(CouponHistory::getCreateTime); Page couponList = couponHistoryService.page(new Page<>(page, limit), wrapper); for (CouponHistory ch:couponList.getRecords()){ ch.setCouponEntity(couponService.getByIdSetRange(ch.getCouponId())); } return R.ok().put("couponList",couponList); } //用户优惠劵详情 @RequestMapping("/getCouponHistoryInfo") public R getCouponHistoryInfo(@RequestBody Map params){ CouponHistory CouponHistory = couponHistoryService.getById(Integer.parseInt(params.get("id").toString())); CouponEntity couponEntity = couponService.setRangeList(couponService.getByIdSetRange(CouponHistory.getCouponId())); CouponHistory.setCouponEntity(couponEntity); return R.ok().put("couponHistory",CouponHistory); } //优惠劵详情 @RequestMapping("/getCouponInfo") public R getCouponInfo(@RequestBody Map params){ CouponEntity couponEntity = couponService.getByIdSetRange(Integer.parseInt(params.get("id").toString())); couponService.setRangeList(couponEntity); couponEntity.setGrantCount(couponHistoryService.count(new LambdaQueryWrapper() .eq(CouponHistory::getCouponId,couponEntity.getId()))); return R.ok().put("couponEntity",couponEntity); } //修改优惠劵状态 @RequestMapping("/updateCouponState") public R updateCouponState(@RequestBody Map params){ CouponEntity coupon = couponService.getById(params.get("id").toString()); coupon.setCurrentState((int)params.get("currentState")); couponService.updateById(coupon); return R.ok(); } //修改优惠劵 @RequestMapping("/updateCoupon") public R updateCoupon(@RequestBody CouponEntity couponEntity){ couponService.updateById(couponEntity); return R.ok(); } //删除优惠劵 @RequestMapping("/delCoupon") @Transactional public R delCoupon(@RequestBody Map params){ int couponId = Integer.parseInt(params.get("id").toString()); int count = couponHistoryService.count(new LambdaQueryWrapper() .eq(CouponHistory::getCouponId,couponId)); if (count>0){ return R.error("已有用户拥有优惠券"); } CouponEntity c = couponService.getById(couponId); if (c.getCurrentState()==0){ return R.error("优惠券发放中,请先结束再删除"); } couponService.removeById(couponId); //删除优惠券商品绑定 List ctps = couponToProductService.list(new LambdaQueryWrapper() .eq(CouponToProduct::getCouponId,couponId)); for (CouponToProduct ctp:ctps){ couponToProductService.removeById(ctp.getId()); } return R.ok(); } //新建优惠劵 @RequestMapping("/addCoupon") public R addCoupon(@RequestBody CouponEntity couponEntity){ couponService.save(couponEntity); return R.ok(); } //发放优惠劵 @RequestMapping("/insertCouponHistory") public R insertCouponHistory(@RequestBody Map params){ int couponId = (int)params.get("couponId"); int userId = (int)params.get("userId"); int getType = (int)params.get("getType"); String remark = params.get("remark").toString(); return couponService.insertCouponHistory(couponId,userId,getType,remark); } //删除用户优惠卷 @RequestMapping("/delCouponHistory") public R delCouponHistory(@RequestBody Map params){ couponHistoryService.removeById(params.get("couponHistoryId").toString()); return R.ok(); } //优惠卷可绑定课程 @RequestMapping("/getCourseList") public R getCourseList(@RequestBody Map params){ Integer limit = Integer.valueOf(params.get("limit").toString()); Integer page = Integer.valueOf(params.get("page").toString()); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.select(CourseEntity::getId,CourseEntity::getTitle); wrapper.like(CourseEntity::getTitle,params.get("title")); wrapper.orderByDesc(CourseEntity::getId); Page courses = courseService.page(new Page<>(page, limit), wrapper); return R.ok().put("courses",courses); } //付款时优惠卷列表 @RequestMapping("/getCouponListPayment") public R getCouponListPayment(@RequestBody Map params){ List couponHistoryList = couponService.getCouponListPayment(params); return R.ok().put("couponHistoryList",couponHistoryList); } //购买商品绑定优惠卷列表 @RequestMapping("/couponToProductList") public R couponToShopproductList(@RequestBody Map params){ Integer limit = Integer.valueOf(params.get("limit").toString()); Integer page = Integer.valueOf(params.get("page").toString()); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(CouponToProduct::getProductId,params.get("productId")); wrapper.orderByDesc(CouponToProduct::getId); Page couponToShopproductPage = couponToProductService.page(new Page<>(page, limit), wrapper); for (CouponToProduct couponToProduct : couponToShopproductPage.getRecords()) { couponToProduct.setCouponEntity(couponService.getById(couponToProduct.getCouponId())); couponToProduct.setShopProduct(shopProductService.getById(couponToProduct.getShopProduct())); } return R.ok().put("couponToShopproductPage",couponToShopproductPage); } //商品绑定优惠卷 @RequestMapping("/setCouponToProduct") public R setCouponToProduct(@RequestBody CouponToProduct couponToProduct){ CouponToProduct c = couponToProductService.getOne(new LambdaQueryWrapper() .eq(CouponToProduct::getCouponId, couponToProduct.getCouponId()) .eq(CouponToProduct::getProductId, couponToProduct.getProductId())); if (c == null){ couponToProductService.save(couponToProduct); return R.ok(); }else { return R.error("已存在"); } } //解绑商品优惠卷 @RequestMapping("/unbindCouponToProduct") public R unbindCouponToProduct(@RequestBody Map params){ couponToProductService.removeById(params.get("id").toString()); return R.ok(); } }