package com.style24.core.biz.service;

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

import com.style24.core.biz.dao.TscPointDao;
import com.style24.core.support.env.TscConstants;
import com.style24.core.biz.service.TscKakaotalkService;
import com.style24.core.biz.dao.TscDeliveryDao;

import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.Point;

import com.style24.core.support.env.TscConstants.MallGb;

import lombok.extern.slf4j.Slf4j;

import java.util.Collection;


/**
 * 포인트 Service
 * 
 * @author jsshin
 * @since 2020. 01. 27
 */
@Service
@Slf4j
public class TscPointService {

	@Autowired
	private TscPointDao pointDao;

	@Autowired
	private TscEnvsetService envsetService;

	@Autowired
	private TscOrderService orderService;

	@Autowired
	private TscKakaotalkService kakaotalkService;	
	
	@Autowired
	private TscDeliveryDao deliveryDao;	
		
	
	
	/**
	 * 포인트 적립 / 차감
	 * @param point - 포인트 정보
	 * @author jsshin
	 * @since 2021. 01. 27
	 */
	@Transactional("shopTxnManager")
	public void saveCustomerPoint(Point point) {
		if ("+".equals(point.getSignGb())) {
			pointDao.createCustomerPoint(point);
			pointDao.createCustomerPointHst(point);
		} else {
			int sumRmPntAmt = pointDao.getCustomerSumRmPntAmt(point.getCustNo());
				sumRmPntAmt += point.getRmPntAmt();
			if (sumRmPntAmt < 0 ) {
				throw new IllegalStateException("보유 포인트 금액이 차감 포인트 금액 보다 작습니다.");
			}
			Collection<Point> rmPntAmtList = pointDao.getCustomerRmPntAmtList(point.getCustNo());
			int rmPntAmt = point.getRmPntAmt() * -1; // 차감금액 :  -1000 * -1
			for (Point rmPntAmtInfo: rmPntAmtList) {
				if (rmPntAmt > 0 ) {
					if (rmPntAmt < rmPntAmtInfo.getRmPntAmt()) {
						rmPntAmtInfo.setPntAmt(rmPntAmt * -1);
						rmPntAmt -= rmPntAmt;
					} else {
						rmPntAmtInfo.setPntAmt(rmPntAmtInfo.getRmPntAmt() * -1);
						rmPntAmt -= rmPntAmtInfo.getRmPntAmt();
					}
					rmPntAmtInfo.setCustNo(point.getCustNo());
					rmPntAmtInfo.setOccurGb(point.getOccurGb());
					rmPntAmtInfo.setPntUploadStat(point.getPntUploadStat());
					rmPntAmtInfo.setOccurDtlDesc(point.getOccurDtlDesc());
					rmPntAmtInfo.setRegNo(point.getRegNo());
					rmPntAmtInfo.setUpdNo(point.getUpdNo());
					pointDao.createCustomerPointHst(rmPntAmtInfo);
					pointDao.updatePointDeduction(rmPntAmtInfo);
				}
			}
		}

	}

	/**
	 * 구매확정 포인트 생성
	 * @param point
	 * @return int
	 * @author card007
	 * @since 2021. 02. 17
	 */
	@Transactional("shopTxnManager")
	public int createOrderDecisionPoint(Point point) {
		// 구매포인트소멸기한(일)
		int expireDays = envsetService.getBuyPointExpireDays(TscConstants.Site.STYLE24.value());
		point.setBuyPointExpireDays(expireDays);

		// 구매확정 포인트 생성
		pointDao.createOrderDecisionPoint(point);
		// 구매확정 포인트 히스토리 수정
		pointDao.updateOrderDecisionPointHst(point);

		// 구매확정 포인트 적립금액 리턴
		return pointDao.getOrderDecisionPoint(point);
	}

	/**
	 * 자동 구매확정 포인트 생성
	 * @param point - 고객번호, 주문번호, 주문상세번호
	 * @author jsshin
	 * @since 2021. 04. 21
	 */
	@Transactional("shopTxnManager")
	public void saveAutoBuyConfirm(Point point) {
		// 1.구매포인트소멸기한(일)
		int expireDays = envsetService.getBuyPointExpireDays(TscConstants.Site.STYLE24.value());
		point.setBuyPointExpireDays(expireDays);

		// 2.구매확정 포인트 생성
		pointDao.createOrderDecisionPoint(point);

		// 3.구매확정 포인트 히스토리 수정
		pointDao.updateOrderDecisionPointHst(point);
	}

	/**
	 * 구매확정 포인트 전환 가능 여부 조회
	 *
	 * @param Point
	 * @return int
	 * @author card007
	 * @since 2021. 04. 07
	 */
	public int getOrderDecisionPointHstCount(Point point) {
		return pointDao.getOrderDecisionPointHstCount(point);
	}

	/**
	 * 가용 포인트
	 * @param custNo - 고객번호
	 * @return int - 가용포인트 합
	 * @author jsshin
	 * @since 2021. 06. 14
	 */
	public int getCustomerSumRmPntAmt(Integer custNo) {
		return pointDao.getCustomerSumRmPntAmt(custNo);
	}

}
