package com.yeshi.fanli.service.impl.count;
|
|
import java.math.BigDecimal;
|
import java.util.Date;
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
import org.yeshi.utils.DateUtil;
|
|
import com.yeshi.fanli.dao.user.count.DailyCountCouponDao;
|
import com.yeshi.fanli.entity.admin.count.DailyCountCoupon;
|
import com.yeshi.fanli.entity.admin.count.DailyCountCoupon.DailyCountCouponEnum;
|
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
|
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
|
import com.yeshi.fanli.service.inter.count.DailyCountCouponService;
|
import com.yeshi.fanli.service.inter.count.UserSystemCouponCountService;
|
import com.yeshi.fanli.util.StringUtil;
|
import org.yeshi.utils.TimeUtil;
|
|
@Service
|
public class DailyCountCouponServiceImpl implements DailyCountCouponService {
|
|
@Resource
|
private DailyCountCouponDao dailyCountCouponDao;
|
|
@Resource
|
private UserSystemCouponCountService userSystemCouponCountService;
|
|
@Override
|
public List<DailyCountCoupon> getDailyCountList(String type, Date startTime, Date endTime) throws Exception {
|
// 查询类型
|
DailyCountCouponEnum typeEnum = getTypeEnum(type);
|
if (typeEnum == null) {
|
return null;
|
}
|
|
// 初始化数据
|
initData(typeEnum);
|
|
return dailyCountCouponDao.query(typeEnum, startTime, endTime);
|
}
|
|
/**
|
* 获取枚举类型
|
* @param type
|
* @return
|
*/
|
private DailyCountCouponEnum getTypeEnum(String name) {
|
if (StringUtil.isNullOrEmpty(name)) {
|
return null;
|
}
|
|
DailyCountCouponEnum[] array = DailyCountCouponEnum.values();
|
for (int i = 0; i < array.length; i ++) {
|
if (array[i].name().equals(name)) {
|
return array[i];
|
}
|
}
|
return null;
|
}
|
|
|
/**
|
* 获取枚举类型
|
* @param type
|
* @return
|
*/
|
@Override
|
public String getTypeEnumDesc(String name) {
|
if (StringUtil.isNullOrEmpty(name)) {
|
return "";
|
}
|
|
DailyCountCouponEnum[] array = DailyCountCouponEnum.values();
|
for (int i = 0; i < array.length; i ++) {
|
if (array[i].name().equals(name)) {
|
return array[i].getDesc();
|
}
|
}
|
|
return "";
|
}
|
|
|
|
/**
|
* 初始数据
|
* @param typeEnum
|
* @throws Exception
|
*/
|
@Override
|
public void initData(DailyCountCouponEnum typeEnum) throws Exception {
|
Date lastDate = null;
|
DailyCountCoupon lastRecord = dailyCountCouponDao.getMaxDate(typeEnum);
|
if (lastRecord != null) {
|
lastDate = lastRecord.getUpdateDate();
|
} else {
|
lastDate = TimeUtil.parse("2019-03-04");
|
}
|
|
long min = DateUtil.dateDiffMin(lastDate, new Date());
|
if (min <= 10) { // 10分钟以内不统计
|
return;
|
}
|
|
|
Date today = new Date();
|
int betweenDays = DateUtil.daysBetween2(lastDate, today);
|
for (int i = 0; i <= betweenDays; i++) {
|
// 计算日期
|
String preDay = DateUtil.plusDay(i, lastDate);
|
// 统计数据
|
BigDecimal total = getCountByType(preDay, typeEnum);
|
|
// 保存信息
|
DailyCountCoupon obj = new DailyCountCoupon();
|
obj.setTotal(total);
|
obj.setType(typeEnum);
|
obj.setUpdateDate(new Date());
|
obj.setDay(TimeUtil.parse(preDay));
|
obj.setId(StringUtil.Md5(preDay + typeEnum.name()));
|
dailyCountCouponDao.save(obj);
|
}
|
}
|
|
/**
|
* 数据统计
|
* @param preDay
|
* @param typeEnum
|
* @return
|
*/
|
private BigDecimal getCountByType(String preDay, DailyCountCouponEnum typeEnum) {
|
BigDecimal total = null;
|
if (DailyCountCouponEnum.freeCouponNum == typeEnum) {
|
total = userSystemCouponCountService.countCouponNumByDay(CouponTypeEnum.freeCoupon, preDay);
|
} else if (DailyCountCouponEnum.freeCouponMoney == typeEnum) {
|
total = userSystemCouponCountService.countFreeMoneyByTypeAndDay(CouponTypeEnum.freeCoupon, preDay);
|
} else if (DailyCountCouponEnum.freeCouponBuyNum == typeEnum) {
|
total = userSystemCouponCountService.countCouponNumByDay(CouponTypeEnum.freeCouponBuy, preDay);
|
} else if (DailyCountCouponEnum.freeCouponBuyMoney == typeEnum) {
|
total = userSystemCouponCountService.countFreeMoneyByTypeAndDay(CouponTypeEnum.freeCouponBuy, preDay);
|
} else if (DailyCountCouponEnum.welfareFreeCouponNum == typeEnum) {
|
total = userSystemCouponCountService.countCouponNumByDay(CouponTypeEnum.welfareFreeCoupon, preDay);
|
} else if (DailyCountCouponEnum.welfareFreeCouponMoney == typeEnum) {
|
total = userSystemCouponCountService.countFreeMoneyByTypeAndDay(CouponTypeEnum.welfareFreeCoupon, preDay);
|
} else if (DailyCountCouponEnum.freeCouponGiveNum == typeEnum) {
|
total = userSystemCouponCountService.countCouponNumByDay(CouponTypeEnum.freeCouponGive, preDay);
|
} else if (DailyCountCouponEnum.rebateCouponNum == typeEnum) {
|
total = userSystemCouponCountService.countCouponNumByDay(CouponTypeEnum.rebatePercentCoupon, preDay);
|
} else if (DailyCountCouponEnum.rebateCouponMoney == typeEnum) {
|
total = userSystemCouponCountService.countRebateMoneyByDay(preDay);
|
} else if (DailyCountCouponEnum.pullNewCouponNum == typeEnum) {
|
total = userSystemCouponCountService.countCouponNumByDay(CouponTypeEnum.freeCoupon, preDay, UserSystemCoupon.SOURCE_PULL_NEW);
|
} else if (DailyCountCouponEnum.pullNewCouponMoney == typeEnum) {
|
total = userSystemCouponCountService.countFreeMoneyByTypeAndDay(CouponTypeEnum.freeCoupon, preDay, UserSystemCoupon.SOURCE_PULL_NEW);
|
}
|
|
if (total == null) {
|
total = new BigDecimal(0);
|
}
|
return total;
|
}
|
|
}
|