package com.softwarebr.nunu.service.user.impl;

import com.brframework.commonweb.exception.HandleException;
import com.softwarebr.nunu.entity.train.Activity;
import com.softwarebr.nunu.entity.user.Order;
import com.softwarebr.nunu.entity.user.RefundInfo;
import com.softwarebr.nunu.dao.user.RefundInfoDao;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.service.train.ActivityService;
import com.softwarebr.nunu.service.user.OrderService;
import com.softwarebr.nunu.service.user.RefundInfoService;
import com.brframework.commondb.core.AbstractEntityService;
import com.brframework.commondb.core.CommonRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @date 2020-02-27 13:36:08
 * @author lilin
 */

@Slf4j
@Service
public class RefundInfoServiceImpl extends AbstractEntityService<RefundInfo, Long, Object> implements RefundInfoService {

	@Autowired
	private RefundInfoDao refundInfoDao;

	@Autowired
	private OrderService orderService;

	@Autowired
	private ActivityService activityService;

	@Override
	public CommonRepository<Long, RefundInfo> getRepository() {
		return refundInfoDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void refundVerify(Long id, Integer verify, String verifyRemark){
		RefundInfo refundInfo = accessObject(id);
		if(!refundInfo.getVerify().equals(SystemConst.VERIFY_AWAIT)){
			throw new HandleException("退款订单已审核，请勿重复操作！");
		}
		if(verify.equals(SystemConst.VERIFY_ADOPT)){
			Order order = orderService.accessObject(refundInfo.getOrderId());
			//执行退款流程
			orderService.refundOrder(refundInfo.getOrderId(), Order.ORDER_STATUS_REFUNDED);

		}else{
			orderService.refundOrder(refundInfo.getOrderId(), Order.ORDER_STATUS_REJECTED);
		}
		refundInfo.setVerifyRemark(verifyRemark);
		refundInfo.setVerify(verify);
		save(refundInfo);

	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public RefundInfo refundInfoSave(Long memberId, Long orderId, String reason, String voucherPicture){
		RefundInfo refundInfo = getRefundInfoByOrderId(orderId);
		if(null != refundInfo){
			throw new HandleException("退款申请已提交，正在审核中");
		}
		Order order = orderService.accessObject(orderId);
		Activity activity = activityService.accessObject(order.getActivityId());
		if(activity.getSignEndDate().compareTo(LocalDateTime.now())<=0){
			throw new HandleException("报名时间已截止，申请退款操作失败");
		}
		refundInfo = new RefundInfo();
		refundInfo.setVerify(SystemConst.VERIFY_AWAIT);
		refundInfo.setCreateDate(LocalDateTime.now());
		refundInfo.setMemberId(memberId);
		refundInfo.setOrderId(orderId);
		refundInfo.setReason(reason);
		refundInfo.setVoucherPicture(voucherPicture);
		refundInfo.setActivityId(order.getActivityId());
		refundInfo = save(refundInfo);
		order.setOrderStatus(Order.ORDER_STATUS_REFUNDING);
		orderService.save(order);
		return refundInfo;
	}

	@Override
	public RefundInfo getRefundInfoByOrderId(Long orderId){

		return refundInfoDao.findTopByOrderId(orderId);
	}
}