2021与蓝度共同重构项目,服务端
liuhaonan
2022-08-24 70c66f5e065b6d467812f8e702f87c2262bce67e
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
package com.sandu.ximon.admin.pay.wxpay;
 
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.admin.param.WxpayConfigParam;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.service.ClientService;
import com.sandu.ximon.admin.service.PoleService;
import com.sandu.ximon.admin.utils.SpringContextUtil;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.domain.WxConfigEntity;
import com.sandu.ximon.dao.mapper.UsrWxPayConfigMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * @Author liuhaonan
 * @Date 2022/3/18 14:37
 * @Version 1.0
 */
@Service
@AllArgsConstructor
public class UsrWxPayConfigService extends BaseServiceImpl<UsrWxPayConfigMapper, WxConfigEntity> {
 
    private final UsrWxPayConfigMapper wxPayConfigMapper;
    private final ClientService clientService;
    private final PoleService poleService;
 
    public WxConfigEntity getConfigByPoleId(Long poleId) {
        Pole pole = poleService.getById(poleId);
        if (pole == null) {
            throw new BusinessException("灯杆id错误或灯杆不存在");
        }
        WxConfigEntity config;
        //未设置支付参数,则使用默认支付参数
        config = getOne(Wrappers.lambdaQuery(WxConfigEntity.class).eq(WxConfigEntity::getClientId, pole.getClientId()).eq(WxConfigEntity::getState, 1));
//        if (pole.getClientId() == null) {
        //没有归属的灯杆使用默认配置
//            config = getById(15);
//        }
        if (config == null) {
            throw new BusinessException("用户未配置支付配置,请配置后重试");
        }
        return config;
    }
 
    public WxConfigEntity getConfigByAppId(String appId) {
        if (appId == null) {
            throw new BusinessException("回调失败,未解析到appid");
        }
        WxConfigEntity config;
        //未设置支付参数,则使用默认支付参数
        config = getOne(Wrappers.lambdaQuery(WxConfigEntity.class).eq(WxConfigEntity::getAppappid, appId).eq(WxConfigEntity::getState, 1));
 
        if (config == null) {
            throw new BusinessException("未找到支付配置或配置未启用,请联系管理员!");
        }
//        if (pole.getClientId() == null) {
        //没有归属的灯杆使用默认配置
//            config = getById(15);
//        }
        return config;
    }
 
 
    public boolean addWxPayConfig(WxpayConfigParam configParam) {
        WxConfigEntity config = new WxConfigEntity();
        BeanUtil.copyProperties(configParam, config);
        config.setAppid(configParam.getAppId());
        config.setAppappid(configParam.getAppAppId());
        if (SecurityUtils.getClientId() != null) {
            if (clientService.findClientId()) {
                config.setClientId(clientService.getClientId());
            } else {
                config.setClientId(SecurityUtils.getUserId());
            }
            config.setCreateUserId(SecurityUtils.getUserId());
        } else {
            config.setClientId(SecurityUtils.getUserId());
            config.setCreateUserId(SecurityUtils.getUserId());
        }
        return save(config);
    }
 
    public boolean updateWxPayConfig(Long id, WxpayConfigParam configParam) {
        WxConfigEntity byId = getById(id);
        if (byId == null) {
            throw new BusinessException("配置id错误或配置不存在");
        }
        WxConfigEntity config = new WxConfigEntity();
        BeanUtil.copyProperties(configParam, config);
        config.setAppid(configParam.getAppId());
        config.setAppappid(configParam.getAppAppId());
        config.setConfigId(id);
        return updateById(config);
    }
 
    public boolean deleteWxPayConfig(Long id) {
        WxConfigEntity byId = getById(id);
        if (byId == null) {
            throw new BusinessException("配置id错误或配置不存在");
        }
        return removeById(id);
    }
 
 
    public List<WxConfigEntity> configList(BaseConditionVO baseConditionVO, String keyword) {
        LambdaQueryWrapper<WxConfigEntity> wrapper = new LambdaQueryWrapper<>();
        System.out.println("----------------------------" + clientService.findClientId());
        PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
        if (SecurityUtils.getClientId() != null) {
            //客户
            if (clientService.findClientId()) {
                //二级客户
                PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
                wrapper = Wrappers.lambdaQuery(WxConfigEntity.class).eq(WxConfigEntity::getCreateUserId, SecurityUtils.getUserId());
            } else if (!clientService.findClientId()) {
                //一级客户
                PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
                wrapper = Wrappers.lambdaQuery(WxConfigEntity.class).eq(WxConfigEntity::getClientId, SecurityUtils.getUserId());
            }
        } else {
            //管理
            PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
            wrapper = Wrappers.lambdaQuery(WxConfigEntity.class);
        }
        if (keyword != null && !keyword.isEmpty()) {
            wrapper = wrapper.like(WxConfigEntity::getConfigId, keyword).or(
                    appid -> {
                        appid.like(WxConfigEntity::getAppid, keyword);
                    }
            ).or(
                    appappid -> {
                        appappid.like(WxConfigEntity::getAppappid, keyword);
                    }
            ).or(
                    mchId -> {
                        mchId.like(WxConfigEntity::getMchId, keyword);
                    }
            ).or(
                    privateKey -> {
                        privateKey.like(WxConfigEntity::getPrivateKey, keyword);
                    }
            );
        }
        List<WxConfigEntity> list = list(wrapper);
//        list.forEach(
//                wxConfigEntity -> {
//                    //设置支付参数中的appid和appappid  mchId和privateKey中的部分替换为*
//                    wxConfigEntity.setAppid(wxConfigEntity.getAppid().replaceAll("(?<=.{4}).*(?=.{4})", "*"));
//                    wxConfigEntity.setAppappid(wxConfigEntity.getAppappid().replaceAll("(?<=.{4}).*(?=.{4})", "*"));
//                    wxConfigEntity.setMchId(wxConfigEntity.getMchId().replaceAll("(?<=.{4}).*(?=.{4})", "*"));
//                    wxConfigEntity.setPrivateKey(wxConfigEntity.getPrivateKey().replaceAll("(?<=.{4}).*(?=.{4})", "*"));
//                    wxConfigEntity.setAppSecret(wxConfigEntity.getAppSecret().replaceAll("(?<=.{4}).*(?=.{4})", "*"));
//                }
//        );
        return list;
    }
 
    /**
     * 支付参数配置的停用/启用
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean updateState(Long id, Integer state) {
        WxConfigEntity byId = getById(id);
        if (byId == null) {
            throw new BusinessException("配置id错误或配置不存在");
        }
        /**
         * 获取当前用户所有的wx支付配置
         */
        List<WxConfigEntity> configList;
        if (SecurityUtils.getClientId() != null) {
            //客户
            configList = list(Wrappers.lambdaQuery(WxConfigEntity.class).eq(WxConfigEntity::getClientId, SecurityUtils.getUserId())
                    .or(
                            user -> {
                                user.eq(WxConfigEntity::getCreateUserId, SecurityUtils.getUserId());
                            }
                    ));
        } else {
            //管理
            configList = list(Wrappers.lambdaQuery(WxConfigEntity.class).eq(WxConfigEntity::getClientId, byId.getCreateUserId())
                    .or(
                            user -> {
                                user.eq(WxConfigEntity::getCreateUserId, SecurityUtils.getUserId());
                            }
                    ));
        }
        if (configList.size() == 1 && state == 0) {
            throw new BusinessException("当前用户只有一个支付配置,不能停用");
        }
        //设置当前用户所有的支付配置为停用
        configList.forEach(
                wxConfigEntity -> {
                    wxConfigEntity.setState(0);
                }
        );
        boolean b = updateBatchById(configList);
        if (!b) {
            throw new BusinessException("更新失败");
        }
 
        WxConfigEntity config = new WxConfigEntity();
        config.setConfigId(id);
        config.setState(state);
        return updateById(config);
    }
 
}