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

import com.ym.mec.biz.dal.dao.StudentRegistrationDao;
import com.ym.mec.biz.dal.entity.StudentRegistration;
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.SysUserCashAccountDao;
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.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.util.Date;

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

	@Resource
	private SysUserCashAccountDao sysUserCashAccountDao;

	@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);

		//扣减账户
		sysUserCashAccountDao.incrAccount(rechargeDto.getUserId(),rechargeDto.getRechargeAmount());

		return true;
	}

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