package com.taoke.autopay.controller;
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.TypeAdapter;
|
import com.google.gson.stream.JsonReader;
|
import com.google.gson.stream.JsonWriter;
|
import com.taoke.autopay.dao.credit.CreditExchangeRecordMapper;
|
import com.taoke.autopay.dao.credit.UserCreditRecordMapper;
|
import com.taoke.autopay.dto.admin.CreditInfoDto;
|
import com.taoke.autopay.entity.SystemConfigKeyEnum;
|
import com.taoke.autopay.entity.WxUserInfo;
|
import com.taoke.autopay.entity.credit.CreditExchangeRecord;
|
import com.taoke.autopay.entity.credit.UserAlipayBinding;
|
import com.taoke.autopay.entity.credit.UserCreditBalance;
|
import com.taoke.autopay.entity.credit.UserCreditRecord;
|
import com.taoke.autopay.exception.UserCreditExchangeException;
|
import com.taoke.autopay.manager.UserCreditExchangeManager;
|
import com.taoke.autopay.service.SystemConfigService;
|
import com.taoke.autopay.service.credit.CreditExchangeRecordService;
|
import com.taoke.autopay.service.credit.UserAlipayBindingService;
|
import com.taoke.autopay.service.credit.UserCreditBalanceService;
|
import com.taoke.autopay.service.credit.UserCreditRecordService;
|
import com.taoke.autopay.utils.Constant;
|
import com.taoke.autopay.utils.JsonUtil;
|
import com.taoke.autopay.utils.StringUtil;
|
import net.sf.json.JSONObject;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpSession;
|
import java.io.IOException;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.Date;
|
import java.util.List;
|
|
@RestController
|
@RequestMapping("/credit/api")
|
public class CreditController {
|
|
private static final Logger logger = LoggerFactory.getLogger(CreditController.class);
|
|
@Resource
|
private UserCreditBalanceService userCreditBalanceService;
|
|
@Resource
|
private UserCreditExchangeManager userCreditExchangeManager;
|
|
@Resource
|
private CreditExchangeRecordService creditExchangeRecordService;
|
|
@Resource
|
private UserAlipayBindingService userAlipayBindingService;
|
|
@Resource
|
private UserCreditRecordService userCreditRecordService;
|
|
@Resource
|
private CreditExchangeRecordService userCreditExchangeRecordService;
|
|
@Resource
|
private SystemConfigService systemConfigService;
|
|
private final Gson gson = new GsonBuilder().registerTypeAdapter(BigDecimal.class, new TypeAdapter<BigDecimal>() {
|
@Override
|
public void write(JsonWriter out, BigDecimal value) throws IOException {
|
String desc = "";
|
if (value != null) {
|
out.value(value.setScale(2, RoundingMode.HALF_UP).toString());
|
} else {
|
out.value("");
|
}
|
}
|
|
@Override
|
public BigDecimal read(JsonReader in) throws IOException {
|
return new BigDecimal("0");
|
}
|
}).create();
|
|
private Long getUserId(HttpSession session) {
|
WxUserInfo user = (WxUserInfo) session.getAttribute(Constant.SESSION_KEY_USER);
|
if (user != null) {
|
return user.getId();
|
}
|
return null;
|
}
|
|
private String getLoginLinkContent() {
|
String redictLink = systemConfigService.getValueCache(SystemConfigKeyEnum.WX_REDIRECT_LINK);
|
redictLink = redictLink.replace("snsapi_base", "snsapi_userinfo");
|
// 没有登录,返回登录链接
|
JSONObject root = new JSONObject();
|
root.put("link", redictLink);
|
return JsonUtil.loadTrueResult(Constant.RESULT_CODE_NEED_LOGIN, root);
|
}
|
|
/**
|
* 获取积分信息接口
|
* 返回积分余额,剩余积分可兑换的红包,正在兑换中的积分数量
|
*/
|
@RequestMapping("/info")
|
public String getCreditInfo(HttpSession session) {
|
Long userId = getUserId(session);
|
if (userId == null) {
|
return getLoginLinkContent();
|
}
|
int balanceAmount = 0;
|
UserCreditBalance balance = userCreditBalanceService.getCreditBalanceByUserId(userId);
|
if (balance != null) {
|
balanceAmount = balance.getCreditBalance();
|
}
|
|
BigDecimal money = new BigDecimal(0);
|
try {
|
money = userCreditExchangeManager.calculateExchangeAmount(userId, balanceAmount, false);
|
} catch (UserCreditExchangeException e) {
|
throw new RuntimeException(e);
|
}
|
|
List<CreditExchangeRecord> recordList = creditExchangeRecordService.listExchangeRecords(CreditExchangeRecordMapper.DaoQuery.builder()
|
.uid(userId)
|
.exchangeStatus(CreditExchangeRecord.STATUS_NOT_VERIFY)
|
.count(100)
|
.build());
|
int exchaningCredits = 0;
|
if (recordList != null) {
|
for (CreditExchangeRecord record : recordList) {
|
exchaningCredits += record.getConsumedCredits();
|
}
|
}
|
|
|
try {
|
return JsonUtil.loadTrueResult(gson.toJson(CreditInfoDto.builder()
|
.balance(balanceAmount)
|
.exchangeMoney(money)
|
.exchangingCredits(exchaningCredits)
|
.build()));
|
} catch (Exception e) {
|
logger.error("获取积分信息失败", e);
|
return JsonUtil.loadFalseResult("获取积分信息失败");
|
}
|
}
|
|
/**
|
* 获取支付宝绑定信息
|
*/
|
@RequestMapping("/getAlipayBinding")
|
public String getAlipayBinding(HttpSession session) {
|
Long uid = getUserId(session);
|
if (uid == null) {
|
return getLoginLinkContent();
|
}
|
|
List<UserAlipayBinding> alipayBindings = userAlipayBindingService.getBindingsByUid(uid);
|
if (alipayBindings == null || alipayBindings.isEmpty()) {
|
return JsonUtil.loadFalseResult("用户未绑定支付宝账户");
|
}
|
try {
|
return JsonUtil.loadTrueResult(alipayBindings.get(0));
|
} catch (Exception e) {
|
logger.error("获取支付宝绑定信息失败", e);
|
return JsonUtil.loadFalseResult("获取支付宝绑定信息失败");
|
}
|
}
|
|
/**
|
* 修改支付宝绑定信息
|
*/
|
@PostMapping("/updateAlipayBinding")
|
public String updateAlipayBinding(String alipayName, String alipayAccount, HttpSession session) {
|
Long userId = getUserId(session);
|
if (userId == null) {
|
return getLoginLinkContent();
|
}
|
|
List<UserAlipayBinding> alipayBindings = userAlipayBindingService.getBindingsByUid(userId);
|
if (alipayBindings != null && !alipayBindings.isEmpty()) {
|
userAlipayBindingService.updateBinding(UserAlipayBinding.builder()
|
.id(alipayBindings.get(0).getId())
|
.alipayName(alipayName)
|
.alipayAccount(alipayAccount)
|
.updateTime(new Date())
|
.build());
|
} else {
|
userAlipayBindingService.addBinding(UserAlipayBinding.builder()
|
.uid(userId)
|
.alipayName(alipayName)
|
.alipayAccount(alipayAccount)
|
.createTime(new Date())
|
.build());
|
}
|
return JsonUtil.loadTrueResult("修改成功");
|
|
}
|
|
/**
|
* 获取积分记录接口(可分页)
|
*/
|
@RequestMapping("/records")
|
public String getCreditRecords(@RequestParam(defaultValue = "1") int page,
|
@RequestParam(defaultValue = "10") int pageSize,
|
HttpSession session) {
|
Long userId = getUserId(session);
|
if (userId == null) {
|
return getLoginLinkContent();
|
}
|
UserCreditRecordMapper.DaoQuery query = UserCreditRecordMapper.DaoQuery.builder()
|
.uid(userId)
|
.start((long) (page - 1) * pageSize)
|
.count(pageSize)
|
.build();
|
List<UserCreditRecord> recordList = userCreditRecordService.listCreditRecords(query);
|
long count = userCreditRecordService.countCreditRecords(query);
|
JSONObject root = new JSONObject();
|
root.put("list", gson.toJson(recordList));
|
root.put("count", count);
|
return JsonUtil.loadTrueResult(root);
|
}
|
|
/**
|
* 积分兑换接口
|
*/
|
@PostMapping("/exchange")
|
public String exchangeCredits(@RequestParam int credits, HttpSession session) {
|
Long userId = getUserId(session);
|
if (userId == null) {
|
return getLoginLinkContent();
|
}
|
|
try {
|
userCreditExchangeManager.exchangeCredit(CreditExchangeRecord.builder()
|
.consumedCredits(credits)
|
.uid(userId)
|
.exchangeType(CreditExchangeRecord.ExchangeType.FUND_EXCHANGE)
|
.createTime(new Date())
|
.build());
|
return JsonUtil.loadTrueResult("兑换成功");
|
} catch (UserCreditExchangeException e) {
|
logger.error("积分兑换失败", e);
|
return JsonUtil.loadFalseResult(e.getMessage());
|
} catch (Exception e) {
|
logger.error("积分兑换失败", e);
|
return JsonUtil.loadFalseResult("兑换失败,请稍后重试");
|
}
|
}
|
|
/**
|
* 积分兑换接口(可分页)
|
*/
|
@PostMapping("/exchange_records")
|
public String exchangeCredits(
|
@RequestParam(defaultValue = "1") int page,
|
@RequestParam(defaultValue = "10") int pageSize,
|
HttpSession session) {
|
Long userId = getUserId(session);
|
if (userId == null) {
|
return getLoginLinkContent();
|
}
|
CreditExchangeRecordMapper.DaoQuery query = CreditExchangeRecordMapper.DaoQuery.builder()
|
.uid(userId)
|
.start((long) (page - 1) * pageSize)
|
.count(pageSize)
|
.build();
|
List<CreditExchangeRecord> recordList = userCreditExchangeRecordService.listExchangeRecords(query);
|
long count = userCreditExchangeRecordService.countExchangeRecords(query);
|
JSONObject root = new JSONObject();
|
root.put("list", gson.toJson(recordList));
|
root.put("count", count);
|
return JsonUtil.loadTrueResult(root);
|
}
|
}
|