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

import java.math.BigDecimal;
import java.util.*;

import com.ym.mec.biz.dal.dto.CashAccountDetail;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.util.collection.MapUtil;
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.SysUserCashAccountDao;
import com.ym.mec.biz.dal.dao.SysUserCashAccountDetailDao;
import com.ym.mec.biz.dal.entity.SysUserCashAccount;
import com.ym.mec.biz.dal.entity.SysUserCashAccountDetail;
import com.ym.mec.biz.dal.enums.DealStatusEnum;
import com.ym.mec.biz.dal.enums.PlatformCashAccountDetailTypeEnum;
import com.ym.mec.biz.dal.enums.TransTypeEnum;
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.impl.BaseServiceImpl;

@Service
public class SysUserCashAccountDetailServiceImpl extends BaseServiceImpl<Long, SysUserCashAccountDetail> implements SysUserCashAccountDetailService {

	@Autowired
	private SysUserCashAccountDetailDao sysUserCashAccountDetailDao;

	@Autowired
	private SysUserCashAccountDao sysUserCashAccountDao;

	@Autowired
	private SysUserCashAccountService sysUserCashAccountService;

	@Override
	public BaseDAO<Long, SysUserCashAccountDetail> getDAO() {
		return sysUserCashAccountDetailDao;
	}

	@Override
	public SysUserCashAccountDetail queryDetailByTransNo(String transNo) {
		return sysUserCashAccountDetailDao.selectDetailByTransNo(transNo);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean addCashAccountDetail(Integer userId, BigDecimal amount, String origOrderId, String transNo, PlatformCashAccountDetailTypeEnum type,
			TransTypeEnum transType, DealStatusEnum status, String comment,String description) {

		SysUserCashAccountDetail sysUserCashAccountDetail = new SysUserCashAccountDetail();
		sysUserCashAccountDetail.setUserId(userId);
		sysUserCashAccountDetail.setType(type);
		sysUserCashAccountDetail.setStatus(status);
		sysUserCashAccountDetail.setAmount(amount);
		sysUserCashAccountDetail.setAttribute(origOrderId);
		sysUserCashAccountDetail.setComment(comment);
		sysUserCashAccountDetail.setDescription(description);
		sysUserCashAccountDetail.setTransNo(transNo);
		sysUserCashAccountDetail.setTransType(transType);
		sysUserCashAccountDetail.setCreateTime(new Date());

		SysUserCashAccount cashAccount = sysUserCashAccountDao.getLocked(userId);
		if (cashAccount == null) {
			throw new BizException("用户[{}]现金账户不存在", userId);
		}

		if (DealStatusEnum.SUCCESS == status) {
			sysUserCashAccountService.updateBalance(userId, amount);
			sysUserCashAccountDetail.setBalance(cashAccount.getBalance().add(amount).doubleValue() > 0 ? cashAccount.getBalance().add(amount) : new BigDecimal(
					0));
		} else {
			sysUserCashAccountDetail.setBalance(cashAccount.getBalance());
		}

		sysUserCashAccountDetailDao.insert(sysUserCashAccountDetail);

		return true;
	}

	@Override
	public PageInfo<SysUserCashAccountDetail> queryAccountDetail(CashAccountDetail queryInfo) {
		PageInfo<SysUserCashAccountDetail> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<>();
		MapUtil.populateMap(params, queryInfo);

		List<SysUserCashAccountDetail> dataList = null;
		int count = sysUserCashAccountDetailDao.countAccountDetails(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = sysUserCashAccountDetailDao.queryAccountDetail(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}
}