package com.yami.shop.api.controller;
|
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.util.StrUtil;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.yami.shop.bean.vo.DeviceIncomeVO;
|
import com.yami.shop.bean.model.*;
|
import com.yami.shop.common.constants.DictConstant;
|
import com.yami.shop.common.response.ServerResponseEntity;
|
import com.yami.shop.common.util.DateUtils;
|
import com.yami.shop.service.*;
|
import org.apache.commons.lang3.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
@RestController
|
@RequestMapping("/tests")
|
public class TestsController {
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
@Autowired
|
private CdnTeamRelationService cdnTeamRelationService;
|
|
@Autowired
|
private AgentLevelService agentLevelService;
|
|
@Autowired
|
private CdnFlowService cdnFlowService;
|
|
@Autowired
|
private CdnUserWalletService cdnUserWalletService;
|
|
@Autowired
|
private CdnConfigService cdnConfigService;
|
|
@Autowired
|
private DeviceService deviceService;
|
|
@Autowired
|
private UserService userService;
|
|
@Autowired
|
private MarsAdverIncomeService marsAdverIncomeService;
|
|
@Autowired
|
private DeviceProfitLogService deviceProfitLogService;
|
|
/**
|
* 用户等级 判定算法(手机卡)
|
*/
|
@GetMapping("/cancelUserLevel")
|
public ServerResponseEntity<?> cancelUserLevel() {
|
Date now = new Date();
|
logger.info("用户等级调整开始。。。"+ DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
|
List<CdnTeamRelation> cdnTeamRelationList = cdnTeamRelationService.list(new LambdaQueryWrapper<CdnTeamRelation>());
|
List<AgentLevel> agentLevelList = agentLevelService.getCdnAgentLevelList();
|
List<CdnTeamRelation> lists = new ArrayList<>();
|
for (CdnTeamRelation relation : cdnTeamRelationList) {
|
for (AgentLevel agentLevel : agentLevelList) {
|
if(relation.getLevel() >= agentLevel.getId()){//等级只能升 不能降
|
continue;
|
}else{
|
//直推人
|
Integer num = 0;//直推会员人数
|
Integer total = 0;//激活账号数
|
List<CdnTeamRelation> cdnTeamRelations = cdnTeamRelationService.getTeamRelationListSort(relation.getUserId());
|
if(cdnTeamRelations.size() >= agentLevel.getInvite()){//判断直推人条件
|
for (CdnTeamRelation c:cdnTeamRelations){//直推会员人数
|
if(c.getLevel() == (agentLevel.getId()-1)){
|
num++;
|
}
|
}
|
String pids = null;
|
if (StringUtils.isNotBlank(relation.getPids())) {
|
pids = relation.getPids() + relation.getUserId() + ",";
|
} else {
|
pids = "," + relation.getUserId() + ",";
|
}
|
CdnTeamRelation cdns = new CdnTeamRelation();
|
cdns.setPids(pids);
|
List<CdnTeamRelation> cdnTeamList = cdnTeamRelationService.getDividendList(cdns);
|
for (CdnTeamRelation c:cdnTeamList){
|
User d = new User();
|
d.setUserId(c.getUserId());
|
d.setActiveStatus("1");
|
total += userService.getctiveStatusCount(d);
|
}
|
if(num >= agentLevel.getMember() && total >= agentLevel.getDeviceActive()){
|
relation.setLevel(agentLevel.getId());
|
if("V7".equals(agentLevel.getName())){
|
User user = userService.getUserByUserId(relation.getUserId());
|
if(user != null && user.getActiveTime() != null){
|
if(DateUtils.flagIn30DayDate(new SimpleDateFormat("yyyy-MM-dd").format(user.getActiveTime()))){
|
user.setActiveStatus("1");
|
userService.updateById(user);
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
lists.add(relation);
|
}
|
cdnTeamRelationService.updateBatchById(lists);
|
return ServerResponseEntity.success();
|
}
|
|
|
/**
|
* 帮扶、分享 、平级(手机卡)
|
*/
|
@GetMapping("/cancelAssist")
|
public ServerResponseEntity<?> cancelAssist() {
|
Date now = new Date();
|
logger.info("团队奖励计算开始。。。"+DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
|
//先计算合伙人分红 查询
|
BigDecimal companyPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("company_point")));//公司沉淀
|
BigDecimal teamPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("team_point")));//团队奖比例
|
BigDecimal partnerPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("partner_point")));//合伙人
|
BigDecimal directPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("share")));//直推奖
|
BigDecimal jcPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("team_range_point")));//团队中的级差比例
|
BigDecimal incomeCount = marsAdverIncomeService.selectAllIncome(null);
|
if(incomeCount != null){
|
//合伙人总数
|
List<User> userList = userService.list(new LambdaQueryWrapper<User>().eq(User::getIsPartner,1));
|
for (User user:userList){
|
//全天的总收益 *(1-公司沉淀0.3) * 50%(团队奖) * 8% / 合伙人(查询)的总数
|
BigDecimal money = incomeCount.multiply(BigDecimal.valueOf(1).subtract(companyPoint)).multiply(teamPoint).multiply(partnerPoint).divide(BigDecimal.valueOf(userList.size()),2, RoundingMode.HALF_UP);
|
CdnFlow cdnFlows = new CdnFlow();
|
CdnUserWallet cdnUserWallets = cdnUserWalletService.getOne(Wrappers.lambdaQuery(CdnUserWallet.class)
|
.eq(CdnUserWallet::getUserId, user.getUserId())
|
.eq(CdnUserWallet::getWalletId, DictConstant.WALLET_ID_10));
|
if(cdnUserWallets.getMoney().compareTo(new BigDecimal(0)) == 0){
|
continue;
|
}
|
cdnFlows.setUserId(user.getUserId());
|
cdnFlows.setUserIds(user.getUserId());
|
if(cdnUserWallets.getMoney().compareTo(money) < 0){
|
cdnFlows.setMemo("合伙人("+user.getUserId()+")释放"+cdnUserWallets.getMoney());
|
cdnFlows.setMoney(cdnUserWallets.getMoney());
|
}else{
|
cdnFlows.setMemo("合伙人("+user.getUserId()+")释放"+money);
|
cdnFlows.setMoney(money);
|
}
|
cdnFlows.setOldmoney(cdnUserWallets != null ? cdnUserWallets.getMoney() : BigDecimal.ZERO);
|
cdnFlows.setFlownameId(DictConstant.FLOWNAME_ID_30);
|
cdnFlows.setCreateTime(new Date());
|
cdnFlows.setWalletId(DictConstant.WALLET_ID_10);
|
cdnFlows.setDate(new Date());
|
cdnFlowService.save(cdnFlows);
|
cdnUserWallets.setMoney(cdnFlows.getOldmoney().subtract(cdnFlows.getMoney()));
|
cdnUserWallets.setCreateTime(new Date());
|
cdnUserWalletService.updateById(cdnUserWallets);
|
}
|
}
|
List<CdnTeamRelation> cdnTeamRelationLists = cdnTeamRelationService.getTeamRelationListSort(null);
|
Map<Integer,BigDecimal> map =new HashMap<>();
|
for (CdnTeamRelation cdnTeamRelation :cdnTeamRelationLists){
|
//查询个人的幸运池 没有直接无收益
|
CdnUserWallet cdnUserWallet = cdnUserWalletService.getOne(Wrappers.lambdaQuery(CdnUserWallet.class)
|
.eq(CdnUserWallet::getUserId,cdnTeamRelation.getUserId())
|
.eq(CdnUserWallet::getWalletId,10));
|
if(cdnUserWallet.getMoney().compareTo(BigDecimal.ZERO) == 0){
|
continue;
|
}
|
BigDecimal income = marsAdverIncomeService.selectAllIncome(cdnTeamRelation.getUserId());
|
if(income == null || income.compareTo(BigDecimal.ZERO) == 0){//没有业绩的
|
continue;
|
}
|
CdnTeamRelation pCdnTeamRelation = cdnTeamRelationService.geteamRelationByUserId(cdnTeamRelation.getPid());
|
if(pCdnTeamRelation != null){
|
//直推奖 个人业绩 * (1-公司沉淀0.3) * 50%(团队奖) * 10%
|
BigDecimal ztmoney = income.multiply(BigDecimal.valueOf(1).subtract(companyPoint)).multiply(teamPoint).multiply(directPoint);
|
if(ztmoney.compareTo(BigDecimal.valueOf(0.01)) >= 0) {
|
setFlowLogs(pCdnTeamRelation.getUserId(), cdnTeamRelation.getUserId(), ztmoney, DictConstant.WALLET_ID_10, "直推奖励", DictConstant.FLOWNAME_ID_30);
|
}
|
//循环所有上级用户 计算平级奖 直到1毛为止
|
if(cdnTeamRelation.getPids() != null) {
|
String[] pidsArrays = Arrays.copyOfRange(cdnTeamRelation.getPids().split(StrUtil.COMMA), 1, cdnTeamRelation.getPids().split(StrUtil.COMMA).length);
|
AgentLevel agentLevel = agentLevelService.getById(cdnTeamRelation.getLevel());
|
//加速释放比例
|
Integer id = 0;
|
BigDecimal releases= BigDecimal.ZERO;
|
if(agentLevel != null){
|
releases= agentLevel.getReleases();
|
}
|
Integer ids = 0;
|
// if(agentLevel != null){
|
// ids= agentLevel.getId();
|
// }
|
Integer parallels = 0;
|
// 将各个会员等级的收益全部汇总 个人今日收益 * (1-公司沉淀0.3) * 50%(团队奖) * 10%
|
BigDecimal countPoint = income.multiply(BigDecimal.valueOf(1).subtract(companyPoint)).multiply(teamPoint);
|
BigDecimal djPoint = countPoint.multiply(BigDecimal.valueOf(1).subtract(jcPoint));
|
BigDecimal pjCount = countPoint;
|
for (int i = pidsArrays.length - 1; i >= 0; i--) {
|
BigDecimal childMoeny = BigDecimal.ZERO;
|
CdnTeamRelation cdnTeamRelationPids = cdnTeamRelationService.getOne(new LambdaQueryWrapper<CdnTeamRelation>().eq(CdnTeamRelation::getUserId, pidsArrays[i]));
|
AgentLevel agentLevels = agentLevelService.getById(cdnTeamRelationPids.getLevel());
|
if(agentLevels != null){
|
if(map.containsKey(agentLevels.getId())){
|
BigDecimal value = map.get(agentLevels.getId()).add(djPoint);
|
map.put(agentLevels.getId(),value);
|
}else{
|
BigDecimal value = djPoint;
|
map.put(agentLevels.getId(),value);
|
}
|
//获取收益
|
if(id < agentLevels.getId()) {
|
childMoeny = countPoint.multiply(jcPoint).multiply((agentLevels.getReleases().subtract(releases)));
|
if(childMoeny.compareTo(BigDecimal.valueOf(0.01)) >= 0){
|
setFlowLogs(cdnTeamRelationPids.getUserId(),cdnTeamRelation.getUserId(),childMoeny,DictConstant.WALLET_ID_10,"团队奖(级差)",DictConstant.FLOWNAME_ID_30);
|
}
|
releases = agentLevels.getReleases();
|
id = agentLevels.getId();
|
}
|
|
if (cdnTeamRelationPids.getLevel() == ids && cdnTeamRelationPids.getLevel() > parallels && ids != 0) {
|
//平级奖 本用户收益的10% 贡献给上级
|
BigDecimal pjmoney = pjCount.multiply(agentLevels.getParallels());
|
if (pjmoney.compareTo(BigDecimal.valueOf(0.01)) >= 0) {
|
setFlowLogs(cdnTeamRelationPids.getUserId(), cdnTeamRelation.getUserId(), pjmoney, DictConstant.WALLET_ID_10, "管理津贴", DictConstant.FLOWNAME_ID_30);
|
}
|
ids = cdnTeamRelationPids.getLevel();
|
parallels = ids;
|
pjCount = pjmoney;
|
} else {
|
ids = cdnTeamRelationPids.getLevel();
|
}
|
}else{
|
ids = 0;
|
}
|
}
|
}
|
}
|
}
|
//计算团队奖的业绩部分 例如:V1总业绩(是否带团队的收益)
|
List<AgentLevel> list = agentLevelService.list();
|
for (AgentLevel agentLevel : list) {
|
//根据会员等级计算今日总业绩
|
BigDecimal count = cdnTeamRelationService.selectTMell(agentLevel.getId());
|
if(count == null || count.compareTo(BigDecimal.ZERO) == 0){//没有业绩的
|
continue;
|
}
|
//循环所有会员(V1会员)
|
List<CdnTeamRelation> cdnTeamRelations = cdnTeamRelationService.getTeamRelationListByAgentLevel(agentLevel.getId());
|
for (CdnTeamRelation c :cdnTeamRelations){
|
//查询个人的幸运池 没有直接无收益
|
CdnUserWallet cdnUserWallet = cdnUserWalletService.getOne(Wrappers.lambdaQuery(CdnUserWallet.class)
|
.eq(CdnUserWallet::getUserId,c.getUserId())
|
.eq(CdnUserWallet::getWalletId,10));
|
if(cdnUserWallet.getMoney().compareTo(BigDecimal.ZERO) == 0){
|
continue;
|
}
|
if(c.getTeamTodayMobile() > 0){
|
BigDecimal pjmoney = map.get(c.getLevel()).multiply(BigDecimal.valueOf(c.getTeamTodayMobile())).divide(count,BigDecimal.ROUND_CEILING);
|
if (pjmoney.compareTo(BigDecimal.valueOf(0.01)) >= 0) {
|
setFlowLogs(c.getUserId(), null, pjmoney, DictConstant.WALLET_ID_10, "团队奖(业绩部分)", DictConstant.FLOWNAME_ID_30);
|
}
|
}
|
}
|
}
|
return ServerResponseEntity.success();
|
}
|
|
/**
|
*
|
* @param userId 受益人
|
* @param userIds 贡献人
|
* @param money 收益金额
|
* @param walletId 钱包类型
|
* @param desc 收益描述
|
* @param flownameId 流水类型
|
*/
|
private void setFlowLogs(String userId,String userIds,BigDecimal money,Integer walletId,String desc,Integer flownameId){
|
CdnFlow cdnFlow = new CdnFlow();
|
cdnFlow.setUserIds(userIds);
|
cdnFlow.setUserId(userId);
|
cdnFlow.setFlownameId(flownameId);
|
cdnFlow.setWalletId(walletId);
|
//钱包更改
|
CdnUserWallet cdnUserWallet = cdnUserWalletService.getOne(Wrappers.lambdaQuery(CdnUserWallet.class)
|
.eq(CdnUserWallet::getUserId,userId)
|
.eq(CdnUserWallet::getWalletId,walletId));
|
if(cdnUserWallet.getMoney().compareTo(BigDecimal.ZERO) == 0){
|
return;
|
}
|
if(money.compareTo(BigDecimal.ZERO) <= 0){//绿积分为0 已经释放完毕
|
return;
|
}else{
|
if(money.compareTo(cdnUserWallet.getMoney()) >= 0){//分润超过剩余绿积分 释放最后的绿积分 并将SN改为结束
|
cdnFlow.setMoney(cdnUserWallet.getMoney());
|
}else{
|
cdnFlow.setMoney(money);
|
}
|
}
|
cdnFlow.setMemo(desc+":释放用户"+userIds);
|
cdnFlow.setCreateTime(new Date());
|
cdnFlow.setDate(new Date());
|
cdnFlow.setOldmoney(cdnUserWallet.getMoney());
|
cdnFlowService.save(cdnFlow);
|
cdnUserWallet.setUserId(userId);
|
cdnUserWallet.setWalletId(walletId);
|
cdnUserWallet.setMoney(cdnUserWallet.getMoney().subtract(cdnFlow.getMoney()));
|
cdnUserWallet.setUpdateTime(new Date());
|
cdnUserWalletService.updateById(cdnUserWallet);
|
}
|
|
/**
|
* 个人抽成
|
* @return
|
*/
|
@GetMapping("/cancelGeneral")
|
public ServerResponseEntity<?> cancelGeneral() {
|
Date now = new Date();
|
logger.info("个人分成计算开始。。。"+DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
|
BigDecimal companyPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("company_point")));//公司沉淀
|
BigDecimal mellPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("mell_point")));//个人比例
|
List<CdnTeamRelation> cdnTeamRelationLists = cdnTeamRelationService.getTeamRelationListSort(null);
|
for (CdnTeamRelation cdnTeamRelation : cdnTeamRelationLists) {
|
//查询个人的幸运池 没有直接无收益
|
CdnUserWallet cdnUserWallet = cdnUserWalletService.getOne(Wrappers.lambdaQuery(CdnUserWallet.class)
|
.eq(CdnUserWallet::getUserId,cdnTeamRelation.getUserId())
|
.eq(CdnUserWallet::getWalletId,DictConstant.WALLET_ID_10));
|
if(cdnUserWallet.getMoney().compareTo(BigDecimal.ZERO) == 0){
|
continue;
|
}
|
BigDecimal income = marsAdverIncomeService.selectAllIncome(cdnTeamRelation.getUserId());
|
if(income != null && income.compareTo(BigDecimal.ZERO) > 0){
|
setFlowLogs(cdnTeamRelation.getUserId(), cdnTeamRelation.getUserId(), income.multiply(BigDecimal.valueOf(1).subtract(companyPoint)).multiply(mellPoint), DictConstant.WALLET_ID_10, "个人分润", DictConstant.FLOWNAME_ID_30);
|
}
|
}
|
return ServerResponseEntity.success();
|
}
|
|
|
@PostMapping("/insertIncome")
|
public ServerResponseEntity<?> cancelUserLevel(@RequestBody DeviceIncomeVO deviceVO) {
|
Date now = new Date();
|
logger.info("插入设备收益。。。"+DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
|
boolean b = deviceProfitLogService.saveBatch(deviceVO.getDeviceProfitLogs());
|
if (b){
|
logger.info("插入设备收益结束。。。"+DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
|
return ServerResponseEntity.success();
|
}else{
|
return ServerResponseEntity.showFailMsg("插入失败");
|
}
|
}
|
|
}
|