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 TsbCustAccountJob 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 {

		int startRow = 0;
		int rows = 10000;

		Migration params = new Migration();

		int totCnt = migrationService.getCustAccountCnt();

		params.setRows(rows);

		double loopCnt = (totCnt / rows) + 1;

		for (int i = 0; i < loopCnt; i++) {

			Collection<Migration> migrationCollection = migrationService.getCustAccountList(params);

			for (Migration migration : migrationCollection) {
				//System.out.println(migration);
				migration.setAccountNo(CryptoUtils.encryptAES(migration.getAccountNo()));

				//System.out.println(migration);

				migrationService.updateCustAccountEnc(migration);
			}

		}

		return result;
	}

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

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