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.inter.user.UserExtraInfoService;
|
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.data.redis.core.RedisTemplate;
|
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.concurrent.TimeUnit;
|
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;
|
|
@Resource
|
private UserExtraInfoService userExtraInfoService;
|
|
@Resource
|
private RedisTemplate<String, Object> redisTemplate;
|
|
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, GoldCornDoTaskVO vo) {
|
|
if (vo.getType() == null) {
|
return JsonUtil.loadFalseResult("参数不完整");
|
}
|
|
try {
|
JSONObject data = new JSONObject();
|
Integer result = null;
|
if (vo.getType() == GoldCornGetType.watchVideo) {
|
result = goldCornMakeService.watchVideo(uid, vo.getDouble(), vo.getFinishTime() == null ? new Date() : new Date(vo.getFinishTime()), vo.getTimeSeconds());
|
} else if (vo.getType() == GoldCornGetType.readNovel) {
|
result = goldCornMakeService.readNovel(uid, vo.getDouble(), vo.getFinishTime() == null ? new Date() : new Date(vo.getFinishTime()), vo.getTimeSeconds());
|
} else if (vo.getType() == GoldCornGetType.scanNews) {
|
result = goldCornMakeService.scanNews(uid, vo.getDouble(), vo.getFinishTime() == null ? new Date() : new Date(vo.getFinishTime()), vo.getNum());
|
} else {
|
return JsonUtil.loadFalseResult("任务类型出错");
|
}
|
data.put("goldCorn", result);
|
|
return JsonUtil.loadTrueResult(data);
|
|
} catch (GoldCornGetPriceException e) {
|
e.printStackTrace();
|
return JsonUtil.loadFalseResult("业务错误");
|
} catch (UserInfoException e) {
|
e.printStackTrace();
|
return JsonUtil.loadFalseResult(e.getMessage());
|
} catch (GoldCornMakeException e) {
|
e.printStackTrace();
|
return JsonUtil.loadFalseResult(e.getMessage());
|
} catch (GoldCornGetFrequencyConfigException e) {
|
e.printStackTrace();
|
return JsonUtil.loadFalseResult(e.getMessage());
|
}
|
}
|
|
|
/**
|
* @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));
|
vo.setNotify(userExtraInfoService.getSignInNotify(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() + (vo.isSignIned() ? 0 : 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("签到出错");
|
}
|
|
|
}
|
|
|
/**
|
* @return java.lang.String
|
* @author hxh
|
* @description 设置签到提醒
|
* @date 10:39 2022/4/22
|
* @param: acceptData
|
* @param: uid
|
* @param: notify 是否提醒
|
**/
|
@UserLogin(uid = "#uid")
|
@RequestMapping("setSignInNotify")
|
@ResponseBody
|
public String setSignInNotify(AcceptData acceptData, Long uid, boolean notify) {
|
userExtraInfoService.setSignInNotify(uid, notify);
|
return JsonUtil.loadTrueResult("");
|
}
|
|
|
}
|