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

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gagaframework.web.parameter.GagaMap;
import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbOrderService;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.biz.service.TscKakaotalkService;
import com.style24.core.biz.service.TscMailService;
import com.style24.core.biz.service.TscOrderChangeService;
import com.style24.core.biz.service.TscOrderService;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.OrderChange;

import lombok.extern.slf4j.Slf4j;

/**
 * 선물하기 배송지 등록 기간 만료 주문 취소
 *
 * @author xodud1202
 * @since 2021. 05. 06
 */
@Component
@Slf4j
public class TsbOrderGiftExpirationJob extends TsbAbstractJob<Collection<Order>, Collection<Order>, OrderChange> {

	@Autowired
	private TscOrderChangeService coreOrderChangeService;

	@Autowired
	private TscOrderService coreOrderService;

	@Autowired
	private TsbOrderService orderService;

	@Autowired
	private TscKakaotalkService coreKakaotalkService;

	@Autowired
	private TscMailService coreMailService;

	private int succCnt = 0;
	private int failCnt = 0;

	@Override
	public Collection<Order> read() throws Exception {
		return orderService.getOrderGiftExpirationList();
	}

	@Override
	public Collection<Order> process(Collection<Order> params) throws Exception {
		return params;
	}

	@Override
	public OrderChange write(Collection<Order> params) throws Exception {
		OrderChange result = new OrderChange();
		for(Order order : params) {
			OrderChange orderChange = new OrderChange();
			orderChange.setOrdNo(order.getOrdNo());
			orderChange.setCustNo(order.getCustNo());
			orderChange.setReqGbn("cnclComplete");		// 전체취소
			orderChange.setChgReason(TscConstants.OrderCancelReason.ADMIN_CANCEL.value());
			orderChange.setChgReasonNm("전체취소 - 선물 배송지 입력 기한 만료");
			orderChange.setPgStat("N");					// PG점프 여부
			orderChange.setRegNo(TsbConstants.REG_NO);
			orderChange.setUpdNo(TsbConstants.REG_NO);
			orderChange.setBatchYn("Y");

			try {
				// 선물하기는 무통장입금 결제 없음. 전체취소.
				int ordChgSq = coreOrderChangeService.allCnclComplete(orderChange);
				orderChange.setOrdChgSq(ordChgSq);
				succCnt++;

				// 선물하기 취소 완료 알림톡 송부
				try {
					if(ordChgSq > 0) {
						OrderChange change = new OrderChange();
						change = coreOrderChangeService.getKakaoOrderCancelInfo(ordChgSq);

						// 취소 알림톡 발송
						orderChange.setChgGb(TscConstants.OrderCancelReason.ADMIN_CANCEL.value());
						coreKakaotalkService.sendDontWriteGiftAddress(change, TsbConstants.REG_NO);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

				// 취소 완료 메일 송부
				try {
					if(ordChgSq > 0) {
						// 취소 안내 메일 정보 조회
						GagaMap replaceInfo = coreOrderChangeService.getMailOrderCancelReplaceInfo(orderChange);

						// 취소 안내 메일 발송
						if (StringUtils.isNotBlank(replaceInfo.getString("ordEmail"))) {
							coreMailService.sendOrderCancel(replaceInfo, TsbConstants.REG_NO);
						}
					}
				} catch(Exception e) {
					e.printStackTrace();
				}


			} catch (Exception e) {
				failCnt++;
				e.printStackTrace();
			}
		}

		return result;
	}

	@Override
	public void notify(OrderChange param) throws Exception {
		super.printResult(succCnt, failCnt);
	}

}