package com.style24.admin.biz.service;

import java.util.Collection;

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

import com.style24.admin.biz.dao.TsaReviewDao;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.core.biz.service.TscEnvsetService;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Review;

import lombok.extern.slf4j.Slf4j;

/**
 * 상품평 Service
 *
 * @author gagamel
 * @since 2021. 1. 25
 */
@Service
@Slf4j
public class TsaReviewService {

	@Autowired
	private TsaReviewDao reviewDao;

	@Autowired
	private TscEnvsetService envsetService;

	/**
	 * 상품평 목록
	 * @param review - 상품평 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 25
	 */
	public Collection<Review> getReviewList(Review review) {
		return reviewDao.getReviewList(review);
	}

	/**
	 * 상품평 상세
	 * @param reviewSq - 상품평일련번호
	 * @return
	 * @author gagamel
	 * @since 2021. 4. 22
	 */
	public Review getReview(Integer reviewSq) {
		// 상품평 정보
		Review review = reviewDao.getReview(reviewSq);

		// 상품평 첨부파일
		review.setAttachList(reviewDao.getReviewAttachList(reviewSq));

		return review;
	}

	/**
	 * 상품평 관리자 답변 저장
	 * @param review - 상품평 정보
	 * @author gagamel
	 * @since 2021. 4. 22
	 */
	@Transactional("shopTxnManager")
	public void updateReviewUserReply(Review review) {
		reviewDao.updateReviewUserReply(review);
	}

	/**
	 * 상품평 동영상 노출 처리
	 * @param review - 상품평 정보
	 * @author gagamel
	 * @since 2021. 4. 22
	 */
	@Transactional("shopTxnManager")
	public void updateVideoReviewDisplay(Review review) {
		// 포인트 처리
		// 동영상(=포토)상품평포인트금액
		int pntAmt = envsetService.getPhotoReviewPoint(TscConstants.Site.STYLE24.value());
		review.setGvPntAmt(pntAmt);
		review.setRmPntAmt(pntAmt);
		review.setRegNo(TsaSession.getInfo().getUserNo());
		review.setUpdNo(TsaSession.getInfo().getUserNo());

		// 동영상상품평포인트 생성
		reviewDao.createVideoReviewPoint(review);

		// 고객포인트일련번호 설정
		Integer custPntSq = review.getCustPntSq();
		if (review.getCustPntSq() == null) {
			review.setCustPntSq(custPntSq);
		}

		review.setOccurGb(TscConstants.PointOccurGb.REVIEW_POINT.value());
		review.setOccurDtlDesc("동영상상품평포인트 지급");
		review.setPntAmt(pntAmt);
		review.setPntUploadStat(TscConstants.PntUploadStat.APPLY_COMPLETE.value());

		// 동영상상품평포인트이력 생성
		reviewDao.createBestReviewPointHistory(review);

		// 동영상상품평 노출 처리
		review.setGiveDuePnt(pntAmt);
		reviewDao.updateVideoReviewDisplay(review);
	}

	/**
	 * 베스트상품평선정 처리
	 * @param reviewList - 상품평 목록
	 * @author gagamel
	 * @since 2021. 4. 27
	 */
	@Transactional("shopTxnManager")
	public void saveBestReviewList(Collection<Review> reviewList) {
		// 1.베스트상품평포인트금액
		int bestPntAmt = envsetService.getBestReviewPoint(TscConstants.Site.STYLE24.value());

		for (Review review : reviewList) {
			review.setGvPntAmt(bestPntAmt);
			review.setRmPntAmt(bestPntAmt);
			review.setRegNo(TsaSession.getInfo().getUserNo());
			review.setUpdNo(TsaSession.getInfo().getUserNo());

			// 2.베스트상품평포인트 생성
			reviewDao.createBestReviewPoint(review);

			// 고객포인트일련번호 설정
			Integer custPntSq = review.getCustPntSq();
			if (review.getCustPntSq() == null) {
				review.setCustPntSq(custPntSq);
			}

			review.setOccurGb(TscConstants.PointOccurGb.REVIEW_POINT.value());
			review.setOccurDtlDesc("베스트상품평포인트 지급");
			review.setPntAmt(bestPntAmt);
			review.setPntUploadStat(TscConstants.PntUploadStat.APPLY_COMPLETE.value());

			// 3.베스트상품평포인트이력 생성
			reviewDao.createBestReviewPointHistory(review);

			// 4.베스트상품평선정 처리
			review.setGiveDueBpnt(bestPntAmt);
			reviewDao.updateBestReviewSelection(review);
		}
	}

}
