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

import java.util.Collection;

import com.style24.batch.support.env.TsbConstants;
import com.style24.core.biz.dao.TscNaverPayDao;
import com.style24.core.biz.service.TscNaverPayService;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.OrderChange;
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.TsbDeliveryService;
import com.style24.batch.biz.service.TsbWmsDeliveryService;
import com.style24.persistence.domain.Delivery;

import com.style24.core.support.env.TscConstants.OrderChangeGb;
import com.style24.core.support.env.TscConstants.OrderDetailStat;



import lombok.extern.slf4j.Slf4j;

/**
 * 회수결과 (wmsif to mall) 
 *
 * @author moon
 * @since 2021. 03. 19
 */
@Component
@Slf4j
public class TsbWithdrawRsltJob extends TsbAbstractJob<Collection<Delivery>, Collection<Delivery> , Collection<Delivery>> {

	@Autowired
	private TsbDeliveryService deliveryService;

	@Autowired
	private TsbWmsDeliveryService wmsDeliveryService;

	@Autowired
	private TscNaverPayService coreNaverPayService;

	@Autowired
	private TscNaverPayDao coreNaverPayDao;

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

	@Override
	public Collection<Delivery> read() throws Exception {
		// WMS_IF 회수결과 조회
		Collection<Delivery> delvList = wmsDeliveryService.getWmsIfRecallDeliveryOrdChgNoList();
		
		
		return delvList;
	}

	@Override
	public Collection<Delivery> process(Collection<Delivery> delvList) throws Exception {
		succCnt = 0;
		failCnt = 0;
		
		/**
		 * 1. 회수결과 조회 
		 * 2. tb_order_change, tb_order_change_detail  update
		 * 3. tb_withdraw, tb_withdraw_detail insert
		 * 4. tb_order_detail update 
		 * 4. 교환일경우 교환상품 출고시키기 
		 * 5. WMS 수신결과 업데이트 
		 * G680_30	반품요청
		 * G680_40	교환요청
		 * */
		for(Delivery data: delvList) {
			try {
				// 교환회수, 반품회수 정보 WMS 조회 처리
				// 교환회수 ---> 교환완료, 교환결제완료
				// 반품회수 ---> 반품진행중
				deliveryService.saveWithdraw2(data);

				// 네이버페이 교환 회수완료 데이터 송부
				if (OrderChangeGb.EXCHANGE.value().equals(data.getChgGb())) {  // 교환
					for(Integer ordDtlNo : data.getOrdDtlNoArr()) {
						Order order = new Order();
						order.setOrdDtlNo(ordDtlNo);
						order = coreNaverPayDao.getOrderDetailInfoFromOrdDtlNo(order);
						if (order != null && TscConstants.PgGb.NAVER_ORDER.value().equals(order.getPgGb())) {
							// coreNaverPayService.sendNaverPayExchangeCollectComplete(change, TsbConstants.REG_NO);
							String addUrl = "/naverpay/send/exchange/collect/complete?ordDtlNo=" + order.getOrdDtlNo() + "&userNo=" + TsbConstants.REG_NO;
							coreNaverPayService.sendScmServerForNaverpay(addUrl);                        // 교환 회수완료
							log.info("<< 교환 회수 완료 네이버페이 송부 완료 >>");
						}
					}
				}
				
				// WMS 회수정보 업데이트
				wmsDeliveryService.updateWmsIfRecallDeliveryRslt(data);

				succCnt++;
			}catch(Exception e) {
				log.info("Exception"+e);
				failCnt++;
			}
		}

		return delvList;
	}

	@Override
	public Collection<Delivery> write(Collection<Delivery> delivery) throws Exception {

		return delivery;
	}

	@Override
	public void notify(Collection<Delivery> delivery) throws Exception {
		super.printResult(succCnt, failCnt);
	}

}