package com.yeshi.fanli.service.impl.tlj;
|
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.HashSet;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.TreeMap;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
|
import com.yeshi.fanli.dao.mybatis.tlj.UserTaoLiJinDetailMapper;
|
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinDetail;
|
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinDetail.TaoLiJinDetailTypeEnum;
|
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinOrigin;
|
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinOrigin.TaoLiJinOriginEnum;
|
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinDetailService;
|
import com.yeshi.fanli.util.TimeUtil;
|
import com.yeshi.fanli.vo.tlj.TaoLiJinDetailVO;
|
import com.yeshi.fanli.vo.tlj.TaoLiJinMonthVO;
|
|
@Service
|
public class UserTaoLiJinDetailServiceImpl implements UserTaoLiJinDetailService{
|
|
@Resource
|
private UserTaoLiJinDetailMapper userTaoLiJinDetailMapper;
|
|
|
@Override
|
public void insertSelective(UserTaoLiJinDetail record) {
|
userTaoLiJinDetailMapper.insertSelective(record);
|
}
|
|
@Override
|
public Long countDetail(long uid) {
|
return userTaoLiJinDetailMapper.countDetail(uid);
|
}
|
|
|
@Override
|
public BigDecimal countUseMoneyByDate(long uid, int dateType) {
|
return userTaoLiJinDetailMapper.countUseMoneyByDate(uid, dateType);
|
}
|
|
|
@Override
|
public List<TaoLiJinDetailVO> listUserMoneyDetailForClient(Long uid, Long detailId, Date maxTime) {
|
List<TaoLiJinDetailVO> finalList = new ArrayList<>();
|
List<UserTaoLiJinDetail> list = null;
|
if (detailId == null) {// 首次请求
|
if (maxTime == null)// 没有筛选时间
|
{
|
list = userTaoLiJinDetailMapper.selectByMaxCreateTime(uid,
|
new Date(System.currentTimeMillis() + 1000 * 60 * 60L), 20);
|
} else {// 筛选了时间
|
list = userTaoLiJinDetailMapper.selectByMaxCreateTime(uid, maxTime, 20);
|
}
|
if (list != null && list.size() > 0) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTimeInMillis(list.get(0).getCreateTime().getTime());
|
TaoLiJinDetailVO vo = new TaoLiJinDetailVO();
|
vo.setMonth(new TaoLiJinMonthVO(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1));
|
finalList.add(vo);
|
} else {
|
if (maxTime != null) {//
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTimeInMillis(maxTime.getTime());
|
TaoLiJinDetailVO vo = new TaoLiJinDetailVO();
|
vo.setMonth(new TaoLiJinMonthVO(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1));
|
vo.getMonth().setExpend("0");
|
vo.getMonth().setIncome("0");
|
finalList.add(vo);
|
}
|
}
|
|
} else {// 二次请求
|
|
int size = 21;
|
List<UserTaoLiJinDetail> tempList = userTaoLiJinDetailMapper.selectByUidWithIndexId(uid, detailId, size);
|
Set<Date> dateSet = new HashSet<>();// 用于储存是否在同一时间上面(精确到秒)
|
if (tempList.size() > 0) {
|
for (UserTaoLiJinDetail umd : tempList) {
|
dateSet.add(umd.getCreateTime());
|
}
|
|
List<UserTaoLiJinDetail> tempList2 = new ArrayList<>();
|
while (dateSet.size() == 1 && tempList2.size() != tempList.size() && size < 40) {// 只有一个时间点的数据
|
tempList = tempList2;
|
size += 10;
|
tempList2 = userTaoLiJinDetailMapper.selectByUidWithIndexId(uid, detailId, size);
|
dateSet.clear();
|
for (UserTaoLiJinDetail umd : tempList2) {
|
dateSet.add(umd.getCreateTime());
|
}
|
}
|
if (tempList2.size() > 0)
|
tempList = tempList2;
|
}
|
|
for (int i = 0; i < tempList.size(); i++) {
|
if (tempList.get(i).getId().longValue() == detailId) {
|
tempList.remove(i);
|
break;
|
} else {
|
tempList.remove(i);
|
i--;
|
}
|
}
|
list = tempList;
|
}
|
|
if (list != null) {
|
if (detailId != null && list.size() > 0) {
|
UserTaoLiJinDetail umd = userTaoLiJinDetailMapper.selectByPrimaryKey(detailId);
|
if (!TimeUtil.getGernalTime(umd.getCreateTime().getTime(), "yyyy-MM")
|
.equalsIgnoreCase(TimeUtil.getGernalTime(list.get(0).getCreateTime().getTime(), "yyyy-MM"))) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTimeInMillis(list.get(0).getCreateTime().getTime());
|
TaoLiJinDetailVO vo = new TaoLiJinDetailVO();
|
vo.setMonth(new TaoLiJinMonthVO(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1));
|
finalList.add(vo);
|
}
|
|
}
|
for (int i = 0; i < list.size(); i++) {
|
if (i > 0 && !TimeUtil.getGernalTime(list.get(i - 1).getCreateTime().getTime(), "yyyy-MM")
|
.equalsIgnoreCase(TimeUtil.getGernalTime(list.get(i).getCreateTime().getTime(), "yyyy-MM"))) {// 本条数据与上条数据不是同一月则插入月份
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTimeInMillis(list.get(i).getCreateTime().getTime());
|
TaoLiJinDetailVO vo = new TaoLiJinDetailVO();
|
vo.setMonth(new TaoLiJinMonthVO(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1));
|
finalList.add(vo);
|
}
|
TaoLiJinDetailVO vo = new TaoLiJinDetailVO();
|
vo.setDetail(list.get(i));
|
finalList.add(vo);
|
}
|
}
|
|
// 统计月资金
|
|
Map<Integer, TaoLiJinDetailVO> monthMap = new TreeMap<>();
|
if (finalList.size() > 1)
|
for (int i = 0; i < finalList.size(); i++) {
|
if (finalList.get(i).getMonth() != null)
|
monthMap.put(i, finalList.get(i));
|
}
|
|
if (!monthMap.isEmpty()) {
|
List<String> dateFormat = new ArrayList<>();
|
Iterator<Integer> keys = monthMap.keySet().iterator();
|
while (keys.hasNext()) {
|
Integer key = keys.next();
|
String date = "";
|
date += monthMap.get(key).getMonth().getYear();
|
date += "-";
|
date += (monthMap.get(key).getMonth().getMonth() + "").length() < 2
|
? "0" + monthMap.get(key).getMonth().getMonth() : monthMap.get(key).getMonth().getMonth();
|
dateFormat.add(date);
|
}
|
List<TaoLiJinMonthVO> voList = userTaoLiJinDetailMapper.selectMonthMoneyByUid(uid, dateFormat);
|
|
int p = 0;
|
keys = monthMap.keySet().iterator();
|
while (keys.hasNext()) {
|
Integer key = keys.next();
|
|
BigDecimal expend = new BigDecimal(voList.get(p).getExpend());
|
BigDecimal income = new BigDecimal(voList.get(p).getIncome());
|
|
// 去除支出负号
|
finalList.get(key).getMonth().setExpend(expend.setScale(2).toString().replace("-", ""));
|
finalList.get(key).getMonth().setIncome(income.setScale(2).toString());
|
p++;
|
}
|
}
|
return finalList;
|
}
|
|
|
@Override
|
public long countUserMoneyDetailForClient(Long uid, Long detailId, Date maxTime) {
|
long monthCount = 0L;
|
long detailCount = 0L;
|
// 未通过时间筛选,查询所有
|
if (maxTime == null) {
|
detailCount = userTaoLiJinDetailMapper.selectCountByUid(uid);
|
// 用于表示当前所有
|
monthCount = userTaoLiJinDetailMapper.selectMonthCountByUid(uid, new Date(System.currentTimeMillis() + 1000 * 60 * 60L));
|
} else {// 通过时间筛选了的,需要查询所有
|
detailCount = userTaoLiJinDetailMapper.selectCountByUidAndMaxCreateTime(uid, maxTime);
|
monthCount = userTaoLiJinDetailMapper.selectMonthCountByUid(uid, maxTime);
|
}
|
|
return monthCount + detailCount;
|
}
|
|
|
@Override
|
public void createAddDetail(UserTaoLiJinOrigin taoLiJinOrigin) {
|
UserTaoLiJinDetail detail = new UserTaoLiJinDetail();
|
detail.setUid(taoLiJinOrigin.getUid());
|
detail.setMoney(taoLiJinOrigin.getMoney());
|
|
// 红包退回
|
if (taoLiJinOrigin.getOrigin().equals(TaoLiJinOriginEnum.refund)) {
|
detail.setType(TaoLiJinDetailTypeEnum.refund);
|
detail.setTitle(TaoLiJinDetailTypeEnum.refund.getDesc());
|
} else {
|
detail.setType(TaoLiJinDetailTypeEnum.add);
|
detail.setTitle(TaoLiJinDetailTypeEnum.add.getDesc());
|
String desc = taoLiJinOrigin.getOrigin().getDesc();
|
if (taoLiJinOrigin.getOrigin().equals(TaoLiJinOriginEnum.newbiesWin) && taoLiJinOrigin.getType() == 1) {
|
desc = "分享红包"; // 老用户看到的是分享红包
|
}
|
detail.setDescInfo("来源:" + desc);
|
}
|
detail.setCreateTime(new Date());
|
userTaoLiJinDetailMapper.insertSelective(detail);
|
}
|
|
|
@Override
|
public Long countNumByDay(String preDay) {
|
return userTaoLiJinDetailMapper.countNumByDay(preDay);
|
}
|
|
@Override
|
public BigDecimal countMoneyByDay(String preDay) {
|
return userTaoLiJinDetailMapper.countMoneyByDay(preDay);
|
}
|
}
|