package com.yeshi.fanli.service.impl.user;
|
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
import org.yeshi.utils.DateUtil;
|
|
import com.yeshi.fanli.dao.mybatis.user.UserInfoExtraMapper;
|
import com.yeshi.fanli.dao.mybatis.user.UserRankRecordMapper;
|
import com.yeshi.fanli.entity.bus.user.UserInfo;
|
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
|
import com.yeshi.fanli.entity.bus.user.UserRank;
|
import com.yeshi.fanli.entity.bus.user.UserRankRecord;
|
import com.yeshi.fanli.exception.ThreeSaleException;
|
import com.yeshi.fanli.exception.user.UserInfoExtraException;
|
import com.yeshi.fanli.log.LogHelper;
|
import com.yeshi.fanli.service.inter.config.ConfigService;
|
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
|
import com.yeshi.fanli.service.inter.order.CommonOrderCountService;
|
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
|
import com.yeshi.fanli.service.inter.user.UserInfoService;
|
import com.yeshi.fanli.service.inter.user.UserRankService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.account.UserUtil;
|
import com.yeshi.fanli.vo.user.UserInfoExtraVO;
|
|
|
@Service
|
public class UserInfoExtraServiceImpl implements UserInfoExtraService {
|
|
@Resource
|
private UserInfoExtraMapper userInfoExtraMapper;
|
|
@Resource
|
private UserRankRecordMapper userRankRecordMapper;
|
|
@Resource
|
private UserRankService userRankService;
|
|
@Resource
|
private CommonOrderCountService commonOrderCountService;
|
|
@Resource
|
private ConfigService configService;
|
|
@Resource
|
private ThreeSaleSerivce threeSaleSerivce;
|
|
@Resource
|
private UserInfoService userInfoService;
|
|
|
@Override
|
public UserInfoExtraVO getRankInfo(Long uid) throws UserInfoExtraException, Exception {
|
|
UserInfoExtraVO extraVO = userInfoExtraMapper.getInfoExtraVOByUid(uid);
|
if (extraVO == null) {
|
throw new UserInfoExtraException(1, "用户附加信息不存在");
|
}
|
|
// 等级对照表
|
String contrast = configService.get("user_rank_contrast");
|
extraVO.setRankContrast(contrast);
|
// 特别提醒
|
String rankTip = configService.get("user_rank_tip");
|
extraVO.setRankTip(rankTip);
|
|
Integer rankOrderNum = extraVO.getRankOrderNum();
|
|
UserRank userRank = extraVO.getUserRank();
|
// 青铜等级不返回恭喜语句 注:青铜等级id 必须为1
|
if (userRank != null && userRank.getId() != null && userRank.getId() > 1) {
|
|
String rankMsg = null;
|
Integer rankSource = extraVO.getRankSource();
|
switch (rankSource) {
|
case Constant.TYPE_REBATE:
|
rankMsg = "返利订单";
|
break;
|
case Constant.TYPE_SHAER:
|
rankMsg = "分享订单";
|
break;
|
case Constant.TYPE_INVITE:
|
rankMsg = "邀请订单";
|
break;
|
default:
|
break;
|
}
|
|
List<Object> listMsg = new ArrayList<Object>();
|
|
if (rankMsg != null) {
|
String color_red = "#E5005C";
|
String color_black = "#333333";
|
|
Map<String, String> map1 = new HashMap<String, String>();
|
map1.put("content", "上月");
|
map1.put("color", color_black);
|
listMsg.add(map1);
|
|
Map<String, String> map2 = new HashMap<String, String>();
|
map2.put("content", rankMsg);
|
map2.put("color", color_red);
|
listMsg.add(map2);
|
|
Map<String, String> map3 = new HashMap<String, String>();
|
map3.put("content", "达到了");
|
map3.put("color", color_black);
|
listMsg.add(map3);
|
|
Map<String, String> map4 = new HashMap<String, String>();
|
map4.put("content", rankOrderNum + "");
|
map4.put("color", color_red);
|
listMsg.add(map4);
|
|
Map<String, String> map5 = new HashMap<String, String>();
|
map5.put("content", "单");
|
map5.put("color", color_black);
|
listMsg.add(map5);
|
extraVO.setGalaMsg(listMsg);
|
}
|
}
|
|
return extraVO;
|
}
|
|
@Override
|
public UserInfoExtra updateUserRankByUid(Long uid) throws UserInfoExtraException{
|
|
boolean isupdateRank = true;
|
|
UserInfoExtra userInfoExtra = userInfoExtraMapper.getInfoExtraByUid(uid);
|
if (userInfoExtra != null && userInfoExtra.getId() != null) {
|
// 判断当月是否已更新
|
Date rankUpdateTime = userInfoExtra.getRankUpdateTime();
|
if (rankUpdateTime != null) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
|
if (sdf.format(rankUpdateTime).equals(sdf.format(new Date()))) {
|
isupdateRank = false;
|
}
|
}
|
} else {
|
userInfoExtra = new UserInfoExtra();
|
userInfoExtra.setUserInfo(new UserInfo(uid));
|
}
|
|
// 更新等级
|
if (isupdateRank) {
|
updateRank(userInfoExtra);
|
}
|
|
return userInfoExtra;
|
}
|
|
|
@Override
|
public UserInfoExtra updateRank(UserInfoExtra userInfoExtra) throws UserInfoExtraException {
|
UserInfo userInfo = userInfoExtra.getUserInfo();
|
if (userInfo == null ) {
|
throw new UserInfoExtraException(1, "用户不存在");
|
}
|
|
Long uid = userInfo.getId();
|
if (uid == null ) {
|
throw new UserInfoExtraException(1, "用户ID不存在");
|
}
|
|
List<UserRank> listRank = userRankService.getAllRank();
|
if (listRank == null || listRank.size() == 0) {
|
throw new UserInfoExtraException(1, "系统等级不存在");
|
}
|
|
int selfOrderNum = 0;
|
int sharedOrderNum = 0;
|
int inviteOrderNum = 0;
|
Map<String, Object> map = commonOrderCountService.lastMonthSettleOrderNumber(uid);
|
if (map != null) {
|
// 返利订单
|
if (map.get("totalSelf") != null) {
|
selfOrderNum = Integer.parseInt(map.get("totalSelf").toString());
|
}
|
|
// 分享订单
|
if (map.get("totalShared") != null) {
|
sharedOrderNum = Integer.parseInt(map.get("totalShared").toString());
|
}
|
|
// 邀请订单
|
if (map.get("totalInvite") != null) {
|
inviteOrderNum = Integer.parseInt(map.get("totalInvite").toString());
|
}
|
}
|
|
UserRank rank = null;
|
for (UserRank userRank : listRank) {
|
// 邀请满足
|
Integer inviteNum = userRank.getInviteNum();
|
if (inviteOrderNum >= inviteNum) {
|
rank = userRank;
|
userInfoExtra.setRankOrderNum(inviteOrderNum);
|
userInfoExtra.setRankSource(Constant.TYPE_INVITE);
|
}
|
|
// 分享满足
|
int shareNum = userRank.getShareNum();
|
if (sharedOrderNum >= shareNum) {
|
rank = userRank;
|
userInfoExtra.setRankOrderNum(sharedOrderNum);
|
userInfoExtra.setRankSource(Constant.TYPE_SHAER);
|
}
|
|
// 返利满足
|
int directNum = userRank.getRebateNum();
|
if (selfOrderNum >= directNum) {
|
rank = userRank;
|
userInfoExtra.setRankOrderNum(selfOrderNum);
|
userInfoExtra.setRankSource(Constant.TYPE_REBATE);
|
}
|
|
|
if (rank != null) {
|
userInfoExtra.setUserRank(rank);
|
} else {
|
rank = userRank;
|
// 默认最低等级:青铜
|
userInfoExtra.setUserRank(rank);
|
userInfoExtra.setRankOrderNum(selfOrderNum);
|
userInfoExtra.setRankSource(Constant.TYPE_REBATE);
|
break;
|
}
|
}
|
|
// 等级更新时间 当月1号
|
Calendar calendar1=Calendar.getInstance();
|
calendar1.set(Calendar.DAY_OF_MONTH, 1);
|
userInfoExtra.setRankUpdateTime(calendar1.getTime());
|
|
// 保存信息并返回
|
saveUserInfoExtra(userInfoExtra);
|
|
UserRankRecord userRankRecord = new UserRankRecord();
|
userRankRecord.setUid(uid);
|
userRankRecord.setRankId(rank.getId());
|
userRankRecord.setCreateTime(new Date());
|
userRankRecordMapper.insertSelective(userRankRecord);
|
|
return userInfoExtra;
|
}
|
|
@Override
|
public UserInfoExtra saveUserInfoExtra(UserInfoExtra userInfoExtra) throws UserInfoExtraException {
|
|
if (userInfoExtra == null) {
|
throw new UserInfoExtraException(1, "附加信息不能为空");
|
}
|
|
Long extraId = userInfoExtra.getId();
|
if (extraId != null) {
|
userInfoExtra.setUpdateTime(new Date());
|
userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra);
|
} else {
|
UserInfo userInfo = userInfoExtra.getUserInfo();
|
if (userInfo == null || userInfo.getId() == null) {
|
throw new UserInfoExtraException(1, "用户不存在");
|
}
|
|
Long uid = userInfo.getId();
|
UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid);
|
if (extra != null && extra.getId() != null) {
|
userInfoExtra.setId(extra.getId());
|
userInfoExtra.setUpdateTime(new Date());
|
userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra);
|
} else {
|
userInfoExtra.setCreateTime(new Date());
|
userInfoExtra.setUpdateTime(new Date());
|
userInfoExtraMapper.insertSelective(userInfoExtra);
|
}
|
|
}
|
return userInfoExtra;
|
}
|
|
@Override
|
public String activateInviteCode(Long uid, String inviteCode) throws UserInfoExtraException{
|
if (uid == null || inviteCode == null) {
|
throw new UserInfoExtraException(1, "用户id、邀请码不能为空");
|
}
|
|
// 被邀请人信息
|
UserInfo invitee = userInfoService.selectByPKey(uid);
|
if (invitee == null) {
|
throw new UserInfoExtraException(1, "用户不存在");
|
}
|
|
// 邀请人信息
|
UserInfo inviter = userInfoService.getInfoByPhoneOrInviteCode(inviteCode, inviteCode);
|
if (inviter == null) {
|
throw new UserInfoExtraException(1, "请输入有效的邀请码");
|
}
|
|
// 绑定关系
|
try {
|
threeSaleSerivce.bindRelationshipByInviteCode(invitee, inviter);
|
} catch (ThreeSaleException e) {
|
try {
|
LogHelper.errorDetailInfo(e);
|
} catch (Exception e1) {
|
e1.printStackTrace();
|
}
|
throw new UserInfoExtraException(1, "激活失败");
|
}
|
|
|
// 邀请码有效、生成邀请码
|
String code = UserUtil.getInviteCode(uid);
|
if (code == null || code.trim().length() == 0) {
|
throw new UserInfoExtraException(1, "激活码生成失败");
|
}
|
|
UserInfoExtra userInfoExtra = new UserInfoExtra();
|
userInfoExtra.setUserInfo(invitee);
|
userInfoExtra.setInviteCode(code);
|
|
// 保存额外信息
|
saveUserInfoExtra(userInfoExtra);
|
|
|
Long id = userInfoExtra.getId();
|
if (id == null) {
|
throw new UserInfoExtraException(1, "激活码生成失败");
|
}
|
|
return code;
|
}
|
|
@Override
|
public UserInfoExtra getUserInfoExtra(Long uid) {
|
return userInfoExtraMapper.getInfoExtraByUid(uid);
|
}
|
|
@Override
|
public UserInfoExtraVO getInfoExtraVOByUid(Long uid) {
|
return userInfoExtraMapper.getInfoExtraVOByUid(uid);
|
}
|
|
|
@Override
|
public String getUserInviteCode(Long uid) throws UserInfoExtraException {
|
|
UserInfoExtra userInfoExtra = userInfoExtraMapper.getInfoExtraByUid(uid);
|
if (userInfoExtra == null) {
|
userInfoExtra = new UserInfoExtra();
|
}
|
|
// 邀请码
|
String inviteCode = userInfoExtra.getInviteCode();
|
|
if (inviteCode == null || inviteCode.trim().length() == 0) {
|
// 判断用户
|
int relationshipNum = threeSaleSerivce.getSuccessRelationshipNum(uid);
|
|
if (relationshipNum > 0) {
|
// 邀请码有效、生成邀请码
|
inviteCode = UserUtil.getInviteCode(uid);
|
if (inviteCode == null || inviteCode.trim().length() == 0) {
|
throw new UserInfoExtraException(1, "激活码生成失败");
|
}
|
|
// 保存邀请码
|
userInfoExtra.setUserInfo(new UserInfo(uid));
|
userInfoExtra.setInviteCode(inviteCode);
|
// 保存附加信息
|
saveUserInfoExtra(userInfoExtra);
|
}
|
}
|
|
return inviteCode;
|
}
|
|
}
|