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

import java.util.Date;

import javax.annotation.Resource;

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

import com.ym.mec.biz.dal.dao.StudentRechargeDao;
import com.ym.mec.biz.dal.dao.StudentRegistrationDao;
import com.ym.mec.biz.dal.dao.SysUserCashAccountDetailDao;
import com.ym.mec.biz.dal.dto.RechargeDto;
import com.ym.mec.biz.dal.entity.StudentRecharge;
import com.ym.mec.biz.dal.entity.SysUserCashAccountDetail;
import com.ym.mec.biz.dal.enums.PlatformCashAccountDetailTypeEnum;
import com.ym.mec.biz.service.StudentRechargeService;
import com.ym.mec.biz.service.SysUserCashAccountService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;

@Service
public class StudentRechargeServiceImpl extends BaseServiceImpl<String, StudentRecharge>  implements StudentRechargeService {
	
	@Resource
	private StudentRechargeDao studentRechargeDao;
    @Autowired
    private StudentRegistrationDao studentRegistrationDao;
	
	@Autowired
	private SysUserCashAccountService sysUserCashAccountService;

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

	@Override
	public Boolean recharge(RechargeDto rechargeDto) {

		//TODO ���õ�����֧��
		Date now  = new Date();

		//��ֵ��¼
        StudentRecharge studentRecharge = new StudentRecharge();
        studentRecharge.setAmount(rechargeDto.getRechargeAmount());
		studentRecharge.setCreateTime(now);
		studentRecharge.setUserId(rechargeDto.getUserId().longValue());
		studentRecharge.setTransNo(null);
		studentRechargeDao.insert(studentRecharge);
		//�˻��ʽ���ˮ
		SysUserCashAccountDetail cashAccount = new SysUserCashAccountDetail();
		cashAccount.setAmount(rechargeDto.getRechargeAmount());
		cashAccount.setBalance(null);
		cashAccount.setTransNo(null);
		cashAccount.setType(PlatformCashAccountDetailTypeEnum.RECHARGE);
		cashAccount.setUserId(rechargeDto.getUserId());
		cashAccount.setCreateTime(now);
		sysUserCashAccountDetailDao.insert(cashAccount);

		//�ۼ��˻�
		sysUserCashAccountService.updateBalance(rechargeDto.getUserId(),rechargeDto.getRechargeAmount());

		return true;
	}

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