package com.style24.admin.biz.service;

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

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

import com.style24.admin.biz.dao.TsaDeliveryDao;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.admin.support.util.TsitUtil;

import com.style24.persistence.domain.Delivery;
import com.style24.admin.support.env.TsaConstants.OrderDetailStat;
import com.style24.admin.support.env.TsaConstants;
import com.style24.core.support.message.TscMessageByLocale;

import lombok.extern.slf4j.Slf4j;

/**
 * 배송관리 Service
 *
 * @author moon
 * @since 2020. 11. 05
 */
@Service
@Slf4j
public class TsaDeliveryService {

	@Autowired
	private TscMessageByLocale message;
	
	@Autowired
	private TsaDeliveryDao deliveryDao;

	/**
	 * 배송 목록
	 *
	 * @param -Delivery
	 * @return Collection<Delivery>
	 * @author moon
	 * @since 2020. 11. 05
	 */
	public Collection<Delivery> getDeliveryList(Delivery delivery) {
		// multi row 검색관련 처리
		
		if (StringUtils.isNotBlank(delivery.getCondition())) {
			TsitUtil tsitUtil = new TsitUtil();
			String searchCondition = tsitUtil.replaceMultiSelectParamsArr(delivery.getCondition());
			delivery.setConditions(searchCondition.split(","));
		}

		// 외부몰권한일 때 외부몰벤더ID 설정
		if (TsaSession.getInfo().getRoleCd().startsWith("C")) {
			delivery.setMallGb(TsaConstants.MallGb.EXTMALL.value());
			delivery.setVendorId(TsaSession.getInfo().getVendorId());
		}
		
		return deliveryDao.getDeliveryList(delivery);
	}
	
	/**
	 * 주문상세
	 *
	 * @param  ordDtlNo
	 * @return Collection<Delivery>
	 * @author moon
	 * @since 2020. 11. 05
	 */
	public Collection<Delivery> getOrderDetail(long ordDtlNo) {
		return deliveryDao.getOrderDetail(ordDtlNo);
	}	
	
	/**
	 * 수기송장등록/수정
	 *
	 * @param -Delivery
	 * @return void
	 * @author moon
	 * @since 2020. 11. 05
	 */
	@Transactional("shopTxnManager")
	public void saveOrderDetail(Collection<Delivery> deliveryList) {
	
		int userId = TsaSession.getInfo().getUserNo();

		for (Delivery oneDelivery : deliveryList) {
			oneDelivery.setUpdNo(userId);
			oneDelivery.setRegNo(userId);
			// 배송 준비중, 배송중이면 해당 상태값으로 업데이트
			if (OrderDetailStat.DELIVERY_PREPARE.equals(oneDelivery.getOrgOrdDtlStat()) || OrderDetailStat.SHIPPING.equals(oneDelivery.getOrgOrdDtlStat())) {
				oneDelivery.setOrdDtlStat(oneDelivery.getOrgOrdDtlStat());
			} else {
				oneDelivery.setOrdDtlStat(OrderDetailStat.DELIVERY_PREPARE.value());
			}
			oneDelivery.setHstMemo("수기송장 수정");
			// 주문상세수정
			deliveryDao.updateOrderDetailStat(oneDelivery);
			// 주문상세이력
			deliveryDao.createOrderDetailHst(oneDelivery);
		}

	}	
	
	/**
	 * 출고금지상품 목록
	 *
	 * @param  Delivery
	 * @return Delivery List
	 * @author moon
	 * @since 2020. 11. 05
	 */
	public Collection<Delivery> getBangoodsList(Delivery delivery) {
		return deliveryDao.getBangoodsList(delivery);
	}	
	
	/**
	 * 출고금지상품 삭제
	 *
	 * @param -TsaDelivery
	 * @return void
	 * @author moon
	 * @since 2020. 11. 05
	 */
	@Transactional("shopTxnManager")
	public void deleteBangoods(Collection<Delivery> bangoodsList) {
		int userId = TsaSession.getInfo().getUserNo();

		for (Delivery oneBangood : bangoodsList) {
			oneBangood.setUpdNo(userId);
			deliveryDao.deleteBangoods(oneBangood);
		}
	}
	
	/**
	 * 출고금지상품 등록 대상목록
	 *
	 * @param  Delivery
	 * @return Collection<Delivery>
	 * @author moon
	 * @since 2020. 11. 05
	 */
	public Collection<Delivery> getBangoodsRegistList(Delivery delivery) {
		return deliveryDao.getBangoodsRegistList(delivery);
	}
	
	/**
	 * 출고금지상품 등록
	 *
	 * @param -TsaDelivery
	 * @return void
	 * @author moon
	 * @since 2020. 11. 05
	 */
	@Transactional("shopTxnManager")
	public void createBangoods(Collection<Delivery> bangoodsList) {
		int userId = TsaSession.getInfo().getUserNo();

		for (Delivery oneBangood : bangoodsList) {

			int cnt = deliveryDao.getBangoodsListCnt(oneBangood);
			if (cnt > 0) {
				throw new IllegalStateException(message.getMessage("DELIVERY_0001"));
			}

			oneBangood.setRegNo(userId);
			oneBangood.setUpdNo(userId);
			deliveryDao.createBangoods(oneBangood);
		}
	}
	
	/**
	 * 배송/회수지시 목록
	 *
	 * @param -Delivery
	 * @return Collection<Delivery>
	 * @author moon
	 * @since 2020. 11. 11
	 */	
	public Collection<Delivery> getDeliveryWithdrawDirectiveList(Delivery delivery) {
		return deliveryDao.getDeliveryWithdrawDirectiveList(delivery);
	}
	
}
