package com.example.demo.serviceImpl; import com.example.demo.Util.GoldTistV2; import com.example.demo.domain.entity.*; import com.example.demo.domain.vo.ConsumeDetail; import com.example.demo.domain.vo.SumConsume; import com.example.demo.mapper.ConsumeMapper; import com.example.demo.mapper.UserMapper; import com.example.demo.sevice.ConsumeService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.ObjectUtils; import java.math.BigDecimal; import java.time.LocalDate; import java.time.Month; import java.util.List; import java.util.UUID; @Service @Transactional @RequiredArgsConstructor @CacheConfig(cacheNames = "consume") public class ConsumeServiceImpl implements ConsumeService { @Autowired ConsumeMapper consumeMapper; private final UserMapper userMapper; private final UserServiceImpl userService; @CacheEvict(value = {"consume", "detailY"}, allEntries = true) @Override //新增消费记录 public int insert(DetailY detailY) throws Exception { User sUser = userMapper.select(detailY.getJwcode()); if(ObjectUtils.isEmpty(sUser)){ throw new Exception("无此精网号"); } else if (detailY.getJwcode()==null||detailY.getJwcode().equals("")) { throw new Exception("精网号不能为空"); } else if (detailY.getAllGold()==null||detailY.getAllGold().compareTo(BigDecimal.ZERO) < 0) { throw new Exception("消费金币总数不能为空或小于等于0"); } // 生成UUID作为订单编号 String uuid = UUID.randomUUID().toString().replace("-", ""); // 去掉UUID中的'-' detailY.setOrderCode(uuid); int result =consumeMapper.insert(detailY); System.out.println(detailY+"----------------------------------------"); if (result != 1) { throw new Exception("Failed to insert another entity"); } BigDecimal paidGold1 =detailY.getRechargeCoin(); BigDecimal freeGold1 =detailY.getFreeCoin(); BigDecimal taskGold1 =detailY.getTaskCoin(); String name = detailY.getName(); String username =detailY.getUsername(); String area = detailY.getArea(); String jwcode = detailY.getJwcode(); String product = detailY.getProductName(); UserGold userGold = userMapper.selectGold(jwcode); BigDecimal buyJb =userGold.getBuyJb(); BigDecimal coreJb=userGold.getCoreJb(); buyJb = buyJb.add(paidGold1); coreJb = coreJb.add(taskGold1); // 设置更新后的Sumgold回到user对象 userGold.setBuyJb(buyJb); userGold.setCoreJb(coreJb); LocalDate now = LocalDate.now(); // 判断当前日期是在六月之前还是之后 Month currentMonth = now.getMonth(); boolean isBeforeJune = currentMonth.getValue() < Month.JUNE.getValue(); boolean isJune = currentMonth.getValue() == Month.JUNE.getValue(); boolean isAfterJune = currentMonth.getValue() > Month.JUNE.getValue(); // 根据月份更新 free6 或 free12 if (isBeforeJune || isJune) { // 如果是六月前,优先扣减 free6 BigDecimal free6 = userGold.getFree6(); if (free6.add(freeGold1).compareTo(BigDecimal.ZERO) >= 0) { // 如果 free6 足够扣减,直接扣减 userGold.setFree6(free6.add(freeGold1)); } else { // 如果 free6 不足,扣减 free6 到 0,剩余部分从 free12 扣减 BigDecimal remaining = free6.add(freeGold1); // 剩余需要扣减的金币 userGold.setFree6(BigDecimal.ZERO); // 将 free6 归零 userGold.setFree12(userGold.getFree12().add(remaining)); // 从 free12 扣减剩余部分 } } else if (isAfterJune) { // 如果是六月后,优先扣减 free12 BigDecimal free12 = userGold.getFree12(); if (free12.add(freeGold1).compareTo(BigDecimal.ZERO) >= 0) { // 如果 free12 足够扣减,直接扣减 userGold.setFree12(free12.add(freeGold1)); } else { // 如果 free12 不足,扣减 free12 到 0,剩余部分从 free6 扣减 BigDecimal remaining = free12.add(freeGold1); // 剩余需要扣减的金币 userGold.setFree12(BigDecimal.ZERO); // 将 free12 归零 userGold.setFree6(userGold.getFree6().add(remaining)); // 从 free6 扣减剩余部分 } } // 设置更新后的Sumgold回到user对象 System.out.println(userGold+"----------------------------------------------------------"); result = userMapper.updateGold(userGold); if (result != 1) { throw new Exception("Failed to insert another entity"); } //添加表单数据 // 更新用户对象以反映新的余额 result = userMapper.updateGold(userGold); GoldTistV2.addCoinNew(jwcode, 65, (paidGold1.add(freeGold1).add(taskGold1).divide(new BigDecimal("100"))).doubleValue(), "购买商品", paidGold1.doubleValue(),name,product); return result; } @Cacheable(key="#root.method.name") @Override public User getByUserId(Integer userId) { return consumeMapper.getByUserId(userId); } @Cacheable(key="#root.method.name") @Override public Admin getByadminId(Integer adminId) { return consumeMapper.getByadminId(adminId); } @Cacheable(key="#root.method.name") @Override public List search(ConsumeDetail consumeDetail) { return consumeMapper.select(consumeDetail); } @Cacheable(key = "#root.method.name + ':' + #pageNum + '-' + #pageSize + '-' + T(java.util.Objects).hashCode(#consumeDetail)") @Override public PageInfo searchForPage(Integer pageNum, Integer pageSize, ConsumeDetail consumeDetail) { PageHelper.startPage(pageNum, pageSize); List list = consumeMapper.select(consumeDetail); return new PageInfo<>(list); } @Override public SumConsume getSumConsume(SumConsume sumConsume) { return consumeMapper.getSumConsume(sumConsume); } @Override public List getConsume() { return consumeMapper.getConsume(); } public List getDeatil(String jwcode){ return consumeMapper.getDeatil(jwcode); } public List getProduct(String name){ return consumeMapper.getProduct(name); } }