luodangjia
2024-12-10 ee7ce5d1cbf80bee0a15c1e5bc5eaa30858d812b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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();
        }
    }
 
}