兑换码优化

This commit is contained in:
张松
2025-10-22 11:07:53 +08:00
parent f210284292
commit ca68666a36
28 changed files with 215 additions and 903 deletions

View File

@@ -1,7 +1,7 @@
package com.czg.service.market.mapper;
import com.mybatisflex.core.BaseMapper;
import com.czg.market.entity.MkCouponRedemptionCode;
import com.czg.market.entity.MkRedemptionCode;
/**
* 兑换码明细 映射层。
@@ -9,6 +9,6 @@ import com.czg.market.entity.MkCouponRedemptionCode;
* @author zs
* @since 2025-10-22
*/
public interface MkCouponRedemptionCodeMapper extends BaseMapper<MkCouponRedemptionCode> {
public interface MkCouponRedemptionCodeMapper extends BaseMapper<MkRedemptionCode> {
}

View File

@@ -1,7 +1,7 @@
package com.czg.service.market.mapper;
import com.mybatisflex.core.BaseMapper;
import com.czg.market.entity.MkCouponRedemptionConfig;
import com.czg.market.entity.MkRedemptionConfig;
/**
* 充值优惠券修改 映射层。
@@ -9,6 +9,6 @@ import com.czg.market.entity.MkCouponRedemptionConfig;
* @author zs
* @since 2025-10-22
*/
public interface MkCouponRedemptionConfigMapper extends BaseMapper<MkCouponRedemptionConfig> {
public interface MkCouponRedemptionConfigMapper extends BaseMapper<MkRedemptionConfig> {
}

View File

@@ -1,14 +0,0 @@
package com.czg.service.market.mapper;
import com.mybatisflex.core.BaseMapper;
import com.czg.market.entity.MkRechargeRedemptionCode;
/**
* 兑换码明细 映射层。
*
* @author zs
* @since 2025-10-21
*/
public interface MkRechargeRedemptionCodeMapper extends BaseMapper<MkRechargeRedemptionCode> {
}

View File

@@ -1,14 +0,0 @@
package com.czg.service.market.mapper;
import com.mybatisflex.core.BaseMapper;
import com.czg.market.entity.MkRechargeRedemptionConfig;
/**
* 充值优惠券修改 映射层。
*
* @author zs
* @since 2025-10-21
*/
public interface MkRechargeRedemptionConfigMapper extends BaseMapper<MkRechargeRedemptionConfig> {
}

View File

@@ -1,8 +1,8 @@
package com.czg.service.market.service.impl;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkCouponRedemptionCode;
import com.czg.market.service.MkCouponRedemptionCodeService;
import com.czg.market.entity.MkRedemptionCode;
import com.czg.market.service.MkRedemptionCodeService;
import com.czg.service.market.mapper.MkCouponRedemptionCodeMapper;
import org.springframework.stereotype.Service;
@@ -13,6 +13,6 @@ import org.springframework.stereotype.Service;
* @since 2025-10-22
*/
@Service
public class MkCouponRedemptionCodeServiceImpl extends ServiceImpl<MkCouponRedemptionCodeMapper, MkCouponRedemptionCode> implements MkCouponRedemptionCodeService{
public class MkCouponRedemptionCodeServiceImpl extends ServiceImpl<MkCouponRedemptionCodeMapper, MkRedemptionCode> implements MkRedemptionCodeService {
}

View File

@@ -1,271 +0,0 @@
package com.czg.service.market.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson2.JSONArray;
import com.czg.account.dto.shopuser.ShopUserMoneyEditDTO;
import com.czg.account.entity.ShopUser;
import com.czg.account.service.ShopInfoService;
import com.czg.account.service.ShopUserService;
import com.czg.constant.TableValueConstant;
import com.czg.enums.ShopUserFlowBizEnum;
import com.czg.exception.CzgException;
import com.czg.market.dto.CouponInfoDTO;
import com.czg.market.dto.MkCouponRedemptionConfigDTO;
import com.czg.market.dto.MkRedemptionDTO;
import com.czg.market.dto.MkShopCouponGiftDTO;
import com.czg.market.entity.MkCouponRedemptionCode;
import com.czg.market.entity.MkCouponRedemptionConfig;
import com.czg.market.entity.MkRechargeRedemptionCode;
import com.czg.market.entity.MkRechargeRedemptionConfig;
import com.czg.market.service.MkCouponRedemptionCodeService;
import com.czg.market.service.MkEnableConfigService;
import com.czg.market.service.MkShopCouponRecordService;
import com.czg.market.vo.CouponInfoVO;
import com.czg.market.vo.MkCouponRedemptionConfigVO;
import com.czg.market.vo.MkCouponRedemptionCodeVO;
import com.czg.utils.AssertUtil;
import com.czg.utils.MyQueryWrapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.service.MkCouponRedemptionConfigService;
import com.czg.service.market.mapper.MkCouponRedemptionConfigMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
* 充值优惠券修改 服务层实现。
*
* @author zs
* @since 2025-10-22
*/
@Service
public class MkCouponRedemptionConfigServiceImpl extends ServiceImpl<MkCouponRedemptionConfigMapper, MkCouponRedemptionConfig> implements MkCouponRedemptionConfigService{
@Resource
private MkCouponRedemptionCodeService codeService;
@Resource
private MkEnableConfigService enableConfigService;
@Resource
private MkShopCouponRecordService shopCouponRecordService;
@DubboReference
private ShopInfoService shopInfoService;
@DubboReference
private ShopUserService shopUserService;
@Override
public MkCouponRedemptionConfigVO detail(Long mainShopId, Long id) {
MkCouponRedemptionConfig redemptionConfig = getOne(new QueryWrapper().eq(MkCouponRedemptionConfig::getMainShopId, mainShopId).eq(MkCouponRedemptionConfig::getId, id));
if (redemptionConfig == null) {
return null;
}
MkCouponRedemptionConfigVO infoList = BeanUtil.copyProperties(redemptionConfig, MkCouponRedemptionConfigVO.class, "couponInfoList");
if (StrUtil.isNotBlank(redemptionConfig.getCouponInfoList())) {
infoList.setCouponInfoList(JSONArray.parseArray(redemptionConfig.getCouponInfoList(), CouponInfoDTO.class));
}
return infoList;
}
@Override
public Page<MkCouponRedemptionConfigVO> pageInfo(Long mainShopId, Integer status) {
QueryWrapper queryWrapper = new QueryWrapper().eq(MkCouponRedemptionConfig::getMainShopId, mainShopId)
.orderBy(MkCouponRedemptionConfig::getId, false);
switch (status) {
case 0:
queryWrapper.le(MkCouponRedemptionConfig::getStartTime, DateUtil.date()).ge(MkCouponRedemptionConfig::getEndTime, DateUtil.date());
break;
case 1:
queryWrapper.gt(MkCouponRedemptionConfig::getStartTime, DateUtil.date()).lt(MkCouponRedemptionConfig::getEndTime, DateUtil.date());
break;
}
Page<MkCouponRedemptionConfig> page = page(PageUtil.buildPage(), queryWrapper);
ArrayList<MkCouponRedemptionConfigVO> configList = new ArrayList<>();
page.getRecords().forEach(item -> {
if (item.getStartTime() != null && item.getEndTime() != null) {
item.setStatus(!item.getStartTime().isAfter(DateUtil.date().toLocalDateTime()) && item.getEndTime().isBefore(DateUtil.date().toLocalDateTime()) ? 0 : 1);
}else {
item.setStatus(0);
}
MkCouponRedemptionConfigVO config = BeanUtil.copyProperties(item, MkCouponRedemptionConfigVO.class, "couponInfoList");
if (StrUtil.isNotBlank(item.getCouponInfoList())) {
config.setCouponInfoList(JSONArray.parseArray(item.getCouponInfoList(), CouponInfoDTO.class));
config.setCouponNum(config.getCouponInfoList().stream().map(CouponInfoDTO::getNum).reduce(0, Integer::sum));
}
configList.add(config);
});
Page<MkCouponRedemptionConfigVO> page2 = new Page<>();
BeanUtil.copyProperties(page, page2, "records");
page2.setRecords(configList);
return page2;
}
@Override
public boolean add(Long mainShopId, MkCouponRedemptionConfigDTO dto) {
long count = count(new QueryWrapper().eq(MkCouponRedemptionConfig::getName, dto.getName()).eq(MkCouponRedemptionConfig::getMainShopId, mainShopId));
AssertUtil.isTrue(count > 0, "名称已存在");
MkCouponRedemptionConfig config = BeanUtil.copyProperties(dto, MkCouponRedemptionConfig.class, "couponInfoList");
config.setMainShopId(mainShopId);
config.setStock(config.getTotal());
if (dto.getCouponInfoList() != null && !dto.getCouponInfoList().isEmpty()) {
config.setCouponInfoList(JSONArray.toJSONString(dto.getCouponInfoList()));
}
save(config);
ArrayList<MkCouponRedemptionCode> codeArrayList = new ArrayList<>();
MkRechargeRedemptionConfigServiceImpl.generateCodes(config.getId(), config.getTotal()).forEach(code -> {
codeArrayList.add(new MkCouponRedemptionCode().setRedemptionId(config.getId())
.setMainShopId(mainShopId).setCode(code));
});
return codeService.saveBatch(codeArrayList);
}
@Override
public Boolean edit(Long shopId, MkCouponRedemptionConfigDTO dto) {
MkCouponRedemptionConfig config = getOne(new QueryWrapper().eq(MkCouponRedemptionConfig::getMainShopId, shopId).eq(MkCouponRedemptionConfig::getId, dto.getId()));
AssertUtil.isNull(config, "活动不存在");
if (dto.getStock() != null) {
if (config.getStock() < dto.getStock()) {
throw new CzgException("库存仅允许删减");
}
int subStock = config.getStock() - dto.getStock();
List<MkCouponRedemptionCode> subCodeList = codeService.list(new QueryWrapper().eq(MkCouponRedemptionCode::getRedemptionId, config.getId()).eq(MkCouponRedemptionCode::getStatus, 0).limit(subStock));
subCodeList.forEach(item -> item.setStatus(1));
codeService.updateBatch(subCodeList);
}
BeanUtil.copyProperties(dto, config, "total", "amount", "startTime", "endTime", "couponInfoList");
if (dto.getCouponInfoList() != null && !dto.getCouponInfoList().isEmpty()) {
config.setCouponInfoList(JSONArray.toJSONString(dto.getCouponInfoList()));
}
return updateById(config);
}
@Override
public Page<MkCouponRedemptionCodeVO> codeList(Long mainShopId, Long id, String code, Integer status) {
QueryWrapper queryWrapper = new MyQueryWrapper()
.selectAll(MkCouponRedemptionCode.class)
.select(ShopUser::getNickName, ShopUser::getPhone)
.eq(MkCouponRedemptionCode::getMainShopId, mainShopId)
.eq(MkCouponRedemptionCode::getRedemptionId, id)
.orderBy(MkCouponRedemptionCode::getId, false);
queryWrapper.eq(MkCouponRedemptionCode::getStatus, status);
if (StrUtil.isNotBlank(code)) {
queryWrapper.like(MkCouponRedemptionCode::getCode, code);
}
queryWrapper.leftJoin(ShopUser.class).on(ShopUser::getId, MkCouponRedemptionCode::getShopUserId);
return codeService.pageAs(PageUtil.buildPage(), queryWrapper, MkCouponRedemptionCodeVO.class);
}
@Override
public void exportCodeList(Long mainShopId, Long redemptionId, String code, Integer status, HttpServletResponse response, HttpServletRequest request) {
QueryWrapper queryWrapper = new MyQueryWrapper()
.selectAll(MkCouponRedemptionCode.class)
.select(ShopUser::getNickName, ShopUser::getPhone)
.eq(MkCouponRedemptionCode::getMainShopId, mainShopId)
.eq(MkCouponRedemptionCode::getRedemptionId, redemptionId);
queryWrapper.eq(MkCouponRedemptionCode::getStatus, status);
if (StrUtil.isNotBlank(code)) {
queryWrapper.like(MkCouponRedemptionCode::getCode, code);
}
queryWrapper.leftJoin(ShopUser.class).on(ShopUser::getId, MkCouponRedemptionCode::getShopUserId);
List<MkCouponRedemptionCodeVO> MkCouponRedemptionCodeVOS = codeService.listAs(queryWrapper, MkCouponRedemptionCodeVO.class);
List<Map<String, Object>> list = MkCouponRedemptionCodeVOS.stream()
.map(item -> {
Map<String, Object> map = new HashMap<>();
map.put("code", item.getCode());
map.put("redemptionTime", item.getRedemptionTime());
map.put("userInfo", item.getShopUserId() == null ? "" : item.getNickName() + " " + item.getPhone());
return map;
})
.toList();
// 1. 创建 ExcelWriter
ExcelWriter writer = ExcelUtil.getWriter(true);
// 2. 自定义标题别名(可选)
// 3. 写入数据
// 4. 设置响应头,告诉浏览器下载文件
try (writer) {
writer.addHeaderAlias("code", "兑换码");
writer.addHeaderAlias("redemptionTime", "兑换时间");
writer.addHeaderAlias("userInfo", "用户信息");
writer.write(list, true);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
String fileName;
fileName = URLEncoder.encode("兑换码", StandardCharsets.UTF_8);
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
// 5. 输出到浏览器
writer.flush(response.getOutputStream(), true);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void exchange(long userId, MkRedemptionDTO dto) {
Long mainShopId = shopInfoService.getMainIdByShopId(dto.getShopId());
enableConfigService.checkEnable(mainShopId, dto.getShopId(), TableValueConstant.EnableConfig.Type.RECHARGE_REDEMPTION, true);
ShopUser shopUserInfo = shopUserService.getShopUserInfo(dto.getShopId(), userId);
MkCouponRedemptionCode redemptionCode = codeService.getOne(new QueryWrapper().eq(MkCouponRedemptionCode::getMainShopId, mainShopId).eq(MkCouponRedemptionCode::getCode, dto.getCode()));
AssertUtil.isNull(redemptionCode, "兑换码不存在");
AssertUtil.isTrue(redemptionCode.getStatus() != 0, "兑换码不存在");
MkCouponRedemptionConfigVO config = detail(mainShopId, redemptionCode.getId());
AssertUtil.isNull(config, "兑换配置不存在");
AssertUtil.isTrue(config.getStock() <= 0, "兑换码可兑换库存不足");
if (config.getStartTime() != null) {
AssertUtil.isTrue(config.getStartTime().isAfter(DateUtil.date().toLocalDateTime()), "兑换活动未开始");
}
if (config.getEndTime() != null) {
AssertUtil.isTrue(config.getEndTime().isBefore(DateUtil.date().toLocalDateTime()), "兑换活动已结束");
}
if (config.getCouponInfoList() != null && !config.getCouponInfoList().isEmpty()) {
config.getCouponInfoList().forEach(item -> {
MkShopCouponGiftDTO giftDTO = new MkShopCouponGiftDTO().setCouponId(item.getId())
.setShopId(mainShopId)
.setSourceId(redemptionCode.getId())
.setShopUserId(shopUserInfo.getId())
.setSource("兑换码兑换");
shopCouponRecordService.receiveCoupon(giftDTO, item.getNum(), false);
});
}
redemptionCode.setStatus(1);
redemptionCode.setUserId(shopUserInfo.getUserId());
redemptionCode.setShopUserId(shopUserInfo.getId());
redemptionCode.setRedemptionTime(DateUtil.date().toLocalDateTime());
codeService.updateById(redemptionCode);
boolean update = updateChain().eq(MkRechargeRedemptionConfig::getId, config.getId()).setRaw(MkRechargeRedemptionConfig::getStock, "stock - 1")
.where("stock - 1 >= 0").update();
AssertUtil.isTrue(!update, "库存不足");
}
}

View File

@@ -1,18 +0,0 @@
package com.czg.service.market.service.impl;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkRechargeRedemptionCode;
import com.czg.market.service.MkRechargeRedemptionCodeService;
import com.czg.service.market.mapper.MkRechargeRedemptionCodeMapper;
import org.springframework.stereotype.Service;
/**
* 兑换码明细 服务层实现。
*
* @author zs
* @since 2025-10-21
*/
@Service
public class MkRechargeRedemptionCodeServiceImpl extends ServiceImpl<MkRechargeRedemptionCodeMapper, MkRechargeRedemptionCode> implements MkRechargeRedemptionCodeService{
}

View File

@@ -6,30 +6,33 @@ import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson2.JSONArray;
import com.czg.account.dto.shopuser.ShopUserMoneyEditDTO;
import com.czg.account.entity.ShopUser;
import com.czg.account.service.ShopInfoService;
import com.czg.account.service.ShopUserService;
import com.czg.account.service.UserInfoService;
import com.czg.constant.TableValueConstant;
import com.czg.enums.ShopUserFlowBizEnum;
import com.czg.exception.CzgException;
import com.czg.market.dto.MkRechargeRedemptionConfigDTO;
import com.czg.market.dto.CouponInfoDTO;
import com.czg.market.dto.MkRedemptionConfigDTO;
import com.czg.market.dto.MkRedemptionDTO;
import com.czg.market.entity.MkRechargeRedemptionCode;
import com.czg.market.dto.MkShopCouponGiftDTO;
import com.czg.market.entity.MkRedemptionCode;
import com.czg.market.entity.MkRedemptionConfig;
import com.czg.market.service.MkRedemptionCodeService;
import com.czg.market.service.MkEnableConfigService;
import com.czg.market.service.MkRechargeRedemptionCodeService;
import com.czg.market.vo.MkRechargeRedemptionCodeVO;
import com.czg.market.vo.MkRechargeRedemptionConfigVO;
import com.czg.market.service.MkShopCouponRecordService;
import com.czg.market.vo.MkRedemptionConfigVO;
import com.czg.market.vo.MkRedemptionCodeVO;
import com.czg.utils.AssertUtil;
import com.czg.utils.MyQueryWrapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkRechargeRedemptionConfig;
import com.czg.market.service.MkRechargeRedemptionConfigService;
import com.czg.service.market.mapper.MkRechargeRedemptionConfigMapper;
import com.czg.market.service.MkRedemptionConfigService;
import com.czg.service.market.mapper.MkCouponRedemptionConfigMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@@ -45,21 +48,21 @@ import java.util.*;
* 充值优惠券修改 服务层实现
*
* @author zs
* @since 2025-10-21
* @since 2025-10-22
*/
@Service
public class MkRechargeRedemptionConfigServiceImpl extends ServiceImpl<MkRechargeRedemptionConfigMapper, MkRechargeRedemptionConfig> implements MkRechargeRedemptionConfigService{
public class MkRedemptionConfigServiceImpl extends ServiceImpl<MkCouponRedemptionConfigMapper, MkRedemptionConfig> implements MkRedemptionConfigService {
@Resource
private MkRechargeRedemptionCodeService codeService;
private MkRedemptionCodeService codeService;
@Resource
private MkEnableConfigService enableConfigService;
@Resource
private MkShopCouponRecordService shopCouponRecordService;
@DubboReference
private ShopInfoService shopInfoService;
@DubboReference
private ShopUserService shopUserService;
/**
* 生成指定活动的兑换码集合
* @param activityId 活动ID
@@ -117,53 +120,76 @@ public class MkRechargeRedemptionConfigServiceImpl extends ServiceImpl<MkRecharg
}
}
public static void main(String[] args) {
Set<String> codes = generateCodes(12, 1000);
codes.forEach(System.out::println);
}
@Override
public MkRechargeRedemptionConfigVO detail(Long mainShopId) {
return getOneAs(new QueryWrapper().eq(MkRechargeRedemptionConfig::getMainShopId, mainShopId), MkRechargeRedemptionConfigVO.class);
public MkRedemptionConfigVO detail(Long mainShopId, Long id) {
MkRedemptionConfig redemptionConfig = getOne(new QueryWrapper().eq(MkRedemptionConfig::getMainShopId, mainShopId).eq(MkRedemptionConfig::getId, id));
if (redemptionConfig == null) {
return null;
}
MkRedemptionConfigVO infoList = BeanUtil.copyProperties(redemptionConfig, MkRedemptionConfigVO.class, "couponInfoList");
if (StrUtil.isNotBlank(redemptionConfig.getCouponInfoList())) {
infoList.setCouponInfoList(JSONArray.parseArray(redemptionConfig.getCouponInfoList(), CouponInfoDTO.class));
}
return infoList;
}
@Override
public Page<MkRechargeRedemptionConfig> pageInfo(Long mainShopId, Integer status) {
QueryWrapper queryWrapper = new QueryWrapper().eq(MkRechargeRedemptionConfig::getMainShopId, mainShopId)
.orderBy(MkRechargeRedemptionConfig::getId, false);
public Page<MkRedemptionConfigVO> pageInfo(Long mainShopId, Integer status, boolean isRecharge) {
QueryWrapper queryWrapper = new QueryWrapper().eq(MkRedemptionConfig::getMainShopId, mainShopId)
.eq(MkRedemptionConfig::getType, isRecharge ? 0 : 1)
.orderBy(MkRedemptionConfig::getId, false);
switch (status) {
case 0:
queryWrapper.le(MkRechargeRedemptionConfig::getStartTime, DateUtil.date()).ge(MkRechargeRedemptionConfig::getEndTime, DateUtil.date());
queryWrapper.le(MkRedemptionConfig::getStartTime, DateUtil.date()).ge(MkRedemptionConfig::getEndTime, DateUtil.date());
break;
case 1:
queryWrapper.gt(MkRechargeRedemptionConfig::getStartTime, DateUtil.date()).lt(MkRechargeRedemptionConfig::getEndTime, DateUtil.date());
queryWrapper.gt(MkRedemptionConfig::getStartTime, DateUtil.date()).lt(MkRedemptionConfig::getEndTime, DateUtil.date());
break;
}
Page<MkRechargeRedemptionConfig> page = page(PageUtil.buildPage(), queryWrapper);
Page<MkRedemptionConfig> page = page(PageUtil.buildPage(), queryWrapper);
ArrayList<MkRedemptionConfigVO> configList = new ArrayList<>();
page.getRecords().forEach(item -> {
if (item.getStartTime() != null && item.getEndTime() != null) {
item.setStatus(!item.getStartTime().isAfter(DateUtil.date().toLocalDateTime()) && item.getEndTime().isBefore(DateUtil.date().toLocalDateTime()) ? 0 : 1);
}else {
item.setStatus(0);
}
MkRedemptionConfigVO config = BeanUtil.copyProperties(item, MkRedemptionConfigVO.class, "couponInfoList");
if (StrUtil.isNotBlank(item.getCouponInfoList())) {
config.setCouponInfoList(JSONArray.parseArray(item.getCouponInfoList(), CouponInfoDTO.class));
config.setCouponNum(config.getCouponInfoList().stream().map(CouponInfoDTO::getNum).reduce(0, Integer::sum));
}
configList.add(config);
});
return page;
Page<MkRedemptionConfigVO> page2 = new Page<>();
BeanUtil.copyProperties(page, page2, "records");
page2.setRecords(configList);
return page2;
}
@Override
public boolean add(Long mainShopId, MkRechargeRedemptionConfigDTO dto) {
long count = count(new QueryWrapper().eq(MkRechargeRedemptionConfig::getName, dto.getName()).eq(MkRechargeRedemptionConfig::getMainShopId, mainShopId));
public boolean add(Long mainShopId, MkRedemptionConfigDTO dto, boolean isRecharge) {
long count = count(new QueryWrapper().eq(MkRedemptionConfig::getName, dto.getName()).eq(MkRedemptionConfig::getMainShopId, mainShopId)
.eq(MkRedemptionConfig::getType, isRecharge ? 0 : 1));
AssertUtil.isTrue(count > 0, "名称已存在");
MkRechargeRedemptionConfig config = BeanUtil.copyProperties(dto, MkRechargeRedemptionConfig.class);
MkRedemptionConfig config = BeanUtil.copyProperties(dto, MkRedemptionConfig.class, "couponInfoList");
config.setMainShopId(mainShopId);
config.setStock(config.getTotal());
config.setType(isRecharge ? 0 : 1);
if (dto.getCouponInfoList() != null && !dto.getCouponInfoList().isEmpty()) {
config.setCouponInfoList(JSONArray.toJSONString(dto.getCouponInfoList()));
}
save(config);
ArrayList<MkRechargeRedemptionCode> codeArrayList = new ArrayList<>();
ArrayList<MkRedemptionCode> codeArrayList = new ArrayList<>();
generateCodes(config.getId(), config.getTotal()).forEach(code -> {
codeArrayList.add(new MkRechargeRedemptionCode().setRedemptionId(config.getId())
codeArrayList.add(new MkRedemptionCode().setRedemptionId(config.getId())
.setMainShopId(mainShopId).setCode(code));
});
@@ -171,54 +197,59 @@ public class MkRechargeRedemptionConfigServiceImpl extends ServiceImpl<MkRecharg
}
@Override
public Boolean edit(Long shopId, MkRechargeRedemptionConfigDTO dto) {
MkRechargeRedemptionConfig config = getOne(new QueryWrapper().eq(MkRechargeRedemptionConfig::getMainShopId, shopId).eq(MkRechargeRedemptionConfig::getId, dto.getId()));
public Boolean edit(Long shopId, MkRedemptionConfigDTO dto, boolean isRecharge) {
MkRedemptionConfig config = getOne(new QueryWrapper().eq(MkRedemptionConfig::getMainShopId, shopId).eq(MkRedemptionConfig::getId, dto.getId())
.eq(MkRedemptionConfig::getType, isRecharge ? 0 : 1));
AssertUtil.isNull(config, "活动不存在");
if (dto.getStock() != null) {
if (config.getStock() < dto.getStock()) {
throw new CzgException("库存仅允许删减");
}
int subStock = config.getStock() - dto.getStock();
List<MkRechargeRedemptionCode> subCodeList = codeService.list(new QueryWrapper().eq(MkRechargeRedemptionCode::getRedemptionId, config.getId()).eq(MkRechargeRedemptionCode::getStatus, 0).limit(subStock));
List<MkRedemptionCode> subCodeList = codeService.list(new QueryWrapper().eq(MkRedemptionCode::getRedemptionId, config.getId()).eq(MkRedemptionCode::getStatus, 0).limit(subStock));
subCodeList.forEach(item -> item.setStatus(1));
codeService.updateBatch(subCodeList);
}
BeanUtil.copyProperties(dto, config, "total", "amount", "startTime", "endTime");
BeanUtil.copyProperties(dto, config, "total", "amount", "startTime", "endTime", "couponInfoList");
if (dto.getCouponInfoList() != null && !dto.getCouponInfoList().isEmpty()) {
config.setCouponInfoList(JSONArray.toJSONString(dto.getCouponInfoList()));
}
return updateById(config);
}
@Override
public Page<MkRechargeRedemptionCodeVO> codeList(Long mainShopId, Long id, String code, Integer status) {
public Page<MkRedemptionCodeVO> codeList(Long mainShopId, Long id, String code, Integer status, boolean isRecharge) {
QueryWrapper queryWrapper = new MyQueryWrapper()
.selectAll(MkRechargeRedemptionCode.class)
.selectAll(MkRedemptionCode.class)
.select(ShopUser::getNickName, ShopUser::getPhone)
.eq(MkRechargeRedemptionCode::getMainShopId, mainShopId)
.eq(MkRechargeRedemptionCode::getRedemptionId, id)
.orderBy(MkRechargeRedemptionCode::getId, false);
queryWrapper.eq(MkRechargeRedemptionCode::getStatus, status);
.eq(MkRedemptionCode::getMainShopId, mainShopId)
.eq(MkRedemptionCode::getRedemptionId, id)
.orderBy(MkRedemptionCode::getId, false);
queryWrapper.eq(MkRedemptionCode::getStatus, status);
if (StrUtil.isNotBlank(code)) {
queryWrapper.like(MkRechargeRedemptionCode::getCode, code);
queryWrapper.like(MkRedemptionCode::getCode, code);
}
queryWrapper.leftJoin(ShopUser.class).on(ShopUser::getId, MkRechargeRedemptionCode::getShopUserId);
return codeService.pageAs(PageUtil.buildPage(), queryWrapper, MkRechargeRedemptionCodeVO.class);
queryWrapper.leftJoin(ShopUser.class).on(ShopUser::getId, MkRedemptionCode::getShopUserId);
return codeService.pageAs(PageUtil.buildPage(), queryWrapper, MkRedemptionCodeVO.class);
}
@Override
public void exportCodeList(Long mainShopId, Long redemptionId, String code, Integer status, HttpServletResponse response, HttpServletRequest request) {
public void exportCodeList(Long mainShopId, Long redemptionId, String code, Integer status, HttpServletResponse response, HttpServletRequest request, boolean isRecharge) {
QueryWrapper queryWrapper = new MyQueryWrapper()
.selectAll(MkRechargeRedemptionCode.class)
.selectAll(MkRedemptionCode.class)
.select(ShopUser::getNickName, ShopUser::getPhone)
.eq(MkRechargeRedemptionCode::getMainShopId, mainShopId)
.eq(MkRechargeRedemptionCode::getRedemptionId, redemptionId);
queryWrapper.eq(MkRechargeRedemptionCode::getStatus, status);
.eq(MkRedemptionCode::getMainShopId, mainShopId)
.eq(MkRedemptionCode::getRedemptionId, redemptionId);
queryWrapper.eq(MkRedemptionCode::getStatus, status);
if (StrUtil.isNotBlank(code)) {
queryWrapper.like(MkRechargeRedemptionCode::getCode, code);
queryWrapper.like(MkRedemptionCode::getCode, code);
}
queryWrapper.leftJoin(ShopUser.class).on(ShopUser::getId, MkRechargeRedemptionCode::getShopUserId);
List<MkRechargeRedemptionCodeVO> mkRechargeRedemptionCodeVOS = codeService.listAs(queryWrapper, MkRechargeRedemptionCodeVO.class);
queryWrapper.leftJoin(ShopUser.class).on(ShopUser::getId, MkRedemptionCode::getShopUserId);
List<MkRedemptionCodeVO> MkCouponRedemptionCodeVOS = codeService.listAs(queryWrapper, MkRedemptionCodeVO.class);
List<Map<String, Object>> list = mkRechargeRedemptionCodeVOS.stream()
List<Map<String, Object>> list = MkCouponRedemptionCodeVOS.stream()
.map(item -> {
Map<String, Object> map = new HashMap<>();
map.put("code", item.getCode());
@@ -247,9 +278,8 @@ public class MkRechargeRedemptionConfigServiceImpl extends ServiceImpl<MkRecharg
// 5. 输出到浏览器
writer.flush(response.getOutputStream(), true);
} catch (Exception e) {
throw new RuntimeException(e);
throw new RuntimeException(e);
}
}
@Override
@@ -259,11 +289,11 @@ public class MkRechargeRedemptionConfigServiceImpl extends ServiceImpl<MkRecharg
enableConfigService.checkEnable(mainShopId, dto.getShopId(), TableValueConstant.EnableConfig.Type.RECHARGE_REDEMPTION, true);
ShopUser shopUserInfo = shopUserService.getShopUserInfo(dto.getShopId(), userId);
MkRechargeRedemptionCode redemptionCode = codeService.getOne(new QueryWrapper().eq(MkRechargeRedemptionCode::getMainShopId, mainShopId).eq(MkRechargeRedemptionCode::getCode, dto.getCode()));
MkRedemptionCode redemptionCode = codeService.getOne(new QueryWrapper().eq(MkRedemptionCode::getMainShopId, mainShopId).eq(MkRedemptionCode::getCode, dto.getCode()));
AssertUtil.isNull(redemptionCode, "兑换码不存在");
AssertUtil.isTrue(redemptionCode.getStatus() != 0, "兑换码不存在");
MkRechargeRedemptionConfig config = getOne(new QueryWrapper().eq(MkRechargeRedemptionConfig::getId, redemptionCode.getRedemptionId()).eq(MkRechargeRedemptionConfig::getMainShopId, mainShopId));
MkRedemptionConfigVO config = detail(mainShopId, redemptionCode.getId());
AssertUtil.isNull(config, "兑换配置不存在");
AssertUtil.isTrue(config.getStock() <= 0, "兑换码可兑换库存不足");
if (config.getStartTime() != null) {
@@ -273,21 +303,37 @@ public class MkRechargeRedemptionConfigServiceImpl extends ServiceImpl<MkRecharg
AssertUtil.isTrue(config.getEndTime().isBefore(DateUtil.date().toLocalDateTime()), "兑换活动已结束");
}
ShopUserMoneyEditDTO shopUserMoneyEditDTO = new ShopUserMoneyEditDTO()
.setId(shopUserInfo.getId())
.setMoney(config.getAmount())
.setType(1)
.setBizEnum(ShopUserFlowBizEnum.RECHARGE_REDEMPTION)
.setRelationId(redemptionCode.getId());
if (config.getType() == 0) {
ShopUserMoneyEditDTO shopUserMoneyEditDTO = new ShopUserMoneyEditDTO()
.setId(shopUserInfo.getId())
.setMoney(config.getAmount())
.setType(1)
.setBizEnum(ShopUserFlowBizEnum.RECHARGE_REDEMPTION)
.setRelationId(redemptionCode.getId());
shopUserService.updateMoney(shopUserMoneyEditDTO);
redemptionCode.setStatus(1);
redemptionCode.setUserId(shopUserInfo.getUserId());
redemptionCode.setShopUserId(shopUserInfo.getId());
redemptionCode.setRedemptionTime(DateUtil.date().toLocalDateTime());
codeService.updateById(redemptionCode);
shopUserService.updateMoney(shopUserMoneyEditDTO);
}else {
if (config.getCouponInfoList() != null && !config.getCouponInfoList().isEmpty()) {
config.getCouponInfoList().forEach(item -> {
MkShopCouponGiftDTO giftDTO = new MkShopCouponGiftDTO().setCouponId(item.getId())
.setShopId(mainShopId)
.setSourceId(redemptionCode.getId())
.setShopUserId(shopUserInfo.getId())
.setSource("兑换码兑换");
shopCouponRecordService.receiveCoupon(giftDTO, item.getNum(), false);
boolean update = updateChain().eq(MkRechargeRedemptionConfig::getId, config.getId()).setRaw(MkRechargeRedemptionConfig::getStock, "stock - 1")
});
}
}
redemptionCode.setStatus(1);
redemptionCode.setUserId(shopUserInfo.getUserId());
redemptionCode.setShopUserId(shopUserInfo.getId());
redemptionCode.setRedemptionTime(DateUtil.date().toLocalDateTime());
codeService.updateById(redemptionCode);
boolean update = updateChain().eq(MkRedemptionConfig::getId, config.getId()).setRaw(MkRedemptionConfig::getStock, "stock - 1")
.where("stock - 1 >= 0").update();
AssertUtil.isTrue(!update, "库存不足");

View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czg.service.market.mapper.MkRechargeRedemptionCodeMapper">
</mapper>

View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czg.service.market.mapper.MkRechargeRedemptionConfigMapper">
</mapper>