package com.style24.admin.biz.service;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.gagaframework.web.parameter.GagaMap;
import com.style24.admin.biz.dao.TsaGiftCardDao;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.core.biz.thirdparty.Yes24Giftcard;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Customer;
import com.style24.persistence.domain.GiftCard;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

/**
 * 상품권 Service
 *
 * @author jsshin
 * @since 2021. 05. 12
 */
@Service
@Slf4j
public class TsaGiftCardService {

	@Autowired
	private TsaGiftCardDao giftCardDao;

	@Autowired
	private TsaCustomerService customerService;

	@Autowired
	private Yes24Giftcard yes24Giftcard;

	@Autowired
	private ObjectMapper objectMapper;


	/**
	 * 상품권 대량 등록수
	 *
	 * @param giftCard - 상품권, 고객 정보
	 * @author jsshin
	 * @since 2021. 5. 12
	 */
	public int getGiftCardListCount(GiftCard giftCard) {
		return giftCardDao.getGiftCardListCount(giftCard);
	}

	/**
	 * 상품권 대량등록 목록
	 *
	 * @param giftCard - 상품권, 고객 정보
	 * @author jsshin
	 * @since 2021. 5. 12
	 */
	public Collection<GiftCard> getGiftCardList(GiftCard giftCard) {
		return giftCardDao.getGiftCardList(giftCard);
	}

	/**
	 * 상품권 저장
	 *
	 * @param gagaMap - 상품권, 고객 정보
	 * @author jsshin
	 * @throws Exception
	 * @since 2021. 5. 12
	 */
	@Transactional("shopTxnManager")
	public void saveGiftcardUseConfirm(GagaMap gagaMap) throws Exception {
		Integer userNo = TsaSession.getInfo().getUserNo();
		String ip = TsaSession.getIpAddress();
		String resultMsg;
		GiftCard giftCard = objectMapper.convertValue(gagaMap, GiftCard.class);

		giftCard.setRegNo(userNo);
		giftCard.setUpdNo(userNo);
		giftCard.setIpAddr(ip);

		// 1. 엑셀 내용 데이터 생성
		giftCardDao.createGfcdBulkReg(giftCard);

		// 2. 활동회원 확인
		Customer customer = customerService.getCustomerInfoByCustId(giftCard.getCustId().trim());

		// 3. 유효한 회원이면
		if (customer != null) {
			Integer custNo = customer.getCustNo();
			String pubNo = giftCard.getGfcdNo().trim();
			// 상품권 API 호출
			String result = yes24Giftcard.soapDataTransfer(pubNo, ip, custNo);

			giftCard.setGfcdNo(pubNo);
			giftCard.setCustNo(custNo);

			if ("01".equals(result)) {
				giftCard.setResultVal(result);
				resultMsg = "존재하지 않는 상품권";
			} else if ("02".equals(result)) {
				giftCard.setResultVal(result);
				resultMsg = "이미 등록된 상품권";
			} else if ("03".equals(result)) {
				giftCard.setResultVal(result);
				resultMsg = "삭제된 상품권";
			} else if ("04".equals(result)) {
				giftCard.setResultVal(result);
				resultMsg = "기간이 만료된 상품권";
			} else if ("99".equals(result)) {
				giftCard.setResultVal(result);
				resultMsg = "시스템 오류";
			} else {
				giftCard.setResultVal("00");
				resultMsg = "상품권등록 성공";
			}
			// 상품권 API 이력 저장
			giftCardDao.createGiftcardApi(giftCard);

			// 성공일 경우
			if ("00".equals(giftCard.getResultVal())) {
				String[] param = result.split("[|]");
				// [2021-04-01, 오전, 12:00:00]
				String[] param2 = param[2].split(" ");
				String[] param3 = param[3].split(" ");

				// 고객 상품권 등록
				giftCard.setGfcdNm("YES24상품권");
				giftCard.setChgGfcdAmt(Integer.parseInt(param[1])); // 승인금액
				giftCard.setRmGfcdAmt(Integer.parseInt(param[1])); // 승인금액
				giftCard.setUseStDate(param2[0]);
				giftCard.setUseExpDate(param3[0]);
				giftCardDao.createGiftcardUseConfirm(giftCard);

				// 고객 상품권 이력 등록
				giftCard.setOccurGb(TscConstants.GiftCardOccurGb.REGISTER_GIFTCARD.value());
				giftCard.setOccurDtlDesc("상품권등록");
				giftCard.setGfcdAmt(Integer.parseInt(param[1]));
				giftCardDao.createCustGiftcardHistory(giftCard);
			}
		} else {
			resultMsg = "존재하지 않는 회원";
		}

		giftCard.setResultMsg(resultMsg);
		giftCardDao.updateGfcdBulkReg(giftCard);

	}

}
