admin
2021-08-27 8fee151ffae0c3818694b7318583814bf92663e2
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package com.yeshi.buwan.controller.api;
 
import com.google.gson.*;
import com.yeshi.buwan.domain.user.LoginUser;
import com.yeshi.buwan.domain.vip.*;
import com.yeshi.buwan.dto.order.PPTVVideoPrice;
import com.yeshi.buwan.dto.order.PayWayInfoDTO;
import com.yeshi.buwan.exception.PPTVException;
import com.yeshi.buwan.exception.goldcorn.GoldCornException;
import com.yeshi.buwan.exception.order.OrderException;
import com.yeshi.buwan.exception.order.PayException;
import com.yeshi.buwan.exception.vip.VIPException;
import com.yeshi.buwan.exception.vip.VideoBuyRecordException;
import com.yeshi.buwan.videos.pptv.entity.PPTVSeries;
import com.yeshi.buwan.videos.pptv.entity.VideoPPTVMap;
import com.yeshi.buwan.service.inter.LoginUserService;
import com.yeshi.buwan.service.inter.juhe.PPTVService;
import com.yeshi.buwan.service.inter.order.OrderService;
import com.yeshi.buwan.service.inter.system.SystemConfigService;
import com.yeshi.buwan.service.inter.vip.VIPPriceService;
import com.yeshi.buwan.service.inter.vip.VIPService;
import com.yeshi.buwan.util.*;
import com.yeshi.buwan.vo.AcceptData;
import com.yeshi.buwan.vo.client.user.UserInfoVO;
import com.yeshi.buwan.vo.order.OrderInfoVO;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yeshi.utils.annotation.RequestSerializableByKey;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Controller
@RequestMapping("api/v2/vip")
public class VIPController {
 
    Logger logger = LoggerFactory.getLogger(VIPController.class);
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private VIPService vipService;
 
    @Resource
    private OrderService orderService;
 
    @Resource
    private LoginUserService loginUserService;
 
    @Resource
    private VIPPriceService vipPriceService;
 
    @Resource
    private SystemConfigService systemConfigService;
 
    @Resource
    private PPTVService pptvService;
 
 
    @RequestMapping("getVIPPriceList")
    @ResponseBody
    public String getVIPPriceList(AcceptData acceptData, String loginUid) {
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(VIPPriceType.class, new JsonSerializer<VIPPriceType>() {
            @Override
            public JsonElement serialize(VIPPriceType value, Type theType, JsonSerializationContext context) {
                if (value == null) {
                    return new JsonPrimitive("");
                } else {
                    return new JsonPrimitive(value.getName());
                }
            }
        }).create();
        JSONObject root = new JSONObject();
        if (!StringUtil.isNullOrEmpty(loginUid)) {
            LoginUser user = loginUserService.getLoginUser(loginUid);
            if (user == null) {
                return JsonUtilV2.loadFalseJson("用户不存在");
            }
            UserVIPInfo vipInfo = vipService.getVIPInfo(loginUid);
            UserInfoVO userInfoVO = new UserInfoVO();
            userInfoVO.setId(user.getId());
            userInfoVO.setNickName(user.getName());
            userInfoVO.setPortrait(user.getPortrait());
            if (StringUtil.isNullOrEmpty(userInfoVO.getPortrait())) {
                String portrait = systemConfigService.getConfigValueByKeyCache("default_portrait");
                userInfoVO.setPortrait(portrait);
            }
 
            if (vipInfo != null && vipInfo.getExpireDate() != null)
                userInfoVO.setVipExpireTime(vipInfo.getExpireDate().getTime());
            root.put("user", new Gson().toJson(userInfoVO));
        }
 
        List<VIPPrice> vipPriceList = vipPriceService.listValidPrice();
        root.put("list", gson.toJson(vipPriceList));
        return JsonUtilV2.loadTrueJson(root.toString());
    }
 
 
    @RequestMapping("getOrderList")
    @ResponseBody
    public String getOrderList(AcceptData acceptData, String loginUid, String type, int page) {
        Gson gson = new GsonBuilder().registerTypeAdapter(VIPPriceType.class, new JsonSerializer<VIPPriceType>() {
            @Override
            public JsonElement serialize(VIPPriceType value, Type theType, JsonSerializationContext context) {
                if (value == null) {
                    return new JsonPrimitive("");
                } else {
                    return new JsonPrimitive(value.getName());
                }
            }
        }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
            @Override
            public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) {
                if (value == null) {
                    return new JsonPrimitive("");
                } else {
                    return new JsonPrimitive(TimeUtil.getGernalTime(value.getTime(), "yyyy.MM.dd HH:mm"));
                }
            }
        }).create();
        JSONObject root = new JSONObject();
 
        List<OrderRecord> list = orderService.listOrderRecord(loginUid, type == null ? null : OrderType.valueOf(type), null, page, Constant.pageCount);
 
        List<OrderInfoVO> voList = new ArrayList<>();
        for (OrderRecord record : list) {
            voList.add(OrderInfoVO.create(record));
        }
        long count = orderService.countOrderRecord(loginUid, type == null ? null : OrderType.valueOf(type), null);
        root.put("list", gson.toJson(voList));
        root.put("count", count);
        return JsonUtilV2.loadTrueJson(root.toString());
    }
 
    /**
     * 生成订单
     *
     * @param acceptData
     * @param loginUid
     * @return
     */
    @RequestMapping("createOrder")
    @ResponseBody
    public String createOrder(AcceptData acceptData, HttpServletRequest request, String loginUid, String priceId, String cid, String vid, Integer goldCorn, int payWay) {
 
        if (StringUtil.isNullOrEmpty(loginUid)) {
            return JsonUtilV2.loadFalseJson("用户未登录");
        }
 
        LoginUser user = loginUserService.getLoginUser(loginUid);
        if (user == null) {
            return JsonUtilV2.loadFalseJson("用户不存在");
        }
 
        if (StringUtil.isNullOrEmpty(user.getPhone()) && payWay != OrderRecord.PAY_WAY_IAPP) {
            return JsonUtilV2.loadFalseJson(10001, "请绑定电话号码");
        }
 
        if (StringUtil.isNullOrEmpty(priceId) && StringUtil.isNullOrEmpty(cid)) {
            return JsonUtilV2.loadFalseJson("请选择购买类型");
        }
 
        if (goldCorn == null)
            goldCorn = 0;
 
        OrderType orderType = OrderType.vip;
 
        if (!StringUtil.isNullOrEmpty(cid)) {
            orderType = OrderType.video;
        }
 
 
        String ip = IPUtil.getRemotIP(request);
 
        OrderRecord record = new OrderRecord();
 
        VIPPrice vipPrice = null;
 
        if (orderType == OrderType.video) {
            PPTVSeries pptvSeries = pptvService.selectSeriesBySeriesCode(cid);
            if (pptvSeries == null) {
                return JsonUtilV2.loadFalseJson("影片不存在");
            }
            //视频
            VideoPPTVMap map = pptvService.selectVideoPPTVMapByPPInfo(pptvSeries.getInfoID(), vid);
            if (map == null) {
                return JsonUtilV2.loadFalseJson("影片不存在");
            }
            record.setRemarks(pptvSeries.getName());
        } else {
            vipPrice = vipPriceService.selectByPrimaryKey(priceId);
            if (vipPrice == null) {
                return JsonUtilV2.loadFalseJson("套餐不存在");
            }
        }
 
 
        record.setUid(loginUid);
        if (vipPrice != null) {
            record.setType(vipPrice.getType());
            record.setMoney(vipPrice.getActualPrice());
        } else {
            PPTVVideoPrice price = new Gson().fromJson(systemConfigService.getConfigValueByKeyCache("videoPrice"), PPTVVideoPrice.class);
            record.setMoney(price.getActualPrice().subtract(new BigDecimal(goldCorn).divide(new BigDecimal(100), 2, RoundingMode.UP)));
        }
 
        record.setOrderType(orderType);
        record.setVideoCid(cid);
        record.setVideoVid(vid);
        record.setGoldCorn(goldCorn);
        record.setPayWay(payWay);
        record.setState(OrderRecord.STATE_NOT_PAY);
        record.setIpInfo(IPUtil.getRemotIP(request) + ":" + IPUtil.getRemotePort(request));
        try {
            orderService.createOrder(record);
        } catch (OrderException e) {
            logger.error("生成订单出错", e);
            return JsonUtilV2.loadFalseJson("生成订单出错");
        }
 
 
        PayWayInfoDTO payResult = null;
 
        try {
            payResult = orderService.payOrder(record);
        } catch (OrderException e) {
            return JsonUtilV2.loadFalseJson(e.getMessage());
        } catch (GoldCornException e) {
            e.printStackTrace();
            return JsonUtilV2.loadFalseJson(e.getMessage());
        } catch (PayException e) {
            e.printStackTrace();
            return JsonUtilV2.loadFalseJson(e.getMessage());
        } catch (VIPException e) {
            return JsonUtilV2.loadFalseJson(e.getMessage());
        } catch (PPTVException e) {
            return JsonUtilV2.loadFalseJson(e.getMessage());
        } catch (VideoBuyRecordException e) {
            return JsonUtilV2.loadFalseJson("单片购买失败");
        }
        if (payResult.getPayWay() == OrderRecord.PAY_WAY_IAPP) {
            //苹果内购
            JSONObject root = new JSONObject();
            root.put("orderNo", record.getId());
            root.put("productId", vipPrice.getIosProductId());
            return JsonUtilV2.loadTrueJson(root.toString());
        } else {
            return JsonUtilV2.loadTrueJson(new Gson().toJson(payResult));
        }
    }
 
 
    /**
     * 检查是否支付
     *
     * @param acceptData
     * @param loginUid
     * @param id
     * @param receipt    -ios支付结果receipt
     * @return
     */
    @RequestMapping("checkPay")
    @ResponseBody
    @RequestSerializableByKey(key = "'vip-checkPay-'+#id")
    public String checkPay(AcceptData acceptData, String loginUid, String id, String receipt) {
        OrderRecord record = orderService.getOrderRecord(id);
        if (record == null || !record.getUid().equalsIgnoreCase(loginUid)) {
            return JsonUtilV2.loadFalseJson("记录不存在/不是您的订单");
        }
 
        if (record.getPayWay() == OrderRecord.PAY_WAY_IAPP) {
            try {
                orderService.checkApplePay(record.getId(), receipt);
                JSONObject data = new JSONObject();
                return JsonUtilV2.loadTrueJson(data.toString());
            } catch (Exception e) {
                return JsonUtilV2.loadFalseJson(e.getMessage());
            }
        } else {
            record = orderService.checkOrderPayState(id);
            //未支付
            if (record != null && record.getState() != OrderRecord.STATE_PAY) {
                return JsonUtilV2.loadFalseJson(1, "支付未完成");
            }
 
            JSONObject data = new JSONObject();
            data.put("money", record.getPayMoney() == null ? record.getMoney() : record.getPayMoney());
            return JsonUtilV2.loadTrueJson(data.toString());
        }
    }
 
 
}