admin
2022-04-21 2464548977f4720bc8fceeb5d999c1a3852c2d10
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package com.yeshi.makemoney.app.controller.client.api;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornConsumeRecord;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornConsumeType;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetRecord;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetType;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornGetFrequencyConfigException;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornGetPriceException;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornMakeException;
import com.yeshi.makemoney.app.exception.user.UserInfoException;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornConsumeRecordService;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetPriceService;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetRecordService;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornMakeService;
import com.yeshi.makemoney.app.service.inter.team.TeamInviteRelationService;
import com.yeshi.makemoney.app.service.query.goldcorn.GoldCornConsumeRecordQuery;
import com.yeshi.makemoney.app.service.query.goldcorn.GoldCornGetRecordQuery;
import com.yeshi.makemoney.app.utils.Constant;
import com.yeshi.makemoney.app.utils.annotation.UserLogin;
import com.yeshi.makemoney.app.utils.goldcorn.GoldCornUtil;
import com.yeshi.makemoney.app.vo.AcceptData;
import com.yeshi.makemoney.app.vo.goldcorn.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.TimeUtil;
 
import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author hxh
 * @title: ConfigController
 * @description: 金币接口
 * @date 2021/11/16 17:37
 */
@Controller
@RequestMapping("api/v1/goldcorn")
public class GoldCornController {
    @Resource
    private GoldCornGetRecordService goldCornGetRecordService;
 
    @Resource
    private GoldCornConsumeRecordService goldCornConsumeRecordService;
 
 
    @Resource
    private GoldCornMakeService goldCornMakeService;
 
    @Resource
    private GoldCornGetPriceService goldCornGetPriceService;
 
    @Resource
    private TeamInviteRelationService teamInviteRelationService;
 
    private Gson gson = JsonUtil.getConvertBigDecimalToStringBuilder(new GsonBuilder()).create();
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 金币信息(包括当日金币余额,兑换中的金币)
     * @date 13:15 2021/12/2
     * @param: acceptData
     **/
    @UserLogin(uid = "#uid")
    @RequestMapping("getGoldCornInfo")
    @ResponseBody
    public String getGoldCornInfo(AcceptData acceptData, Long uid) {
        Date day = new Date();
        GoldCornInfoVO vo = new GoldCornInfoVO();
        vo.setBalance(Integer.parseInt(goldCornGetRecordService.getGoldCornByDay(uid, day) + ""));
        vo.setExchanging(Integer.parseInt(goldCornMakeService.getExchangingGoldCorn(uid, 3) + ""));
        return JsonUtil.loadTrueResult(gson.toJson(vo));
    }
 
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 获得金币记录列表
     * @date 14:02 2022/4/21
     * @param: acceptData
     * @param: uid
     * @param: page
     **/
    @UserLogin(uid = "#uid")
    @RequestMapping("listGetRecord")
    @ResponseBody
    public String listGetRecord(AcceptData acceptData, Long uid, int page) {
 
        GoldCornGetRecordQuery query = new GoldCornGetRecordQuery();
        query.setUid(uid);
 
        List<GoldCornGetRecord> recordList = goldCornGetRecordService.list(query, page, Constant.PAGE_SIZE);
        long count = goldCornGetRecordService.count(query);
 
        List<GoldCornGetRecordVO> voList = new ArrayList<>();
        for (GoldCornGetRecord record : recordList) {
            GoldCornGetRecordVO vo = new GoldCornGetRecordVO();
            vo.setDateTime(TimeUtil.getGernalTime(record.getCreateTime().getTime(), "yyyy.MM.dd HH:mm"));
            vo.setNum(record.getCornNum());
            if (record.getDubble() != null && record.getDubble()) {
                vo.setTitle(record.getType().getName() + "(加倍)");
            } else {
                vo.setTitle(record.getType().getName());
            }
            voList.add(vo);
        }
 
        JSONObject data = new JSONObject();
        data.put("count", count);
        data.put("list", gson.toJson(voList));
        return JsonUtil.loadTrueResult(data);
    }
 
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 获取兑换记录
     * @date 14:02 2022/4/21
     * @param: acceptData
     * @param: uid
     * @param: page
     **/
    @UserLogin(uid = "#uid")
    @RequestMapping("listExchangeRecord")
    @ResponseBody
    public String listExchangeRecord(AcceptData acceptData, Long uid, int page) {
        GoldCornConsumeRecordQuery query = new GoldCornConsumeRecordQuery();
        query.setUid(uid);
        query.setType(GoldCornConsumeType.changeMoney);
 
 
        List<GoldCornConsumeRecord> recordList = goldCornConsumeRecordService.list(query, page, Constant.PAGE_SIZE);
        long count = goldCornConsumeRecordService.count(query);
 
        List<GoldCornExchangeRecordVO> voList = new ArrayList<>();
        for (GoldCornConsumeRecord record : recordList) {
            GoldCornExchangeRecordVO vo = new GoldCornExchangeRecordVO();
            vo.setDateTime(TimeUtil.getGernalTime(record.getCreateTime().getTime(), "yyyy.MM.dd HH:mm"));
            vo.setNum(record.getCornNum());
            vo.setMoney(record.getMoney().setScale(2).toString());
            voList.add(vo);
        }
 
        JSONObject data = new JSONObject();
        data.put("count", count);
        data.put("list", gson.toJson(voList));
        return JsonUtil.loadTrueResult(data);
    }
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 获取任务的价格
     * @date 14:01 2022/4/21
     * @param: acceptData
     * @param: uid
     * @param: type
     **/
    @RequestMapping("getTaskPrice")
    @ResponseBody
    public String getTaskPrice(AcceptData acceptData, Long uid, GoldCornGetType type) {
        boolean hasBoss = false;
 
        if (uid != null) {
            hasBoss = teamInviteRelationService.getBossUid(uid) != null;
        }
        Integer cornNum = goldCornGetPriceService.getPriceCornNum(acceptData.getSystem(), type, new Date(), hasBoss);
        JSONObject data = new JSONObject();
        data.put("cornNum", cornNum);
        return JsonUtil.loadTrueResult(data);
    }
 
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 做任务
     * @date 14:02 2022/4/21
     * @param: acceptData
     * @param: uid
     * @param: taskData
     **/
    @UserLogin(uid = "#uid")
    @RequestMapping("doTask")
    @ResponseBody
    public String doTask(AcceptData acceptData, Long uid, String taskData) {
        Type type = new TypeToken<List<GoldCornDoTaskVO>>() {
        }.getType();
        List<GoldCornDoTaskVO> list = null;
 
        try {
            list = gson.fromJson(taskData, type);
        } catch (Exception e) {
 
        }
        if (list == null) {
            return JsonUtil.loadFalseResult("数据为空");
        }
 
        for (GoldCornDoTaskVO vo : list) {
            try {
                goldCornMakeService.addGoldCorn(uid, vo.getType(), vo.getDouble() == null ? false : vo.getDouble(), new Date(vo.getFinishTime() == null ? System.currentTimeMillis() : vo.getFinishTime()), null);
            } catch (UserInfoException e) {
                e.printStackTrace();
            } catch (GoldCornGetPriceException e) {
                e.printStackTrace();
            } catch (GoldCornGetFrequencyConfigException e) {
                e.printStackTrace();
            } catch (GoldCornMakeException e) {
                e.printStackTrace();
            }
        }
        return JsonUtil.loadTrueResult("");
    }
 
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 获取签到信息
     * @date 17:12 2022/4/21
     * @param: acceptData
     * @param: uid
     * @param: taskData
     **/
    @RequestMapping("getSignInInfo")
    @ResponseBody
    public String getSignInInfo(AcceptData acceptData, Long uid) {
        Date now = new Date();
        //设置默认值
        SignInInfoVO vo = new SignInInfoVO();
        vo.setContinueSignInDay(0);
        vo.setNotify(true);
        vo.setSignIned(false);
        if (uid != null) {
            vo.setSignIned(goldCornMakeService.isSignIn(uid, now));
            vo.setContinueSignInDay(goldCornMakeService.getContinueSignDay(uid));
        }
        /*************签到日期列表开始**************/
 
        //制造日期
        List<SignInInfoVO.SignInDayInfo> dayList = getDayList(now);
        List<String> days = new ArrayList<>();
        for (SignInInfoVO.SignInDayInfo day : dayList) {
            days.add(GoldCornUtil.getFormatDay(day.getDate()));
            //查询是否已经签到
            if (day.isToday()) {
                break;
            }
        }
        //查询签到的得到的金币数
        List<GoldCornGetRecord> recordList = new ArrayList<>();
        if (uid != null) {
            GoldCornGetRecordQuery query = new GoldCornGetRecordQuery();
            query.setDayList(days);
            query.setUid(uid);
            query.setType(GoldCornGetType.signIn);
            recordList = goldCornGetRecordService.list(query, 1, days.size());
        }
 
        Map<String, GoldCornGetRecord> recordMap = recordList.stream().collect(Collectors.toMap(GoldCornGetRecord::getDay, record -> record, (key1, key2) -> key2));
        int dayDiff = 0;
        for (SignInInfoVO.SignInDayInfo dayInfo : dayList) {
            String day = GoldCornUtil.getFormatDay(dayInfo.getDate());
            //现在/过去的日期
            if (days.contains(day)) {
                if (recordMap.get(day) != null) {
                    //已经签到
                    dayInfo.setState(SignInInfoVO.SignInDayInfo.STATE_GOT);
                    dayInfo.setGoldCorn(recordMap.get(day).getCornNum());
                } else {
                    //尚未签到
                    if (dayInfo.isToday()) {
                        //今天签到预计的金币
                        dayInfo.setGoldCorn(goldCornGetPriceService.getSingInPrice(acceptData.getSystem(), vo.getContinueSignInDay() + 1));
                        dayInfo.setState(SignInInfoVO.SignInDayInfo.STATE_NOT_GOT);
                    } else {
                        dayInfo.setState(SignInInfoVO.SignInDayInfo.STATE_MISS);
                        dayInfo.setGoldCorn(0);
                    }
                }
            } else {
                dayDiff++;
                //未来的时间
                dayInfo.setGoldCorn(goldCornGetPriceService.getSingInPrice(acceptData.getSystem(), vo.getContinueSignInDay() + 1 + dayDiff));
                dayInfo.setState(SignInInfoVO.SignInDayInfo.STATE_NOT_GOT);
            }
            //清除临时变量
            dayInfo.setDate(null);
        }
        vo.setDayList(dayList);
        /*************签到日期列表结束**************/
 
 
        return JsonUtil.loadTrueResult(gson.toJson(vo));
    }
 
    private static List<SignInInfoVO.SignInDayInfo> getDayList(Date date) {
        List<SignInInfoVO.SignInDayInfo> dayList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        if (day == 1) {
            day += 7;
        }
        //星期几(1-7)
        day = day - 1;
        for (int i = 1; i <= 7; i++) {
            Date dateTime = new Date(date.getTime() - (day - i) * 1000 * 60 * 60 * 24L);
            SignInInfoVO.SignInDayInfo dayInfo = new SignInInfoVO.SignInDayInfo();
            dayInfo.setDay(getDayDesc(i, day, dateTime));
            dayInfo.setDate(dateTime);
            dayInfo.setToday(day == i);
            dayList.add(dayInfo);
        }
        return dayList;
    }
 
    private static String getDayDesc(int day, int now, Date dayTime) {
        if (now == day) {
            return "今天";
        }
        return TimeUtil.getGernalTime(dayTime.getTime(), "MM.dd");
    }
 
 
    @UserLogin(uid = "#uid")
    @RequestMapping("signIn")
    @ResponseBody
    public String signIn(AcceptData acceptData, Long uid) {
        try {
            GoldCornGetRecord record = goldCornMakeService.signIn(uid);
            int count = goldCornMakeService.getContinueSignDay(uid);
 
            JSONObject data = new JSONObject();
            data.put("goldCorn", record.getCornNum());
            data.put("continueDay", count);
            return JsonUtil.loadTrueResult(data);
        } catch (Exception e) {
            return JsonUtil.loadFalseResult("签到出错");
        }
 
 
    }
 
}