package com.yeshi.fanli.controller.client.v1; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.yeshi.utils.JsonUtil; import org.yeshi.utils.exception.WXOrderException; import com.yeshi.fanli.entity.accept.AcceptData; import com.yeshi.fanli.entity.bus.homemodule.SwiperPicture; import com.yeshi.fanli.entity.redpack.RedPackBalance; import com.yeshi.fanli.entity.shop.BanLiShopGoods; import com.yeshi.fanli.entity.shop.BanLiShopGoodsSets; import com.yeshi.fanli.entity.shop.BanLiShopGoodsSetsPay; import com.yeshi.fanli.entity.shop.BanLiShopOrder; import com.yeshi.fanli.exception.redpack.RedPackBalanceException; import com.yeshi.fanli.exception.shop.BanLiShopOrderException; import com.yeshi.fanli.service.inter.homemodule.SwiperPictureService; import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService; import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsService; import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetPayService; import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetService; import com.yeshi.fanli.service.inter.shop.BanLiShopOrderPayService; import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService; import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.StringUtil; import com.yeshi.fanli.util.factory.shop.BanLiShopOrderGoodsVOFactory; import com.yeshi.fanli.util.wx.BanLiShopWXPayUtil; import com.yeshi.fanli.vo.shop.BanLiShopOrderVO; import net.sf.json.JSONObject; /** * 板栗商城 * * @author Administrator * */ @Controller @RequestMapping("api/v1/banlishop") public class BanLiShopController { @Resource private BanLiShopGoodsService banLiShopGoodsService; @Resource private BanLiShopOrderService banLiShopOrderService; @Resource private BanLiShopOrderPayService banLiShopOrderPayService; @Resource private SwiperPictureService swiperPictureService; @Resource private RedPackBalanceService redPackBalanceService; @Resource private BanLiShopGoodsSetPayService banLiShopGoodsSetPayService; @Resource private BanLiShopGoodsSetService banLiShopGoodsSetService; /** * 商品列表 * * @param acceptData * @param page * @param callback * @param out */ @RequestMapping(value = "goodsList") public void goodsList(AcceptData acceptData, int page,Long uid, String callback, PrintWriter out) { if (page <= 0) page = 1; List goodsList = banLiShopGoodsService.listGoods(null, BanLiShopGoods.STATE_ONLINE, page, Constant.PAGE_SIZE); long count = banLiShopGoodsService.countGoods(null, BanLiShopGoods.STATE_ONLINE); // 获取banner List pictureList = swiperPictureService.getByBannerCard("hongbao_exchange_goods_list"); JSONObject data = new JSONObject(); if (pictureList != null && pictureList.size() > 0) { data.put("picture", pictureList.get(0).getSrc()); } data.put("data", goodsList); data.put("count", count); RedPackBalance redPackBalance = redPackBalanceService.selectByPrimaryKey(uid); if (redPackBalance != null && redPackBalance.getState() == RedPackBalance.STATE_LOCKED) { data.put("redPackLock", true); } else { data.put("redPackLock", false); } if (!StringUtil.isNullOrEmpty(callback)) out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data))); else out.print(JsonUtil.loadTrueResult(data)); } @RequestMapping(value = "orderList") public void orderList(AcceptData acceptData, Long uid, int page, String callback, String from, PrintWriter out) { if (page <= 0) page = 1; List orderList = banLiShopOrderService.listByUid(uid, page, Constant.PAGE_SIZE); if (!StringUtil.isNullOrEmpty("from") && from.equalsIgnoreCase("pay")) {// 从支付界面过来 // 判断第一个订单是否支付成功 if (orderList.size() > 0) { BanLiShopOrder latestOrder = orderList.get(0); if (latestOrder.getMoneyPayment() != null && latestOrder.getMoneyPayment().compareTo(new BigDecimal(0)) > 0) { // 判断微信支付是否成功 try { boolean isPaySuccess = BanLiShopWXPayUtil.isPaySuccess(latestOrder.getOrderNo()); if (isPaySuccess) { try { banLiShopOrderPayService.payOrderByMoney(latestOrder.getId(), latestOrder.getMoneyPayment()); orderList = banLiShopOrderService.listByUid(uid, page, Constant.PAGE_SIZE); } catch (BanLiShopOrderException e) { e.printStackTrace(); } } } catch (WXOrderException e) { e.printStackTrace(); } } } } List voList = new ArrayList<>(); for (BanLiShopOrder order : orderList) { voList.add(BanLiShopOrderGoodsVOFactory.create(order)); } long count = banLiShopOrderService.countByUid(uid); JSONObject data = new JSONObject(); data.put("data", voList); data.put("count", count); if (!StringUtil.isNullOrEmpty(callback)) out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data))); else out.print(JsonUtil.loadTrueResult(data)); } private String getPayName(int payType) { switch (payType) { case BanLiShopGoodsSetsPay.PAY_TYPE_BALANCE: return "仅账户余额"; case BanLiShopGoodsSetsPay.PAY_TYPE_HONGBAO: return "仅用红包"; case BanLiShopGoodsSetsPay.PAY_TYPE_HONGBAO_MONEY: return "现金和红包"; case BanLiShopGoodsSetsPay.PAY_TYPE_MONEY: return "仅用现金"; } return ""; } /** * 获取商品详情 * * @param acceptData * @param uid * @param id * @param callback * @param out */ @RequestMapping(value = "getGoodsDetail") public void getGoodsDetail(AcceptData acceptData, Long uid, Long id, String callback, PrintWriter out) { BanLiShopGoods goods = banLiShopGoodsService.getGoodsDetail(id); if (goods != null) for (BanLiShopGoodsSets set : goods.getSetsList()) { for (BanLiShopGoodsSetsPay pay : set.getPayList()) { pay.setName(getPayName(pay.getPayType())); } } BigDecimal money = new BigDecimal(0); try { money = redPackBalanceService.getBalance(uid); } catch (RedPackBalanceException e) { e.printStackTrace(); } JSONObject data = new JSONObject(); data.put("goods", goods); data.put("hongBaoBalance", money); if (!StringUtil.isNullOrEmpty(callback)) out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data))); else out.print(JsonUtil.loadTrueResult(data)); } @RequestMapping(value = "buy") public void buy(AcceptData acceptData, Long uid, Long payId, String account, String callback, HttpServletRequest request, PrintWriter out) { if (payId == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请选择支付方式")); return; } if (uid == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("用户未登录")); return; } try { BanLiShopOrder order = banLiShopOrderService.createOrder(payId, account, uid); banLiShopOrderService.addOrder(order); // 红包支付 if (order.getHongBaoPayment() != null) { try { banLiShopOrderPayService.payOrderByHongBao(order.getId()); // 红包支付成功 } catch (RedPackBalanceException e) { if (!StringUtil.isNullOrEmpty(callback)) out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult(100, "红包支付失败"))); else out.print(JsonUtil.loadFalseResult(100, "红包支付失败")); return; } } // 看是否需要微信支付 if (order.getMoneyPayment() != null && order.getMoneyPayment().compareTo(new BigDecimal(0)) > 0) { // 走微信支付 String payUrl = BanLiShopWXPayUtil.getWXH5PayUrl(order.getOrderNo(), request.getRemoteAddr(), order.getOrderGoods().getGoodsName(), order.getMoneyPayment(), acceptData.getPlatform()); if (StringUtil.isNullOrEmpty(payUrl)) { if (!StringUtil.isNullOrEmpty(callback)) out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult(102, "微信支付创建失败"))); else out.print(JsonUtil.loadFalseResult(102, "微信支付创建失败")); return; } JSONObject data = new JSONObject(); data.put("wxPayUrl", payUrl); if (!StringUtil.isNullOrEmpty(callback)) out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data))); else out.print(JsonUtil.loadTrueResult(data)); } else {// 不需要微信支付 JSONObject data = new JSONObject(); if (!StringUtil.isNullOrEmpty(callback)) out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data))); else out.print(JsonUtil.loadTrueResult(data)); } } catch (BanLiShopOrderException e) { if (!StringUtil.isNullOrEmpty(callback)) out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult(e.getCode(), e.getMsg()))); else out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg())); } } }