package com.ruoyi.other.controller; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import com.ruoyi.common.core.utils.WebUtils; import com.ruoyi.common.core.domain.R; import com.ruoyi.other.util.QRCodeUtils; import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.core.web.page.PageInfo; import com.ruoyi.common.log.annotation.Log; import com.ruoyi.common.log.enums.BusinessType; import com.ruoyi.common.log.enums.OperatorType; import com.ruoyi.common.security.annotation.Logical; import com.ruoyi.common.security.annotation.RequiresPermissions; import com.ruoyi.other.api.dto.ExchangeCodeBindDTO; import com.ruoyi.other.api.dto.ExchangeCodeConsumeDTO; import com.ruoyi.other.api.dto.ExchangeCodePackageSaveDTO; import com.ruoyi.other.api.dto.ExchangeCodeUseRecordQuery; import com.ruoyi.other.api.domain.TExchangeCodeUseRecord; import com.ruoyi.other.api.vo.ExchangeCodeBindResultVO; import com.ruoyi.other.api.vo.ExchangeCodeConsumeResultVO; import com.ruoyi.other.api.vo.ExchangeCodePackageDetailVO; import com.ruoyi.other.query.ExchangeCodeBatchInvalidDTO; import com.ruoyi.other.query.ExchangeCodeDetailQuery; import com.ruoyi.other.query.ExchangeCodePackageQuery; import com.ruoyi.other.service.TExchangeCodePackageService; import com.ruoyi.other.vo.ExchangeCodeBatchInvalidResultVO; import com.ruoyi.other.vo.ExchangeCodeDetailExportVO; import com.ruoyi.other.vo.ExchangeCodeDetailVO; import io.swagger.annotations.ApiOperation; import org.springframework.beans.BeanUtils; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URLEncoder; import java.util.List; import java.math.BigDecimal; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @RestController @RequestMapping("/exchange-code-package") public class TExchangeCodePackageController { @Resource private TExchangeCodePackageService exchangeCodePackageService; @RequiresPermissions(value = {"/exchangeCode"}, logical = Logical.OR) @PostMapping("/pageList") @ApiOperation(value = "兑换码卡包分页", tags = {"管理后台-兑换码管理"}) public AjaxResult> pageList(@RequestBody ExchangeCodePackageQuery query) { return AjaxResult.ok(exchangeCodePackageService.pageList(query)); } @RequiresPermissions(value = {"/exchangeCode/add"}, logical = Logical.OR) @PostMapping("/save") @ApiOperation(value = "新增兑换码卡包", tags = {"管理后台-兑换码管理"}) @Log(title = "【兑换码管理】新增卡包", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE) public AjaxResult save(@RequestBody ExchangeCodePackageSaveDTO dto) { exchangeCodePackageService.savePackage(dto); return AjaxResult.success(); } @RequiresPermissions(value = {"/exchangeCode/update"}, logical = Logical.OR) @PostMapping("/update") @ApiOperation(value = "编辑兑换码卡包", tags = {"管理后台-兑换码管理"}) @Log(title = "【兑换码管理】编辑卡包", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE) public AjaxResult update(@RequestBody ExchangeCodePackageSaveDTO dto) { exchangeCodePackageService.updatePackage(dto); return AjaxResult.success(); } @RequiresPermissions(value = {"/exchangeCode/update"}, logical = Logical.OR) @PostMapping("/updateStatus") @ApiOperation(value = "更新兑换码卡包启用状态", tags = {"管理后台-兑换码管理"}) @Log(title = "【兑换码管理】更新启用状态", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE) public AjaxResult updateStatus(@RequestBody ExchangeCodePackageSaveDTO dto) { exchangeCodePackageService.updatePackageStatus(dto.getId(), dto.getStatus()); return AjaxResult.success(); } @RequiresPermissions(value = {"/exchangeCode/del"}, logical = Logical.OR) @GetMapping("/delete") @ApiOperation(value = "删除兑换码卡包", tags = {"管理后台-兑换码管理"}) @Log(title = "【兑换码管理】删除卡包", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE) public AjaxResult delete(String ids) { exchangeCodePackageService.deletePackage(ids); return AjaxResult.success(); } @RequiresPermissions(value = {"/exchangeCode/select"}, logical = Logical.OR) @GetMapping("/detail") @ApiOperation(value = "兑换码卡包详情", tags = {"管理后台-兑换码管理"}) public AjaxResult detail(@RequestParam("id") Long id) { return AjaxResult.ok(exchangeCodePackageService.getDetail(id)); } @RequiresPermissions(value = {"/exchangeCode/select"}, logical = Logical.OR) @PostMapping("/codePage") @ApiOperation(value = "兑换码明细分页", tags = {"管理后台-兑换码管理"}) public AjaxResult> codePage(@RequestBody ExchangeCodeDetailQuery query) { return AjaxResult.ok(exchangeCodePackageService.codePage(query)); } @RequiresPermissions(value = {"/exchangeCode/del"}, logical = Logical.OR) @PostMapping("/batchInvalidate") @ApiOperation(value = "批量失效兑换码", tags = {"管理后台-兑换码管理"}) @Log(title = "【兑换码管理】批量失效", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE) public AjaxResult batchInvalidate(@RequestBody ExchangeCodeBatchInvalidDTO dto) { ExchangeCodeBatchInvalidResultVO result = exchangeCodePackageService.batchInvalidate(dto); return AjaxResult.ok(result).put("msg", "已失效" + result.getSuccessCount() + "条,跳过" + result.getSkipCount() + "条"); } @RequiresPermissions(value = {"/exchangeCode/select"}, logical = Logical.OR) @PutMapping("/export") @ApiOperation(value = "导出兑换码明细", tags = {"管理后台-兑换码管理"}) @Log(title = "【兑换码管理】导出明细", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE) public void export(@RequestBody ExchangeCodeDetailQuery query) throws IOException { List list = exchangeCodePackageService.exportCodes(query); List exportList = new java.util.ArrayList<>(); for (ExchangeCodeDetailVO detailVO : list) { ExchangeCodeDetailExportVO exportVO = new ExchangeCodeDetailExportVO(); BeanUtils.copyProperties(detailVO, exportVO); exportList.add(exportVO); } Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), ExchangeCodeDetailExportVO.class, exportList); HttpServletResponse response = WebUtils.response(); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode("兑换码明细.xls", "utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); ServletOutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } @RequiresPermissions(value = {"/exchangeCode/select"}, logical = Logical.OR) @GetMapping("/downloadQRCode/{id}") @ApiOperation(value = "下载单个兑换码二维码", tags = {"管理后台-兑换码管理"}) @Log(title = "【兑换码管理】下载二维码", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE) public void downloadQRCode(@PathVariable("id") Long id) throws Exception { ExchangeCodeDetailVO detail = exchangeCodePackageService.getCodeDetail(id); HttpServletResponse response = WebUtils.response(); response.setContentType("image/jpeg"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(detail.getCode() + ".jpg", "UTF-8")); QRCodeUtils.encode(buildQrPayload(detail), response.getOutputStream()); } @RequiresPermissions(value = {"/exchangeCode/select"}, logical = Logical.OR) @PutMapping("/downloadUnboundQRCodes") @ApiOperation(value = "批量下载未绑定二维码", tags = {"管理后台-兑换码管理"}) @Log(title = "【兑换码管理】批量下载未绑定二维码", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE) public void downloadUnboundQRCodes(@RequestBody ExchangeCodeDetailQuery query) throws Exception { List list = exchangeCodePackageService.listDownloadableCodes(query.getPackageId(), true); HttpServletResponse response = WebUtils.response(); response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("未绑定二维码.zip", "UTF-8")); ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream()); for (ExchangeCodeDetailVO detail : list) { zipOutputStream.putNextEntry(new ZipEntry(detail.getCode() + ".jpg")); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); QRCodeUtils.encode(buildQrPayload(detail), outputStream); zipOutputStream.write(outputStream.toByteArray()); zipOutputStream.closeEntry(); } zipOutputStream.finish(); zipOutputStream.flush(); zipOutputStream.close(); } @PostMapping("/bind") @ApiOperation(value = "小程序兑换码绑定", tags = {"小程序-兑换码"}) public R bind(@RequestBody ExchangeCodeBindDTO dto, @RequestParam("userId") Long userId, @RequestParam("phone") String phone) { return R.ok(exchangeCodePackageService.bindExchangeCode(dto, userId, phone)); } @PostMapping("/balanceMap") @ApiOperation(value = "查询用户兑换码余额Map", tags = {"内部-兑换码"}) public R> balanceMap(@RequestBody List userIds) { return R.ok(exchangeCodePackageService.getUserExchangeCodeBalanceMap(userIds)); } @PostMapping("/consumeBalance") @ApiOperation(value = "扣减用户兑换码余额", tags = {"内部-兑换码"}) public R consumeBalance(@RequestBody ExchangeCodeConsumeDTO dto) { return R.ok(exchangeCodePackageService.consumeBalance(dto)); } @PostMapping("/refundBalance") @ApiOperation(value = "退还兑换码余额", tags = {"小程序-兑换码"}) public R refundBalance(@RequestBody ExchangeCodeConsumeDTO dto) { return R.ok(exchangeCodePackageService.refundBalance(dto)); } @PostMapping("/useRecord/page") @ApiOperation(value = "兑换码余额流水分页", tags = {"小程序-兑换码"}) public R> useRecordPage(@RequestBody ExchangeCodeUseRecordQuery query) { return R.ok(exchangeCodePackageService.useRecordPage(query)); } @GetMapping("/useRecord/detail") @ApiOperation(value = "兑换码余额流水详情", tags = {"小程序-兑换码"}) public R useRecordDetail(@RequestParam("id") Long id, @RequestParam("userId") Long userId) { return R.ok(exchangeCodePackageService.useRecordDetail(id, userId)); } private String buildQrPayload(ExchangeCodeDetailVO detail) { return "code=" + detail.getCode() + "&password=" + detail.getPassword(); } }