package com.yeshi.buwan.service.manager;
|
|
import com.ks.goldcorn.exception.GoldAppException;
|
import com.ks.goldcorn.exception.GoldTradeException;
|
import com.ks.goldcorn.exception.GoldUserException;
|
import com.ks.goldcorn.service.remote.GoldCornBalanceService;
|
import com.ks.goldcorn.service.remote.GoldCornTradeService;
|
import com.yeshi.buwan.domain.vip.OrderType;
|
import com.yeshi.buwan.domain.vip.OrderRecord;
|
import com.yeshi.buwan.exception.goldcorn.GoldCornException;
|
import com.yeshi.buwan.service.inter.system.SystemConfigService;
|
import com.yeshi.buwan.util.StringUtil;
|
import org.apache.dubbo.config.annotation.Reference;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
|
@Component
|
public class GoldCornManager {
|
|
|
@Resource
|
private SystemConfigService systemConfigService;
|
|
@Reference(version = "1.0", check = false)
|
private GoldCornTradeService goldCornTradeService;
|
|
@Reference(version = "1.0", check = false)
|
private GoldCornBalanceService goldCornBalanceService;
|
|
public void init(String uid) throws Exception {
|
goldCornBalanceService.init(getAppCode(), getUid(uid));
|
}
|
|
|
/**
|
* 获取金币系统的用户ID
|
*
|
* @param uid
|
* @return
|
* @throws Exception
|
*/
|
public String getUid(String uid) throws Exception {
|
String prefix = systemConfigService.getConfigValueByKeyCache("thirdUidPrefix");
|
if (StringUtil.isNullOrEmpty(prefix)) {
|
throw new Exception("用户ID前缀获取出错");
|
}
|
return prefix + uid;
|
}
|
|
public String getAppCode() throws GoldAppException {
|
String appCode = systemConfigService.getConfigValueByKeyCache("goldCornAppCode");
|
if (StringUtil.isNullOrEmpty(appCode)) {
|
throw new GoldAppException(GoldAppException.CODE_NOT_EXIST, "应用不存在");
|
}
|
return appCode;
|
}
|
|
public long getBalance(String uid) {
|
try {
|
Long count = goldCornBalanceService.getBalance(getAppCode(), getUid(uid));
|
if (count == null) {
|
init(uid);
|
}
|
return count == null ? 0 : count;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return 0;
|
}
|
|
|
/**
|
* 金币消耗
|
*
|
* @param record
|
* @param goldCornCount 金币数量
|
* @throws GoldCornException
|
* @throws Exception
|
*/
|
public void consumeGoldCorn(OrderRecord record, int goldCornCount) throws GoldCornException, Exception {
|
String tuid = getUid(record.getUid());
|
String appCode = getAppCode();
|
String sourceCode = "";
|
String title = "";
|
String desc = "";
|
if (record.getOrderType() == OrderType.vip) {
|
sourceCode = "buyVIP";
|
title = "购买VIP";
|
} else if (record.getOrderType() == OrderType.video) {
|
sourceCode = "buyVideo";
|
title = "购买单片";
|
}
|
consumeGoldCorn(appCode, tuid, sourceCode, goldCornCount, title, desc);
|
}
|
|
/**
|
* 返回影视豆
|
*
|
* @param record
|
* @throws GoldCornException
|
* @throws Exception
|
*/
|
public void drawbackGoldCorn(OrderRecord record) throws GoldCornException, Exception {
|
String tuid = getUid(record.getUid());
|
String appCode = getAppCode();
|
String sourceCode = "";
|
String title = "";
|
String desc = "";
|
if (record.getOrderType() == OrderType.vip) {
|
sourceCode = "buyVIP";
|
title = "购买VIP退款";
|
} else if (record.getOrderType() == OrderType.video) {
|
sourceCode = "buyVideo";
|
title = "购买单片退款";
|
}
|
addGoldCorn(appCode, tuid, sourceCode, record.getGoldCorn(), title, desc);
|
}
|
|
private void addGoldCorn(String appCode, String uid, String sourceCode, Integer goldCornCount, String title, String desc) throws GoldUserException, GoldAppException, GoldTradeException {
|
goldCornTradeService.addGoldCorn(appCode, uid, sourceCode, goldCornCount, title, desc);
|
}
|
|
|
private void consumeGoldCorn(String appCode, String uid, String sourceCode, Integer goldCornCount, String title, String desc) throws GoldUserException, GoldAppException, GoldTradeException {
|
goldCornTradeService.consumeGoldCorn(appCode, uid, sourceCode, goldCornCount, title, desc);
|
}
|
|
|
}
|