| | |
| | | package com.mzl.flower.service.point; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | 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.point.PointGoodsCreateDTO; |
| | | import com.mzl.flower.dto.request.point.PointGoodsQueryDTO; |
| | | import com.mzl.flower.dto.request.point.PointGoodsUpdateDTO; |
| | | import com.mzl.flower.dto.request.point.*; |
| | | import com.mzl.flower.dto.response.point.PointGoodsDTO; |
| | | import com.mzl.flower.dto.response.point.PointGoodsListDTO; |
| | | import com.mzl.flower.dto.response.point.PointGoodsRecordDTO; |
| | | import com.mzl.flower.entity.point.CustomerPoint; |
| | | import com.mzl.flower.entity.point.CustomerPointDetail; |
| | | import com.mzl.flower.entity.point.PointGoods; |
| | | import com.mzl.flower.entity.point.PointGoodsRecord; |
| | | import com.mzl.flower.mapper.point.CustomerPointDetailMapper; |
| | | import com.mzl.flower.mapper.point.CustomerPointMapper; |
| | | import com.mzl.flower.mapper.point.PointGoodsMapper; |
| | | import com.mzl.flower.mapper.point.PointGoodsRecordMapper; |
| | | import com.mzl.flower.service.BaseService; |
| | | import com.mzl.flower.utils.UUIDGenerator; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | |
| | | @Autowired |
| | | private PointGoodsMapper pointGoodsMapper; |
| | | |
| | | @Autowired |
| | | private PointGoodsRecordMapper pointGoodsRecordMapper; |
| | | |
| | | @Autowired |
| | | private CustomerPointMapper customerPointMapper; |
| | | |
| | | @Autowired |
| | | private CustomerPointDetailMapper customerPointDetailMapper; |
| | | |
| | | |
| | | public Long addPointGoods(PointGoodsCreateDTO dto){ |
| | | PointGoods p = new PointGoods(); |
| | |
| | | |
| | | return dto; |
| | | } |
| | | |
| | | public void updateStatus(Long id, String status){ |
| | | PointGoods p = pointGoodsMapper.selectById(id); |
| | | if(p == null){ |
| | | throw new ValidationException("商品未找到"); |
| | | } |
| | | |
| | | p.setStatus(status); |
| | | p.update(SecurityUtils.getUserId()); |
| | | pointGoodsMapper.updateById(p); |
| | | } |
| | | |
| | | public synchronized void exchangeGoods(ExchangeGoodsDTO dto) { |
| | | PointGoods p = pointGoodsMapper.selectById(dto.getGoodsId()); |
| | | if(p == null){ |
| | | throw new ValidationException("商品未找到"); |
| | | } |
| | | if(!Constants.POINT_GOODS_STATUS.A.name().equals(p.getStatus())){ |
| | | throw new ValidationException("商品未上架"); |
| | | } |
| | | if(p.getStock()< dto.getNum()){ |
| | | throw new ValidationException("商品库存不足"); |
| | | } |
| | | CustomerPoint cp = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>() |
| | | .eq(CustomerPoint::getUserId, SecurityUtils.getUserId())); |
| | | if(cp == null || (cp.getTotalPoint()-cp.getUsedPoint()-cp.getExpiredPoint()) < p.getPoint() * dto.getNum()){ |
| | | throw new ValidationException("积分不足"); |
| | | } |
| | | //记录兑换记录 |
| | | PointGoodsRecord record = new PointGoodsRecord(); |
| | | record.setUserId(SecurityUtils.getUserId()); |
| | | record.setCustomerId(getCustomerByUserId(SecurityUtils.getUserId()).getId()); |
| | | record.setGoodsId(dto.getGoodsId()); |
| | | record.setNum(dto.getNum()); |
| | | record.setPoint(p.getPoint()); |
| | | record.setName(p.getName()); |
| | | record.setPictures(p.getPictures()); |
| | | record.setDescription(p.getDescription()); |
| | | record.setTotalPoint(p.getPoint() * dto.getNum()); |
| | | record.setCover(p.getCover()); |
| | | record.setRedeemCode(UUIDGenerator.getUUID()); |
| | | record.setStatus(Constants.POINT_GOODS_RECORD_STATUS.A.name());//未使用 |
| | | pointGoodsRecordMapper.insert(record); |
| | | |
| | | //更新积分汇总 |
| | | cp.setUsedPoint(cp.getUsedPoint()+record.getTotalPoint()); |
| | | customerPointMapper.updateById(cp); |
| | | |
| | | //记录积分明细 |
| | | CustomerPointDetail detail = new CustomerPointDetail(); |
| | | detail.setUserId(SecurityUtils.getUserId()); |
| | | detail.setCustomerId(record.getCustomerId()); |
| | | detail.setPoint(record.getTotalPoint()); |
| | | detail.setChangeType(Constants.POINT_CHANGE_TYPE.reduce.name()); |
| | | detail.setType(Constants.POINT_TYPE.exchange.name()); |
| | | detail.create(SecurityUtils.getUserId()); |
| | | customerPointDetailMapper.insert(detail); |
| | | |
| | | //更新库存 |
| | | p.setStock(p.getStock()- dto.getNum()); |
| | | pointGoodsMapper.updateById(p); |
| | | } |
| | | |
| | | public Page<PointGoodsRecordDTO> myExchangeGoods(QueryExchangeGoodsDTO dto, Page page) { |
| | | dto.setUserId(SecurityUtils.getUserId()); |
| | | List<PointGoodsRecordDTO> list = pointGoodsRecordMapper.selectMyExchangeGoods(dto,page); |
| | | page.setRecords(list); |
| | | return page; |
| | | } |
| | | |
| | | public void useExchangeGoods(Long recordId,String orderId) { |
| | | PointGoodsRecord record = pointGoodsRecordMapper.selectById(recordId); |
| | | if(record == null){ |
| | | throw new ValidationException("兑换券不存在"); |
| | | } |
| | | if(!Constants.POINT_GOODS_RECORD_STATUS.A.name().equals(record.getStatus())){ |
| | | throw new ValidationException("兑换券已使用或过期"); |
| | | } |
| | | record.setStatus(Constants.POINT_GOODS_RECORD_STATUS.U.name()); |
| | | record.setOrderId(orderId); |
| | | pointGoodsRecordMapper.updateById(record); |
| | | } |
| | | } |