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() { @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 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() { @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() { @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 list = orderService.listOrderRecord(loginUid, type == null ? null : OrderType.valueOf(type), null, page, Constant.pageCount); List 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()); } } }