Helius
2021-08-04 84a3ef10e4219ad844e6ea86af65d8dec84f28d4
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
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.xzx.gc.GcUserApplication;
import com.xzx.gc.common.Result;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.utils.RedisUtil;
import com.xzx.gc.common.utils.gdmap.GdTraceUtil;
import com.xzx.gc.common.utils.wxpay.WxUtil;
import com.xzx.gc.entity.*;
import com.xzx.gc.user.mapper.UserBatchMapper;
import com.xzx.gc.user.service.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
 
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
 
 
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {GcUserApplication.class})
@ActiveProfiles(profiles = {"xc"})
public class SpringTest {
 
 
    @Autowired
    private AddressLevelService addressLevelService;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private OtherUserService otherUserService;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private OrderService orderService;
 
    @Autowired
    private PartnerFenceService partnerFenceService;
 
    @Autowired
    private PartnerGaodeService partnerGaodeService;
 
    @Autowired
    private UserBatchMapper userBatchMapper;
 
    @Test
    public  void test(){
//        List<AddressLevelInfo> byTownIds = addressLevelService.findByTownIds("1812");
//        for (AddressLevelInfo byTownId : byTownIds) {
//            System.out.println(byTownId);
//        }
//        if(redisUtil.exists(Constants.REDIS_USER_KEY+"token:"+"YH1197504485698961408")){
//            System.out.println("123");
//        }else {
//            System.out.println("456");
//        }
        OtherUserInfo otherUserInfo=new OtherUserInfo();
        otherUserInfo.setUserId(RandomUtil.randomString(11));
        otherUserInfo.setMobilePhone("1");
        otherUserService.add(otherUserInfo);
 
    }
 
    @Test
    public void 修复用户合伙人() throws InterruptedException {
        List<UserInfo> list=new ArrayList<>();
 
        List<UserInfo> forBidden = userService.findForBidden();
        CountDownLatch countDownLatch = ThreadUtil.newCountDownLatch(forBidden.size());
        for (UserInfo userInfo : forBidden) {
 
 
            Runnable callable=()->{
                UserInfo userInfo1 = fixPa(userInfo);
                if(userInfo1!=null){
                    list.add(userInfo1);
                }
                countDownLatch.countDown();
            };
 
            ThreadUtil.execAsync(callable,true);
        }
 
        countDownLatch.await();
 
        System.out.println("批量更新");
        if(CollUtil.isNotEmpty(list)) {
            userBatchMapper.updateBatchByPrimaryKeySelective(list);
        }
        System.out.println("完成");
 
 
    }
 
 
    public UserInfo fixPa(UserInfo userInfo){
        //先根据用户的订单查询所属的合伙人  没有再根据经纬度查询
        List<OrderInfo> byCreateUserIdAndNotNullTown = orderService.findByCreateUserIdAndNotNullTown(userInfo.getUserId());
 
        //筛选正常围栏
        byCreateUserIdAndNotNullTown = byCreateUserIdAndNotNullTown.stream().filter(x -> Convert.toInt(x.getTownId()) >= 182 && Convert.toInt(x.getTownId()) <= 209).collect(Collectors.toList());
 
        //去重围栏ID
        byCreateUserIdAndNotNullTown = byCreateUserIdAndNotNullTown.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(OrderInfo::getTownId))), ArrayList::new));
 
        List<String> partnerIds = CollUtil.newArrayList();
        for (OrderInfo orderInfo : byCreateUserIdAndNotNullTown) {
            String townId = orderInfo.getTownId();
            PartnerFence byFenceId = partnerFenceService.findByFenceId(townId);
            if(byFenceId!=null){
                partnerIds.add(byFenceId.getPartnerId());
            }
        }
 
        if(CollUtil.isNotEmpty(partnerIds)){
            partnerIds=partnerIds.stream().distinct().collect(Collectors.toList());
 
            String join = CollUtil.join(partnerIds, ",");
 
            userInfo.setPartnerId(join);
 
        }else {
 
            //根据经纬度查询
            if(StrUtil.isNotBlank(userInfo.getLongitude())){
                String partnerIdByLon = partnerGaodeService.findPartnerIdByLon(userInfo.getLongitude(), userInfo.getLatitude());
                userInfo.setPartnerId(partnerIdByLon);
            }
        }
 
        if(StrUtil.isNotBlank(userInfo.getPartnerId())){
            UserInfo userInfo1=new UserInfo();
            userInfo1.setUserId(userInfo.getUserId());
            userInfo1.setPartnerId(userInfo.getPartnerId());
            return userInfo1;
        }
        return null;
    }
 
 
 
    @Test
    public void test2(){
        UserInfo  userInfo=new UserInfo();
        userInfo.setNickName("12321");
 
        redisUtil.setex("aa",JSONUtil.toJsonStr(userInfo),10);
    }
 
    /**
     * 给所有回收员和推广员分配终端
     */
    @Test
    public void createTeramal(){
 
        //回收员轨迹服务id
        String serviceId11="115777";
        //推广员轨迹服务id
        String serviceId22="115797";
 
        //回收员
        List<OtherUserInfo> byUserType = otherUserService.findByUserType(CommonEnum.回收员.getValue());
        if(CollUtil.isNotEmpty(byUserType)){
            for (OtherUserInfo otherUserInfo : byUserType) {
                String terminalId = otherUserInfo.getTerminalId();
                if(StrUtil.isBlank(terminalId)){
                    Result terminal = GdTraceUtil.createTerminal(Constants.GD_MAP_KEY, serviceId11, otherUserInfo.getUserId());
                    if(terminal.getCode()==0){
                        String s = terminal.getData().toString();
                        otherUserInfo.setTerminalId(s);
                        otherUserService.update(otherUserInfo);
                    }
                }
            }
        }
 
 
        //回收员
         byUserType = otherUserService.findByUserType(CommonEnum.推广员.getValue());
        if(CollUtil.isNotEmpty(byUserType)){
            for (OtherUserInfo otherUserInfo : byUserType) {
                String terminalId = otherUserInfo.getTerminalId();
                if(StrUtil.isBlank(terminalId)){
                    Result terminal = GdTraceUtil.createTerminal(Constants.GD_MAP_KEY, serviceId22, otherUserInfo.getUserId());
                    if(terminal.getCode()==0){
                        String s = terminal.getData().toString();
                        otherUserInfo.setTerminalId(s);
                        otherUserService.update(otherUserInfo);
                    }
                }
            }
        }
 
        System.out.println("执行完成");
 
    }
 
 
    @Autowired
    private WxUtil wxUtil;
 
    @Test
    public void acodeTest() {
 
        String path = "pages/login/login?poster=18390984915&chiefId=YH1417670707298828288";
        String imgPath = "/Users/helius/Desktop/YH1417670707298828288_acode.jpg";
 
        String url = StrFormatter.format("https://api.weixin.qq.com/wxa/getwxacode?access_token={}", wxUtil.getAccessToken());
        cn.hutool.json.JSONObject obj = JSONUtil.createObj();
        obj.put("path", path);
        //最小 280px,最大 1280px
        obj.put("width", "800px");
        obj.put("auto_color", false);
        cn.hutool.json.JSONObject obj2 = JSONUtil.createObj();
        obj2.put("r", 0);
        obj2.put("g", 0);
        obj2.put("b", 0);
        obj.put("line_color", obj2);
        //是否需要透明底色,为 true 时,生成透明底色的小程序码
        obj.put("is_hyaline", false);
        try {
            InputStream inputStream = HttpRequest.post(url).body(obj.toString(), "application/json").execute().bodyStream();
            File file = new File(imgPath);
            FileUtil.writeFromStream(inputStream, file);
            long uploadUrl = FileUtil.size(file);
            //小于10kb重新生成
            if(uploadUrl<= Constants.MIN_FILE_SIZE){
                log.error("生成微信小程序码失败:图片大小异常:{}",uploadUrl);
            }
        } catch (Exception e) {
            log.error("生成微信小程序码失败",e);
        }
 
    }
 
 
}