@ -1,27 +1,37 @@
package com.ccsens.braintraining.service ;
import cn.hutool.core.collection.CollectionUtil ;
import cn.hutool.core.date.DateUtil ;
import cn.hutool.core.lang.Snowflake ;
import cn.hutool.core.util.StrUtil ;
import cn.hutool.http.HttpResponse ;
import cn.hutool.http.HttpUtil ;
import com.alibaba.fastjson.JSONObject ;
import com.ccsens.braintraining.bean.dto.RaffleDto ;
import com.ccsens.braintraining.bean.po.RaffleActive ;
import com.ccsens.braintraining.bean.po.RaffleRecord ;
import com.ccsens.braintraining.bean.dto.TallDto ;
import com.ccsens.braintraining.bean.po.* ;
import com.ccsens.braintraining.bean.vo.RaffleVo ;
import com.ccsens.braintraining.bean.vo.TallVo ;
import com.ccsens.braintraining.persist.dao.RaffleDao ;
import com.ccsens.braintraining.persist.mapper.RaffleActiveMapper ;
import com.ccsens.braintraining.persist.mapper.RaffleRecordMapper ;
import com.ccsens.braintraining.persist.mapper.RaffleTaskParamMapper ;
import com.ccsens.braintraining.persist.mapper.RaffleTimesMapper ;
import com.ccsens.braintraining.util.BrainTrainingCodeError ;
import com.ccsens.braintraining.util.BrainTrainingConstant ;
import com.ccsens.util.bean.dto.QueryDto ;
import com.ccsens.util.exception.BaseException ;
import com.github.pagehelper.PageHelper ;
import com.github.pagehelper.PageInfo ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.beans.BeanUtils ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Propagation ;
import org.springframework.transaction.annotation.Transactional ;
import javax.annotation.Resource ;
import java.util.List ;
import java.util.Random ;
import java.util.* ;
/ * *
* @description :
@ -33,6 +43,8 @@ import java.util.Random;
@Transactional ( propagation = Propagation . REQUIRED , rollbackFor = Exception . class )
public class RaffleService implements IRaffleService {
@Value ( "${url.subscriptWx}" )
private String subscriptWxUrl ;
@Resource
private Snowflake snowflake ;
@Resource
@ -41,10 +53,22 @@ public class RaffleService implements IRaffleService {
private RaffleActiveMapper raffleActiveMapper ;
@Resource
private RaffleRecordMapper raffleRecordMapper ;
@Resource
private RaffleTimesMapper raffleTimesMapper ;
@Resource
private RaffleTaskParamMapper raffleTaskParamMapper ;
@Override
public RaffleVo . Active active ( RaffleDto . Equipment param , Long userId ) {
return raffleDao . queryActiveNow ( param . getEquipmentId ( ) , userId ) ;
RaffleVo . Active active = raffleDao . queryActiveNow ( param . getEquipmentId ( ) , userId ) ;
if ( active ! = null ) {
Integer remainTimes = raffleDao . countTimes ( active . getActive ( ) . getActiveId ( ) , userId ) ;
active . getActive ( ) . setPrizeTimes ( remainTimes ) ;
}
return active ;
}
@Override
@ -103,9 +127,112 @@ public class RaffleService implements IRaffleService {
record . setPrizeId ( prize . getPrizeId ( ) ) ;
record . setUserId ( userId ) ;
raffleRecordMapper . insertSelective ( record ) ;
// 设置最小
// 设置奖品剩余数-1
raffleDao . decreasePrize ( prize . getPrizeId ( ) ) ;
return prize ;
}
}
@Override
public PageInfo < RaffleVo . PrizeTime > queryUserPrize ( RaffleDto . SelfPrize param , Long userId ) {
PageHelper . startPage ( param . getPageNum ( ) , param . getPageSize ( ) ) ;
List < RaffleVo . PrizeTime > records = raffleDao . queryUserPrize ( userId ) ;
return new PageInfo ( records ) ;
}
@Override
public RaffleVo . DoTask doTask ( RaffleDto . DoTask param , Long userId ) {
// 判断
return null ;
}
@Override
public RaffleVo . CheckTask checkTask ( RaffleDto . DoTask param , Long userId , String token , String deviceId ) {
RaffleVo . CheckTask checkTask = new RaffleVo . CheckTask ( ) ;
// 判断活动是否是当前的活动
RaffleTask task = raffleDao . getRunningTask ( param . getTaskId ( ) ) ;
log . info ( "活动:{}" , task ) ;
if ( task = = null ) {
checkTask . setFinishStatus ( BrainTrainingConstant . Raffle . TASK_FINISH_END ) ;
return checkTask ;
}
// 判断是否已经有记录
List < RaffleTimes > raffleTimes = null ;
RaffleTimesExample timesExample = new RaffleTimesExample ( ) ;
timesExample . createCriteria ( ) . andTaskIdEqualTo ( param . getTaskId ( ) )
. andUserIdEqualTo ( userId ) . andFinishTypeGreaterThan ( BrainTrainingConstant . Raffle . TASK_FINISH_NO ) ;
switch ( task . getPeriod ( ) ) {
case BrainTrainingConstant . Raffle . TASK_PERIOD_ALL :
raffleTimes = raffleTimesMapper . selectByExample ( timesExample ) ;
break ;
case BrainTrainingConstant . Raffle . TASK_PERIOD_EVERY_DAY :
timesExample . createCriteria ( ) . andCreatedAtGreaterThanOrEqualTo ( DateUtil . beginOfDay ( new Date ( ) ) ) ;
raffleTimes = raffleTimesMapper . selectByExample ( timesExample ) ;
break ;
default :
throw new BaseException ( BrainTrainingCodeError . SETTING_ERROR ) ;
}
// 执行的次数大于任务最大的执行次数,则不能再做
if ( raffleTimes . size ( ) > = task . getRunTimes ( ) ) {
checkTask . setFinishStatus ( raffleTimes . get ( 0 ) . getFinishType ( ) ) ;
return checkTask ;
}
// 判断是否以前做过了
if ( task . getType ( ) = = BrainTrainingConstant . Raffle . TASK_TYPE_SUBSCRIBE ) {
// 关注公众号
// 查询记录中有没有关注的是同一个公众号的
Integer recordTimes = raffleDao . countSubscribe ( param . getTaskId ( ) , userId ) ;
if ( recordTimes ! = null & & recordTimes > 0 ) {
checkTask . setFinishStatus ( BrainTrainingConstant . Raffle . TASK_FINISH_BEFORE ) ;
return checkTask ;
}
// 查询该用户是否已经关注了
// 查询公众号appId
RaffleTaskParamExample paramExample = new RaffleTaskParamExample ( ) ;
paramExample . createCriteria ( ) . andTaskIdEqualTo ( param . getTaskId ( ) ) . andKeyWordEqualTo ( BrainTrainingConstant . Raffle . TASK_PARAM_APP_ID ) ;
paramExample . setOrderByClause ( "id desc limit 1" ) ;
List < RaffleTaskParam > params = raffleTaskParamMapper . selectByExample ( paramExample ) ;
if ( CollectionUtil . isEmpty ( params ) ) {
throw new BaseException ( BrainTrainingCodeError . SETTING_ERROR ) ;
}
String appId = params . get ( 0 ) . getKeyValue ( ) ;
byte status = pageSubscript ( token , deviceId , appId ) ;
} else if ( task . getType ( ) = = BrainTrainingConstant . Raffle . TASK_TYPE_PERFECT ) {
// TODO 查询详情表
}
checkTask . setFinishStatus ( BrainTrainingConstant . Raffle . TASK_FINISH_NO ) ;
return checkTask ;
}
private byte pageSubscript ( String token , String deviceId , String appId ) {
TallDto . Tencent tencent = new TallDto . Tencent ( ) ;
tencent . setAppId ( appId ) ;
QueryDto < TallDto . Tencent > dto = new QueryDto < > ( ) ;
dto . setParam ( tencent ) ;
log . info ( "调用判断接口, dto:{}, token:{}" , dto , token ) ;
Map < String , String > tokenMap = new HashMap < > ( ) ;
tokenMap . put ( BrainTrainingConstant . User . AUTHORIZATION , token ) ;
tokenMap . put ( BrainTrainingConstant . User . DEVICE_ID , deviceId ) ;
HttpResponse execute = HttpUtil . createPost ( subscriptWxUrl ) . addHeaders ( tokenMap ) . body ( JSONObject . toJSONString ( dto ) ) . execute ( ) ;
log . info ( "http:{}" , HttpUtil . createPost ( subscriptWxUrl ) . addHeaders ( tokenMap ) . body ( JSONObject . toJSONString ( dto ) ) ) ;
log . info ( "调用判断用户是否关注公众号接口的结果:{}" , execute ) ;
if ( execute = = null | | StrUtil . isEmpty ( execute . body ( ) ) | | execute . body ( ) . contains ( "<html" ) ) {
throw new BaseException ( BrainTrainingCodeError . SYS_ERROR ) ;
}
JSONObject json = JSONObject . parseObject ( execute . body ( ) ) ;
log . info ( "json:{}" , json ) ;
if ( json . getIntValue ( BrainTrainingConstant . CODE ) ! = BrainTrainingCodeError . SUCCESS . getCode ( ) ) {
throw new BaseException ( json . getIntValue ( BrainTrainingConstant . CODE ) , json . getString ( BrainTrainingConstant . MSG ) ) ;
}
TallVo . UserOfficial official = JSONObject . parseObject ( json . getString ( "data" ) , TallVo . UserOfficial . class ) ;
return official . getStatus ( ) ;
}
}