liujie
2026-05-08 0426a6e77ad62134685d7541b2410198de3e716f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
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;
 
    /**
     * 分页查询兑换码卡包列表。
     *
     * @param query 查询条件
     * @return 卡包分页结果
     */
    @RequiresPermissions(value = {"/exchangeCode"}, logical = Logical.OR)
    @PostMapping("/pageList")
    @ApiOperation(value = "兑换码卡包分页", tags = {"管理后台-兑换码管理"})
    public AjaxResult<PageInfo<?>> pageList(@RequestBody ExchangeCodePackageQuery query) {
        return AjaxResult.ok(exchangeCodePackageService.pageList(query));
    }
 
    /**
     * 新增兑换码卡包。
     *
     * @param dto 卡包保存参数
     * @return 操作结果
     */
    @RequiresPermissions(value = {"/exchangeCode/add"}, logical = Logical.OR)
    @PostMapping("/save")
    @ApiOperation(value = "新增兑换码卡包", tags = {"管理后台-兑换码管理"})
    @Log(title = "【兑换码管理】新增卡包", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public AjaxResult<Void> save(@RequestBody ExchangeCodePackageSaveDTO dto) {
        exchangeCodePackageService.savePackage(dto);
        return AjaxResult.success();
    }
 
    /**
     * 编辑兑换码卡包。
     *
     * @param dto 卡包保存参数
     * @return 操作结果
     */
    @RequiresPermissions(value = {"/exchangeCode/update"}, logical = Logical.OR)
    @PostMapping("/update")
    @ApiOperation(value = "编辑兑换码卡包", tags = {"管理后台-兑换码管理"})
    @Log(title = "【兑换码管理】编辑卡包", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public AjaxResult<Void> update(@RequestBody ExchangeCodePackageSaveDTO dto) {
        exchangeCodePackageService.updatePackage(dto);
        return AjaxResult.success();
    }
 
    /**
     * 更新兑换码卡包启用状态。
     *
     * @param dto 卡包ID及状态参数
     * @return 操作结果
     */
    @RequiresPermissions(value = {"/exchangeCode/update"}, logical = Logical.OR)
    @PostMapping("/updateStatus")
    @ApiOperation(value = "更新兑换码卡包启用状态", tags = {"管理后台-兑换码管理"})
    @Log(title = "【兑换码管理】更新启用状态", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public AjaxResult<Void> updateStatus(@RequestBody ExchangeCodePackageSaveDTO dto) {
        exchangeCodePackageService.updatePackageStatus(dto.getId(), dto.getStatus());
        return AjaxResult.success();
    }
 
    /**
     * 删除兑换码卡包。
     *
     * @param ids 卡包ID,多个ID用英文逗号分隔
     * @return 操作结果
     */
    @RequiresPermissions(value = {"/exchangeCode/del"}, logical = Logical.OR)
    @GetMapping("/delete")
    @ApiOperation(value = "删除兑换码卡包", tags = {"管理后台-兑换码管理"})
    @Log(title = "【兑换码管理】删除卡包", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public AjaxResult<Void> delete(String ids) {
        exchangeCodePackageService.deletePackage(ids);
        return AjaxResult.success();
    }
 
    /**
     * 查询兑换码卡包详情。
     *
     * @param id 卡包ID
     * @return 卡包详情
     */
    @RequiresPermissions(value = {"/exchangeCode/select"}, logical = Logical.OR)
    @GetMapping("/detail")
    @ApiOperation(value = "兑换码卡包详情", tags = {"管理后台-兑换码管理"})
    public AjaxResult<ExchangeCodePackageDetailVO> detail(@RequestParam("id") Long id) {
        return AjaxResult.ok(exchangeCodePackageService.getDetail(id));
    }
 
    /**
     * 分页查询兑换码明细。
     *
     * @param query 查询条件
     * @return 兑换码明细分页结果
     */
    @RequiresPermissions(value = {"/exchangeCode/select"}, logical = Logical.OR)
    @PostMapping("/codePage")
    @ApiOperation(value = "兑换码明细分页", tags = {"管理后台-兑换码管理"})
    public AjaxResult<PageInfo<?>> codePage(@RequestBody ExchangeCodeDetailQuery query) {
        return AjaxResult.ok(exchangeCodePackageService.codePage(query));
    }
 
    /**
     * 批量失效兑换码。
     *
     * @param dto 失效参数
     * @return 失效结果
     */
    @RequiresPermissions(value = {"/exchangeCode/del"}, logical = Logical.OR)
    @PostMapping("/batchInvalidate")
    @ApiOperation(value = "批量失效兑换码", tags = {"管理后台-兑换码管理"})
    @Log(title = "【兑换码管理】批量失效", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public AjaxResult<ExchangeCodeBatchInvalidResultVO> batchInvalidate(@RequestBody ExchangeCodeBatchInvalidDTO dto) {
        ExchangeCodeBatchInvalidResultVO result = exchangeCodePackageService.batchInvalidate(dto);
        return AjaxResult.ok(result).put("msg", "已失效" + result.getSuccessCount() + "条,跳过" + result.getSkipCount() + "条");
    }
 
    /**
     * 导出兑换码明细。
     *
     * @param query 查询条件
     * @throws IOException 输出流写出异常
     */
    @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<ExchangeCodeDetailVO> list = exchangeCodePackageService.exportCodes(query);
        List<ExchangeCodeDetailExportVO> 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();
    }
 
    /**
     * 下载单个兑换码二维码。
     *
     * @param id 兑换码明细ID
     * @throws Exception 二维码生成或输出异常
     */
    @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());
    }
 
    /**
     * 批量下载未绑定兑换码二维码。
     *
     * @param query 查询条件
     * @throws Exception 二维码生成或压缩输出异常
     */
    @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<ExchangeCodeDetailVO> 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();
    }
 
    /**
     * 小程序兑换码绑定。
     *
     * @param dto 绑定参数
     * @param userId 用户ID
     * @param phone 手机号
     * @return 绑定结果
     */
    @PostMapping("/bind")
    @ApiOperation(value = "小程序兑换码绑定", tags = {"小程序-兑换码"})
    public R<ExchangeCodeBindResultVO> bind(@RequestBody ExchangeCodeBindDTO dto,
                                            @RequestParam("userId") Long userId,
                                            @RequestParam("phone") String phone) {
        return R.ok(exchangeCodePackageService.bindExchangeCode(dto, userId, phone));
    }
 
    /**
     * 查询用户兑换码余额映射。
     *
     * @param userIds 用户ID集合
     * @return 用户余额映射
     */
    @PostMapping("/balanceMap")
    @ApiOperation(value = "查询用户兑换码余额Map", tags = {"内部-兑换码"})
    public R<Map<Long, BigDecimal>> balanceMap(@RequestBody List<Long> userIds) {
        return R.ok(exchangeCodePackageService.getUserExchangeCodeBalanceMap(userIds));
    }
 
    /**
     * 扣减用户兑换码余额。
     *
     * @param dto 扣减参数
     * @return 扣减结果
     */
    @PostMapping("/consumeBalance")
    @ApiOperation(value = "扣减用户兑换码余额", tags = {"内部-兑换码"})
    public R<ExchangeCodeConsumeResultVO> consumeBalance(@RequestBody ExchangeCodeConsumeDTO dto) {
        return R.ok(exchangeCodePackageService.consumeBalance(dto));
    }
 
    /**
     * 退款返还用户兑换码余额。
     *
     * @param dto 退款参数
     * @return 退款结果
     */
    @PostMapping("/refundBalance")
    @ApiOperation(value = "退还兑换码余额", tags = {"小程序-兑换码"})
    public R<ExchangeCodeConsumeResultVO> refundBalance(@RequestBody ExchangeCodeConsumeDTO dto) {
        return R.ok(exchangeCodePackageService.refundBalance(dto));
    }
 
    /**
     * 分页查询兑换码余额流水。
     *
     * @param query 查询条件
     * @return 流水分页结果
     */
    @PostMapping("/useRecord/page")
    @ApiOperation(value = "兑换码余额流水分页", tags = {"小程序-兑换码"})
    public R<Page<TExchangeCodeUseRecord>> useRecordPage(@RequestBody ExchangeCodeUseRecordQuery query) {
        return R.ok(exchangeCodePackageService.useRecordPage(query));
    }
 
    /**
     * 查询兑换码余额流水详情。
     *
     * @param id 流水ID
     * @param userId 用户ID
     * @return 流水详情
     */
    @GetMapping("/useRecord/detail")
    @ApiOperation(value = "兑换码余额流水详情", tags = {"小程序-兑换码"})
    public R<TExchangeCodeUseRecord> useRecordDetail(@RequestParam("id") Long id,
                                                      @RequestParam("userId") Long userId) {
        return R.ok(exchangeCodePackageService.useRecordDetail(id, userId));
    }
 
    /**
     * 构建二维码内容。
     *
     * @param detail 兑换码明细
     * @return 二维码载荷内容
     */
    private String buildQrPayload(ExchangeCodeDetailVO detail) {
        return "code=" + detail.getCode() + "&password=" + detail.getPassword();
    }
}