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.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.*;
|
|
@Service
|
public class SignInServiceImpl implements SignInService {
|
|
@Resource
|
private GoldCornManager goldCornManager;
|
|
@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) {
|
throw new LoginUserException(e.getCode(), e.getMsg());
|
} catch (GoldAppException e) {
|
throw new SignInException(1, "服务器内部出错");
|
} catch (GoldTradeException e) {
|
throw new SignInException(e.getCode(), e.getMsg());
|
} catch (Exception e) {
|
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;
|
}
|
}
|