无关风月
2025-01-22 99367ea1c11a68b420936e7f7db5fa7367da4f44
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
package com.xinquan.user.controller.client;
 
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinquan.common.core.domain.R;
import com.xinquan.common.core.utils.WebUtils;
import com.xinquan.common.core.utils.page.CollUtils;
import com.xinquan.common.core.utils.page.PageDTO;
import com.xinquan.common.log.annotation.Log;
import com.xinquan.common.log.enums.BusinessType;
import com.xinquan.common.security.service.TokenService;
import com.xinquan.common.security.utils.SecurityUtils;
 
import com.xinquan.meditation.api.feign.RemoteMeditationService;
import com.xinquan.system.api.RemoteUserService;
import com.xinquan.system.api.domain.*;
import com.xinquan.system.api.model.LoginUser;
import com.xinquan.user.api.domain.dto.OrderListDTO;
import com.xinquan.user.api.domain.dto.PrizeRecordDTO;
import com.xinquan.user.domain.export.PrizeExport;
import com.xinquan.user.domain.export.WithdrawExport;
import com.xinquan.user.service.AppUserService;
import com.xinquan.user.service.PrizeRedemptionRecordService;
import com.xinquan.user.service.PrizeService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 奖品兑换记录表 前端控制器
 * </p>
 *
 * @author mitao
 * @since 2024-08-21
 */
@RestController
@RequestMapping("/client/prize-redemption-record")
public class ClientPrizeRedemptionRecordController {
    @Resource
    private PrizeService prizeService;
    @Resource
    private PrizeRedemptionRecordService prizeRedemptionRecordService;
    @Resource
    private AppUserService appUserService;
    @Resource
    private RemoteUserService remoteUserService;
    @PostMapping("/getPrizeRecordCount")
    public R<String> getPrizeRecordCount(){
        List<PrizeRedemptionRecord> list = prizeRedemptionRecordService.lambdaQuery()
                .eq(PrizeRedemptionRecord::getStatus, 1).list();
        return R.ok(list.size()+"");
    }
 
    @ApiOperation(value = "兑换记录管理导出", tags = {"管理后台-兑换记录管理"})
    @PutMapping("/export")
    public void export(@RequestBody PrizeRecordDTO courseDTO){
        String startTime = null;
        String endTime = null;
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getTime())){
            String[] split = courseDTO.getTime().split(" - ");
            startTime = split[0]+" 00:00:00";
            endTime = split[1]+" 23:59:59";
        }
        List<Long> longs = new ArrayList<>();
        LambdaQueryWrapper<PrizeRedemptionRecord> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(courseDTO.getIds())){
            courseLambdaQueryWrapper.in(PrizeRedemptionRecord::getId, Arrays.asList(courseDTO.getIds().split(",")));
        }
        if (startTime!=null){
            courseLambdaQueryWrapper.between(PrizeRedemptionRecord::getCreateTime, startTime, endTime);
        }
        if (courseDTO.getStatus()!=null){
            courseLambdaQueryWrapper.eq(PrizeRedemptionRecord::getStatus, courseDTO.getStatus());
        }
        courseLambdaQueryWrapper.orderByDesc(PrizeRedemptionRecord::getCreateTime);
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getUserNameOrPhone())){
            List<Long> collect = appUserService.lambdaQuery().like(AppUser::getNickname, courseDTO.getUserNameOrPhone()).list()
                    .stream().map(AppUser::getId).collect(Collectors.toList());
            longs.addAll(collect);
            List<Long> collect1 = appUserService.lambdaQuery().like(AppUser::getCellPhone, courseDTO.getUserNameOrPhone()).list()
                    .stream().map(AppUser::getId).collect(Collectors.toList());
            longs.addAll(collect1);
            if (longs.isEmpty()){
                longs.add(-1L);
            }
            courseLambdaQueryWrapper.in(PrizeRedemptionRecord::getAppUserId, longs);
        }
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getName())){
            List<Long> collect = prizeService.lambdaQuery().like(Prize::getName, courseDTO.getName()).list()
                    .stream().map(Prize::getId).collect(Collectors.toList());
            if (collect.isEmpty()){
                collect.add(-1L);
            }
            courseLambdaQueryWrapper.in(PrizeRedemptionRecord::getPrizeId, collect);
        }
        List<PrizeExport> prizeExports = new ArrayList<>();
 
        List<PrizeRedemptionRecord> page = prizeRedemptionRecordService.list(courseLambdaQueryWrapper);
        for (PrizeRedemptionRecord record : page) {
            Prize byId = prizeService.getById(record.getPrizeId());
 
            record.setUid(record.getId().toString());
            AppUser byId1 = appUserService.getById(record.getAppUserId());
 
            PrizeExport prizeExport = new PrizeExport();
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String format = df.format(record.getCreateTime());
            prizeExport.setCreateTime(format);
            if (Objects.nonNull(byId1)){
                record.setUserName(byId1.getNickname());
                record.setAvatar(byId1.getAvatar());
                record.setCellPhone(byId1.getCellPhone());
                prizeExport.setUserName(byId1.getNickname());
                prizeExport.setCellphone(byId1.getCellPhone());
            }
            if (Objects.nonNull(byId)){
                record.setName(byId.getName());
                record.setWorth(byId.getWorth());
                record.setEnergyValue(byId.getEnergyValue());
                prizeExport.setPrizeName(byId.getName());
                prizeExport.setAmount(byId.getWorth()+"");
                prizeExport.setEnergyValue(byId.getEnergyValue()+"");
            }
            prizeExport.setStatus(record.getStatus()+"");
            prizeExports.add(prizeExport);
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), PrizeExport.class, prizeExports);
        HttpServletResponse response = WebUtils.response();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("兑换记录管理导出.xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
 
    @PostMapping("/prizeRedemptionRecordList")
    @ApiOperation(value = "兑换记录管理列表-分页", tags = {"管理后台-兑换记录管理"})
 
    public R<PageDTO<PrizeRedemptionRecord>> prizeRedemptionRecordList(@RequestBody PrizeRecordDTO courseDTO) {
        String startTime = null;
        String endTime = null;
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getTime())){
            String[] split = courseDTO.getTime().split(" - ");
            startTime = split[0]+" 00:00:00";
            endTime = split[1]+" 23:59:59";
        }
        List<Long> longs = new ArrayList<>();
        LambdaQueryWrapper<PrizeRedemptionRecord> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (startTime!=null){
            courseLambdaQueryWrapper.between(PrizeRedemptionRecord::getCreateTime, startTime, endTime);
        }
        if (courseDTO.getStatus()!=null){
            courseLambdaQueryWrapper.eq(PrizeRedemptionRecord::getStatus, courseDTO.getStatus());
        }
        courseLambdaQueryWrapper.orderByDesc(PrizeRedemptionRecord::getCreateTime);
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getUserNameOrPhone())){
            List<Long> collect = appUserService.lambdaQuery().like(AppUser::getNickname, courseDTO.getUserNameOrPhone()).list()
                    .stream().map(AppUser::getId).collect(Collectors.toList());
            longs.addAll(collect);
            List<Long> collect1 = appUserService.lambdaQuery().like(AppUser::getCellPhone, courseDTO.getUserNameOrPhone()).list()
                    .stream().map(AppUser::getId).collect(Collectors.toList());
            longs.addAll(collect1);
            if (longs.isEmpty()){
                longs.add(-1L);
            }
            courseLambdaQueryWrapper.in(PrizeRedemptionRecord::getAppUserId, longs);
        }
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getName())){
            List<Long> collect = prizeService.lambdaQuery().like(Prize::getName, courseDTO.getName()).list()
                    .stream().map(Prize::getId).collect(Collectors.toList());
            if (collect.isEmpty()){
                collect.add(-1L);
            }
            courseLambdaQueryWrapper.in(PrizeRedemptionRecord::getPrizeId, collect);
        }
        Page<PrizeRedemptionRecord> page = prizeRedemptionRecordService.page(new Page<>(courseDTO.getPageCurr(), courseDTO.getPageSize()), courseLambdaQueryWrapper);
        if (CollUtils.isEmpty(page.getRecords())) {
            return R.ok(PageDTO.empty(page));
        }
        for (PrizeRedemptionRecord record : page.getRecords()) {
            record.setUuid(record.getAppUserId()+"");
            Prize byId = prizeService.getById(record.getPrizeId());
            if (Objects.nonNull(byId)){
                record.setName(byId.getName());
                record.setWorth(byId.getWorth());
                record.setEnergyValue(byId.getEnergyValue());
                record.setCoverUrl(byId.getCoverUrl());
            }
            record.setUid(record.getId().toString());
            AppUser byId1 = appUserService.getById(record.getAppUserId());
            if (Objects.nonNull(byId1)){
                record.setUserName(byId1.getNickname());
                record.setAvatar(byId1.getAvatar());
                record.setCellPhone(byId1.getCellPhone());
            }
        }
        return R.ok(PageDTO.of(page, PrizeRedemptionRecord.class));
    }
 
    @GetMapping("/detailPrizeRedemptionRecord")
    @ApiOperation(value = "查看详情兑换记录管理", tags = "管理后台-兑换记录管理")
    public R<PrizeRedemptionRecord> detailPrizeRedemptionRecord(String uid) {
        PrizeRedemptionRecord byId = prizeRedemptionRecordService.getById(uid);
        Prize byId1 = prizeService.getById(byId.getPrizeId());
        if (Objects.nonNull(byId1)){
            byId.setName(byId1.getName());
            byId.setWorth(byId1.getWorth());
            byId.setEnergyValue(byId1.getEnergyValue());
        }
        byId.setUid(byId.getId().toString());
        AppUser byId2 = appUserService.getById(byId.getAppUserId());
        if (Objects.nonNull(byId2)){
            byId.setUserName(byId2.getNickname());
            byId.setAvatar(byId2.getAvatar());
            byId.setCellPhone(byId2.getCellPhone());
        }
        return R.ok();
    }
    @Autowired
    private TokenService tokenService;
    @GetMapping("/confirm")
    @ApiOperation(value = "确认兑换", tags = "管理后台-兑换记录管理")
    @Log(title = "【奖品管理】确认兑换", businessType = BusinessType.UPDATE)
    public R updateState(String uid,String code) {
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser==null){
            return R.tokenError("登录失效");
        }
        Long userId = loginUser.getUserid();
        PrizeRedemptionRecord byId = prizeRedemptionRecordService.getById(uid);
        if (!byId.getCode().equals(code)){
            return R.fail("兑换码错误");
        }else{
            byId.setStatus(2);
            byId.setUpdateTime(LocalDateTime.now());
            SysUser data = remoteUserService.getSysUserById(userId + "").getData();
            byId.setUpdateBy(data.getUserName()+data.getNickName()+"("+data.getUserName()+")");
            byId.setHandleName(data.getUserName()+data.getNickName()+"("+data.getUserName()+")");
            prizeRedemptionRecordService.updateById(byId);
        }
        return R.ok();
    }
 
 
}