@@ -5,40 +5,92 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.common.utils.R ;
import com.peanut.common.utils.ShiroUtils ;
import com.peanut.modules.common.dao.MyUserDao ;
import com.peanut.modules.common.dao.UserVipDao ;
import com.peanut.modules.common.dao.VipBuyConfigDao ;
import com.peanut.modules.common.entity.MyUserEntity ;
import com.peanut.modules.common.entity.UserVip ;
import com.peanut.modules.common.entity.VipBuyConfigEntity ;
import com.peanut.modules.common.service.UserVipService ;
import com.peanut.modules.sociology.service.VipService ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import java.util.Date ;
import java.util.Lis t ;
import java.math.BigDecimal ;
import java.text.DateForma t ;
import java.text.ParseException ;
import java.text.SimpleDateFormat ;
import java.util.* ;
import java.util.concurrent.TimeUnit ;
import java.util.stream.Collectors ;
@Slf4j
@Service ( " sociologyUserService " )
public class VipServiceImpl extends ServiceImpl < VipBuyConfigDao , VipBuyConfigEntity > implements VipService {
@Autowired
private MyUserDao myUserDao ;
@Autowired
private UserVipDao userVipDao ;
/**
* 获取国学vip商品
* @return
*/
@Override
public R getVipProductForUser ( ) {
public R getVipProductForUser ( ) throws ParseException {
int uid = ShiroUtils . getUId ( ) ;
MyUserEntity userEntity = myUserDao . selectById ( uid ) ;
Integer chaoCount = userVipDao . selectCount ( new LambdaQueryWrapper < UserVip > ( ) . eq ( UserVip : : getUserId , uid ) . eq ( UserVip : : getType , 1 ) . lt ( UserVip : : getEndTime , new Date ( ) ) . eq ( UserVip : : getState , 0 ) ) ;
Integer sociologyCount = userVipDao . selectCount ( new LambdaQueryWrapper < UserVip > ( ) . eq ( UserVip : : getUserId , uid ) . eq ( UserVip : : getType , 3 ) . lt ( UserVip : : getEndTime , new Date ( ) ) . eq ( UserVip : : getState , 0 ) ) ;
//获取超v列表
LambdaQueryWrapper < VipBuyConfigEntity > wrapper = new LambdaQueryWrapper < > ( ) ;
wrapper . eq ( VipBuyConfigEntity : : getType , 1 ) ;
wrapper . in ( VipBuyConfigEntity : : getType , userEntity . getVip ( ) . equals ( " 1 " ) | | userEntity . getVip ( ) . equals ( " 2 " ) ? Collections . singletonList ( 1 ) : Arrays . asList ( 1 , 3 ) ) ;
wrapper . and ( r - > r . eq ( VipBuyConfigEntity : : getDateType , 0 ) . or ( f - > f . eq ( VipBuyConfigEntity : : getDateType , 1 ) . lt ( VipBuyConfigEntity : : getStartTime , new Date ( ) ) . gt ( VipBuyConfigEntity : : getEndTime , new Date ( ) ) ) ) ;
List < VipBuyConfigEntity > vipBuyConfigEntities = this . getBaseMapper ( ) . selectList ( wrapper ) ;
return R . ok ( ) . put ( " list " , vipBuyConfigEntities ) ;
for ( VipBuyConfigEntity v : vipBuyConfigEntities ) {
if ( v . getType ( ) = = 1 ) {
if ( ! Objects . equals ( userEntity . getVip ( ) , " 1 " ) & & chaoCount = = 0 ) {
v . setLastFee ( v . getFee ( ) ) ;
} else {
if ( userEntity . getVip ( ) . equals ( " 2 " ) ) { //医学转超v计算价格
List < UserVip > userVips = userVipDao . selectList ( new LambdaQueryWrapper < UserVip > ( ) . eq ( UserVip : : getUserId , uid ) . eq ( UserVip : : getType , 2 ) . lt ( UserVip : : getEndTime , new Date ( ) ) . eq ( UserVip : : getState , 0 ) ) ;
if ( userVips . size ( ) ! = 1 ) {
return R . error ( " 用户vip信息异常, 请联系管理员 " ) ;
}
UserVip userVip = userVips . get ( 0 ) ;
Date nowDate = new Date ( ) ; // 第一个日期
Date endDate = userVip . getEndTime ( ) ; // 第二个日期
long differenceInMilliseconds = endDate . getTime ( ) - nowDate . getTime ( ) ;
int differenceInDays = ( int ) TimeUnit . MILLISECONDS . toDays ( differenceInMilliseconds ) ;
int i = ( 12000 / ( 3 * 365 ) ) * differenceInDays ; //默认按照打折价格抵扣
BigDecimal chaFee = BigDecimal . valueOf ( i ) ;
v . setLastFee ( chaoCount > 0 ? v . getRebateFee ( ) . subtract ( chaFee ) : v . getFee ( ) . subtract ( chaFee ) ) ;
} else if ( userEntity . getVip ( ) . equals ( " 3 " ) ) { //国学vip转超v计算价格
List < UserVip > userVips = userVipDao . selectList ( new LambdaQueryWrapper < UserVip > ( ) . eq ( UserVip : : getUserId , uid ) . eq ( UserVip : : getType , 3 ) . lt ( UserVip : : getEndTime , new Date ( ) ) . eq ( UserVip : : getState , 0 ) ) ;
if ( userVips . size ( ) ! = 1 ) {
return R . error ( " 用户vip信息异常, 请联系管理员 " ) ;
}
UserVip userVip = userVips . get ( 0 ) ;
Date nowDate = new Date ( ) ; // 第一个日期
Date endDate = userVip . getEndTime ( ) ; // 第二个日期
long differenceInMilliseconds = endDate . getTime ( ) - nowDate . getTime ( ) ;
int differenceInDays = ( int ) TimeUnit . MILLISECONDS . toDays ( differenceInMilliseconds ) ;
int i = ( 5888 / ( 3 * 365 ) ) * differenceInDays ; //默认按照打折价格抵扣
BigDecimal chaFee = BigDecimal . valueOf ( i ) ;
v . setLastFee ( chaoCount > 0 ? v . getRebateFee ( ) . subtract ( chaFee ) : v . getFee ( ) . subtract ( chaFee ) ) ;
} else {
v . setLastFee ( v . getRebateFee ( ) ) ;
}
}
} else {
v . setLastFee ( chaoCount > 0 | | sociologyCount > 0 ? v . getRebateFee ( ) : v . getFee ( ) ) ;
}
}
Map < String , List < VipBuyConfigEntity > > flag = new HashMap < > ( ) ;
List < VipBuyConfigEntity > collect = vipBuyConfigEntities . stream ( ) . filter ( r - > r . getType ( ) = = 1 ) . collect ( Collectors . toList ( ) ) ;
List < VipBuyConfigEntity > collect1 = vipBuyConfigEntities . stream ( ) . filter ( r - > r . getType ( ) = = 3 ) . collect ( Collectors . toList ( ) ) ;
if ( collect . size ( ) > 0 ) flag . put ( " super " , collect ) ;
if ( collect1 . size ( ) > 0 ) flag . put ( " sociology " , collect1 ) ;
return R . ok ( ) . put ( " list " , flag ) ;
}
}