package com.ks.app.service.manager; 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.DO.GoldCornRecordCountMap; import com.ks.goldcorn.pojo.Query.GoldCornRecordQuery; import com.ks.goldcorn.service.remote.GoldCornBalanceService; import com.ks.goldcorn.service.remote.GoldCornGetSourceService; import com.ks.goldcorn.service.remote.GoldCornRecordService; import com.ks.goldcorn.service.remote.GoldCornTradeService; import com.ks.app.entity.vip.OrderRecord; import com.ks.app.entity.vip.OrderType; import com.ks.app.exception.goldcorn.GoldCornException; import com.ks.app.service.inter.config.SystemConfigService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.stereotype.Component; import org.yeshi.utils.StringUtil; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author hxh * @title: GoldCornManager * @description: TODO * @date 2021/11/17 14:54 */ @Component public class GoldCornManager { @Resource private SystemConfigService systemConfigService; // @Reference(version = "1.0", check = false) private GoldCornTradeService goldCornTradeService; // @Reference(version = "1.0", check = false) private GoldCornRecordService goldCornRecordService; // @Reference(version = "1.0", check = false) private GoldCornBalanceService goldCornBalanceService; // @Reference(version = "1.0", check = false) private GoldCornGetSourceService goldCornGetSourceService; public void init(Long uid) throws Exception { goldCornBalanceService.init(getAppCode(), getUid(uid)); } /** * 获取金币系统的用户ID * * @param uid * @return * @throws Exception */ public String getUid(Long uid) throws Exception { String prefix = "";//systemConfigService.getValueCache(SystemEnum.location,"thirdUidPrefix"); if (StringUtil.isNullOrEmpty(prefix)) { throw new Exception("用户ID前缀获取出错"); } return prefix + uid; } public String getAppCode() { String appCode = "XXXX"; //TODO 获取appcode return appCode; } public long getBalance(Long uid) { if (uid == null) { return 0; } 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"; } consumeGoldCorn(appCode, tuid, sourceCode, goldCornCount, title, desc); } /** * 添加金币 * * @param uid * @param source * @param goldCorn * @param title * @param desc * @throws GoldUserException * @throws GoldAppException * @throws GoldTradeException * @throws Exception */ // public void addGoldCorn(String uid, CodeCornGetSourceType source, Integer goldCorn, String title, String desc) throws GoldUserException, GoldAppException, GoldTradeException, Exception { // addGoldCorn(getAppCode(), getUid(uid), source.name(), goldCorn, title, desc); // } // // public GoldCornGetSource getGoldCornGetSource(CodeCornGetSourceType source) throws GoldAppException { // return goldCornGetSourceService.selectByAppAndCode(getAppCode(), source.name()); // } /** * 返回影视豆 * * @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退款"; } addGoldCorn(appCode, tuid, sourceCode, record.getGoldCorn(), title, desc); } public List getRecordList(GoldCornRecordQuery query, Long uid) throws Exception { query.setAppCode(getAppCode()); query.setUid(getUid(uid)); return goldCornRecordService.listUserRecord(query); } public long getRecordCount(GoldCornRecordQuery query, Long uid) throws Exception { query.setAppCode(getAppCode()); query.setUid(getUid(uid)); return goldCornRecordService.countUserRecord(query); } 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); } public List listGetCornSource(int page, int pageSize) throws GoldAppException { return goldCornGetSourceService.listShow(getAppCode(), page, pageSize); } public List countRecordByGetSource(Long uid, List sourceCodes, Date minCreateTime, Date maxCreateTime) throws Exception { if (uid == null) { return new ArrayList<>(); } GoldCornRecordQuery query = new GoldCornRecordQuery(); query.setUid(getUid(uid)); query.setAppCode(getAppCode()); query.setSourceCodes(sourceCodes); query.setMinCreateTime(minCreateTime); query.setMaxCreateTime(maxCreateTime); query.setType(GoldCornRecord.TYPE_GET); return goldCornRecordService.countUserRecordBySource(query); } }