package com.hollywood.applet.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.hollywood.applet.dto.ScriptPayDto;
|
import com.hollywood.applet.dto.ShortPayDto;
|
import com.hollywood.applet.mapper.*;
|
import com.hollywood.applet.query.TShortPlayQuery;
|
import com.hollywood.applet.service.TShortPlayService;
|
import com.hollywood.applet.vo.TShortPlayVO;
|
import com.hollywood.common.basic.ApiResult;
|
import com.hollywood.common.basic.PageInfo;
|
import com.hollywood.common.exception.ServiceException;
|
import com.hollywood.common.model.*;
|
|
|
import com.hollywood.common.utils.PayMoneyUtil;
|
import com.hollywood.common.utils.UUIDUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDateTime;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 短剧管理 服务实现类
|
* </p>
|
*
|
* @author xiaochen
|
* @since 2024-02-29
|
*/
|
@Service
|
public class TShortPlayServiceImpl extends ServiceImpl<TShortPlayMapper, TShortPlay> implements TShortPlayService {
|
|
@Autowired
|
private TShortPlayToTypeMapper shortPlayToTypeMapper;
|
@Autowired
|
private TShortPlayTypeMapper shortPlayTypeMapper;
|
@Autowired
|
private TShortPlayVideoMapper shortPlayVideoMapper;
|
@Resource
|
private TOrderMapper orderMapper;
|
@Autowired
|
private PayMoneyUtil payMoneyUtil;
|
|
@Override
|
public PageInfo<TShortPlayVO> pageList(TShortPlayQuery query,List<Long> ids) {
|
PageInfo<TShortPlayVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
|
List<TShortPlayVO> list = this.baseMapper.pageList(query,pageInfo,ids);
|
List<TShortPlayToType> tShortPlayToTypes = shortPlayToTypeMapper.selectList(Wrappers.lambdaQuery(TShortPlayToType.class));
|
for (TShortPlayVO tShortPlayVO : list) {
|
List<TShortPlayToType> shortPlayToTypes = tShortPlayToTypes.stream().filter(e -> tShortPlayVO.getId().equals(e.getShortPlayId())).collect(Collectors.toList());
|
List<Long> typeIds = shortPlayToTypes.stream().map(TShortPlayToType::getTypeId).collect(Collectors.toList());
|
tShortPlayVO.setTypeIds(typeIds);
|
// 封装类型名称
|
List<TShortPlayType> tShortPlayTypes = shortPlayTypeMapper.selectList(Wrappers.lambdaQuery(TShortPlayType.class)
|
.in(TShortPlayType::getId, typeIds));
|
String typeName = tShortPlayTypes.stream().map(TShortPlayType::getTypeName).collect(Collectors.joining(","));
|
tShortPlayVO.setTypeName(typeName);
|
tShortPlayVO.setShortPlayVideos(shortPlayVideoMapper.selectList(Wrappers.lambdaQuery(TShortPlayVideo.class).eq(TShortPlayVideo::getShortPlayId,tShortPlayVO.getId()).orderByAsc(TShortPlayVideo::getSortBy)));
|
}
|
pageInfo.setRecords(list);
|
return pageInfo;
|
}
|
|
@Override
|
public int upAndDown(Long id, Integer status) {
|
TShortPlay shortPlay = this.baseMapper.selectById(id);
|
shortPlay.setStatus(status);
|
return this.baseMapper.updateById(shortPlay);
|
}
|
|
@Override
|
public ApiResult pay(ShortPayDto shortPayDto,Long userId) throws Exception {
|
|
//判断当前用户是否购买
|
List<TOrder> tOrders = orderMapper.selectList(Wrappers.lambdaQuery(TOrder.class).
|
eq(TOrder::getProductId, shortPayDto.getShortId())
|
.eq(TOrder::getProductType, 2)
|
.eq(TOrder::getIsPay, 1).eq(TOrder::getUserId,userId)
|
);
|
if (!tOrders.isEmpty()){
|
return ApiResult.failed("你已购买当前短剧");
|
}
|
TShortPlay tShortPlay = this.baseMapper.selectById(shortPayDto.getShortId());
|
|
if(shortPayDto.getPayType() == 1){//微信支付
|
return weChatPaymentShort(userId,tShortPlay);
|
}
|
if (shortPayDto.getPayType()==2){
|
return aliPayShort(userId,tShortPlay);
|
|
}
|
return ApiResult.failed("请选择支付方式");
|
}
|
|
private ApiResult aliPayShort(Long userId, TShortPlay tShortPlay) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
|
TOrder order = new TOrder();
|
order.setUserId(userId);
|
order.setProductType(2);
|
order.setProductName(tShortPlay.getShortPlayName());
|
order.setProductId(tShortPlay.getId());
|
order.setReleasePerson(tShortPlay.getReleasePerson());
|
order.setReleasePhone(tShortPlay.getReleasePhone());
|
order.setProductDeposit(tShortPlay.getShortPlayDeposit());
|
order.setPayTime(LocalDateTime.now());
|
if (tShortPlay.getShortPlayDeposit()==null|| tShortPlay.getShortPlayDeposit().equals(new BigDecimal(0))){
|
order.setIsPay(1);
|
}else {
|
order.setIsPay(2);}
|
order.setPayMoney(tShortPlay.getShortPlayDeposit());
|
order.setCode(code);
|
orderMapper.insert(order);
|
ApiResult alipay = payMoneyUtil.alipay("购买剧本", "购买剧本", code, tShortPlay.getShortPlayDeposit().toString(), "/base/short/alicallback");
|
return alipay;
|
}
|
|
private ApiResult weChatPaymentShort(Long userId, TShortPlay tShortPlay) throws Exception {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
|
TOrder order = new TOrder();
|
order.setUserId(userId);
|
order.setProductType(2);
|
order.setProductName(tShortPlay.getShortPlayName());
|
order.setProductId(tShortPlay.getId());
|
order.setReleasePerson(tShortPlay.getReleasePerson());
|
order.setReleasePhone(tShortPlay.getReleasePhone());
|
order.setProductDeposit(tShortPlay.getShortPlayDeposit());
|
order.setPayTime(LocalDateTime.now());
|
if (tShortPlay.getShortPlayDeposit()==null|| tShortPlay.getShortPlayDeposit().compareTo(BigDecimal.ZERO)==0){
|
order.setIsPay(1);
|
}else {
|
order.setIsPay(2);
|
}
|
order.setPayMoney(tShortPlay.getShortPlayDeposit());
|
order.setCode(code);
|
orderMapper.insert(order);
|
|
|
ApiResult weixinpay = payMoneyUtil.weixinpay("购买短剧", "", code, tShortPlay.getShortPlayDeposit().toString(), "/base/short/callback", "APP");
|
// if(weixinpay.getCode() == 200){
|
// new Thread(new Runnable() {
|
// @Override
|
// public void run() {
|
// try {
|
// int num = 1;
|
// int wait = 0;
|
// while (num <= 10){
|
// int min = 5000;
|
// wait += (min * num);
|
// Thread.sleep(wait);
|
// TOrder order1 = orderMapper.selectById(order.getId());
|
//
|
// if(order1.getIsPay() == 2){
|
// break;
|
// }
|
// ApiResult<Map<String, String>> resultUtil = payMoneyUtil.queryWXOrder(code, "");
|
// if(resultUtil.getCode() == 200 && order1.getIsPay() == 1){
|
// /**
|
// * SUCCESS—支付成功,
|
// * REFUND—转入退款,
|
// * NOTPAY—未支付,
|
// * CLOSED—已关闭,
|
// * REVOKED—已撤销(刷卡支付),
|
// * USERPAYING--用户支付中,
|
// * PAYERROR--支付失败(其他原因,如银行返回失败)
|
// */
|
// Map<String, String> data1 = resultUtil.getData();
|
// String s = data1.get("trade_state");
|
// String transaction_id = data1.get("transaction_id");
|
// if("REFUND".equals(s) || "NOTPAY".equals(s) || "CLOSED".equals(s) || "REVOKED".equals(s) || "PAYERROR".equals(s) || num == 10){
|
//
|
// break;
|
// }
|
// if("SUCCESS".equals(s)){
|
// wxcallback(code,transaction_id);
|
// break;
|
// }
|
// if("USERPAYING".equals(s)){
|
// num++;
|
// }
|
// }
|
// }
|
// }catch (Exception e){
|
// e.printStackTrace();
|
// }
|
// }
|
// }).start();
|
// }
|
return weixinpay;
|
}
|
|
@Resource
|
private TUserMapper userMapper;
|
|
@Override
|
public void alicallback(String code, String tradeNo) {
|
try {
|
TOrder order = orderMapper.selectOne(Wrappers.lambdaQuery(TOrder.class).eq(TOrder::getCode, code));
|
if(order.getIsPay() == 2){
|
order.setIsPay(1);
|
order.setOrderNum(tradeNo);
|
orderMapper.updateById(order);
|
TUser byId = userMapper.selectById(order.getUserId());
|
byId.setHasPay(byId.getHasPay()==null? order.getPayMoney() :byId.getHasPay().add(order.getPayMoney()));
|
userMapper.updateById(byId);
|
}
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public List<TShortPlayVO> getHotTwo(QueryWrapper<TShortPlay> last) {
|
List<TShortPlayVO> list = this.baseMapper.getHotTwo();
|
List<TShortPlayToType> tShortPlayToTypes = shortPlayToTypeMapper.selectList(Wrappers.lambdaQuery(TShortPlayToType.class));
|
for (TShortPlayVO tShortPlayVO : list) {
|
List<TShortPlayToType> shortPlayToTypes = tShortPlayToTypes.stream().filter(e -> tShortPlayVO.getId().equals(e.getShortPlayId())).collect(Collectors.toList());
|
List<Long> typeIds = shortPlayToTypes.stream().map(TShortPlayToType::getTypeId).collect(Collectors.toList());
|
tShortPlayVO.setTypeIds(typeIds);
|
// 封装类型名称
|
List<TShortPlayType> tShortPlayTypes = shortPlayTypeMapper.selectList(Wrappers.lambdaQuery(TShortPlayType.class)
|
.in(TShortPlayType::getId, typeIds));
|
String typeName = tShortPlayTypes.stream().map(TShortPlayType::getTypeName).collect(Collectors.joining(","));
|
tShortPlayVO.setTypeName(typeName);
|
tShortPlayVO.setShortPlayVideos(shortPlayVideoMapper.selectList(Wrappers.lambdaQuery(TShortPlayVideo.class).eq(TShortPlayVideo::getShortPlayId,tShortPlayVO.getId()).orderByAsc(TShortPlayVideo::getSortBy)));
|
}
|
return list;
|
}
|
|
@Override
|
public void wxcallback(String code, String transaction_id) {
|
try {
|
TOrder order = orderMapper.selectOne(Wrappers.lambdaQuery(TOrder.class).eq(TOrder::getCode, code));
|
if(order.getIsPay() == 1){
|
order.setIsPay(2);
|
order.setOrderNum(transaction_id);
|
orderMapper.updateById(order);
|
}
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}
|
|
}
|