@@ -1,18 +1,27 @@
package com.sqx.modules.orders.service.impl ;
import cn.hutool.core.collection.CollUtil ;
import cn.hutool.core.util.StrUtil ;
import cn.hutool.json.JSONUtil ;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSONObject ;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper ;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper ;
import com.baomidou.mybatisplus.core.metadata.IPage ;
import com.baomidou.mybatisplus.core.toolkit.Wrappers ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.sqx.common.utils.DateUtils ;
import com.sqx.common.utils.PageUtils ;
import com.sqx.common.utils.Result ;
import com.sqx.modules.app.entity.* ;
import com.sqx.modules.app.entity.UserMoney ;
import com.sqx.modules.app.entity.UserMoneyDetails ;
import com.sqx.modules.app.entity.UserVip ;
import com.sqx.modules.app.entity.VipDetails ;
import com.sqx.modules.app.service.* ;
import com.sqx.modules.common.service.CommonInfoService ;
import com.sqx.modules.course.dao.CourseDao ;
import com.sqx.modules.course.dao.CourseDetailsDao ;
import com.sqx.modules.course.dao.CourseUserDao ;
import com.sqx.modules.course.entity.Course ;
import com.sqx.modules.course.entity.CourseDetails ;
@@ -38,6 +47,7 @@ import java.text.ParseException;
import java.text.SimpleDateFormat ;
import java.util.* ;
import java.util.concurrent.locks.ReentrantReadWriteLock ;
import java.util.stream.Collectors ;
@Service
@Slf4j
@@ -72,6 +82,8 @@ public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements
private InviteMoneyService inviteMoneyService ;
@Autowired
private DyService dyService ;
@Autowired
private CourseDetailsDao courseDetailsDao ;
private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock ( true ) ;
@@ -86,6 +98,32 @@ public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements
public Result insertOrders ( Orders orders ) {
//如果订单的种类是短剧
if ( orders . getOrdersType ( ) = = 1 ) {
if ( StrUtil . isNotEmpty ( orders . getCourseDetailsIds ( ) ) ) {
List < Long > courseDetailsIdList = JSONUtil . parseArray ( orders . getCourseDetailsIds ( ) ) . toList ( Long . class ) ;
List < CourseUser > courseUserList = new ArrayList < > ( ) ;
courseDetailsIdList . parallelStream ( ) . forEach ( courseDetailsId - > {
//将短剧加入到我的列表
CourseUser courseUser = new CourseUser ( ) ;
//设置短剧id
courseUser . setCourseId ( orders . getCourseId ( ) ) ;
courseUser . setCourseDetailsId ( orders . getCourseDetailsId ( ) ) ;
if ( courseUser . getCourseDetailsId ( ) ! = null ) {
courseUser . setClassify ( 2 ) ;
} else {
courseUser . setClassify ( 1 ) ;
}
//设置用户id
courseUser . setUserId ( orders . getUserId ( ) ) ;
//设置订单id
courseUser . setOrderId ( orders . getOrdersId ( ) ) ;
SimpleDateFormat df = new SimpleDateFormat ( " yyyy-MM-dd HH:mm:ss " ) ;
courseUser . setCreateTime ( df . format ( new Date ( ) ) ) ;
courseUserList . add ( courseUser ) ;
} ) ;
//加入我的列表
courseUserService . saveBatch ( courseUserList ) ;
return Result . success ( " 短剧订单处理完成! " ) ;
}
//将短剧加入到我的列表
CourseUser courseUser = new CourseUser ( ) ;
//设置短剧id
@@ -280,6 +318,112 @@ public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements
return Result . error ( " 系统繁忙,请稍后再试! " ) ;
}
/**
* 生成商品订单信息(购买10集)
*
* @param courseId
* @param userId
* @return
*/
@Override
public Result insertCourseOrdersLimit10 ( Long courseId , Long userId ) {
log . info ( " 生成商品订单信息接口入参为:{},{} " , courseId , userId ) ;
reentrantReadWriteLock . writeLock ( ) . lock ( ) ;
try {
/*CourseUser courseUser1 = courseUserDao.selectCourseUser(courseId, userId);
if(courseUser1!=null){
return Result.error("您已经购买过了,请不要重复点击!");
}*/
//返回的类型
Map < String , Object > result = new HashMap < > ( ) ;
//查询会员信息
SimpleDateFormat df = new SimpleDateFormat ( " yyyy-MM-dd HH:mm:ss " ) ;
if ( courseId = = null ) {
return Result . error ( " 短剧的id不能为空 " ) ;
}
//订单模板对象
Orders orders = new Orders ( ) ;
//根据短剧id去查询短剧相关信息 来填充订单模板
QueryWrapper < Course > queryWrapper = new QueryWrapper < > ( ) ;
queryWrapper . eq ( " is_delete " , 0 ) ;
//短剧必须是未删除的
queryWrapper . eq ( " course_id " , courseId ) ;
Course course = courseDao . selectOne ( queryWrapper ) ;
if ( course = = null ) {
return Result . error ( " 系统繁忙,请刷新后重试! " ) ;
}
if ( course . getWholesalePrice ( ) = = null ) {
return Result . error ( " 该剧暂不支持10集购买方式! " ) ;
}
//是否以购买全集
Integer isBuyAllCount = courseUserDao . selectCount ( Wrappers . < CourseUser > lambdaQuery ( ) . eq ( CourseUser : : getCourseId , courseId ) . eq ( CourseUser : : getCourseUserId , userId ) . eq ( CourseUser : : getClassify , 1 ) ) ;
if ( isBuyAllCount ! = null & & isBuyAllCount > 0 ) {
return Result . error ( " 您已经购买过全集,请不要重复购买! " ) ;
}
List < CourseUser > courseUserList = courseUserDao . selectList ( Wrappers . < CourseUser > lambdaQuery ( ) . select ( CourseUser : : getCourseDetailsId ) . eq ( CourseUser : : getCourseId , courseId ) . eq ( CourseUser : : getCourseUserId , userId ) ) ;
// 已购买剧集
List < Long > courseDetailsIdList = courseUserList . stream ( ) . map ( CourseUser : : getCourseDetailsId ) . filter ( Objects : : nonNull ) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
LambdaQueryWrapper < CourseDetails > wrapper = Wrappers . < CourseDetails > lambdaQuery ( ) ;
wrapper . eq ( CourseDetails : : getCourseId , courseId ) ;
wrapper . eq ( CourseDetails : : getIsPrice , 1 ) ;
wrapper . select ( CourseDetails : : getCourseDetailsId ) ;
wrapper . orderByAsc ( CourseDetails : : getSort ) ;
List < CourseDetails > courseDetailsList = courseDetailsDao . selectList ( wrapper ) ;
if ( CollUtil . isEmpty ( courseDetailsList ) ) {
return Result . error ( " 该剧的视频资源不存在! " ) ;
}
List < Long > allIds = courseDetailsList . stream ( ) . map ( CourseDetails : : getCourseDetailsId ) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
allIds . removeAll ( courseDetailsIdList ) ;
// 需要购买的剧集id集合( 没有权限的10集)
List < Long > buyCourseDetailsIdList = allIds . stream ( ) . filter ( Objects : : nonNull ) . limit ( 10 ) . collect ( Collectors . toList ( ) ) ;
if ( CollUtil . isEmpty ( buyCourseDetailsIdList ) ) {
return Result . error ( " 没有需要购买的剧集! " ) ;
}
//设置订单编号
orders . setOrdersNo ( AliPayOrderUtil . createOrderId ( ) ) ;
//设置用户id
orders . setUserId ( userId ) ;
//设置短剧id
orders . setCourseId ( courseId ) ;
orders . setCourseDetailsId ( null ) ;
orders . setCourseDetailsIds ( JSONUtil . parseArray ( buyCourseDetailsIdList ) . toString ( ) ) ;
// 金币和金额的比例
String value = commonInfoService . findOne ( 914 ) . getValue ( ) ;
BigDecimal v = new BigDecimal ( value ) ;
orders . setPayMoney ( course . getWholesalePrice ( ) ) ;
orders . setPayDiamond ( course . getWholesalePrice ( ) . multiply ( v ) ) ;
// BigDecimal payDiamond = orders.getPayMoney().multiply(new BigDecimal(commonInfoService.findOne(892).getValue()));
// orders.setPayDiamond(payDiamond);
//设置支付状态
orders . setStatus ( 0 ) ;
//设置订单创建时间
orders . setCreateTime ( df . format ( new Date ( ) ) ) ;
//设置订单种类
orders . setOrdersType ( 1 ) ;
//不是会员或会员过期直接生成订单直接生成订单
int count = baseMapper . insert ( orders ) ;
result . put ( " flag " , 2 ) ;
result . put ( " orders " , orders ) ;
if ( count > 0 ) {
return Result . success ( " 生成订单成功! " ) . put ( " data " , result ) ;
} else {
return Result . error ( " 生成订单失败! " ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
log . error ( " 生成商品订单错误!!! " + e . getMessage ( ) ) ;
} finally {
reentrantReadWriteLock . writeLock ( ) . unlock ( ) ;
}
return Result . error ( " 系统繁忙,请稍后再试! " ) ;
}
/**
* 生成会员订单
*
@@ -330,6 +474,12 @@ public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements
. eq ( " classify " , 2 )
. eq ( " course_id " , orders . getCourseId ( ) )
. eq ( orders . getCourseDetailsId ( ) ! = null , " course_details_id " , orders . getCourseDetailsId ( ) ) ) ;
} else if ( orders . getCourseDetailsIds ( ) ! = null ) {
List < Long > courseDetailsIds = JSONUtil . parseArray ( orders . getCourseDetailsIds ( ) ) . toList ( Long . class ) ;
count = courseUserDao . selectCount ( new QueryWrapper < CourseUser > ( )
. eq ( " user_id " , orders . getUserId ( ) )
. eq ( " course_id " , orders . getCourseId ( ) )
. in ( " course_details_id " , courseDetailsIds ) ) ;
} else {
count = courseUserDao . selectCount ( new QueryWrapper < CourseUser > ( )
. eq ( " user_id " , orders . getUserId ( ) )
@@ -344,7 +494,7 @@ public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements
if ( userMoney . getMoney ( ) . doubleValue ( ) < orders . getPayDiamond ( ) . doubleValue ( ) ) {
return Result . error ( " 账户不足,请充值! " ) ;
}
UserEntity userEntity = userService. selectUserById( orders. getUserId( ) ) ;
// UserEntity userEntity = userService. selectUserById( orders. getUserId()) ;
userMoneyService . updateMoney ( 2 , orders . getUserId ( ) , orders . getPayDiamond ( ) . doubleValue ( ) ) ;
UserMoneyDetails userMoneyDetails = new UserMoneyDetails ( ) ;
userMoneyDetails . setMoney ( orders . getPayDiamond ( ) ) ;