xuhy
2024-12-12 412ed345ecf217516fa697f0a25cf7e67559958f
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package com.ruoyi.web.controller.api;
 
 
import cn.hutool.http.HttpException;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.BasePage;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vx.WeChatConfig;
import com.ruoyi.system.vx.WeChatUtil;
import com.ruoyi.web.controller.query.InformationQuery;
import com.ruoyi.web.controller.query.PayDto;
import com.ruoyi.web.controller.tool.AlipayTradePagePay;
import com.ruoyi.web.controller.tool.AlipayTradeQuery;
import com.ruoyi.web.controller.tool.PayMoneyUtil;
import com.ruoyi.web.controller.tool.ResultUtil;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.partnerpayments.app.model.PrepayResponse;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.refund.RefundService;
import io.lettuce.core.api.async.RedisAclAsyncCommands;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 咨询列表 前端控制器
 * </p>
 *
 * @author luodangjia
 * @since 2024-09-19
 */
@RestController
@RequestMapping("/t-information")
public class TInformationController {
    @Resource
    private TInformationService informationService;
    @Resource
    private TRegionService regionService;
    @Resource
    private TTechnicalTitleService tTechnicalTitleService;
    @Resource
    private TTitleMajorService majorService;
    @Resource
    private TLevelService levelService;
    @Resource
    private TokenService tokenService;
    @Resource
    private TOrderService orderService;
    @Resource
    private NativePayService nativePayService;
    @Resource
    private WeChatConfig weChatConfig;
    @Resource
    private RedisCache redisCache;
    @Resource
    private RefundService refundService;
    @Resource
    private PayMoneyUtil payMoneyUtil;
 
    //新增咨询
    @ApiOperation(value = "添加",tags = "后台-资料管理")
    @PostMapping(value = "/add")
    public R add(@RequestBody TInformation information) {
        informationService.save(information);
        return R.ok();
    }
    //修改咨询
    @ApiOperation(value = "修改",tags = "后台-资料管理")
    @PostMapping(value = "/edit")
    public R edit(@RequestBody TInformation information) {
        informationService.updateById(information);
        return R.ok();
    }
    //删除咨询
    @ApiOperation(value = "删除",tags = "后台-资料管理")
    @PostMapping(value = "/deleteByIds")
    public R deleteByIds(String ids) {
        informationService.removeByIds(Arrays.asList(ids.split(",")));
        return R.ok();
    }
    //查询咨询
    @ApiOperation(value = "查询",tags = {"后台-资料管理","web资料查询"})
    @PostMapping(value = "/list")
    public R<Page<TInformation>> list(@RequestBody InformationQuery informationQuery) {
        Long userId = tokenService.getLoginUser().getUserId();
        Page<TInformation> page;
        if(StringUtils.isEmpty(informationQuery.getProvinceName())){
            if (informationQuery.getSortType()==1){
                page = informationService.lambdaQuery()
                        .like(!StringUtils.isEmpty(informationQuery.getInformationName()), TInformation::getInformationName, informationQuery.getInformationName())
                        .eq(informationQuery.getRegionId() != null, TInformation::getRegionId, informationQuery.getRegionId())
                        .eq(informationQuery.getTechnicalId() != null, TInformation::getTechnicalId, informationQuery.getTechnicalId())
                        .eq(informationQuery.getMajorId() != null, TInformation::getMajorId, informationQuery.getMajorId())
                        .eq(informationQuery.getLevel() != null, TInformation::getLevel, informationQuery.getLevel())
                        .eq(informationQuery.getFree()!=null&&informationQuery.getFree() == 1, TInformation::getInformationPrice, BigDecimal.ZERO)
                        .ne(informationQuery.getFree()!=null&&informationQuery.getFree() == 2, TInformation::getInformationPrice, BigDecimal.ZERO)
                        .orderByDesc(TInformation::getCreateTime)
                        .page(Page.of(informationQuery.getPageNum(), informationQuery.getPageSize()));
            }else {
                page = informationService.lambdaQuery()
                        .like(!StringUtils.isEmpty(informationQuery.getInformationName()), TInformation::getInformationName, informationQuery.getInformationName())
                        .eq(informationQuery.getRegionId() != null, TInformation::getRegionId, informationQuery.getRegionId())
                        .eq(informationQuery.getTechnicalId() != null, TInformation::getTechnicalId, informationQuery.getTechnicalId())
                        .eq(informationQuery.getMajorId() != null, TInformation::getMajorId, informationQuery.getMajorId())
                        .eq(informationQuery.getLevel() != null, TInformation::getLevel, informationQuery.getLevel())
                        .eq(informationQuery.getFree()!=null&&informationQuery.getFree() == 1, TInformation::getInformationPrice, BigDecimal.ZERO)
                        .ne(informationQuery.getFree()!=null&&informationQuery.getFree() == 2, TInformation::getInformationPrice, BigDecimal.ZERO)
                        .orderByDesc(TInformation::getCommitteeSort)
                        .page(Page.of(informationQuery.getPageNum(), informationQuery.getPageSize()));
            }
        }else {
            List<TRegion> list;
            // 查询省份下的市
            if(informationQuery.getProvinceName().equals("直辖市")){
                List<String> cityList = new ArrayList<>();
                cityList.add("北京市");
                cityList.add("天津市");
                cityList.add("重庆市");
                cityList.add("上海市");
                list = regionService.lambdaQuery().in(TRegion::getProvinceName, cityList).list();
            }else {
                list = regionService.lambdaQuery().eq(TRegion::getProvinceName, informationQuery.getProvinceName()).list();
            }
            List<Integer> ids = list.stream().map(TRegion::getId).collect(Collectors.toList());
            if (informationQuery.getSortType()==1){
                page = informationService.lambdaQuery()
                        .like(!StringUtils.isEmpty(informationQuery.getInformationName()), TInformation::getInformationName, informationQuery.getInformationName())
                        .in( TInformation::getRegionId, ids)
                        .eq(informationQuery.getTechnicalId() != null, TInformation::getTechnicalId, informationQuery.getTechnicalId())
                        .eq(informationQuery.getMajorId() != null, TInformation::getMajorId, informationQuery.getMajorId())
                        .eq(informationQuery.getLevel() != null, TInformation::getLevel, informationQuery.getLevel())
                        .eq(informationQuery.getFree()!=null&&informationQuery.getFree() == 1, TInformation::getInformationPrice, BigDecimal.ZERO)
                        .ne(informationQuery.getFree()!=null&&informationQuery.getFree() == 2, TInformation::getInformationPrice, BigDecimal.ZERO)
                        .orderByDesc(TInformation::getCreateTime)
                        .page(Page.of(informationQuery.getPageNum(), informationQuery.getPageSize()));
            }else {
                page = informationService.lambdaQuery()
                        .like(!StringUtils.isEmpty(informationQuery.getInformationName()), TInformation::getInformationName, informationQuery.getInformationName())
                        .in( TInformation::getRegionId, ids)
                        .eq(informationQuery.getTechnicalId() != null, TInformation::getTechnicalId, informationQuery.getTechnicalId())
                        .eq(informationQuery.getMajorId() != null, TInformation::getMajorId, informationQuery.getMajorId())
                        .eq(informationQuery.getLevel() != null, TInformation::getLevel, informationQuery.getLevel())
                        .eq(informationQuery.getFree()!=null&&informationQuery.getFree() == 1, TInformation::getInformationPrice, BigDecimal.ZERO)
                        .ne(informationQuery.getFree()!=null&&informationQuery.getFree() == 2, TInformation::getInformationPrice, BigDecimal.ZERO)
                        .orderByDesc(TInformation::getCommitteeSort)
                        .page(Page.of(informationQuery.getPageNum(), informationQuery.getPageSize()));
            }
        }
 
        Set<Long> cacheSet = redisCache.getCacheSet("INFORMATION:" + userId);
 
        for (TInformation record : page.getRecords()) {
            TRegion byId = regionService.getById(record.getRegionId());
            record.setRegionName(byId.getProvinceName()+"-"+byId.getName());
            TTechnicalTitle byId1 = tTechnicalTitleService.getById(record.getTechnicalId());
            TTitleMajor byId2 = majorService.getById(record.getMajorId());
            record.setTechnicalName(byId1.getTitileName()+"-"+byId2.getMajorName());
            if (cacheSet!=null){
                if (cacheSet.contains(record.getId())){
                    record.setIsCollect(1);
                }else {
                    record.setIsCollect(0);
                }
            }else {
                record.setIsCollect(0);
            }
        }
        return R.ok(page);
    }
 
    @ApiOperation(value = "检查当前用户是否购买资料",tags = {"web资料查询"})
    @PostMapping(value = "/check")
    public R<Boolean> check(@RequestParam Long id) {
        Long userId = tokenService.getLoginUser().getUserId();
        List<TOrder> list = orderService.lambdaQuery().eq(TOrder::getGoodType, 2).eq(TOrder::getPaymentStatus,2).eq(TOrder::getGoodId,id).eq(TOrder::getUserId, userId).list();
        if (list.isEmpty()){
            return R.ok(false);
        }else {
            return R.ok(true);
        }
 
    }
 
    @ApiOperation(value = "下载累计次数",tags = {"web资料查询"})
    @PostMapping(value = "/downland")
    public R downland(@RequestParam Long id) {
        TInformation byId = informationService.getById(id);
        byId.setDownlandNum(byId.getDownlandNum()+1);
        informationService.updateById(byId);
        return R.ok();
    }
 
 
    //新增咨询
    @ApiOperation(value = "详情",tags = {"后台-资料管理","web资料查询"})
    @PostMapping(value = "/detail")
    public R<TInformation> detail(@RequestParam Long  id) {
        Long userId = tokenService.getLoginUser().getUserId();
 
        TInformation record = informationService.getById(id);
        Set<Long> cacheSet = redisCache.getCacheSet("INFORMATION:" + userId);
 
        TRegion byId = regionService.getById(record.getRegionId());
        record.setRegionName(byId.getProvinceName()+"-"+byId.getName());
        TTechnicalTitle byId1 = tTechnicalTitleService.getById(record.getTechnicalId());
        TTitleMajor byId2 = majorService.getById(record.getMajorId());
        record.setTechnicalName(byId1.getTitileName()+"-"+byId2.getMajorName()+"-"+record.getLevel());
        if (cacheSet!=null){
            if (cacheSet.contains(record.getId())){
                record.setIsCollect(1);
            }else {
                record.setIsCollect(0);
            }
        }else {
            record.setIsCollect(0);
        }
 
        return R.ok(record);
    }
 
 
    @ApiOperation(value = "生成订单",tags = {"web资料查询"})
    @PostMapping(value = "/create")
    public R buy( @RequestParam Long id) throws AlipayApiException {
        Long userId = tokenService.getLoginUser().getUserId();
        TInformation byId = informationService.getById(id);
        String code = "ZL" + WeChatUtil.generateTradeNumber();
        TOrder order = new TOrder();
        order.setCode(code);
        order.setUserId(userId);
        order.setGoodType(2);
        order.setGoodId(id);
        order.setOrderAmount(byId.getInformationPrice());
        order.setPaymentAmount(byId.getInformationPrice());
 
        if (byId.getInformationPrice().compareTo(new BigDecimal(0))==0){
            order.setPaymentStatus(2);
 
        }
        order.setPaymentStatus(1);
        orderService.save(order);
        return R.ok(order.getId());
    }
 
 
 
    @ApiOperation(value = "购买资料",tags = {"web资料查询"})
    @PostMapping(value = "/buy")
    public R buy(@RequestParam Integer type, @RequestParam Long orderId) throws AlipayApiException {
        TOrder byId = orderService.getById(orderId);
        int i = byId.getPaymentAmount().multiply(BigDecimal.valueOf(100)).intValue();
        TOrder byId1 = orderService.getById(orderId);
        if (type == 1) {
            Long userId = tokenService.getLoginUser().getUserId();
 
            com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest prepayRequest = new com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest();
            prepayRequest.setAppid(weChatConfig.appId);
            prepayRequest.setMchid(weChatConfig.merchantId);
            prepayRequest.setOutTradeNo(byId1.getCode());
            prepayRequest.setDescription("购买资料");
            prepayRequest.setNotifyUrl("http://www.zhipingwang.com.cn:8081/call-back/buy");
            com.wechat.pay.java.service.payments.nativepay.model.Amount amount = new com.wechat.pay.java.service.payments.nativepay.model.Amount();
            amount.setTotal(i);
            prepayRequest.setAmount(amount);
            // 调用下单方法,得到应答
            PrepayResponse response;
            try {
                com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse prepay = nativePayService.prepay(prepayRequest);
 
                //预支付成功,创建预支付订单
 
                PayDto payDto = new PayDto();
                payDto.setOrderId(orderId);
                payDto.setQrCode(prepay.getCodeUrl());
                return R.ok(payDto);
            } catch (HttpException e) { // 发送HTTP请求失败
//                log.error("发送HTTP请求失败: {}", e.getHttpRequest());
            } catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
//                log.error("服务返回状态异常: {}", e.getResponseBody());
            } catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
//                log.error("返回体类型不合法: {}", e.getMessage());
            } catch (Exception e) {
//                log.error("预下单异常: {}", e.getMessage());
            }
            return null;
        } else {
            String qrCode = AlipayTradePagePay.pay("购买资料",byId1.getCode(),byId.getPaymentAmount().toString());
 
            PayDto payDto = new PayDto();
            payDto.setOrderId(orderId);
            payDto.setQrCode(qrCode);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        int num = 1;
                        int wait = 0;
                        while (num <= 30) {
                            int min = 2000;
                            Thread.sleep(min);
                            Boolean check = AlipayTradeQuery.check(byId.getCode());
                            if (check){
                                byId.setPaymentStatus(2);
                                byId.setPaymentType(2);
                                byId.setPayTime(LocalDateTime.now());
                                orderService.updateById(byId);
                            }else {
                                num++;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            return R.ok(payDto);
        }
    }
 
 
    @ApiOperation(value = "收藏资料",tags = {"web-个人中心"})
    @PostMapping(value = "/info/list")
    public R<Page<TInformation>> infolist(@RequestBody BasePage basePage) {
 
        Long userId = tokenService.getLoginUser().getUserId();
        Set<Long> cacheSet = redisCache.getCacheSet("INFORMATION:" + userId);
        if (!cacheSet.isEmpty()) {
            Page<TInformation> page = informationService.lambdaQuery().in(TInformation::getId, cacheSet).page(Page.of(basePage.getPageNum(), basePage.getPageSize()));
            return R.ok(page);
        }else {
            return R.ok(new Page<>());
        }
    }
 
 
    @ApiOperation(value = "判断资料",tags = {"web-个人中心"})
    @PostMapping(value = "/collect/is")
    public R<Boolean> collectis(@RequestParam Long informationId) {
 
        Long userId = tokenService.getLoginUser().getUserId();
        Set<Long> cacheSet = redisCache.getCacheSet("INFORMATION:" + userId);
       if (cacheSet.contains(informationId)){
           return R.ok(true);
       }else {
           return R.ok(false);
       }
    }
 
 
 
 
 
 
 
}