无关风月
4 天以前 7fd053651ac11db87fe4f6c57e65eed3b9a59452
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
package com.ruoyi.other.controller;
 
import com.ruoyi.common.core.domain.R;
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.security.service.TokenService;
import com.ruoyi.other.api.domain.ServicePay;
import com.ruoyi.other.api.domain.TIntegralRule;
import com.ruoyi.other.query.IntegralListQuery;
import com.ruoyi.other.query.ServiceListQuery;
import com.ruoyi.other.service.TIntegralRecordService;
import com.ruoyi.other.service.TIntegralRuleService;
import com.ruoyi.other.service.TServicePayService;
import com.ruoyi.other.util.MyQrCodeUtil;
import com.ruoyi.other.util.ObsUploadUtil;
import com.ruoyi.other.util.QRCodeUtil;
import com.ruoyi.other.util.UUIDUtil;
import com.ruoyi.other.util.payment.wx.WechatPayService;
import com.ruoyi.other.vo.IntegralListVO;
import com.ruoyi.other.vo.IntegralVO;
import com.ruoyi.other.vo.ServiceListVO;
import com.ruoyi.other.vo.ServiceVO;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;
 
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 无关风月
 * @since 2024-08-06
 */
@RestController
@RequestMapping("/integralRule")
public class TServiceController {
 
    @Autowired
    private TIntegralRuleService integralRuleService;
    @Autowired
    private TIntegralRecordService integralRecordService;
 
    @Autowired
    private TokenService tokenService;
    @Resource
    private SysUserClient sysUserClient;
    @Resource
    private WechatPayService wechatPayService;
    @Resource
    private TServicePayService servicePayService;
    @PostMapping("/servicePageList")
    @ApiOperation(tags = {"2.0-服务费管理"},value = "服务费管理")
    public R<ServiceVO> integralPageList(@RequestBody ServiceListQuery query) {
        Long userid = tokenService.getLoginUser().getUserid();
        R<SysUser> sysUser = sysUserClient.getSysUser(userid);
        query.setUserId(userid);
        ServiceVO serviceVO = new ServiceVO();
        PageInfo<ServiceListVO> serviceListVOPageInfo = servicePayService.servicePageList(query);
        serviceVO.setServiceList(serviceListVOPageInfo);
        ServicePay servicePayBefore = servicePayService.lambdaQuery()
                .eq(ServicePay::getUserId, userid)
                .eq(ServicePay::getPayStatus,2)
                .orderByDesc(ServicePay::getCreateTime)
                .last("limit 1")
                .one();
        if (servicePayBefore==null){
            serviceVO.setStatus(1);
        } else if (servicePayBefore.getEndTime().isBefore(LocalDateTime.now())) {
            serviceVO.setStatus(2);
        }else{
            serviceVO.setStatus(3);
        }
        if (servicePayBefore!=null){
            serviceVO.setEndTime(servicePayBefore.getEndTime());
        }
        return R.ok(serviceVO);
    }
    @PostMapping("/queryPayStatus")
    @ApiOperation(tags = {"2.0-服务费"},value = "查询支付结果 true成功 false失败")
    public R<Boolean> integralPageList(@RequestParam("code") String code) throws Exception {
        Map<String, String> resMap = wechatPayService.queryOrder(code);
        if("SUCCESS".equals(resMap.get("return_code"))){
            return R.ok(true);
        }else{
            return R.ok(false);
        }
    }
    @PostMapping("/nativePay")
    @ApiOperation(tags = {"2.0-服务费"},value = "获取支付二维码")
    public R nativePay(@RequestParam("amount") String amount) throws Exception {
        // todo 查询每个站点的年服务费
        Long userid = tokenService.getLoginUser().getUserid();
        R<SysUser> sysUser = sysUserClient.getSysUser(userid);
        SysUser data = sysUser.getData();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
        Map<String,String> res = wechatPayService.unifiedOrder(code, amount, "服务费缴纳", "/other/wx/serviceCallback");
        ServicePay servicePay = new ServicePay();
        servicePay.setUserId(data.getUserId());
        servicePay.setAmount(new BigDecimal( amount));
        servicePay.setCode(code);
        servicePay.setPayStatus(1);
        servicePay.setPayType(1);
        servicePay.setDelFlag(0);
        servicePay.setCreateTime(LocalDateTime.now());
        servicePayService.save(servicePay);
        String codeUrl = res.get("code_url");
        MyQrCodeUtil.createCodeToFile(codeUrl);
        BufferedImage blueImage = QRCodeUtil.createImage(codeUrl);
        MultipartFile blueFile = convert(blueImage, new Date().getTime() + UUIDUtil.getRandomCode(3) + ".PNG");
        String s = ObsUploadUtil.obsUpload(blueFile);
        System.err.println(s);
        return R.ok(s);
    }
    public static MultipartFile convert(BufferedImage bufferedImage, String fileName) throws IOException {
        // 将 BufferedImage 转换为字节数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "png", baos);
        byte[] bytes = baos.toByteArray();
 
        // 创建 ByteArrayResource
        ByteArrayResource resource = new ByteArrayResource(bytes);
 
        // 创建 MockMultipartFile
        MockMultipartFile multipartFile = new MockMultipartFile(
                "file",
                fileName,
                "image/png",
                resource.getInputStream()
        );
 
        return multipartFile;
    }
 
}