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

import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbMigrationService;
import com.style24.core.support.util.CryptoUtils;
import com.style24.persistence.domain.Migration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * 주문 수량 부분취소
 *
 * @author swkim
 * @since 2021.05.17
 */
@Component
@Slf4j
public class TsbCancelQtyOrderDetailJob extends TsbAbstractJob<String, String, String> {

	@Autowired
	private TsbMigrationService migrationService;

	@Override
	public String read() throws Exception {
		return "OK";
	}

	@Override
	public String process(String result) throws Exception {


		Collection<Migration> migrationCollection = migrationService.getCancelOrderDtlList();

		for (Migration migration : migrationCollection) {
			Migration params = new Migration();
			int ordChgSq = migration.getOrdChgSq();
			int ordDtlNo = migration.getOrdDtlNo();
			int chgQty = migration.getChgQty();

			params.setOrdChgSq(ordChgSq);
			params.setOrdDtlNo(ordDtlNo);

			Migration orderDetail = migrationService.getCancelOrderDtl(ordDtlNo);

			int ordQty = orderDetail.getOrdQty();
			int cnclRtnQty = orderDetail.getCnclRtnQty();
			int ordAmt = orderDetail.getOrdAmt();
			int rmQty = ordQty - cnclRtnQty;
			int cnclRtnAmt = orderDetail.getCnclRtnAmt();
			int realOrdAmt = orderDetail.getRealOrdAmt();
			int goodsCpnDcAmt = orderDetail.getGoodsCpnDcAmt();
			int cartCpnDcAmt = orderDetail.getCartCpnDcAmt();
			int pntDcAmt = orderDetail.getPntDcAmt();
			int savePntAmt = orderDetail.getSavePntAmt();
			int gfcdUseAmt = orderDetail.getGfcdUseAmt();
			String ordDtlStat = orderDetail.getOrdDtlStat();

			//전체취소됐음
			if (rmQty == chgQty) {
				params.setCnclRtnQty(ordQty);
				params.setCnclRtnAmt(ordAmt);
				params.setGoodsCpnDcAmt(0);
				params.setCartCpnDcAmt(0);
				params.setPntDcAmt(0);
				params.setSavePntAmt(0);
				params.setRealOrdAmt(0);
				params.setGfcdUseAmt(0);
				params.setOrdDtlStat("G013_99");
			} else {
				// REAL_ORD_AMT = ORD_AMT -  CNCL_RTN_AMT - GOODS_CPN_DC_AMT - CART_CPN_DC_AMT - PNT_DC_AMT - GFCD_USE_AMT
				int cCnclRtnAmt = (ordAmt / ordQty) * chgQty;
				int cGoodsCpnDcAmt = (int)(Math.ceil(goodsCpnDcAmt * chgQty / rmQty));
				int cCartCpnDcAmt = (int)(Math.ceil(cartCpnDcAmt * chgQty / rmQty));
				int cPntDcAmt = (int)(Math.ceil(pntDcAmt * chgQty / rmQty));
				int cGfcdUseAmt = (int)(Math.ceil(gfcdUseAmt * chgQty / rmQty));
				int cRealOrdAmt = ordAmt - (cnclRtnAmt + cCnclRtnAmt) - (goodsCpnDcAmt - cGoodsCpnDcAmt) - (cartCpnDcAmt - cCartCpnDcAmt) - (pntDcAmt - cPntDcAmt) - (gfcdUseAmt - cGfcdUseAmt);
				int cSavePntAmt = (int)(Math.ceil(savePntAmt * chgQty / rmQty));

				params.setCnclRtnQty(cnclRtnQty + chgQty);
				params.setCnclRtnAmt(cnclRtnAmt + cCnclRtnAmt);
				params.setGoodsCpnDcAmt(goodsCpnDcAmt - cGoodsCpnDcAmt);
				params.setCartCpnDcAmt(cartCpnDcAmt - cCartCpnDcAmt);
				params.setPntDcAmt(pntDcAmt - cPntDcAmt);
				params.setSavePntAmt(savePntAmt - cSavePntAmt);
				params.setRealOrdAmt(cRealOrdAmt);
				params.setGfcdUseAmt(gfcdUseAmt - cGfcdUseAmt);
				params.setOrdDtlStat(ordDtlStat);
			}

			params.setChgStat("G685_19");
			migrationService.updateOrderDetail(params);
		}

		return result;
	}

	@Override
	public String write(String result) throws Exception {
		return result;
	}

	@Override
	public void notify(String result) throws Exception {
		// Do nothing
	}
}
