net.groupbuy.service.impl.PaymentServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for net.groupbuy.service.impl.PaymentServiceImpl.java

Source

/*
 * Copyright 2005-2013 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 */
package net.groupbuy.service.impl;

import java.util.Date;

import javax.annotation.Resource;
import javax.persistence.LockModeType;

import net.groupbuy.dao.PaymentDao;
import net.groupbuy.entity.Member;
import net.groupbuy.entity.Order;
import net.groupbuy.entity.Payment;
import net.groupbuy.entity.Payment.Status;
import net.groupbuy.entity.Payment.Type;
import net.groupbuy.service.MemberService;
import net.groupbuy.service.OrderService;
import net.groupbuy.service.PaymentService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service - ?
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Service("paymentServiceImpl")
public class PaymentServiceImpl extends BaseServiceImpl<Payment, Long> implements PaymentService {

    @Resource(name = "paymentDaoImpl")
    private PaymentDao paymentDao;
    @Resource(name = "orderServiceImpl")
    private OrderService orderService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    @Resource(name = "paymentDaoImpl")
    public void setBaseDao(PaymentDao paymentDao) {
        super.setBaseDao(paymentDao);
    }

    @Transactional(readOnly = true)
    public Payment findBySn(String sn) {
        return paymentDao.findBySn(sn);
    }

    public void handle(Payment payment) {
        paymentDao.refresh(payment, LockModeType.PESSIMISTIC_WRITE);
        if (payment != null && payment.getStatus() == Status.wait) {
            if (payment.getType() == Type.payment) {
                Order order = payment.getOrder();
                if (order != null) {
                    orderService.payment(order, payment, null);
                }
            } else if (payment.getType() == Type.recharge) {
                Member member = payment.getMember();
                if (member != null) {
                    memberService.update(member, null, payment.getEffectiveAmount(), null, null);
                }
            }
            payment.setStatus(Status.success);
            payment.setPaymentDate(new Date());
            paymentDao.merge(payment);
        }
    }

}