| @@ -109,7 +109,7 @@ public class WxCarCallBackController extends BaseController { | |||||
| // TODO 可能多用户关联同一张车牌 | // TODO 可能多用户关联同一张车牌 | ||||
| List<WxCUserCar> userCarList = wxCUserCarService.getList(userCarQ); | List<WxCUserCar> userCarList = wxCUserCarService.getList(userCarQ); | ||||
| for (WxCUserCar userCar : userCarList) { | for (WxCUserCar userCar : userCarList) { | ||||
| wxCouponSendService.sendCouponToUser(tenantId, userCar.getCUserId(), EnumCouponSendType.CAR_STOP.getCode()); | |||||
| wxCouponSendService.sendCouponToUser(tenantId, userCar.getCUserId(), EnumCouponSendType.CAR_STOP); | |||||
| } | } | ||||
| } | } | ||||
| @@ -0,0 +1,40 @@ | |||||
| package com.simple.enums; | |||||
| /** | |||||
| * Created by Stormeye on 2018/08/09. | |||||
| */ | |||||
| public enum EnumCouponActionChannelType { | |||||
| // 0:精准 1:主动领取 2:用户购买 3:停车 4:核销 | |||||
| PRECISE(0, "精准"), | |||||
| RECEIVE(1, "主动领取"), | |||||
| BUY(2, "用户购买"), | |||||
| CAR(3, "停车"), | |||||
| VERIFY(4, "核销") | |||||
| ; | |||||
| public static EnumCouponActionChannelType getEnum(Integer code) { | |||||
| for (EnumCouponActionChannelType value : values()) { | |||||
| if (value.getCode().equals(code)) { | |||||
| return value; | |||||
| } | |||||
| } | |||||
| return null; | |||||
| } | |||||
| private Integer code; | |||||
| private String message; | |||||
| EnumCouponActionChannelType(Integer code, String message) { | |||||
| this.code = code; | |||||
| this.message = message; | |||||
| } | |||||
| public Integer getCode() { | |||||
| return code; | |||||
| } | |||||
| public String getMessage() { | |||||
| return message; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,38 @@ | |||||
| package com.simple.enums; | |||||
| /** | |||||
| * Created by Stormeye on 2018/08/09. | |||||
| */ | |||||
| public enum EnumEnableType { | |||||
| // 0:启用 1:停用 | |||||
| Enable(0, "启用"), | |||||
| Disable(1, "停用") | |||||
| ; | |||||
| public static EnumEnableType getEnum(Integer code) { | |||||
| for (EnumEnableType value : values()) { | |||||
| if (value.getCode().equals(code)) { | |||||
| return value; | |||||
| } | |||||
| } | |||||
| return null; | |||||
| } | |||||
| private Integer code; | |||||
| private String message; | |||||
| EnumEnableType(Integer code, String message) { | |||||
| this.code = code; | |||||
| this.message = message; | |||||
| } | |||||
| public Integer getCode() { | |||||
| return code; | |||||
| } | |||||
| public String getMessage() { | |||||
| return message; | |||||
| } | |||||
| } | |||||
| @@ -3,6 +3,7 @@ package com.simple.service; | |||||
| import java.util.*; | import java.util.*; | ||||
| import com.github.pagehelper.PageInfo; | import com.github.pagehelper.PageInfo; | ||||
| import com.simple.domain.po.WxCouponSend; | import com.simple.domain.po.WxCouponSend; | ||||
| import com.simple.enums.EnumCouponSendType; | |||||
| public interface WxCouponSendService { | public interface WxCouponSendService { | ||||
| @@ -10,8 +11,8 @@ public interface WxCouponSendService { | |||||
| * 根据实体查询分页列表 | * 根据实体查询分页列表 | ||||
| * | * | ||||
| * @param record | * @param record | ||||
| * @param offset | |||||
| * @param limit | |||||
| * @param pageIndex | |||||
| * @param pageSize | |||||
| * @return | * @return | ||||
| */ | */ | ||||
| PageInfo<WxCouponSend> listAsPage(WxCouponSend record, Integer pageIndex, Integer pageSize); | PageInfo<WxCouponSend> listAsPage(WxCouponSend record, Integer pageIndex, Integer pageSize); | ||||
| @@ -44,7 +45,7 @@ public interface WxCouponSendService { | |||||
| * @param cUserId | * @param cUserId | ||||
| * @param type 2:停车发券 3:核销发券 | * @param type 2:停车发券 3:核销发券 | ||||
| */ | */ | ||||
| void sendCouponToUser(String tenantId,Long cUserId,int type); | |||||
| void sendCouponToUser(String tenantId, Long cUserId, EnumCouponSendType type); | |||||
| void updateStatusByCouponId(Long couponId,String tenantId,int status); | void updateStatusByCouponId(Long couponId,String tenantId,int status); | ||||
| @@ -5,6 +5,7 @@ import java.util.*; | |||||
| import com.github.pagehelper.PageHelper; | import com.github.pagehelper.PageHelper; | ||||
| import com.github.pagehelper.PageInfo; | import com.github.pagehelper.PageInfo; | ||||
| import com.simple.domain.po.WxCUser; | import com.simple.domain.po.WxCUser; | ||||
| import com.simple.domain.po.WxCouponOrder; | |||||
| import com.simple.domain.po.WxOrder; | import com.simple.domain.po.WxOrder; | ||||
| import com.simple.domain.vo.WxOrderCVo; | import com.simple.domain.vo.WxOrderCVo; | ||||
| import com.simple.enums.EnumOrderStatus; | import com.simple.enums.EnumOrderStatus; | ||||
| @@ -34,10 +35,18 @@ public interface WxOrderService { | |||||
| * 免费券订单接口 | * 免费券订单接口 | ||||
| * @param userId | * @param userId | ||||
| * @param couponId | * @param couponId | ||||
| * @return 订单id | |||||
| * @return WxOrder | |||||
| */ | */ | ||||
| WxOrder sendUserFreeCoupon(Long userId, Long couponId); | WxOrder sendUserFreeCoupon(Long userId, Long couponId); | ||||
| /** | |||||
| * 免费券订单接口 | |||||
| * @param userId | |||||
| * @param couponId | |||||
| * @return WxCouponOrder | |||||
| */ | |||||
| WxCouponOrder sendFreeCouponToUser(Long userId, Long couponId); | |||||
| /** | /** | ||||
| * orderSuccess 订单已支付 | * orderSuccess 订单已支付 | ||||
| * @param updateOrder | * @param updateOrder | ||||
| @@ -366,7 +366,7 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { | |||||
| WxOrder wxOrder = wxOrderMapper.selectByPrimaryKey(couponOrder.getOrderId()); | WxOrder wxOrder = wxOrderMapper.selectByPrimaryKey(couponOrder.getOrderId()); | ||||
| if (wxOrder != null) { | if (wxOrder != null) { | ||||
| try { | try { | ||||
| wxCouponSendService.sendCouponToUser(bUser.getTenantId(), wxOrder.getCUserId(), EnumCouponSendType.COUPON_VERIFY.getCode()); | |||||
| wxCouponSendService.sendCouponToUser(bUser.getTenantId(), wxOrder.getCUserId(), EnumCouponSendType.COUPON_VERIFY); | |||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| logger.error("核销发券: " + e.getMessage()); | logger.error("核销发券: " + e.getMessage()); | ||||
| } | } | ||||
| @@ -1,34 +1,41 @@ | |||||
| package com.simple.service.impl; | package com.simple.service.impl; | ||||
| import java.util.*; | |||||
| import com.github.pagehelper.PageHelper; | import com.github.pagehelper.PageHelper; | ||||
| import com.github.pagehelper.PageInfo; | import com.github.pagehelper.PageInfo; | ||||
| import com.simple.domain.po.WxCoupon; | |||||
| import com.simple.domain.po.WxCouponOrder; | |||||
| import com.simple.domain.po.WxCouponSend; | |||||
| import com.simple.domain.po.WxMallConfig; | |||||
| import com.simple.common.IdWorker; | |||||
| import com.simple.domain.po.*; | |||||
| import com.simple.enums.*; | |||||
| import com.simple.mapper.WxCouponOrderMapper; | |||||
| import com.simple.mapper.WxCouponSendMapper; | import com.simple.mapper.WxCouponSendMapper; | ||||
| import com.simple.service.*; | import com.simple.service.*; | ||||
| import org.apache.commons.lang.time.DateUtils; | import org.apache.commons.lang.time.DateUtils; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
| import com.simple.common.IdWorker; | |||||
| import org.springframework.transaction.annotation.Transactional; | import org.springframework.transaction.annotation.Transactional; | ||||
| import java.util.Date; | |||||
| import java.util.List; | |||||
| @Service | @Service | ||||
| public class WxCouponSendServiceImpl implements WxCouponSendService { | public class WxCouponSendServiceImpl implements WxCouponSendService { | ||||
| @Autowired | |||||
| @Autowired | |||||
| WxCouponSendMapper wxCouponSendMapper; | WxCouponSendMapper wxCouponSendMapper; | ||||
| @Autowired | |||||
| @Autowired | |||||
| WxCouponOrderService wxCouponOrderService; | WxCouponOrderService wxCouponOrderService; | ||||
| @Autowired | |||||
| @Autowired | |||||
| WxCouponService wxCouponService; | WxCouponService wxCouponService; | ||||
| @Autowired | |||||
| @Autowired | |||||
| WxCouponActionLogService wxCouponActionLogService; | WxCouponActionLogService wxCouponActionLogService; | ||||
| @Autowired | |||||
| @Autowired | |||||
| WxMallConfigService wxMallConfigService; | WxMallConfigService wxMallConfigService; | ||||
| @Autowired | |||||
| WxOrderService wxOrderService; | |||||
| @Autowired | |||||
| WxCouponOrderMapper wxCouponOrderMapper; | |||||
| @Override | @Override | ||||
| public PageInfo<WxCouponSend> listAsPage(WxCouponSend record, Integer pageIndex, Integer pageSize) { | public PageInfo<WxCouponSend> listAsPage(WxCouponSend record, Integer pageIndex, Integer pageSize) { | ||||
| @@ -45,7 +52,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { | |||||
| if (record.getId() == null) { | if (record.getId() == null) { | ||||
| //record.setId(UUID.randomUUID().toString().replaceAll("-", "")); | //record.setId(UUID.randomUUID().toString().replaceAll("-", "")); | ||||
| final IdWorker idWorker = IdWorker.get(); | final IdWorker idWorker = IdWorker.get(); | ||||
| record.setId(idWorker.nextId()); | |||||
| record.setId(idWorker.nextId()); | |||||
| wxCouponSendMapper.insertSelective(record); | wxCouponSendMapper.insertSelective(record); | ||||
| } else { | } else { | ||||
| wxCouponSendMapper.updateByPrimaryKeySelective(record); | wxCouponSendMapper.updateByPrimaryKeySelective(record); | ||||
| @@ -59,61 +66,46 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { | |||||
| @Override | @Override | ||||
| @Transactional | @Transactional | ||||
| public void sendCouponToUser(String tenantId, Long cUserId, int type) { | |||||
| public void sendCouponToUser(String tenantId, Long cUserId, EnumCouponSendType type) { | |||||
| //查询开关是否打开 | //查询开关是否打开 | ||||
| //查询停车或者核销的券 stopCarCouponSwitch verifyConponSwitch | //查询停车或者核销的券 stopCarCouponSwitch verifyConponSwitch | ||||
| WxCouponSend wxCouponSendQuery = new WxCouponSend(); | WxCouponSend wxCouponSendQuery = new WxCouponSend(); | ||||
| wxCouponSendQuery.setTenantId(tenantId); | wxCouponSendQuery.setTenantId(tenantId); | ||||
| int actionLogType=0; | |||||
| String configKey=""; | |||||
| if(type==2){ | |||||
| int actionLogType = 0; | |||||
| String configKey = ""; | |||||
| if (type == EnumCouponSendType.CAR_STOP) { | |||||
| //停车 | //停车 | ||||
| wxCouponSendQuery.setSendType(2); | |||||
| actionLogType=3; | |||||
| configKey="stopCarCouponSwitch"; | |||||
| wxCouponSendQuery.setSendType(EnumCouponSendType.CAR_STOP.getCode()); | |||||
| actionLogType = EnumCouponActionChannelType.CAR.getCode(); | |||||
| configKey = "stopCarCouponSwitch"; | |||||
| } | } | ||||
| if(type==3){ | |||||
| if (type == EnumCouponSendType.COUPON_VERIFY) { | |||||
| //核销 | //核销 | ||||
| wxCouponSendQuery.setSendType(3); | |||||
| actionLogType=4; | |||||
| configKey="verifyConponSwitch"; | |||||
| }else{ | |||||
| wxCouponSendQuery.setSendType(EnumCouponSendType.COUPON_VERIFY.getCode()); | |||||
| actionLogType = EnumCouponActionChannelType.VERIFY.getCode(); | |||||
| configKey = "verifyConponSwitch"; | |||||
| } else { | |||||
| return; | return; | ||||
| } | } | ||||
| WxMallConfig wxMallConfigQuery = new WxMallConfig(); | WxMallConfig wxMallConfigQuery = new WxMallConfig(); | ||||
| wxMallConfigQuery.setKey(configKey); | wxMallConfigQuery.setKey(configKey); | ||||
| wxMallConfigQuery.setTenantId(tenantId); | wxMallConfigQuery.setTenantId(tenantId); | ||||
| PageInfo<WxMallConfig> page = wxMallConfigService.listAsPage(wxMallConfigQuery, 1, 1); | PageInfo<WxMallConfig> page = wxMallConfigService.listAsPage(wxMallConfigQuery, 1, 1); | ||||
| if(page.getSize()>0) { | |||||
| if (page.getSize() > 0) { | |||||
| WxMallConfig config = page.getList().get(0); | WxMallConfig config = page.getList().get(0); | ||||
| if(config.getValue()==1){ | |||||
| if (config.getValue() == EnumEnableType.Disable.getCode()) { | |||||
| return; | return; | ||||
| } | } | ||||
| } | } | ||||
| List<WxCouponSend> wxCouponSends = wxCouponSendMapper.findList(wxCouponSendQuery); | List<WxCouponSend> wxCouponSends = wxCouponSendMapper.findList(wxCouponSendQuery); | ||||
| for (WxCouponSend send:wxCouponSends) { | |||||
| WxCoupon wxCoupon= wxCouponService.getById(send.getCouponId()); | |||||
| WxCouponOrder wxCouponOrder = new WxCouponOrder(); | |||||
| wxCouponOrder.setCouponId(wxCoupon.getId()); | |||||
| wxCouponOrder.setCouponOrderStatus(0); | |||||
| wxCouponOrder.setCUserId(cUserId); | |||||
| wxCouponOrder.setCouponPrice(0); | |||||
| wxCouponOrder.setCreateDate(new Date()); | |||||
| if (wxCoupon.getValidType() == 1) { //时间范围区间 | |||||
| if(new Date().after(wxCoupon.getValidEndDate())){ | |||||
| continue; | |||||
| } | |||||
| wxCouponOrder.setExpiredTime(wxCoupon.getValidEndDate()); | |||||
| } else { | |||||
| Date date = DateUtils.addDays(new Date(), wxCoupon.getValidDays()); | |||||
| wxCouponOrder.setExpiredTime(date); | |||||
| for (WxCouponSend send : wxCouponSends) { | |||||
| // 发放免费券 | |||||
| WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(cUserId, send.getCouponId()); | |||||
| if (couponOrder != null) { | |||||
| wxCouponActionLogService.addOne(tenantId, send.getCouponId(), couponOrder.getId(), actionLogType, send.getId()); | |||||
| } | } | ||||
| wxCouponOrder.setTenantId(wxCoupon.getTenantId()); | |||||
| Long couponOrderId = wxCouponOrderService.insertOne(wxCouponOrder); | |||||
| wxCouponActionLogService.addOne(tenantId, wxCoupon.getId(), couponOrderId, actionLogType, send.getId()); | |||||
| wxCouponService.reduceInventory(wxCoupon.getId(),1); | |||||
| wxCouponService.reduceInventory(send.getCouponId(), 1); | |||||
| } | } | ||||
| } | } | ||||
| @@ -259,7 +259,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| * @param order | * @param order | ||||
| * @param coupon | * @param coupon | ||||
| */ | */ | ||||
| private void createCouponOrder(WxCUser user, WxOrder order, WxCoupon coupon) { | |||||
| private WxCouponOrder createCouponOrder(WxCUser user, WxOrder order, WxCoupon coupon) { | |||||
| Date curr = new Date(); | Date curr = new Date(); | ||||
| Date valid_date = null; | Date valid_date = null; | ||||
| if (coupon.getValidType() == EnumValidStatus.VALID_RANGE.getCode()) | if (coupon.getValidType() == EnumValidStatus.VALID_RANGE.getCode()) | ||||
| @@ -299,9 +299,11 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| couponOrder.setCouponPrice(order.getPayment()); | couponOrder.setCouponPrice(order.getPayment()); | ||||
| wxCouponOrderMapper.insertSelective(couponOrder); | wxCouponOrderMapper.insertSelective(couponOrder); | ||||
| return couponOrder; | |||||
| } | } | ||||
| @Override | @Override | ||||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||||
| public WxOrder sendUserFreeCoupon(Long userId, Long couponId) { | public WxOrder sendUserFreeCoupon(Long userId, Long couponId) { | ||||
| // check 用户状态 | // check 用户状态 | ||||
| WxCUser user = null; | WxCUser user = null; | ||||
| @@ -346,6 +348,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| record.setId(orderNumber); | record.setId(orderNumber); | ||||
| record.setTenantId(user.getTenantId()); | record.setTenantId(user.getTenantId()); | ||||
| record.setOrderNumber(orderNumber); | record.setOrderNumber(orderNumber); | ||||
| record.setCouponId(couponId); | |||||
| record.setCUserId(user.getId()); | record.setCUserId(user.getId()); | ||||
| record.setMerchantId(coupon.getMerchantId()); | record.setMerchantId(coupon.getMerchantId()); | ||||
| record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode()); | record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode()); | ||||
| @@ -359,25 +362,105 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| try { | try { | ||||
| wxOrderMapper.insertSelective(record); | wxOrderMapper.insertSelective(record); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| logger.error("保存订单:" + e.getMessage()); | |||||
| // 库存恢复 | // 库存恢复 | ||||
| stockBack(record); | stockBack(record); | ||||
| logger.error("保存订单:" + e.getMessage()); | |||||
| throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); | throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); | ||||
| } | } | ||||
| // 创建couponOrder | // 创建couponOrder | ||||
| try { | try { | ||||
| createCouponOrder(user, record, coupon); | |||||
| WxCouponOrder couponOrder = createCouponOrder(user, record, coupon); | |||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| logger.error("保存订单:" + e.getMessage()); | |||||
| // 库存恢复 | // 库存恢复 | ||||
| stockBack(record); | stockBack(record); | ||||
| logger.error("保存订单:" + e.getMessage()); | |||||
| throw new MallinkException(ErrorCode.COUPON_ORDER_SAVE_ERR); | throw new MallinkException(ErrorCode.COUPON_ORDER_SAVE_ERR); | ||||
| } | } | ||||
| return record; | return record; | ||||
| } | } | ||||
| @Override | |||||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||||
| public WxCouponOrder sendFreeCouponToUser(Long userId, Long couponId) { | |||||
| // check 用户状态 | |||||
| WxCUser user = null; | |||||
| try { | |||||
| user = wxCUserMapper.selectByPrimaryKey(userId); | |||||
| } catch (Exception e) { | |||||
| logger.error("userId : " + userId + ", e: " + e.getMessage()); | |||||
| } | |||||
| if (user == null) { | |||||
| logger.error("用户不存在, userId: " + userId); | |||||
| throw new MallinkException(ErrorCode.USER_IS_EMPTY); | |||||
| } | |||||
| // 检查券状态 | |||||
| String couponIdStr = String.valueOf(couponId); | |||||
| WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponId); | |||||
| if (coupon == null) { | |||||
| logger.error("券不存在, couponId: " + couponIdStr); | |||||
| throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); | |||||
| } | |||||
| if (coupon.getStatus() == EnumCouponStatus.COUPON_STATUS_TAKE_OFFF.getCode()) { | |||||
| logger.error("券已下架, couponId: " + couponIdStr); | |||||
| throw new MallinkException(ErrorCode.COUPON_IS_TAKE_OFF); | |||||
| } | |||||
| if (coupon.getSalePrice() != 0) { | |||||
| logger.error("券不免费, couponId: " + couponIdStr); | |||||
| throw new MallinkException(ErrorCode.COUPON_IS_NOT_FREE); | |||||
| } | |||||
| int payment = coupon.getSalePrice(); | |||||
| Date curr = new Date(); | |||||
| final IdWorker idWorker = IdWorker.get(); | |||||
| Long orderNumber = idWorker.nextId(); | |||||
| // body | |||||
| // tenant_id + merchant_id + title + subtitle | |||||
| String bodyStr = coupon.getTitle() + "/" + coupon.getSubTitle(); | |||||
| WxOrder record = new WxOrder(); | |||||
| record.setId(orderNumber); | |||||
| record.setTenantId(user.getTenantId()); | |||||
| record.setOrderNumber(orderNumber); | |||||
| record.setCouponId(couponId); | |||||
| record.setCUserId(user.getId()); | |||||
| record.setMerchantId(coupon.getMerchantId()); | |||||
| record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode()); | |||||
| record.setPayment(payment); | |||||
| record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS.getCode()); | |||||
| record.setDetail(bodyStr); | |||||
| record.setCreateDate(curr); | |||||
| record.setUpdateDate(curr); | |||||
| // 保存订单 | |||||
| try { | |||||
| wxOrderMapper.insertSelective(record); | |||||
| } catch (Exception e) { | |||||
| logger.error("保存订单:" + e.getMessage()); | |||||
| // 库存恢复 | |||||
| stockBack(record); | |||||
| throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); | |||||
| } | |||||
| // 创建couponOrder | |||||
| WxCouponOrder couponOrder = null; | |||||
| try { | |||||
| couponOrder = createCouponOrder(user, record, coupon); | |||||
| } catch (Exception e) { | |||||
| logger.error("保存订单:" + e.getMessage()); | |||||
| // 库存恢复 | |||||
| stockBack(record); | |||||
| throw new MallinkException(ErrorCode.COUPON_ORDER_SAVE_ERR); | |||||
| } | |||||
| return couponOrder; | |||||
| } | |||||
| @Override | @Override | ||||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | ||||
| public int orderSuccess(WxOrder updateOrder) { | public int orderSuccess(WxOrder updateOrder) { | ||||