package com.doumee.service.business.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.doumee.core.constants.Constants;
|
import com.doumee.core.constants.ResponseStatus;
|
import com.doumee.core.exception.BusinessException;
|
import com.doumee.core.model.LoginUserInfo;
|
import com.doumee.core.model.PageData;
|
import com.doumee.core.model.PageWrap;
|
import com.doumee.core.utils.Utils;
|
import com.doumee.dao.business.CouponMapper;
|
import com.doumee.dao.business.model.Coupon;
|
import com.doumee.service.business.CouponService;
|
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
import org.apache.commons.lang3.StringUtils;
|
import org.apache.shiro.SecurityUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Objects;
|
|
@Service
|
public class CouponServiceImpl implements CouponService {
|
|
@Autowired
|
private CouponMapper couponMapper;
|
|
@Override
|
@Transactional(rollbackFor = {Exception.class, BusinessException.class})
|
public Integer create(Coupon coupon) {
|
validateCoupon(coupon);
|
LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
|
coupon.setIsdeleted(Constants.ZERO);
|
coupon.setCreateDate(new Date());
|
coupon.setCreator(loginUserInfo.getId());
|
coupon.setEditDate(new Date());
|
coupon.setEditor(loginUserInfo.getId());
|
// 默认项
|
coupon.setType(Constants.ZERO);
|
coupon.setCouponType(Objects.isNull(coupon.getCouponType()) ? Constants.ZERO : coupon.getCouponType());
|
coupon.setGetMethod(Objects.isNull(coupon.getGetMethod()) ? Constants.ZERO : coupon.getGetMethod());
|
couponMapper.insert(coupon);
|
return coupon.getId();
|
}
|
|
@Override
|
public void deleteById(Integer id) {
|
couponMapper.update(new UpdateWrapper<Coupon>().lambda()
|
.set(Coupon::getIsdeleted, Constants.ONE)
|
.eq(Coupon::getId, id));
|
}
|
|
@Override
|
public void deleteByIdInBatch(List<Integer> ids) {
|
if (CollectionUtils.isEmpty(ids)) {
|
return;
|
}
|
for (Integer id : ids) {
|
this.deleteById(id);
|
}
|
}
|
|
@Override
|
@Transactional(rollbackFor = {Exception.class, BusinessException.class})
|
public void updateById(Coupon coupon) {
|
if (Objects.isNull(coupon) || Objects.isNull(coupon.getId())) {
|
throw new BusinessException(ResponseStatus.BAD_REQUEST);
|
}
|
validateCoupon(coupon);
|
LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
|
coupon.setEditDate(new Date());
|
coupon.setEditor(loginUserInfo.getId());
|
couponMapper.updateById(coupon);
|
}
|
|
@Override
|
public void updateStatus(Coupon coupon) {
|
if (Objects.isNull(coupon) || Objects.isNull(coupon.getId()) || Objects.isNull(coupon.getStatus())) {
|
throw new BusinessException(ResponseStatus.BAD_REQUEST);
|
}
|
LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
|
Coupon update = new Coupon();
|
update.setId(coupon.getId());
|
update.setStatus(coupon.getStatus());
|
update.setEditDate(new Date());
|
update.setEditor(loginUserInfo.getId());
|
couponMapper.updateById(update);
|
}
|
|
@Override
|
public Coupon findById(Integer id) {
|
MPJLambdaWrapper<Coupon> wrapper = new MPJLambdaWrapper<>();
|
wrapper.selectAll(Coupon.class)
|
.select("(SELECT COUNT(*) FROM member_coupon mc WHERE mc.COUPON_ID = t.ID AND mc.ISDELETED = 0 AND mc.STATUS IN (1,2,99)) AS CLAIM_COUNT")
|
.select("(SELECT COUNT(*) FROM member_coupon mc WHERE mc.COUPON_ID = t.ID AND mc.ISDELETED = 0 AND mc.STATUS = 2) AS USED_COUNT")
|
.eq(Coupon::getId, id)
|
.eq(Coupon::getIsdeleted, Constants.ZERO);
|
Coupon coupon = couponMapper.selectJoinOne(Coupon.class, wrapper);
|
if (Objects.isNull(coupon)) {
|
throw new BusinessException(ResponseStatus.DATA_EMPTY);
|
}
|
return coupon;
|
}
|
|
@Override
|
public List<Coupon> findList(Coupon coupon) {
|
QueryWrapper<Coupon> wrapper = new QueryWrapper<>(coupon);
|
return couponMapper.selectList(wrapper);
|
}
|
|
@Override
|
public PageData<Coupon> findPage(PageWrap<Coupon> pageWrap) {
|
IPage<Coupon> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
|
MPJLambdaWrapper<Coupon> wrapper = new MPJLambdaWrapper<>();
|
Utils.MP.blankToNull(pageWrap.getModel());
|
pageWrap.getModel().setIsdeleted(Constants.ZERO);
|
wrapper.selectAll(Coupon.class)
|
.select("u.USERNAME AS EDITOR_NAME")
|
.select("(SELECT COUNT(*) FROM member_coupon mc WHERE mc.COUPON_ID = t.ID AND mc.ISDELETED = 0 AND mc.STATUS IN (1,2,99)) AS CLAIM_COUNT")
|
.select("(SELECT COUNT(*) FROM member_coupon mc WHERE mc.COUPON_ID = t.ID AND mc.ISDELETED = 0 AND mc.STATUS = 2) AS USED_COUNT")
|
.innerJoin("system_user u ON u.ID = t.EDITOR")
|
.eq(Coupon::getIsdeleted, Constants.ZERO);
|
if (pageWrap.getModel().getStatus() != null) {
|
wrapper.eq(Coupon::getStatus, pageWrap.getModel().getStatus());
|
}
|
if (pageWrap.getModel().getName() != null) {
|
wrapper.like(Coupon::getName, pageWrap.getModel().getName());
|
}
|
wrapper.orderByDesc(Coupon::getId);
|
return PageData.from(couponMapper.selectJoinPage(page, Coupon.class, wrapper));
|
}
|
|
@Override
|
public long count(Coupon coupon) {
|
QueryWrapper<Coupon> wrapper = new QueryWrapper<>(coupon);
|
return couponMapper.selectCount(wrapper);
|
}
|
|
private void validateCoupon(Coupon coupon) {
|
if (Objects.isNull(coupon) || StringUtils.isBlank(coupon.getName())) {
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "优惠券名称不能为空");
|
}
|
if (Objects.isNull(coupon.getLimitPrice()) || coupon.getLimitPrice() <= 0) {
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "满额必须大于0");
|
}
|
if (Objects.isNull(coupon.getPrice()) || coupon.getPrice() <= 0) {
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "优惠金额必须大于0");
|
}
|
if (coupon.getPrice() >= coupon.getLimitPrice()) {
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "优惠金额必须小于满额");
|
}
|
if (Objects.isNull(coupon.getPushDays()) || coupon.getPushDays() < 1) {
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "推送后领取有效天数必须大于等于1天");
|
}
|
if (Objects.isNull(coupon.getValidDays()) || coupon.getValidDays() < 1) {
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "领取后有效天数必须大于等于1天");
|
}
|
if (Objects.isNull(coupon.getStatus())) {
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "状态不能为空");
|
}
|
}
|
}
|