| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.system.dto.ExchangeGoodsDto; |
| | | import com.ruoyi.system.mapper.TCrmChangePointsMapper; |
| | | import com.ruoyi.system.mapper.TCrmClinicPointsMapper; |
| | | import com.ruoyi.system.mapper.TSysGoodsExchangeMapper; |
| | | import com.ruoyi.system.mapper.TSysGoodsMapper; |
| | | import com.ruoyi.system.model.TSysGoods; |
| | | import com.ruoyi.system.model.*; |
| | | import com.ruoyi.system.service.TSysGoodsService; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Service |
| | | public class TSysGoodsServiceImpl extends ServiceImpl<TSysGoodsMapper, TSysGoods> implements TSysGoodsService { |
| | | |
| | | @Resource |
| | | private TSysGoodsExchangeMapper sysGoodsExchangeMapper; |
| | | |
| | | @Resource |
| | | private TCrmClinicPointsMapper crmClinicPointsMapper; |
| | | |
| | | @Resource |
| | | private TCrmChangePointsMapper crmChangePointsMapper; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void exchangeGoods(TCrmClinic crmClinic, ExchangeGoodsDto dto) { |
| | | // 盘点库存是否足够 判断积分是否足够 减积分 记录积分变更 添加购买记录 |
| | | TSysGoods tSysGoods = this.getById(dto.getGoodsId()); |
| | | int needPoints = tSysGoods.getRequiredPoints().intValue() * dto.getCount(); |
| | | |
| | | // 判断已经购买的了多少 |
| | | List<TSysGoodsExchange> list = sysGoodsExchangeMapper.selectList(new LambdaQueryWrapper<TSysGoodsExchange>().eq(TSysGoodsExchange::getGoodsId, dto.getGoodsId())); |
| | | int sum = list.stream().mapToInt(TSysGoodsExchange::getGoodsCount).sum(); |
| | | if(sum+dto.getCount()>tSysGoods.getGoodsTotal()){ |
| | | throw new RuntimeException("商品库存不足"); |
| | | } |
| | | if(sum+dto.getCount()>tSysGoods.getConvertibleQuantity()){ |
| | | throw new RuntimeException("可兑换数量不足"); |
| | | } |
| | | List<TCrmClinicPoints> points = crmClinicPointsMapper.selectList(new LambdaQueryWrapper<TCrmClinicPoints>().eq(TCrmClinicPoints::getClinicId, crmClinic.getId()).ge(TCrmClinicPoints::getExpireTime, new Date()).last(" and points > use_points").orderByAsc(TCrmClinicPoints::getCreateTime)); |
| | | // 可用积分 |
| | | int userPoints = points.stream().mapToInt(item -> item.getPoints() - item.getUsePoints()).sum(); |
| | | if(userPoints<needPoints){ |
| | | throw new RuntimeException("积分不足"); |
| | | } |
| | | |
| | | for (TCrmClinicPoints point : points) { |
| | | if(needPoints==0){ |
| | | break; |
| | | } |
| | | if(point.getPoints()-point.getUsePoints()>=needPoints){ |
| | | point.setUsePoints(point.getUsePoints()+needPoints); |
| | | crmClinicPointsMapper.updateById(point); |
| | | needPoints = 0; |
| | | }else { |
| | | point.setUsePoints(point.getPoints()); |
| | | crmClinicPointsMapper.updateById(point); |
| | | needPoints= needPoints - (point.getPoints() - point.getUsePoints()); |
| | | } |
| | | } |
| | | |
| | | // 添加兑换记录 |
| | | TSysGoodsExchange exchange = new TSysGoodsExchange(); |
| | | exchange.setClinicName(crmClinic.getClinicName()); |
| | | exchange.setGoodsName(tSysGoods.getGoodsName()); |
| | | exchange.setGoodsId(tSysGoods.getId()); |
| | | exchange.setGoodsCount(dto.getCount()); |
| | | exchange.setUsePoints(tSysGoods.getRequiredPoints().intValue() * dto.getCount()); |
| | | exchange.setExchangeTime(LocalDateTime.now()); |
| | | exchange.setClinicId(crmClinic.getId()); |
| | | sysGoodsExchangeMapper.insert(exchange); |
| | | |
| | | // 记录 |
| | | TCrmChangePoints tCrmChangePoints = new TCrmChangePoints(); |
| | | tCrmChangePoints.setBranchSalespersonId(crmClinic.getId()); |
| | | tCrmChangePoints.setExchangeId(exchange.getId()); |
| | | tCrmChangePoints.setUserType(3); |
| | | tCrmChangePoints.setChangeType(2); |
| | | tCrmChangePoints.setChangeReason(2); |
| | | tCrmChangePoints.setChangeValue(exchange.getUsePoints()); |
| | | crmChangePointsMapper.insert(tCrmChangePoints); |
| | | |
| | | } |
| | | } |