| @@ -3,6 +3,7 @@ package com.simple.schedule; | |||
| import com.simple.domain.po.WxOrder; | |||
| import com.simple.enums.EnumOrderStatus; | |||
| import com.simple.mapper.WxOrderMapper; | |||
| import com.simple.service.WxOrderService; | |||
| import org.apache.log4j.Logger; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.scheduling.annotation.Scheduled; | |||
| @@ -25,7 +26,10 @@ public class OrderExpiringSchedule { | |||
| @Autowired | |||
| WxOrderMapper wxOrderMapper; | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| @Autowired | |||
| WxOrderService wxOrderService; | |||
| @Scheduled(cron = "0 */5 * * * *?") // 每5分钟检查一次 | |||
| //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次 | |||
| public void orderExpireSchedule() { | |||
| @@ -38,13 +42,21 @@ public class OrderExpiringSchedule { | |||
| List<WxOrder> wxOrderList = wxOrderMapper.findListOfUnpaidOrderByDate(dateMap); | |||
| for(WxOrder wxOrder: wxOrderList){ | |||
| wxOrder.setUpdateDate(new Date()); | |||
| wxOrder.setOrderStatus(EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL.getCode()); | |||
| wxOrderMapper.updateByPrimaryKeySelective(wxOrder); | |||
| logger.info("\nFound " + wxOrder.getId() + "\n" | |||
| + " create at " + wxOrder.getCreateDate() +"\n" | |||
| + " expired at " + new Date()); | |||
| orderExpired(wxOrder); | |||
| } | |||
| } | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void orderExpired(WxOrder order) { | |||
| order.setUpdateDate(new Date()); | |||
| order.setOrderStatus(EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL.getCode()); | |||
| wxOrderMapper.updateByPrimaryKeySelective(order); | |||
| // 微信 关单 | |||
| wxOrderService.orderClose(order); | |||
| logger.info("\nFound " + order.getId() + "\n" | |||
| + " create at " + order.getCreateDate() +"\n" | |||
| + " expired at " + new Date()); | |||
| } | |||
| } | |||
| @@ -4,6 +4,7 @@ import java.util.*; | |||
| import com.github.pagehelper.PageHelper; | |||
| import com.github.pagehelper.PageInfo; | |||
| import com.simple.common.ResultData; | |||
| import com.simple.domain.po.WxCUser; | |||
| import com.simple.domain.po.WxCouponOrder; | |||
| import com.simple.domain.po.WxOrder; | |||
| @@ -60,8 +61,13 @@ public interface WxOrderService { | |||
| * @param enumOrderStatus | |||
| */ | |||
| int updateOrderStatus(WxOrder updateOrder, EnumOrderStatus enumOrderStatus); | |||
| /** | |||
| /** | |||
| * 订单关单 | |||
| */ | |||
| ResultData orderClose(WxOrder order); | |||
| /** | |||
| * 根据Id获得实体 | |||
| * | |||
| * @param id | |||
| @@ -4,16 +4,15 @@ import com.github.pagehelper.PageHelper; | |||
| import com.github.pagehelper.PageInfo; | |||
| import com.simple.common.ErrorCode; | |||
| import com.simple.common.IdWorker; | |||
| import com.simple.domain.po.WxCUser; | |||
| import com.simple.domain.po.WxCoupon; | |||
| import com.simple.domain.po.WxCouponOrder; | |||
| import com.simple.domain.po.WxOrder; | |||
| import com.simple.common.ResultData; | |||
| import com.simple.domain.po.*; | |||
| import com.simple.domain.vo.WxOrderCVo; | |||
| import com.simple.enums.*; | |||
| import com.simple.exception.MallinkException; | |||
| import com.simple.mapper.*; | |||
| import com.simple.service.WxCUserService; | |||
| import com.simple.service.WxOrderService; | |||
| import com.simple.service.WxPayOrderService; | |||
| import com.simple.utils.RedisLock; | |||
| import org.apache.log4j.Logger; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| @@ -60,6 +59,12 @@ public class WxOrderServiceImpl implements WxOrderService { | |||
| @Autowired | |||
| WxCUserService wxCUserService; | |||
| @Autowired | |||
| WxPayOrderMapper wxPayOrderMapper; | |||
| @Autowired | |||
| WxPayOrderService wxPayOrderService; | |||
| @Override | |||
| public PageInfo<WxOrder> listAsPage(WxOrder record, Integer pageIndex, Integer pageSize) { | |||
| return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxOrderMapper.findList(record)); | |||
| @@ -539,6 +544,37 @@ public class WxOrderServiceImpl implements WxOrderService { | |||
| return ret; | |||
| } | |||
| /** | |||
| * 微信关闭订单 | |||
| */ | |||
| @Override | |||
| public ResultData orderClose(WxOrder order) { | |||
| // 1. get c appid | |||
| WxAppinfo appInfo = null; | |||
| WxAppinfo appinfoQ = new WxAppinfo(); | |||
| appinfoQ.setTenantId(order.getTenantId()); | |||
| appinfoQ.setType(EnumAppType.C.getCode()); | |||
| List<WxAppinfo> appList = wxAppinfoMapper.select(appinfoQ); | |||
| if (appList.size() > 0) { | |||
| appInfo = appList.get(0); | |||
| } | |||
| // 2. get pay order | |||
| WxPayOrder payOrderQ = new WxPayOrder(); | |||
| payOrderQ.setTenantId(order.getTenantId()); | |||
| payOrderQ.setPayOrderStatus(EnumPayStatus.PAY_WAY_WAIT.getCode()); | |||
| List<WxPayOrder> payOrders = wxPayOrderMapper.select(payOrderQ); | |||
| for(WxPayOrder payOrder: payOrders) { | |||
| // 3. wx pay order close | |||
| try { | |||
| ResultData payclose = wxPayOrderService.payOrderClose(appInfo, payOrder); | |||
| } catch (Exception e) { | |||
| logger.error("微信关单失败:" + e.getMessage()); | |||
| } | |||
| } | |||
| return new ResultData(); | |||
| } | |||
| @Override | |||
| public WxOrder getById(Long id) { | |||
| return wxOrderMapper.selectByPrimaryKey(id); | |||
| @@ -493,8 +493,9 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| } | |||
| } | |||
| /** | |||
| * 微信关闭订单 | |||
| * 微信关闭支付订单 | |||
| */ | |||
| @Override | |||
| public ResultData payOrderClose(WxAppinfo appInfo, WxPayOrder record) { | |||
| @@ -519,12 +520,18 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| String result_code = returnMap.get("result_code"); | |||
| if ("SUCCESS".equals(result_code)) { | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_WAY_FAIL.getCode()); | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_WAY_CANCEL.getCode()); | |||
| handlePayOrderStatusUpdate(record); | |||
| return new ResultData(Result.SUCCESS, "订单关闭成功", returnMap); | |||
| } else { | |||
| String errMsg = ""; | |||
| JSONObject errObj = errorMapClose.getJSONObject(result_code); | |||
| record.setFailReason(errObj.toJSONString()); | |||
| if (errObj != null) { | |||
| errMsg = errObj.toJSONString(); | |||
| } else { | |||
| errMsg = returnMap.get("return_msg"); | |||
| } | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateByPrimaryKeySelective(record); | |||
| @@ -561,12 +568,18 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| String result_code = returnMap.get("result_code"); | |||
| if ("SUCCESS".equals(result_code)) { | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_WAY_FAIL.getCode()); | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_WAY_CANCEL.getCode()); | |||
| handlePayOrderStatusUpdate(record); | |||
| return new ResultData(Result.SUCCESS, "订单关闭成功", returnMap); | |||
| } else { | |||
| String errMsg = ""; | |||
| JSONObject errObj = errorMapClose.getJSONObject(result_code); | |||
| record.setFailReason(errObj.toJSONString()); | |||
| if (errObj != null) { | |||
| errMsg = errObj.toJSONString(); | |||
| } else { | |||
| errMsg = returnMap.get("return_msg"); | |||
| } | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateByPrimaryKeySelective(record); | |||
| @@ -574,7 +587,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errObj.toJSONString(), returnMap); | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errMsg, returnMap); | |||
| } | |||
| } catch (RuntimeException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| @@ -755,24 +768,6 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "订单数据库更新失败: " + e.getMessage()); | |||
| } | |||
| // 微信模板通知用户 | |||
| /* | |||
| try { | |||
| WxAppinfo appinfoQ = new WxAppinfo(); | |||
| appinfoQ.setTenantId(order.getTenantId()); | |||
| appinfoQ.setType(EnumAppType.C.getCode()); | |||
| List<WxAppinfo> appinfoList = wxAppinfoMapper.select(appinfoQ); | |||
| if (appinfoList.size() >0) { | |||
| WxAppinfo appinfo = appinfoList.get(0); | |||
| sendPaySuccessMsg(appinfo, order, updateOrder); | |||
| } | |||
| } catch (Exception e) { | |||
| logger.error("微信模板通知error: " + e.getMessage()); | |||
| // throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "订单数据库更新失败: " + e.getMessage()); | |||
| } | |||
| */ | |||
| // End of 微信模板通知用户 | |||
| /* | |||
| Map<String, String> msgMap = new HashMap<>(); | |||
| try { | |||
| @@ -825,6 +820,28 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| logger.error(e.getMessage()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| if (record.getPayOrderStatus() == EnumPayStatus.PAY_WAY_CANCEL.getCode()) { | |||
| // 支付订单取消 | |||
| if (record.getPayAmount() > 0) { | |||
| // 1. get appinfo | |||
| WxAppinfo appInfo = null; | |||
| WxAppinfo appinfoQ = new WxAppinfo(); | |||
| appinfoQ.setTenantId(order.getTenantId()); | |||
| appinfoQ.setType(EnumAppType.C.getCode()); | |||
| List<WxAppinfo> appList = wxAppinfoMapper.select(appinfoQ); | |||
| if (appList.size() > 0) { | |||
| appInfo = appList.get(0); | |||
| } | |||
| WxPayOrder payOrderQ = new WxPayOrder(); | |||
| payOrderQ.setTenantId(order.getTenantId()); | |||
| payOrderQ.setPayOrderStatus(EnumPayStatus.PAY_WAY_WAIT.getCode()); | |||
| List<WxPayOrder> payOrders = wxPayOrderMapper.select(payOrderQ); | |||
| for(WxPayOrder payOrder: payOrders) { | |||
| payOrderClose(appInfo, payOrder); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| // 修改订单状态 | |||