package com.style24.core.biz.service;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gagaframework.web.parameter.GagaMap;
import com.style24.core.biz.dao.TscDeliveryDao;
import com.style24.core.biz.dao.TscOrderChangeDao;
import com.style24.core.biz.dao.TscOrderDao;
import com.style24.core.biz.dao.TscScmDao;
import com.style24.core.support.env.TscConstants;
import com.style24.core.biz.service.TscKakaotalkService;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.core.support.util.CryptoUtils;
import com.style24.persistence.domain.DeliveryFee;
import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.OrderChange;

import lombok.extern.slf4j.Slf4j;

/**
 * 주문관리 Service
 *
 * @author moon
 * @since 2021. 04. 23
 */
@Service
@Slf4j
public class TscDeliveryService {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private Environment env;

	@Autowired
	private TscOrderDao orderDao;

	@Autowired
	private TscScmDao scmDao;
	
	@Autowired
	private TscOrderChangeDao orderChangeDao;

	@Autowired
	private TscOrderChangeService orderChangeService;
	
	@Autowired
	private TscPointService pointService;

	@Autowired
	private ObjectMapper mapper;

	@Autowired
	private TscDeliveryDao deliveryDao;
	
	@Autowired
	private TscKakaotalkService kakaotalkService;
		
	
	
	/**
	 * 품절취소
	 *
	 * @param  
	 * @return void
	 * @author moon
	 * @since 2020. 04. 23
	 */
	@Transactional("shopTxnManager")
	
	public void cancelSoldout(Collection<Order> soldoutList, Integer userNo, String batchYn) {

		List<OrderChange> cancelDataList = new ArrayList<OrderChange>();
		
		for(Order param : soldoutList) {
			if(param.getCnclRtnQty() > 0) {
				boolean chkOrd = true;

				for(OrderChange cancelData : cancelDataList) {
					if(param.getOrdNo().equals(cancelData.getOrdNo())) {
						chkOrd = false;
						cancelData.getOrdDtlNoList().add(param.getOrdDtlNo());
						cancelData.getCnclRtnReqQtyList().add(param.getCnclRtnQty());
					}
				}

				if(chkOrd) {
					OrderChange change = new OrderChange();
					change.setOrdNo(param.getOrdNo());
					change.setCustNo(param.getCustNo());
					change.setOrdNm(param.getOrdNm());
					change.setOrdPhnno(param.getOrdPhnno());
					change.setPgGb(param.getPgGb());
					change.setPayMeans(param.getPayMeans());
					change.setBankCd(param.getBankCd());
					change.setAccountNm(param.getAccountNm());
					change.setAccountNo(CryptoUtils.decryptAES(param.getAccountNo()));
					change.getOrdDtlNoList().add(param.getOrdDtlNo());
					change.getCnclRtnReqQtyList().add(param.getCnclRtnQty());
					change.setChgReason(TscConstants.OrderCancelReason.SOLDOUT_CANCEL.value());
					change.setChgReasonNm("품절취소");
					change.setBatchYn(batchYn);
					cancelDataList.add(change);
				}
			}
		}

		// 리스트로 지정된 주문상세번호와 취소수량 배열로 변경 후 취소
		for(OrderChange change : cancelDataList) {
			change.setOrdDtlNoArr(change.getOrdDtlNoList().stream().mapToInt(i->i).toArray());
			change.setCnclRtnReqQtyArr(change.getCnclRtnReqQtyList().stream().mapToInt(i->i).toArray());

			// 무통장입금 주문 중 환불계좌정보 없을 경우 취소신청
			if((TscConstants.PgGb.KCP.value().equals(change.getPgGb()) && TscConstants.PayMeans.BANK_DEPOSIT.value().equals(change.getPayMeans()) && StringUtils.isEmpty(change.getAccountNo()))
				|| TscConstants.PayMeans.CELL_PHONE.value().equals(change.getPayMeans())
				|| TscConstants.PayMeans.BANK_DEPOSIT.value().equals(change.getPayMeans())) {
				// 취소신청
				change.setChgGb(TscConstants.OrderChangeGb.CANCEL.value());
				change.setUserNo(userNo);
				orderChangeService.cancelRequest(change);
			} else {		// 무통장입금 주문 중 환불계좌정보 없을 경우 제외는 취소처리
			
				// 취소완료
				GagaMap cancelInfo = orderChangeService.orderCancelPreInfo(change, userNo);
				
				
				// 품절취소 알림톡 
				try {
					Order order = new Order();
					order.setOrdChgSq(cancelInfo.getInt("ordChgSq"));
					
					Order info = deliveryDao.getSoldoutInfo(order);
					
					order.setOrdNo(info.getOrdNo());
					order.setCustNo(info.getCustNo());
					order.setOrdPhnno(info.getOrdPhnno());
					order.setGoodsCnt(info.getGoodsCnt());
					order.setGoodsNm(info.getGoodsNm());
					order.setOrdNm(info.getOrdNm());
					order.setCancelAllYn(info.getCancelAllYn());
					order.setExtmallNm(info.getExtmallNm());
					order.setMallGb(info.getMallGb());
					order.setExtmallTell(info.getExtmallTell());
					order.setCnclsmsSendYn(info.getCnclsmsSendYn());
					kakaotalkService.sendSoldoutCancel(order, userNo);
				}catch(Exception e) {
					log.debug("kakaotalkService Exception ordChgSq:"+cancelInfo.getInt("ordChgSq"));
				}
			}
		}
		
	}


	/**
	 * 배송비 정책 코드 별 배송비 조회
	 *
	 * @param DeliveryFee
	 * @return Collection<DeliveryFee>
	 * @author card007
	 * @since 2021. 06. 09
	 */
	public Collection<DeliveryFee> getDeliveryFee(DeliveryFee deliveryFee) {
		return deliveryDao.getDeliveryFee(deliveryFee);
	}
}


















