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

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

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

import com.ym.mec.biz.dal.dao.SysTenantAccountDao;
import com.ym.mec.biz.dal.dao.SysTenantAccountDetailDao;
import com.ym.mec.biz.dal.entity.SysTenantAccount;
import com.ym.mec.biz.dal.entity.SysTenantAccount.AccountStatus;
import com.ym.mec.biz.dal.entity.SysTenantAccountDetail;
import com.ym.mec.biz.dal.entity.SysTenantAccountDetail.TransStatus;
import com.ym.mec.biz.dal.entity.SysTenantAccountDetail.TransType;
import com.ym.mec.biz.service.SysTenantAccountService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;

@Service
public class SysTenantAccountServiceImpl extends BaseServiceImpl<Integer, SysTenantAccount> implements SysTenantAccountService {

	@Autowired
	private SysTenantAccountDao sysTenantAccountDao;

	@Autowired
	private SysTenantAccountDetailDao sysTenantAccountDetailDao;

	@Override
	public BaseDAO<Integer, SysTenantAccount> getDAO() {
		return sysTenantAccountDao;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean update(Integer userId, int minutes, String orderNo, TransType type, String transNo, BigDecimal cashAmount, String memo) {

		Date date = new Date();
		SysTenantAccount tenantAccount = sysTenantAccountDao.getLocked(userId);
		if (tenantAccount == null) {
			tenantAccount = new SysTenantAccount();
			tenantAccount.setUserId(userId);
			tenantAccount.setCreateTime(date);
			tenantAccount.setUpdateTime(date);
			tenantAccount.setStatus(AccountStatus.NORMAL);
			tenantAccount.setAvailableMinutes(0);
			tenantAccount.setFrozenMinutes(0);
			sysTenantAccountDao.insert(tenantAccount);
		}

		if (tenantAccount.getStatus() != AccountStatus.NORMAL) {
			throw new BizException("账户不可用");
		}
		int balance = tenantAccount.getAvailableMinutes() + minutes;
		if (balance < 0) {
			throw new BizException("账户[{}]可用时间不足，可用时间剩{}分钟", userId, tenantAccount.getAvailableMinutes());
		}

		tenantAccount.setAvailableMinutes(balance);
		tenantAccount.setUpdateTime(date);
		sysTenantAccountDao.update(tenantAccount);

		SysTenantAccountDetail detail = new SysTenantAccountDetail();
		detail.setOrderNo(orderNo);
		detail.setTransMinutes(minutes);
		detail.setTotalAvailableMinutes(tenantAccount.getAvailableMinutes());
		detail.setMemo(memo);
		detail.setCreateTime(date);
		detail.setTransStatus(TransStatus.SUCCESS);
		detail.setTransType(type);
		detail.setUpdateTime(date);
		detail.setUserId(userId);
		detail.setTransNo(transNo);
		detail.setTransCashAmount(cashAmount);

		sysTenantAccountDetailDao.insert(detail);

		return true;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean manualUpdate(Integer teacherId, int minutes, TransType type, Integer operatorId, String memo) {
		Date date = new Date();
		SysTenantAccount tenantAccount = sysTenantAccountDao.getLocked(teacherId);
		if (tenantAccount == null) {
			tenantAccount = new SysTenantAccount();
			tenantAccount.setUserId(teacherId);
			tenantAccount.setCreateTime(date);
			tenantAccount.setUpdateTime(date);
			tenantAccount.setStatus(AccountStatus.NORMAL);
			tenantAccount.setAvailableMinutes(0);
			tenantAccount.setFrozenMinutes(0);
			sysTenantAccountDao.insert(tenantAccount);
		}

		if (tenantAccount.getStatus() != AccountStatus.NORMAL) {
			throw new BizException("账户不可用");
		}
		int balance = tenantAccount.getAvailableMinutes() + minutes;
		if (balance < 0) {
			throw new BizException("账户[{}]可用时间不足，可用时间剩{}分钟", teacherId, tenantAccount.getAvailableMinutes());
		}

		tenantAccount.setAvailableMinutes(balance);
		tenantAccount.setUpdateTime(date);
		sysTenantAccountDao.update(tenantAccount);

		SysTenantAccountDetail detail = new SysTenantAccountDetail();
		detail.setTransMinutes(minutes);
		detail.setTotalAvailableMinutes(tenantAccount.getAvailableMinutes());
		detail.setMemo(memo);
		detail.setCreateTime(date);
		detail.setTransStatus(TransStatus.SUCCESS);
		detail.setTransType(type);
		detail.setUpdateTime(date);
		detail.setUserId(teacherId);
		detail.setTransCashAmount(BigDecimal.ZERO);
		detail.setOperatorId(operatorId);

		sysTenantAccountDetailDao.insert(detail);

		return true;
	}

}