| ¶Ô±ÈÐÂÎļþ |
| | |
| | | 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); |
| | | } |
| | | |
| | | @Override |
| | | public List<Coupon> findValidList() { |
| | | return couponMapper.selectList(new QueryWrapper<Coupon>().lambda() |
| | | .eq(Coupon::getIsdeleted, Constants.ZERO) |
| | | .eq(Coupon::getStatus, Constants.ZERO) |
| | | .orderByDesc(Coupon::getId)); |
| | | } |
| | | |
| | | 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(), "ç¶æä¸è½ä¸ºç©º"); |
| | | } |
| | | } |
| | | } |