xuhy
2025-09-04 05f53069a91f979ec3d18e0a7abc8ce67c2656b2
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
package com.ruoyi.other.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.chargingPile.api.feignClient.SiteClient;
import com.ruoyi.chargingPile.api.model.Site;
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.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.*;
import com.ruoyi.other.util.pay.CreateLinkStringByGet1;
import com.ruoyi.other.util.pay.HttpRequester;
import com.ruoyi.other.util.pay.HttpRespons;
import com.ruoyi.other.util.pay.Md5_Sign;
import com.ruoyi.other.util.payment.wx.WechatPayService;
import com.ruoyi.other.vo.*;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
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.HashMap;
import java.util.List;
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;
    @Resource
    private SiteClient siteClient;
    // 查询服务费缴纳情况
    @GetMapping("/getServiceStatus/{userId}")
    public R<String> getServiceStatus(@PathVariable("userId") Integer userId) {
        ServicePay servicePayBefore = servicePayService.lambdaQuery()
                .eq(ServicePay::getUserId, userId)
                .eq(ServicePay::getPayStatus,2)
                .eq(ServicePay::getDelFlag,0)
                .orderByDesc(ServicePay::getCreateTime)
                .last("limit 1")
                .one();
        if (servicePayBefore==null){
            return R.ok("1");
        }
        else if (servicePayBefore.getEndTime().isBefore(LocalDateTime.now())) {
            return R.ok("2");
        }
        else{
            return R.ok("3");
        }
 
    }
    @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)
                .eq(ServicePay::getDelFlag,0)
                .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("id") Integer id) throws Exception {
        ServicePay servicePay = servicePayService.getById(id);
        if (servicePay.getPayStatus()==2){
            return R.ok(true);
        }else{
            return R.ok(false);
        }
    }
    @PostMapping("/nativePay")
    @ApiOperation(tags = {"2.0-服务费"},value = "管理后台缴纳服务费获取支付二维码")
    public R<PayDto> nativePay() throws Exception {
        Long userid = tokenService.getLoginUser().getUserid();
        R<SysUser> sysUser = sysUserClient.getSysUser(userid);
        SysUser data = sysUser.getData();
        List<Site> sites = siteClient.getSiteAll().getData();
        Site site = sites.stream().filter(e -> e.getId().equals(data.getSiteId())).findFirst().orElse(null);
        if (site==null){
            return R.fail("当前登录用户未绑定站点");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
        Map<String,String> res = wechatPayService.unifiedOrder(code, site.getAnnualServiceFee()+"", site.getCode()+"服务费缴纳", "/other/wx/serviceCallback");
        ServicePay servicePay = new ServicePay();
        servicePay.setUserId(data.getUserId());
        servicePay.setAmount(site.getAnnualServiceFee());
        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);
        PayDto payDto = new PayDto();
        payDto.setId(servicePay.getId());
        payDto.setQrCode(s);
        payDto.setServiceMoney(site.getAnnualServiceFee());
        return R.ok(payDto);
    }
    @Resource
    private WeChatUtil weChatUtil;
    @PostMapping("/bindOpenId")
    @ApiOperation(tags = {"2.0-服务费"},value = "小程序绑定openId")
    public R bindOpenId(@RequestParam("jscode") String jscode) throws Exception {
        //使用jscode获取微信openid
        Map<String, Object> map = weChatUtil.code2Session(jscode);
        Integer errcode = Integer.valueOf(map.get("errcode").toString());
        if(0 != errcode){
            return R.fail(map.get("msg").toString());
        }
        String openid = map.get("openid").toString();
        Long userid = tokenService.getLoginUser().getUserid();
        R<SysUser> sysUser = sysUserClient.getSysUser(userid);
        SysUser data = sysUser.getData();
        data.setOpenId(openid);
        sysUserClient.updateSysUser(data);
        return R.ok();
    }
    @PostMapping("/servicePayApplet")
    @ApiOperation(tags = {"2.0-服务费"},value = "小程序缴纳服务费")
    public R servicePayApplet() throws Exception {
 
        Long userid = tokenService.getLoginUser().getUserid();
        R<SysUser> sysUser = sysUserClient.getSysUser(userid);
        SysUser data = sysUser.getData();
        List<Site> sites = siteClient.getSiteAll().getData();
        Site site = sites.stream().filter(e -> e.getId().equals(data.getSiteId())).findFirst().orElse(null);
        if (site==null){
            return R.fail("当前登录用户未绑定站点");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
        ServicePay servicePay = new ServicePay();
        servicePay.setUserId(data.getUserId());
        servicePay.setAmount(site.getAnnualServiceFee());
        servicePay.setCode(code);
        servicePay.setPayStatus(1);
        servicePay.setPayType(1);
        servicePay.setDelFlag(0);
        servicePay.setCreateTime(LocalDateTime.now());
        servicePayService.save(servicePay);
 
 
        return wechatPayService.unifiedOrderApplet(servicePay.getId()+"",code, site.getAnnualServiceFee()+"", site.getCode()+"服务费缴纳",data.getOpenId(), "/other/wx/serviceCallback");
    }
    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;
    }
 
}