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

import java.util.ArrayList;
import java.util.Collection;

import com.style24.batch.biz.service.TsbNaverPayService;
import com.style24.batch.support.env.TsbConstants;
import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbDeliveryService;
import com.style24.batch.biz.service.TsbWmsDeliveryService;

import com.style24.core.biz.dao.TscNaverPayDao;
import com.style24.core.biz.service.TscKakaotalkService;

import com.style24.core.biz.dao.TscOrderDao;
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 com.style24.persistence.domain.Delivery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * 출고결과 수신 (wmsif to mall) 
 *
 * @author moon
 * @since 2021. 03. 09
 */
@Component
@Slf4j
public class TsbDeliveryOrderRsltJob extends TsbAbstractJob<Delivery, Delivery, Delivery> {

	@Autowired
	private TsbDeliveryService deliveryService;

	@Autowired
	private TsbWmsDeliveryService wmsDeliveryService;

	@Autowired
	private TscNaverPayService coreNaverPayService;

	@Autowired
	private TscNaverPayDao coreNaverPayDao;

	@Autowired
	private TscKakaotalkService kakaotalkService;

	@Autowired
	private TscOrderDao coreOrderDao;
	
	
	
	

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

	@Override
	public Delivery read() throws Exception {

		Delivery delivery = new Delivery();
		return delivery;
	}

	@Override
	public Delivery process(Delivery delivery) throws Exception {
		succCnt = 0;
		failCnt = 0;
		/**
		 * wms  출고결과 조회 
		 * mall 출고결과 반영
		 * wms  출고결과수신 update 
		 * naverPay 주문 출고 정보 송신
		 * */
		
		// 1.wms  출고결과 조회 
		Collection<Delivery> list = wmsDeliveryService.getWmsIfDeliveryOrdRsltList();
		
		Collection<Delivery> ordNolist = wmsDeliveryService.getWmsIfDeliveryOrderNoRsltList();
		
		String statGb = "SHIPPING";

		for(Delivery data : list) {
			
			try {
				// 2. mall 출고결과 반영
				deliveryService.updateTbOrderDtlStat(data, statGb);
				
				// 3. wms  출고결과 수신완료 update 
				wmsDeliveryService.updateWmsIfDeliveryOrdRslt(data);
				
				succCnt++;
			} catch(Exception e) {
				failCnt++;
				e.printStackTrace();
			}
			
		}
		
		// 4. 알림톡 
		for(Delivery ordNo : ordNolist) {
			
			Delivery info = deliveryService.getDeliveryStart(ordNo);
			
			Order order = new Order();
			order.setOrdNo(info.getOrdNo());
			order.setCustNo(info.getCustNo());
			order.setOrdPhnno(info.getOrdPhnno());
			order.setOrdNm(info.getOrdNm());
			order.setGoodsCnt(info.getGoodsCnt()+"");
			order.setGoodsNm(info.getGoodsNm());
			try {
				kakaotalkService.sendDeliveryStart(order, TsbConstants.REG_NO);
			} catch(Exception e) {
				log.debug("kakaotalkService Exception OrdNo:"+info.getOrdNo());
			}
		}
		
		
		// 5. 네이버페이 출고 정보 송신
		Collection<Order> orderDelvDtlList = new ArrayList<Order>();
		Collection<Order> orderExchDelvDtlList = new ArrayList<Order>();
		for(Delivery delv : list) {
			Order dtl = new Order();
			dtl.setOrdDtlNo(delv.getOrderdtlno());
			dtl = coreNaverPayDao.getOrderDetailInfoFromOrdDtlNo(dtl);
			if(dtl != null && TscConstants.PgGb.NAVER_ORDER.value().equals(dtl.getPgGb())) {
				if("O".equals(dtl.getOrdExchGb())) {
					// 4-1. 네이버페이 원주문 배송중 정보 송부
					// coreNaverPayService.sendNaverPayDeliveryStartOrder(orderDelvDtl, statGb, TsbConstants.REG_NO);
					String addUrl = "/naverpay/send/order/delivery/start?ordDtlNo=" + dtl.getOrdDtlNo() + "&userNo=" + TsbConstants.REG_NO;
					coreNaverPayService.sendScmServerForNaverpay(addUrl);						// 네이버페이 원주문 배송중 정보 송부
				} else if ("E".equals(dtl.getOrdExchGb())) {
					// 4-2. 네이버페이 교환 배송중 정보 송부
					// coreNaverPayService.sendNaverPayExchangeReDelivery(dtl, TsbConstants.REG_NO);
					String addUrl = "/naverpay/send/exchange/delivery?ordDtlNo=" + dtl.getOrdDtlNo() + "&userNo=" + TsbConstants.REG_NO;
					coreNaverPayService.sendScmServerForNaverpay(addUrl);						// 네이버페이 교환 배송중 정보 송부
				}
			}
		}

		
		return delivery;
	}

	@Override
	public Delivery write(Delivery delivery) throws Exception {

		return delivery;
	}

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

}
