admin
2021-07-29 0fc0456a587d985fda71c66b9764fe1d5e3c6421
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
package com.yeshi.buwan.service.imp.goldcorn;
 
import com.ks.goldcorn.exception.GoldAppException;
import com.ks.goldcorn.exception.GoldTradeException;
import com.ks.goldcorn.exception.GoldUserException;
import com.ks.goldcorn.pojo.DO.GoldCornGetSource;
import com.ks.goldcorn.pojo.DO.GoldCornRecord;
import com.ks.goldcorn.pojo.Query.GoldCornRecordQuery;
import com.yeshi.buwan.domain.goldcorn.CodeCornGetSourceType;
import com.yeshi.buwan.dto.goldcorn.SignInGoldCornDateData;
import com.yeshi.buwan.exception.goldcorn.SignInException;
import com.yeshi.buwan.exception.user.LoginUserException;
import com.yeshi.buwan.service.inter.goldcorn.SignInService;
import com.yeshi.buwan.service.manager.GoldCornManager;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
 
@Service
public class SignInServiceImpl implements SignInService {
 
    @Resource
    private GoldCornManager goldCornManager;
    Logger logger = LoggerFactory.getLogger(SignInService.class);
 
    @Override
    public Integer signIn(String uid) throws LoginUserException, SignInException {
        try {
            GoldCornGetSource source = goldCornManager.getGoldCornGetSource(CodeCornGetSourceType.signIn);
            int dayCount = getContinueSignDayCount(uid);
            dayCount = dayCount == 6 ? 6 : dayCount % 6;
            int goldCorn = (dayCount + 1) * source.getGoldCorn();
            goldCornManager.addGoldCorn(uid, CodeCornGetSourceType.signIn, goldCorn, "签到", String.format("连续%s天签到", (dayCount + 1)));
            return goldCorn;
        } catch (GoldUserException e) {
            logger.error("签到出错:", e);
            throw new LoginUserException(e.getCode(), e.getMsg());
        } catch (GoldAppException e) {
            logger.error("签到出错:", e);
            throw new SignInException(1, "服务器内部出错");
        } catch (GoldTradeException e) {
            logger.error("签到出错:", e);
            throw new SignInException(e.getCode(), e.getMsg());
        } catch (Exception e) {
            logger.error("签到出错:", e);
            e.printStackTrace();
            throw new SignInException(1, "服务器内部出错");
        }
 
    }
 
    @Override
    public int getContinueSignDayCount(String uid) throws Exception {
        if (StringUtil.isNullOrEmpty(uid)) {
            return 0;
        }
        GoldCornRecordQuery query = new GoldCornRecordQuery();
        query.setType(GoldCornRecord.TYPE_GET);
        query.setSourceCodes(Arrays.asList(new String[]{CodeCornGetSourceType.signIn.name()}));
        query.setPage(1);
        query.setPageSize(200);
        List<GoldCornRecord> recordList = goldCornManager.getRecordList(query, uid);
 
        return getContinueSignDayCount(recordList) + (isTodaySignIned(uid, new Date()) ? 1 : 0);
    }
 
    @Override
    public List<SignInGoldCornDateData> getContinueSignInList(String uid) throws GoldAppException {
        GoldCornRecordQuery query = new GoldCornRecordQuery();
        query.setType(GoldCornRecord.TYPE_GET);
        query.setSourceCodes(Arrays.asList(new String[]{CodeCornGetSourceType.signIn.name()}));
        query.setPage(1);
        query.setPageSize(7);
        GoldCornGetSource source = goldCornManager.getGoldCornGetSource(CodeCornGetSourceType.signIn);
        int goldCorn = source.getGoldCorn();
 
        List<SignInGoldCornDateData> list = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            SignInGoldCornDateData dateData = new SignInGoldCornDateData();
            dateData.setGoldCorn("+" + (i + 1) * goldCorn);
            list.add(dateData);
        }
        int dayCount = 0;
        Map<String, Integer> dayCountMap = new HashMap<>();
        //填充用户数据
        if (!StringUtil.isNullOrEmpty(uid)) {
            try {
                List<GoldCornRecord> recordList = goldCornManager.getRecordList(query, uid);
                dayCountMap = getContinueSignDayCountMap(recordList);
                dayCount = dayCountMap.size() == 6 ? 6 : dayCountMap.size() % 6;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
 
        //设置日期
        long now = System.currentTimeMillis();
        boolean isTodaySigned = isTodaySignIned(uid, new Date(now));
 
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setDate(TimeUtil.getGernalTime(now - (dayCount - i - (isTodaySigned ? 1 : 0)) * 1000 * 60 * 60 * 24L, "M.d"));
 
            //设置金币与选中
            if (dayCountMap.get(list.get(i).getDate()) != null) {
                list.get(i).setGoldCorn("+" + dayCountMap.get(list.get(i).getDate()));
                list.get(i).setSelected(true);
            }
 
            if (i == dayCount - (isTodaySigned ? 1 : 0)) {
                list.get(i).setDate("今天");
            }
 
        }
 
        return list;
    }
 
    @Override
    public boolean isTodaySignIned(String uid, Date time) {
        if (StringUtil.isNullOrEmpty(uid)) {
            return false;
        }
        GoldCornRecordQuery query = new GoldCornRecordQuery();
        query.setType(GoldCornRecord.TYPE_GET);
        query.setSourceCodes(Arrays.asList(new String[]{CodeCornGetSourceType.signIn.name()}));
        query.setPage(1);
        query.setPageSize(1);
        try {
            List<GoldCornRecord> recordList = goldCornManager.getRecordList(query, uid);
            if (recordList == null || recordList.size() == 0)
                return false;
            if (TimeUtil.getGernalTime(recordList.get(0).getCreateTime().getTime(), "yyyyMMdd").equalsIgnoreCase(TimeUtil.getGernalTime(time.getTime(), "yyyyMMdd"))) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
 
        return false;
    }
 
    /**
     * 获取是第几次签到
     *
     * @param recordList
     * @return
     */
    private int getContinueSignDayCount(List<GoldCornRecord> recordList) {
        int position = 0;
        long lastTime = TimeUtil.convertGernalTime(TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd"), "yyyy-MM-dd");
        if (recordList != null)
            for (int i = 0; i < recordList.size(); i++) {
                long time = TimeUtil.convertGernalTime(TimeUtil.getGernalTime(recordList.get(i).getCreateTime().getTime(), "yyyy-MM-dd"), "yyyy-MM-dd");
                if (lastTime - time == 1000 * 60 * 60 * 24L) {
                    position++;
                    lastTime = time;
                } else if (lastTime - time < 1000 * 60 * 60 * 24L) {
                    continue;
                } else {
                    break;
                }
            }
        return position;
    }
 
 
    private Map<String, Integer> getContinueSignDayCountMap(List<GoldCornRecord> recordList) {
        Map<String, Integer> map = new TreeMap<>();
        long lastTime = TimeUtil.convertGernalTime(TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd"), "yyyy-MM-dd");
        if (recordList != null)
            for (int i = 0; i < recordList.size(); i++) {
                String day = TimeUtil.getGernalTime(recordList.get(i).getCreateTime().getTime(), "M.d");
                long time = TimeUtil.convertGernalTime(TimeUtil.getGernalTime(recordList.get(i).getCreateTime().getTime(), "yyyy-MM-dd"), "yyyy-MM-dd");
                if (lastTime - time == 1000 * 60 * 60 * 24L) {
                    lastTime = time;
                    map.put(day, recordList.get(i).getGoldCorn());
                } else if (lastTime - time < 1000 * 60 * 60 * 24L) {
                    map.put(day, recordList.get(i).getGoldCorn());
                    continue;
                } else {
                    break;
                }
            }
        return map;
    }
}