package com.style24.batch.biz.job.marketing;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbReviewService;
import com.style24.core.biz.service.TscEnvsetService;
import com.style24.core.biz.thirdparty.SsgKakaoSender;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.ReviewGuide;
import com.style24.persistence.domain.SsgDirectMessage;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.parameter.GagaMap;

/**
 * 상품평등록안내 발송 Job
 * 
 * @author gagamel
 * @since 2021. 5. 17
 */
@Component
@Slf4j
public class TsbReviewGuideJob extends TsbAbstractJob<String, String, String> {

	@Autowired
	private TscEnvsetService envsetService;

	@Autowired
	private TsbReviewService reviewService;

	@Autowired
	private SsgKakaoSender kakaoSender;

	@Override
	public String read() throws Exception {
		return "OK";
	}

	@Override
	public String process(String result) throws Exception {
		// 1.상품평등록가능일수 조회
		int expireDays = envsetService.getGoodsReviewRegisterDays(TscConstants.Site.STYLE24.value());
		log.info("1.상품평등록가능일수(구매일로부터): {}일", expireDays);

		// 2.상품평등록안내발송대상건 생성
		int cnt = reviewService.createReviewGuideSendObjectList(expireDays);
		log.info("2.상품평등록안내발송대상건: {}건", cnt);

		if (cnt == 0) {
			log.info("상품평등록안내발송대상건이 없습니다. 서비스를 종료합니다.");
			return result;
		}

		// 3.상품평등록안내발송대상 목록
		Collection<ReviewGuide> reviewGuideList = reviewService.getReviewGuideSendObjectList();
		if (reviewGuideList != null && !reviewGuideList.isEmpty()) {
			for (ReviewGuide reviewGuide : reviewGuideList) {
				SsgDirectMessage dm = new SsgDirectMessage();
				dm.setFuserid(String.valueOf(reviewGuide.getCustNo()));
				dm.setFkkoresendtype("LMS");
				dm.setFdestine(reviewGuide.getCellPhnno());
				dm.setBizTime("Y");

				// 대체할 문자열 설정
				GagaMap replaceInfo = new GagaMap();
				replaceInfo.set("ordNo", reviewGuide.getOrdNo());
				replaceInfo.setString("custNm", reviewGuide.getCustNm());

				// 4.알림톡 발송
				kakaoSender.send(SsgKakaoSender.KakaoAnswerSq.REVIEW_WRITE.value(), dm, replaceInfo);

				// 5.상품평등록안내발송완료 처리
				reviewService.updateReviewGuideSendComplete(reviewGuide);
			}
		}

		return result;
	}

	@Override
	public String write(String result) throws Exception {
		return result;
	}

	@Override
	public void notify(String result) throws Exception {
		// Do nothing
	}

}
