package com.ym.mec.biz.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ym.mec.biz.dal.dao.StudentRechargeDao;
import com.ym.mec.biz.dal.dao.StudentRegistrationDao;
import com.ym.mec.biz.dal.entity.StudentRecharge;
import com.ym.mec.biz.dal.entity.SysUserCashAccount;
import com.ym.mec.biz.dal.enums.DealStatusEnum;
import com.ym.mec.biz.dal.enums.PlatformCashAccountDetailTypeEnum;
import com.ym.mec.biz.dal.enums.PlatformCashAccountStatusEnum;
import com.ym.mec.biz.dal.enums.TransTypeEnum;
import com.ym.mec.biz.service.PayService;
import com.ym.mec.biz.service.StudentRechargeService;
import com.ym.mec.biz.service.SysUserCashAccountDetailService;
import com.ym.mec.biz.service.SysUserCashAccountService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.IdGeneratorService;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.thirdparty.union.NotifyMsg;

@Service
public class StudentRechargeServiceImpl extends BaseServiceImpl<String, StudentRecharge> implements StudentRechargeService {

	@Resource
	private StudentRechargeDao studentRechargeDao;
	@Autowired
	private SysUserCashAccountService sysUserCashAccountService;

	@Resource
	private SysUserCashAccountDetailService sysUserCashAccountDetailService;

	@Autowired
	private PayService payService;

	@Autowired
	private IdGeneratorService idGeneratorService;

	@Override
	public BaseDAO<String, StudentRecharge> getDAO() {
		return studentRechargeDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Map<?, ?> recharge(Integer userId, String bankCardNo, BigDecimal amount) {

		Date now = new Date();

		SysUserCashAccount userCashAccount = sysUserCashAccountService.get(userId);
		if (userCashAccount == null) {
			throw new BizException("账户不存在");
		}

		if (userCashAccount.getStatus() != PlatformCashAccountStatusEnum.NORMAL) {
			throw new BizException("账户不可用");
		}

		StudentRecharge studentRecharge = new StudentRecharge();
		studentRecharge.setAmount(amount);
		studentRecharge.setCreateTime(now);
		studentRecharge.setUserId(userId);
		studentRecharge.setStatus(DealStatusEnum.ING);
		studentRecharge.setTransNo(null);
		studentRecharge.setOrderNo(idGeneratorService.generatorId("payment") + "");
		studentRechargeDao.insert(studentRecharge);

		try {
			return payService.getPayMap(amount, BigDecimal.ZERO,studentRecharge.getOrderNo(), "https://pay.dayaedu.com/api/yqpay/notify", "http://dev.dayaedu.com", "充值",
					"充值",userId,null);
		} catch (Exception e) {
			throw new BizException("调用支付接口出错", e);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean rechargeCallback(NotifyMsg notifyMsg) {

		if (notifyMsg.getStatus().equals("TRADE_SUCCESS") || notifyMsg.getStatus().equals("TRADE_CLOSED")) {

			String orderNo = notifyMsg.getMerOrderId();

			StudentRecharge studentRecharge = studentRechargeDao.queryByOrderNo(orderNo);

			if (studentRecharge == null) {
				throw new BizException("充值记录不存在");
			}

			DealStatusEnum status = notifyMsg.getStatus().equals("TRADE_SUCCESS") ? DealStatusEnum.SUCCESS : DealStatusEnum.FAILED;

			String transNo = notifyMsg.getSeqId();

			String paymentChannel = notifyMsg.getTargetSys();

			Date paymentDate = notifyMsg.getPayTime();

			studentRecharge.setPayPlatform(paymentChannel);
			studentRecharge.setStatus(status);
			studentRecharge.setTransNo(transNo);
			studentRecharge.setModifyTime(paymentDate);

			studentRechargeDao.update(studentRecharge);

			if (status == DealStatusEnum.SUCCESS) {

				TransTypeEnum transType = TransTypeEnum.FAST_PAYMENT;

				if ("WXPay".equals(paymentChannel)) {
					transType = TransTypeEnum.WECHAT;
				} else if ("Alipay".equals(paymentChannel)) {
					transType = TransTypeEnum.ALIPAY;
				}

				// 更新账户余额
				// 新增交易流水
				sysUserCashAccountDetailService.addCashAccountDetail(studentRecharge.getUserId(), studentRecharge.getAmount(), orderNo, transNo,
						PlatformCashAccountDetailTypeEnum.RECHARGE, transType, status, "",null);
			}

		}

		return true;
	}

	@Override
	public StudentRecharge queryByTransNo(String transNo) {
		return studentRechargeDao.queryByTransNo(transNo);
	}
}