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 TsbCustomerCryptoJob6 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();
		params.setCustNo(2200000);
		int totCnt = migrationService.getCustomerCnt(params);
		//int totCnt = 2;

		params.setRows(rows);

		double loopCnt = (totCnt / rows) + 1;

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

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

			for (Migration migration : migrationCollection) {
				//System.out.println(migration);
				migration.setStartRow(startRow);
				migration.setRows(rows);
				migration.setCustNm(CryptoUtils.encryptAES(migration.getCustNm()));
				migration.setBirthYmd(CryptoUtils.encryptAES(migration.getBirthYmd()));
				migration.setSexGb(CryptoUtils.encryptAES(migration.getSexGb()));
				migration.setCellPhnno(CryptoUtils.encryptAES(migration.getCellPhnno()));
				migration.setEmail(CryptoUtils.encryptAES(migration.getEmail()));
				migration.setHomeZipcode(CryptoUtils.encryptAES(migration.getHomeZipcode()));
				migration.setHomeBaseAddr(CryptoUtils.encryptAES(migration.getHomeBaseAddr()));
				migration.setHomeDtlAddr(CryptoUtils.encryptAES(migration.getHomeDtlAddr()));
				//System.out.println(migration);

				migrationService.updateCustomerEncrypt(migration);
			}

		}

		return result;
	}

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

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