admin
2019-08-21 f1a2d46aaa612253afba7b1d61ad04a2f0ef7eed
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
package com.yeshi.fanli.service.impl.integral;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.DateUtil;
 
import com.yeshi.fanli.dao.mybatis.integral.IntegralTaskRecordMapper;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.integral.IntegralTaskRecord;
import com.yeshi.fanli.exception.integral.IntegralTaskRecordException;
import com.yeshi.fanli.exception.user.UserInfoExtraException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.integral.IntegralTaskRecordService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
 
@Service
public class IntegralTaskRecordServiceImpl implements IntegralTaskRecordService {
 
    @Resource
    private IntegralTaskRecordMapper integralTaskRecordMapper;
 
    @Resource
    private UserInfoExtraService userInfoExtraService;
 
    @Override
    public Integer getTotalGoldCoin(long uid, Long cid, Integer dateType) {
        return integralTaskRecordMapper.getTotalGoldCoin(uid, cid, dateType);
    }
 
    @Override
    public Integer countFinished(long uid, Long cid, Integer dateType) {
        return integralTaskRecordMapper.countFinished(uid, cid, dateType);
    }
 
    @Override
    public void finishedTask(long uid, long cid, long taskId, int goldCoin) {
        IntegralTaskRecord taskRecord = new IntegralTaskRecord();
        taskRecord.setUid(uid);
        taskRecord.setCid(cid);
        taskRecord.setTaskId(taskId);
        taskRecord.setGoldCoin(goldCoin);
        taskRecord.setState(0);
        taskRecord.setCreateTime(new Date());
        taskRecord.setUpdateTime(new Date());
        integralTaskRecordMapper.insertSelective(taskRecord);
    }
 
    @Override
    public boolean isToDaySign(long uid, Long cid) {
        Integer countFinished = integralTaskRecordMapper.countFinished(uid, cid, 1);
        if (countFinished != null && countFinished > 0) {
            return true;
        }
        return false;
    }
 
    @Override
    public Integer getNowdaySignNum(long uid, Long cid) {
        int num = 1;
        try {
            // 签到时间倒序查询近7天数据
            List<IntegralTaskRecord> list = integralTaskRecordMapper.listDaySignRecord(uid, cid);
            if (list != null && list.size() > 0) {
                Date today = new Date();
                for (IntegralTaskRecord record : list) {
                    if (DateUtil.daysBetween2(record.getCreateTime(), today) == 0) {
                        continue;
                    }
 
                    if (DateUtil.daysBetween2(record.getCreateTime(), today) != 1) {
                        break; // 天数未连续
                    }
                    today = record.getCreateTime();
                    num++;
                }
            }
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
 
        if (num > 7) {
            num = 1;
        }
        return num;
    }
 
    @Override
    public List<Date> getSignDays(long uid, Long cid) {
        List<Date> listDate = new ArrayList<Date>();
        try {
            // 签到时间正序序查询近7天数据
            List<IntegralTaskRecord> list = integralTaskRecordMapper.listDaySignRecord(uid, cid);
            if (list != null && list.size() > 0) {
                Date date = new Date();
                for (IntegralTaskRecord record : list) {
                    Date createTime = record.getCreateTime();
                    if (DateUtil.daysBetween2(createTime, date) > 1) {
                        break; // 天数未连续
                    }
                    date = createTime;
                    listDate.add(record.getCreateTime());
                }
            }
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
 
        // 倒序
        Collections.reverse(listDate);
        return listDate;
    }
 
    @Override
    public List<IntegralTaskRecord> listNotReceived(int count, long uid) {
        return integralTaskRecordMapper.listNotReceived(count, uid);
    }
 
    @Override
    public List<IntegralTaskRecord> listNotReceivedExcludeId(int count, long uid, Set<Long> idList) {
        if (idList != null && idList.size() == 0) {
            idList = null;
        }
        return integralTaskRecordMapper.listNotReceivedExcludeId(count, uid, idList);
    }
 
    @Override
    @Transactional
    public Integer receiveGoldCoinALL(Long uid) throws IntegralTaskRecordException {
        UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
        if (userInfoExtra == null)
            throw new IntegralTaskRecordException(1, "用户信息不全");
 
        int totalGoldCoin = integralTaskRecordMapper.countTotalGoldCoin(uid);
        if (totalGoldCoin > 0) {
            integralTaskRecordMapper.updateReceived(uid);
        }
 
        Integer goldCoin = userInfoExtra.getGoldCoin();
        if (goldCoin == null)
            goldCoin = 0;
 
        UserInfoExtra extra = new UserInfoExtra();
        extra.setId(userInfoExtra.getId());
        extra.setGoldCoin(goldCoin + totalGoldCoin);
        try {
            userInfoExtraService.saveUserInfoExtra(extra);
        } catch (UserInfoExtraException e) {
            throw new IntegralTaskRecordException(1, "用户信息不全");
        }
 
        return extra.getGoldCoin();
    }
 
    @Override
    @Transactional
    public Integer receiveGoldCoin(Long uid, Set<Long> idList) throws IntegralTaskRecordException {
        if (idList == null || idList.size() == 0) {
            throw new IntegralTaskRecordException(1, "id为空");
        }
 
        UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
        if (userInfoExtra == null) {
            throw new IntegralTaskRecordException(1, "用户信息不全");
        }
 
        Integer totalGoldCoin = 0;
        for (Long id : idList) {
            IntegralTaskRecord record = integralTaskRecordMapper.selectByPrimaryKey(id);
            if (record == null)
                continue;
 
            Long uid2 = record.getUid();
            if (uid2 == null || uid.longValue() != uid2.longValue())
                continue; // 用户id不符合
 
            if (record.getState() != null && record.getState().intValue() == 1) {
                continue; // 已领取
            }
 
            IntegralTaskRecord taskRecord = new IntegralTaskRecord();
            taskRecord.setId(record.getId());
            taskRecord.setState(1); // 已领取
            taskRecord.setUpdateTime(new Date());
            integralTaskRecordMapper.updateByPrimaryKeySelective(taskRecord);
 
            totalGoldCoin += record.getGoldCoin();
        }
 
        Integer goldCoin = userInfoExtra.getGoldCoin();
        if (goldCoin == null)
            goldCoin = 0;
 
        UserInfoExtra extra = new UserInfoExtra();
        extra.setId(userInfoExtra.getId());
        extra.setGoldCoin(goldCoin + totalGoldCoin);
        try {
            userInfoExtraService.saveUserInfoExtra(extra);
        } catch (UserInfoExtraException e) {
            throw new IntegralTaskRecordException(1, "用户信息不全");
        }
 
        return extra.getGoldCoin();
    }
 
    @Override
    public Integer countGetCountByTaskIdAndDay(Long taskId, Long uid, Date day) {
        if (day == null)
            return null;
        Date minTime = new Date(
                TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(day.getTime(), "yyyy-MM-dd"), "yyyy-MM-dd"));
        Date maxTime = new Date(minTime.getTime() + 1000 * 60 * 60 * 24L);
        return integralTaskRecordMapper.countGetCountByTaskIdAndDay(taskId, uid, minTime, maxTime);
    }
 
    @Override
    public IntegralTaskRecord addRecord(IntegralTaskRecord record) throws IntegralTaskRecordException {
        if (record == null)
            throw new IntegralTaskRecordException(1, "记录为空");
 
        if (record.getCid() == null || record.getTaskId() == null || record.getGoldCoin() == null
                || record.getState() == null || record.getUid() == null)
            throw new IntegralTaskRecordException(2, "数据不完成");
 
        if (record.getCreateTime() == null)
            record.setCreateTime(new Date());
 
        if (record.getUpdateTime() == null)
            record.setUpdateTime(new Date());
        integralTaskRecordMapper.insertSelective(record);
        return record;
    }
}