| | |
| | | package com.mzl.flower.service.impl.coupon; |
| | | |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.mzl.flower.config.exception.ValidationException; |
| | | import com.mzl.flower.config.security.SecurityUtils; |
| | | import com.mzl.flower.constant.Constants; |
| | | import com.mzl.flower.dto.request.coupon.*; |
| | | import com.mzl.flower.dto.response.coupon.CouponPointStatisVO; |
| | | import com.mzl.flower.dto.response.coupon.CouponTemplateVO; |
| | | import com.mzl.flower.entity.coupon.CouponRecordDO; |
| | | import com.mzl.flower.entity.coupon.CouponTemplateCustomerDO; |
| | | import com.mzl.flower.entity.coupon.CouponTemplateDO; |
| | | import com.mzl.flower.entity.customer.Customer; |
| | | import com.mzl.flower.entity.point.CustomerPoint; |
| | | import com.mzl.flower.entity.point.CustomerPointDetail; |
| | | import com.mzl.flower.entity.point.PointGoodsRecord; |
| | | import com.mzl.flower.enums.*; |
| | | import com.mzl.flower.mapper.coupon.CouponTemplateMapper; |
| | | import com.mzl.flower.mapper.coupon.CouponTemplateMapperCustom; |
| | | import com.mzl.flower.mapper.customer.CustomerMapper; |
| | | import com.mzl.flower.mapper.point.CustomerPointDetailMapper; |
| | | import com.mzl.flower.mapper.point.CustomerPointMapper; |
| | | import com.mzl.flower.service.coupon.CouponRecordService; |
| | | import com.mzl.flower.service.coupon.CouponTemplateCustomerService; |
| | | import com.mzl.flower.service.coupon.CouponTemplateService2; |
| | | import com.mzl.flower.service.payment.OrderService; |
| | | import com.mzl.flower.utils.UUIDGenerator; |
| | | import org.apache.commons.collections4.CollectionUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.BeanUtils; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | |
| | | private CustomerMapper customerMapper; |
| | | |
| | | @Autowired |
| | | private OrderService orderService; |
| | | private CustomerPointMapper customerPointMapper; |
| | | |
| | | @Autowired |
| | | private CouponTemplateService2 couponTemplateService; |
| | | |
| | | @Autowired |
| | | private CustomerPointDetailMapper customerPointDetailMapper; |
| | | |
| | | @Transactional |
| | | @Override |
| | |
| | | } |
| | | if (couponTemplateDO.getUsageTimeType().equals(CouponUsageTimeTypeEnum.HOUR.getType())) { |
| | | // 小时 |
| | | couponTemplateDO.setUsageEndDate(currentTime.plusDays(couponTemplateDO.getUsageTimeNum())); |
| | | couponTemplateDO.setUsageEndDate(currentTime.plusHours(couponTemplateDO.getUsageTimeNum())); |
| | | } |
| | | if (couponTemplateDO.getUsageTimeType().equals(CouponUsageTimeTypeEnum.MINUTE.getType())) { |
| | | // 分钟 |
| | | couponTemplateDO.setUsageEndDate(currentTime.plusDays(couponTemplateDO.getUsageTimeNum())); |
| | | couponTemplateDO.setUsageEndDate(currentTime.plusMinutes(couponTemplateDO.getUsageTimeNum())); |
| | | } |
| | | |
| | | // 优惠券的总数等于下发人员的总数 |
| | |
| | | } |
| | | |
| | | @Override |
| | | public CouponTemplateVO statisCouponTemplatePoint(QueryCouponStatisticsBO queryCouponStatisticsBO) { |
| | | |
| | | //TODO 优惠券总数 |
| | | couponRecordService.statisCouponTemplateCount(queryCouponStatisticsBO); |
| | | |
| | | //TODO 本月兑换优惠券总数 |
| | | couponRecordService.statisCouponTemplateCurMonCount(queryCouponStatisticsBO); |
| | | |
| | | //TODO 本月兑换积分总数 |
| | | couponRecordService.statisCouponPointCurMonPontAmonut(queryCouponStatisticsBO); |
| | | |
| | | return null; |
| | | public CouponPointStatisVO statisCouponTemplatePoint(QueryCouponStatisticsBO queryCouponStatisticsBO) { |
| | | CouponPointStatisVO vo=new CouponPointStatisVO(); |
| | | //优惠券总数 |
| | | vo.setTotalAmount(couponRecordService.statisCouponTemplateCount(queryCouponStatisticsBO)); |
| | | // 本月兑换优惠券总数 |
| | | vo.setCurMonExchangeAmount(couponRecordService.statisCouponTemplateCurMonCount(queryCouponStatisticsBO)); |
| | | // 本月兑换积分总数 |
| | | vo.setCurMonExchangePointAmount(couponRecordService.statisCouponPointCurMonPontAmonut(queryCouponStatisticsBO)); |
| | | return vo; |
| | | } |
| | | |
| | | @Override |
| | | public List<CouponTemplateVO> getPointList(QueryActivePointCouponDTO dto) { |
| | | if(StringUtils.isBlank(dto.getOrderStr())){ |
| | | dto.setOrderStr(" create_time desc "); |
| | | } |
| | | return couponTemplateMapperCustom.getCouponPointList(dto); |
| | | } |
| | | |
| | | @Override |
| | | public Page<CouponTemplateVO> getPointPage(Page page, QueryActivePointCouponDTO dto) { |
| | | if(StringUtils.isBlank(dto.getOrderStr())){ |
| | | dto.setOrderStr(" create_time desc "); |
| | | } |
| | | return couponTemplateMapperCustom.getPointPage(page,dto); |
| | | } |
| | | |
| | | @Override |
| | | public synchronized void exchangeCoupon(ExchangeCouponDTO dto) { |
| | | dto.setNum(1); |
| | | |
| | | final CouponTemplateDO couponTemplateDO = couponTemplateService.getById(dto.getCouponId()); |
| | | if(couponTemplateDO==null){ |
| | | throw new ValidationException("优惠券不存在"); |
| | | } |
| | | |
| | | CustomerPoint cp = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>() |
| | | .eq(CustomerPoint::getUserId, SecurityUtils.getUserId())); |
| | | if(cp == null || (cp.getTotalPoint()-cp.getUsedPoint()-cp.getExpiredPoint()) < couponTemplateDO.getPoint() * dto.getNum()){ |
| | | throw new ValidationException("积分不足"); |
| | | } |
| | | |
| | | CreateCouponRecordDTO recordDTO =new CreateCouponRecordDTO(); |
| | | recordDTO.setCouponId(dto.getCouponId()); |
| | | recordDTO.setUserId(SecurityUtils.getUserId()); |
| | | Customer customer = customerMapper.selectOne(new LambdaQueryWrapper<Customer>() |
| | | .eq(Customer::getUserId, SecurityUtils.getUserId())); |
| | | if(customer == null){ |
| | | throw new ValidationException("商户不存在"); |
| | | } |
| | | recordDTO.setCustomerId(customer.getId()); |
| | | couponRecordService.createCouponRecord(recordDTO); |
| | | |
| | | //更新积分汇总 |
| | | cp.setUsedPoint(cp.getUsedPoint()+couponTemplateDO.getPoint()); |
| | | customerPointMapper.updateById(cp); |
| | | |
| | | //记录积分明细 |
| | | CustomerPointDetail detail = new CustomerPointDetail(); |
| | | detail.setUserId(customer.getUserId()); |
| | | detail.setCustomerId(customer.getId()); |
| | | detail.setPoint(couponTemplateDO.getPoint()); |
| | | detail.setChangeType(Constants.POINT_CHANGE_TYPE.reduce.name()); |
| | | detail.setType(Constants.POINT_TYPE.exchange.name()); |
| | | detail.setRecordDate(LocalDate.now()); |
| | | detail.setRemarks(couponTemplateDO.getCouponName()); |
| | | detail.create(SecurityUtils.getUserId()); |
| | | customerPointDetailMapper.insert(detail); |
| | | } |
| | | |
| | | } |