券兑换码相关接口

This commit is contained in:
张松
2025-10-22 09:41:25 +08:00
parent 15d93a54cd
commit f0c4ea2242
15 changed files with 827 additions and 4 deletions

View File

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

View File

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

View File

@@ -0,0 +1,18 @@
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.service.market.mapper.MkCouponRedemptionCodeMapper;
import org.springframework.stereotype.Service;
/**
* 兑换码明细 服务层实现。
*
* @author zs
* @since 2025-10-22
*/
@Service
public class MkCouponRedemptionCodeServiceImpl extends ServiceImpl<MkCouponRedemptionCodeMapper, MkCouponRedemptionCode> implements MkCouponRedemptionCodeService{
}

View File

@@ -0,0 +1,204 @@
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.entity.ShopUser;
import com.czg.exception.CzgException;
import com.czg.market.dto.MkCouponRedemptionConfigDTO;
import com.czg.market.entity.MkCouponRedemptionCode;
import com.czg.market.entity.MkCouponRedemptionConfig;
import com.czg.market.service.MkCouponRedemptionCodeService;
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.springframework.stereotype.Service;
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;
@Override
public MkCouponRedemptionConfigVO detail(Long mainShopId) {
MkCouponRedemptionConfig redemptionConfig = getOne(new QueryWrapper().eq(MkCouponRedemptionConfig::getMainShopId, mainShopId));
if (redemptionConfig == null) {
return null;
}
MkCouponRedemptionConfigVO infoList = BeanUtil.copyProperties(redemptionConfig, MkCouponRedemptionConfigVO.class, "couponInfoList");
if (StrUtil.isNotBlank(redemptionConfig.getCouponInfoList())) {
infoList.setCouponInfoList(JSONArray.parseArray(redemptionConfig.getCouponInfoList(), CouponInfoVO.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);
if (StrUtil.isNotBlank(item.getCouponInfoList())) {
config.setCouponInfoList(JSONArray.parseArray(item.getCouponInfoList(), CouponInfoVO.class));
config.setCouponNum(config.getCouponInfoList().stream().map(CouponInfoVO::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);
}
}
}

View File

@@ -0,0 +1,7 @@
<?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.MkCouponRedemptionCodeMapper">
</mapper>

View File

@@ -0,0 +1,7 @@
<?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.MkCouponRedemptionConfigMapper">
</mapper>