package com.example.demo.serviceImpl; import com.example.demo.domain.entity.Detail; import com.example.demo.mapper.RefundMapper; import com.example.demo.sevice.RefundService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lombok.RequiredArgsConstructor; 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 java.util.List; import java.util.UUID; @RequiredArgsConstructor @Transactional @Service @CacheConfig(cacheNames = "refund") public class RefundServiceImpl implements RefundService { private final RefundMapper refundMapper; @CacheEvict(value = {"refund", "recharge"}, allEntries = true) @Override public int add(Detail detail) { // 生成UUID作为订单编号 String uuid = UUID.randomUUID().toString().replaceAll("-", ""); // 去掉UUID中的'-' detail.setOrderCode(uuid); return refundMapper.insert(detail); } @CacheEvict(value = {"refund", "recharge"}, allEntries = true) @Override public int addAudit(Detail detail) { return refundMapper.insertAudit(detail); } //自动软删除数据加更新数据 @CacheEvict(value = {"refund", "recharge"}, allEntries = true) @Override public void edit(Detail newDetail) { // 获取旧的明细记录 Detail oldDetail = refundMapper.selectByDetailId(newDetail.getDetailId()); if (oldDetail == null || oldDetail.getDetailFlag() ==0) { throw new IllegalArgumentException("该记录不存在或已被隐藏!"); } // 软删除旧记录 refundMapper.update(oldDetail.getDetailId()); // 将新的字段值复制到旧记录中,未修改的字段保持旧值 if (newDetail.getJwcode() == null) newDetail.setJwcode(oldDetail.getJwcode()); if (newDetail.getRefundType() == null) newDetail.setRefundType(oldDetail.getRefundType()); if (newDetail.getRefundGoods() == null) newDetail.setRefundGoods(oldDetail.getRefundGoods()); if (newDetail.getRechargeCoin() == null) newDetail.setRechargeCoin(oldDetail.getRechargeCoin()); if (newDetail.getFreeCoin() == null) newDetail.setFreeCoin(oldDetail.getFreeCoin()); if (newDetail.getTaskCoin() == null) newDetail.setTaskCoin(oldDetail.getTaskCoin()); if (newDetail.getRemark() == null) newDetail.setRemark(oldDetail.getRemark()); if (newDetail.getAdminId() == null) newDetail.setAdminId(oldDetail.getAdminId()); // 插入新记录 newDetail.setDetailId(null); // 清空 ID,让其自动生成 newDetail.setDetailFlag(1); // 确保新记录未被删除 refundMapper.insert(newDetail); } @Override @CacheEvict(value = {"refund", "recharge"}, allEntries = true) public int update(Integer contactId) { return refundMapper.updateOrderCode(contactId); } @Cacheable(key="#root.method.name") @Override public boolean existsByContactId(Integer contactId) { return refundMapper.existsByContactId(contactId); } @CacheEvict(value = {"refund", "recharge"}, allEntries = true) @Override public int softDelete(Integer detailId) { return refundMapper.update(detailId); } @Cacheable(key="#root.method.name") @Override public Detail selectByOrderCode(String orderCode) { return refundMapper.selectByOrderCode(orderCode); } @Cacheable(key="#root.method.name") @Override public Detail selectByJWCODE(String jwcode) { return refundMapper.selectByJWCODE(jwcode); } @Cacheable(key="#root.method.name") @Override public Detail selectByDetailId(Integer detailId) { return refundMapper.selectByDetailId(detailId); } @Cacheable(key="#root.method.name") @Override public List search(Detail detail) { return refundMapper.select(detail); } @Cacheable(key="#root.method.name + ':'+ #pageNum + '-' + #pageSize + '-' + #detail.auditStatus ") @Override public PageInfo searchForPage(Integer pageNum, Integer pageSize, Detail detail) { PageHelper.startPage(pageNum, pageSize); List list = refundMapper.select(detail); return new PageInfo<>(list); } }