liujie
2026-05-06 f2209cbd02be8cb1f508b18d0e8b3789d4c0ecc5
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
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<PageInfo<?>> 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<Void> 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<Void> 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<Void> 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<Void> delete(String ids) {
        exchangeCodePackageService.deletePackage(ids);
        return AjaxResult.success();
    }
 
    @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));
    }
 
    @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));
    }
 
    @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() + "条");
    }
 
    @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();
    }
 
    @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<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();
    }
 
    @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));
    }
 
    @PostMapping("/balanceMap")
    @ApiOperation(value = "查询用户兑换码余额Map", tags = {"内部-兑换码"})
    public R<Map<Long, BigDecimal>> balanceMap(@RequestBody List<Long> userIds) {
        return R.ok(exchangeCodePackageService.getUserExchangeCodeBalanceMap(userIds));
    }
 
    @PostMapping("/consumeBalance")
    @ApiOperation(value = "扣减用户兑换码余额", tags = {"内部-兑换码"})
    public R<ExchangeCodeConsumeResultVO> consumeBalance(@RequestBody ExchangeCodeConsumeDTO dto) {
        return R.ok(exchangeCodePackageService.consumeBalance(dto));
    }
 
    @PostMapping("/refundBalance")
    @ApiOperation(value = "退还兑换码余额", tags = {"小程序-兑换码"})
    public R<ExchangeCodeConsumeResultVO> refundBalance(@RequestBody ExchangeCodeConsumeDTO dto) {
        return R.ok(exchangeCodePackageService.refundBalance(dto));
    }
 
    @PostMapping("/useRecord/page")
    @ApiOperation(value = "兑换码余额流水分页", tags = {"小程序-兑换码"})
    public R<Page<TExchangeCodeUseRecord>> useRecordPage(@RequestBody ExchangeCodeUseRecordQuery query) {
        return R.ok(exchangeCodePackageService.useRecordPage(query));
    }
 
    @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));
    }
 
    private String buildQrPayload(ExchangeCodeDetailVO detail) {
        return "code=" + detail.getCode() + "&password=" + detail.getPassword();
    }
}