package com.yeshi.fanli.service.impl.integral;
|
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Set;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.yeshi.utils.DateUtil;
|
|
import com.yeshi.fanli.dao.mybatis.integral.IntegralTaskRecordMapper;
|
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
|
import com.yeshi.fanli.entity.integral.IntegralTaskRecord;
|
import com.yeshi.fanli.exception.integral.IntegralTaskRecordException;
|
import com.yeshi.fanli.exception.user.UserInfoExtraException;
|
import com.yeshi.fanli.log.LogHelper;
|
import com.yeshi.fanli.service.inter.integral.IntegralTaskRecordService;
|
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
|
|
@Service
|
public class IntegralTaskRecordServiceImpl implements IntegralTaskRecordService {
|
|
@Resource
|
private IntegralTaskRecordMapper integralTaskRecordMapper;
|
|
@Resource
|
private UserInfoExtraService userInfoExtraService;
|
|
@Override
|
public Integer getTotalGoldCoin(long uid, Long cid, Integer dateType) {
|
return integralTaskRecordMapper.getTotalGoldCoin(uid, cid, dateType);
|
}
|
|
@Override
|
public Integer countFinished(long uid, Long cid, Integer dateType) {
|
return integralTaskRecordMapper.countFinished(uid, cid, dateType);
|
}
|
|
@Override
|
public void finishedTask(long uid, long cid, long taskId, int goldCoin) {
|
IntegralTaskRecord taskRecord = new IntegralTaskRecord();
|
taskRecord.setUid(uid);
|
taskRecord.setCid(cid);
|
taskRecord.setTaskId(taskId);
|
taskRecord.setGoldCoin(goldCoin);
|
taskRecord.setState(0);
|
taskRecord.setCreateTime(new Date());
|
taskRecord.setUpdateTime(new Date());
|
integralTaskRecordMapper.insertSelective(taskRecord);
|
}
|
|
@Override
|
public boolean isToDaySign(long uid, Long cid) {
|
Integer countFinished = integralTaskRecordMapper.countFinished(uid, cid, 1);
|
if (countFinished != null && countFinished > 0) {
|
return true;
|
}
|
return false;
|
}
|
|
@Override
|
public Integer getNowdaySignNum(long uid, Long cid) {
|
int num = 1;
|
try {
|
// 签到时间倒序查询近7天数据
|
List<IntegralTaskRecord> list = integralTaskRecordMapper.listDaySignRecord(uid, cid);
|
if (list != null && list.size() > 0) {
|
Date today =new Date();
|
for (IntegralTaskRecord record: list) {
|
if (DateUtil.daysBetween2(record.getCreateTime(),today) != 1) {
|
break; // 天数未连续
|
}
|
num ++ ;
|
}
|
}
|
} catch (Exception e) {
|
LogHelper.errorDetailInfo(e);
|
}
|
|
if (num > 7) {
|
num = 1;
|
}
|
return num;
|
}
|
|
|
@Override
|
public List<Date> getSignDays(long uid, Long cid) {
|
List<Date> listDate = new ArrayList<Date>();
|
try {
|
// 签到时间正序序查询近7天数据
|
List<IntegralTaskRecord> list = integralTaskRecordMapper.listDaySignRecord(uid, cid);
|
if (list != null && list.size() > 0) {
|
Date date = new Date();
|
for (IntegralTaskRecord record: list) {
|
Date createTime = record.getCreateTime();
|
if (DateUtil.daysBetween2(createTime,date) > 1) {
|
break; // 天数未连续
|
}
|
date = createTime;
|
listDate.add(record.getCreateTime());
|
}
|
}
|
} catch (Exception e) {
|
LogHelper.errorDetailInfo(e);
|
}
|
|
// 倒序
|
Collections.reverse(listDate);
|
return listDate;
|
}
|
|
@Override
|
public List<IntegralTaskRecord> listNotReceived(int count, long uid) {
|
return integralTaskRecordMapper.listNotReceived(count, uid);
|
}
|
|
|
@Override
|
public List<IntegralTaskRecord> listNotReceivedExcludeId(int count, long uid, Set<Long> idList) {
|
if (idList != null && idList.size() == 0) {
|
idList = null;
|
}
|
return integralTaskRecordMapper.listNotReceivedExcludeId(count, uid, idList);
|
}
|
|
|
@Override
|
@Transactional
|
public Integer receiveGoldCoinALL(Long uid) throws IntegralTaskRecordException {
|
UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
|
if (userInfoExtra == null)
|
throw new IntegralTaskRecordException(1, "用户信息不全");
|
|
int totalGoldCoin = integralTaskRecordMapper.countTotalGoldCoin(uid);
|
if (totalGoldCoin > 0) {
|
integralTaskRecordMapper.updateReceived(uid);
|
}
|
|
Integer goldCoin = userInfoExtra.getGoldCoin();
|
if (goldCoin == null)
|
goldCoin = 0;
|
|
UserInfoExtra extra = new UserInfoExtra();
|
extra.setId(userInfoExtra.getId());
|
extra.setGoldCoin(goldCoin + totalGoldCoin);
|
try {
|
userInfoExtraService.saveUserInfoExtra(extra);
|
} catch (UserInfoExtraException e) {
|
throw new IntegralTaskRecordException(1, "用户信息不全");
|
}
|
|
return extra.getGoldCoin();
|
}
|
|
|
|
@Override
|
@Transactional
|
public Integer receiveGoldCoin(Long uid, Set<Long> idList) throws IntegralTaskRecordException {
|
if(idList == null || idList.size() == 0) {
|
throw new IntegralTaskRecordException(1, "id为空");
|
}
|
|
UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
|
if (userInfoExtra == null) {
|
throw new IntegralTaskRecordException(1, "用户信息不全");
|
}
|
|
Integer totalGoldCoin = 0;
|
for (Long id: idList) {
|
IntegralTaskRecord record = integralTaskRecordMapper.selectByPrimaryKey(id);
|
if (record == null)
|
continue;
|
|
Long uid2 = record.getUid();
|
if (uid2 == null || uid.longValue() != uid2.longValue())
|
continue; // 用户id不符合
|
|
if (record.getState() != null && record.getState().intValue() == 1) {
|
continue; // 已领取
|
}
|
|
IntegralTaskRecord taskRecord = new IntegralTaskRecord();
|
taskRecord.setId(record.getId());
|
taskRecord.setState(1); // 已领取
|
taskRecord.setUpdateTime(new Date());
|
integralTaskRecordMapper.updateByPrimaryKeySelective(taskRecord);
|
|
totalGoldCoin += record.getGoldCoin();
|
}
|
|
Integer goldCoin = userInfoExtra.getGoldCoin();
|
if (goldCoin == null)
|
goldCoin = 0;
|
|
UserInfoExtra extra = new UserInfoExtra();
|
extra.setId(userInfoExtra.getId());
|
extra.setGoldCoin(goldCoin + totalGoldCoin);
|
try {
|
userInfoExtraService.saveUserInfoExtra(extra);
|
} catch (UserInfoExtraException e) {
|
throw new IntegralTaskRecordException(1, "用户信息不全");
|
}
|
|
return extra.getGoldCoin();
|
}
|
}
|
|