无关风月
4 天以前 74f666605450658b86e1b5ca076500aa341b6f49
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
package com.ruoyi.other.controller;
 
import com.alibaba.fastjson.JSONObject;
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.IntegralPay;
import com.ruoyi.other.api.domain.TIntegralRule;
import com.ruoyi.other.query.IntegralListQuery;
import com.ruoyi.other.service.TIntegralPayService;
import com.ruoyi.other.service.TIntegralRecordService;
import com.ruoyi.other.service.TIntegralRuleService;
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.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.math.RoundingMode;
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("/integral")
public class TIntegralController {
 
    @Autowired
    private TIntegralRuleService integralRuleService;
    @Autowired
    private TIntegralRecordService integralRecordService;
    @Autowired
    private TIntegralPayService integralPayService;
 
    @Autowired
    private TokenService tokenService;
    @Resource
    private SysUserClient sysUserClient;
    @Resource
    private WechatPayService wechatPayService;
    @PostMapping("/integralPageList")
    @ApiOperation(tags = {"2.0-积分钱包"},value = "积分钱包")
    public R<IntegralVO> integralPageList(@RequestBody IntegralListQuery query) {
        Long userid = tokenService.getLoginUser().getUserid();
        R<SysUser> sysUser = sysUserClient.getSysUser(userid);
        query.setSiteId(sysUser.getData().getSiteId());
        Integer integral = sysUser.getData().getIntegral();
        IntegralVO integralVO = new IntegralVO();
        PageInfo<IntegralListVO> integralListVOPageInfo = integralRecordService.integralPageList(query);
        integralVO.setIntegralList(integralListVOPageInfo);
        integralVO.setIntegral(integral);
        return R.ok(integralVO);
    }
    @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 {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
        Map<String, String> res =  wechatPayService.unifiedOrder(code, amount, "积分充值", "/other/wx/integralCallback");
        Long userid = tokenService.getLoginUser().getUserid();
        R<SysUser> sysUser = sysUserClient.getSysUser(userid);
        SysUser data = sysUser.getData();
        // 生成待支付记录
        IntegralPay integralPay = new IntegralPay();
        integralPay.setUserId(data.getUserId());
        TIntegralRule one = integralRuleService.lambdaQuery().eq(TIntegralRule::getSiteId, 0).one();
        if (one==null){
            return R.fail("获取支付失败,平台未设置充值积分比例");
        }
        String chargeCredit = one.getChargeCredit();
        JSONObject jsonObject = JSONObject.parseObject(chargeCredit);
        Integer num1 = jsonObject.getInteger("num1");
        BigDecimal bigDecimal = new BigDecimal(amount);
        // 向上取整 比如5.1 当作6 5.9也当作6
        integralPay.setIntegralCount(bigDecimal.divide(new BigDecimal(num1), RoundingMode.UP).intValue());
        int i = bigDecimal.multiply(new BigDecimal(num1)).intValue();
        integralPay.setIntegralCount(i);
        integralPay.setAmount(new BigDecimal(amount));
        integralPay.setCode(code);
        integralPay.setPayStatus(1);
        integralPay.setDelFlag(0);
        integralPay.setCreateTime(LocalDateTime.now());
        integralPayService.save(integralPay);
        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;
    }
    @GetMapping("/getSet")
    @ApiOperation(tags = {"2.0-积分管理"},value = "获取积分设置")
    public R<TIntegralRule> getSet() {
        Long userid = tokenService.getLoginUser().getUserid();
        SysUser data = sysUserClient.getSysUser(userid).getData();
 
        if (data!=null&&data.getSiteId()!=null){
            if (data.getRoleType()==1){
                // 平台
                TIntegralRule res = integralRuleService.lambdaQuery().eq(TIntegralRule::getSiteId, 0).one();
                return R.ok(res);
            }else{
                TIntegralRule res = integralRuleService.lambdaQuery().eq(TIntegralRule::getSiteId, data.getSiteId()).one();
                return R.ok(res);
            }
        }else {
            return R.ok(new TIntegralRule());
        }
    }
    
    @PostMapping("/saveSet")
    @ApiOperation(tags = {"2.0-积分管理"},value = "保存积分设置")
    @Log(title = "【积分管理】保存积分设置", businessType = BusinessType.INSERT)
    public R saveSet(@RequestBody TIntegralRule dto) {
        Long userid = tokenService.getLoginUser().getUserid();
        SysUser data = sysUserClient.getSysUser(userid).getData();
        TIntegralRule one;
        if (data.getRoleType()==1){
            one = integralRuleService.lambdaQuery().eq(TIntegralRule::getSiteId, 0).one();
        }else{
            one = integralRuleService.lambdaQuery().eq(TIntegralRule::getSiteId, data.getSiteId()).one();
        }
        if (one!=null){
            dto.setId(one.getId());
            integralRuleService.saveOrUpdate(dto);
        }else{
            integralRuleService.saveOrUpdate(dto);
        }
        return R.ok();
    }
 
}