package com.style24.batch.biz.service;

import java.util.Collection;

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

import com.style24.batch.biz.dao.TsbCustomerDao;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.biz.service.TscCustomerService;
import com.style24.core.biz.service.TscKakaotalkService;
import com.style24.core.biz.service.TscMailService;
import com.style24.core.biz.thirdparty.NetpathyMailSender;
import com.style24.core.biz.thirdparty.SsgKakaoSender;
import com.style24.core.support.env.TscConstants;
import com.style24.core.support.util.CryptoUtils;
import com.style24.persistence.domain.CustContactHst;
import com.style24.persistence.domain.CustCoupon;
import com.style24.persistence.domain.CustGrade;
import com.style24.persistence.domain.Customer;
import com.style24.persistence.domain.CustomerSearch;
import com.style24.persistence.domain.Netpathy;
import com.style24.persistence.domain.SsgDirectMessage;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.parameter.GagaMap;

/**
 * 회원(고객) Service
 *
 * @author jsshin
 * @since 2021. 03. 05
 */
@Service
@Slf4j
public class TsbCustomerService {

	@Autowired
	private TsbCustomerDao customerDao;

	@Autowired
	private TscCustomerService coreCustomerService;

	@Autowired
	private TscKakaotalkService kakaotalkService;

	@Autowired
	private TscMailService mailService;

	@Autowired
	private SsgKakaoSender kakaoSender;

	@Autowired
	private NetpathyMailSender netpathyMailSender;

	@Autowired
	private Environment env;

	/**
	 * 휴면전환예정 메일 발송 대상
	 *
	 * @param customerSearch - 전환예정기간
	 * @return Collection<Customer>
	 * @author jsshin
	 * @since 2021. 03. 05
	 */
	public int getDormantCustomerScheduleListCount(CustomerSearch customerSearch) {
		return customerDao.getDormantCustomerScheduleListCount(customerSearch);
	}

	/**
	 * 휴면전환예정 메일 발송 대상
	 *
	 * @param customerSearch - 전환예정기간
	 * @return Collection<Customer>
	 * @author jsshin
	 * @since 2021. 03. 05
	 */
	public Collection<Customer> getDormantCustomerScheduleList(CustomerSearch customerSearch) {
		return customerDao.getDormantCustomerScheduleList(customerSearch);
	}

	/**
	 * 휴면전환예정 안내 메일 발송 대상
	 *
	 * @param customerCollection - 대상
	 * @return int - 성공여부
	 * @author jsshin
	 * @since 2021. 03. 08
	 */
	@Transactional("shopTxnManager")
	public GagaMap sendDormantCustomerSchedule(Collection<Customer> customerCollection) {
		GagaMap result = new GagaMap();
		int succCnt = 0;
		int failCnt = 0;
		Integer senderNo = TsbConstants.REG_NO;
		for (Customer customer : customerCollection) {
			try {
				// 이메일 발송
				if (StringUtils.isNotBlank(customer.getEmail())) {
					mailService.sendExpectedDormentCustomer(customer, senderNo);
				}
				// 카카오톡 알림톡
				if (StringUtils.isNotBlank(customer.getCellPhnno())) {
					kakaotalkService.sendExpectedDormentCustomer(customer, senderNo);
				}

				succCnt++;

			} catch (Exception e) {
				failCnt++;
			}
		}

		result.setInt("succCnt", succCnt);
		result.setInt("failCnt", failCnt);

		return result;
	}

	/**
	 * 휴면회원 전환대상
	 *
	 * @param customerSearch - 전환기간
	 * @return Collection<Customer>
	 * @author jsshin
	 * @since 2021. 03. 05
	 */
	public Collection<Customer> getDormentCustomerList(CustomerSearch customerSearch) {
		return customerDao.getDormentCustomerList(customerSearch);
	}

	/**
	 * 휴면회원 전환 처리
	 *
	 * @param customerSearch - 전환기간
	 * @return GagaMap - 결과
	 * @author jsshin
	 * @since 2021. 03. 05
	 */
	@Transactional("shopTxnManager")
	public GagaMap saveDormantCustomer(CustomerSearch customerSearch) {
		GagaMap result = new GagaMap();
		customerSearch.setEncodedCustNm(CryptoUtils.encryptAES("휴면"));

		int createCustCnt = customerDao.createDormantCustomer(customerSearch);
		int updateCustCnt = customerDao.updateDormantCustomer(customerSearch);
		int createOrdCnt = customerDao.createDormantOrder(customerSearch);
		int updateOrdCnt = customerDao.updateDormantOrder(customerSearch);

		result.setInt("createCustCnt", createCustCnt);
		result.setInt("updateCustCnt", updateCustCnt);
		result.setInt("createOrdCnt", createOrdCnt);
		result.setInt("updateOrdCnt", updateOrdCnt);

		return result;
	}

	/**
	 * 탈퇴회원 삭제대상 목록
	 *
	 * @param customerSearch - 전환기간
	 * @return Collection<Customer>
	 * @author jsshin
	 * @since 2021. 03. 08
	 */
	public Collection<Customer> getExpireSecedeCustomerList(CustomerSearch customerSearch) {

		return customerDao.getExpireSecedeCustomerList(customerSearch);
	}

	/**
	 * 탈퇴처리
	 *
	 * @param customerCollection - 삭제대상 목록
	 * @return GagaMap - 결과정보
	 * @author jsshin
	 * @since 2021. 03. 08
	 */
	@Transactional("shopTxnManager")
	public GagaMap saveSecedeCustomer(Collection<Customer> customerCollection) {
		GagaMap result = new GagaMap();
		int succCnt = 0;
		int failCnt = 0;
		for (Customer customer : customerCollection) {
			Integer custNo = customer.getCustNo();
			try {
				coreCustomerService.deleteSecedeCustomer(custNo);
				succCnt++;
			} catch (Exception e) {
				failCnt++;
			}
		}
		result.setInt("succCnt", succCnt);
		result.setInt("failCnt", failCnt);
		return result;
	}

	/**
	 * 개인정보 이용내역 안내 대상목록
	 *
	 * @return Collection<Customer>
	 * @author jsshin
	 * @since 2021. 05. 06
	 */
	public Collection<Customer> getPrivacyPolicyNoticeTargetList(CustomerSearch customerSearch) {
		return customerDao.getPrivacyPolicyNoticeTargetList(customerSearch);
	}

	/**
	 * 개인정보 이용내역 안내 발송
	 *
	 * @param customerCollection - 대상
	 * @return GagaMap - 성공/실패 카운트
	 * @author jsshin
	 * @since 2021. 05. 06
	 */
	@Transactional("shopTxnManager")
	public GagaMap sendPrivacyPolicyNotice(Collection<Customer> customerCollection) {
		GagaMap result = new GagaMap();
		int succCnt = 0;
		int failCnt = 0;
		for (Customer customer : customerCollection) {
			try {
				Integer senderNo = TsbConstants.REG_NO;

				if (StringUtils.isNotBlank(customer.getEmail())) {
					mailService.sendPrivacyPolicyNotice(customer, senderNo);
				}

				if (StringUtils.isNotBlank(customer.getCellPhnno())) {
					kakaotalkService.sendPrivacyPolicyNotice(customer, senderNo);
				}

				succCnt++;

			} catch (Exception e) {
				log.error(e.getMessage());
				failCnt++;
			}
		}
		result.setInt("succCnt", succCnt);
		result.setInt("failCnt", failCnt);
		return result;
	}

	/**
	 * 개인정보 이용내역 안내 발송 - 알림톡
	 *
	 * @param
	 * @return int
	 * @author jsshin
	 * @since 2021. 05. 06
	 */
	@Transactional("shopTxnManager")
	public int sendPrivacyPolicyNoticeKko() {
		String siteNm = "STYLE24";
		String active = env.getProperty("spring.profiles.active");
		SsgDirectMessage dm = new SsgDirectMessage();
		dm.setFkkoresendtype("LMS");
		GagaMap replaceInfo = new GagaMap();
		replaceInfo.setString("siteNm", siteNm);
		int result;
		SsgDirectMessage directMessage = kakaoSender.getDiretMessage(SsgKakaoSender.KakaoAnswerSq.PRIVACY_POLICY.value(), dm, replaceInfo);
		if ("style".equals(active) || "real".equals(active) || "locp".equals(active) || "brdg".equals(active)) {
			result = customerDao.createPrivacyPolicyNoticeKko(directMessage);
		} else {
			result = customerDao.createPrivacyPolicyNoticeKkoDev(directMessage);
		}

		// 고객접촉이력 정보
		Integer senderNo = TsbConstants.REG_NO;
		CustContactHst custContactHst = new CustContactHst();
		custContactHst.setContactType(TscConstants.ContactType.PRIVACY_POLICY.value()); // 접촉유형:회원-휴대전화인증(공통코드G054)
		custContactHst.setContactMethod(TscConstants.ContactMethod.KAKAOTALK.value()); // 접촉방법:알림톡+문자(공통코드G055)
		custContactHst.setContactContents("개인정보이용내역");
		custContactHst.setSenderNo(senderNo);
		custContactHst.setRegNo(senderNo);
		custContactHst.setUpdNo(senderNo);
		customerDao.createCustomerContactHistoryBulk(custContactHst);
		return result;
	}

	/**
	 * 개인정보 이용내역 안내 발송 - 메일
	 *
	 * @param
	 * @return int
	 * @author jsshin
	 * @since 2021. 05. 06
	 */
	@Transactional("shopTxnManager")
	public int sendPrivacyPolicyNoticeEmail() {
		String siteNm = "STYLE24";
		String active = env.getProperty("spring.profiles.active");
		GagaMap replaceInfo = new GagaMap();
		replaceInfo.setString("siteNm", siteNm);

		int result;
		Netpathy netpathy = netpathyMailSender.getMailContents(NetpathyMailSender.MailAnswerSq.PRIVACY_POLICY.value(), replaceInfo);

		if ("style".equals(active) || "real".equals(active) || "locp".equals(active) || "brdg".equals(active)) {
			result = customerDao.createPrivacyPolicyNoticeEmail(netpathy);
		} else {
			result = customerDao.createPrivacyPolicyNoticeEmailDev(netpathy);
		}

		// 고객접촉이력 정보
		Integer senderNo = TsbConstants.REG_NO;
		CustContactHst custContactHst = new CustContactHst();
		custContactHst.setContactType(TscConstants.ContactType.PRIVACY_POLICY.value()); // 접촉유형:임시비밀번호발급(공통코드G054)
		custContactHst.setContactMethod(TscConstants.ContactMethod.EMAIL.value()); // 접촉방법:알림톡+문자(공통코드G055)
		custContactHst.setContactContents("개인정보이용내역");
		custContactHst.setSenderNo(senderNo);
		custContactHst.setRegNo(senderNo);
		customerDao.createCustomerContactHistoryBulk(custContactHst);
		return result;
	}

	/**
	 * 마케팅 정보 수신동의 내역 안내 대상 총 건수
	 *
	 * @return int - 건수
	 * @author jsshin
	 * @since 2021. 07. 27
	 */
	public int getMarketingAgreeNoticeTargetListCount(CustomerSearch customerSearch) {
		return customerDao.getMarketingAgreeNoticeTargetListCount(customerSearch);
	}

	/**
	 * 마케팅 정보 수신동의 내역 안내 대상목록
	 *
	 * @return Collection<Customer>
	 * @author jsshin
	 * @since 2021. 05. 06
	 */
	public Collection<Customer> getMarketingAgreeNoticeTargetList(CustomerSearch customerSearch) {
		return customerDao.getMarketingAgreeNoticeTargetList(customerSearch);
	}

	/**
	 * 마케팅 정보 수신동의 내역 안내 발송
	 *
	 * @param customerCollection - 대상
	 * @return GagaMap - 성공/실패 카운트
	 * @author jsshin
	 * @since 2021. 05. 06
	 */
	@Transactional("shopTxnManager")
	public GagaMap sendMarketingAgreeNotice(Collection<Customer> customerCollection) {
		GagaMap result = new GagaMap();
		int succCnt = 0;
		int failCnt = 0;
		for (Customer customer : customerCollection) {
			try {
				Integer senderNo = TsbConstants.REG_NO;

				// 이메일 발송
				if (StringUtils.isNotBlank(customer.getEmail())) {
					mailService.sendMarketingAgreeNotice(customer, senderNo);
				}

				if (StringUtils.isNotBlank(customer.getCellPhnno())) {
					kakaotalkService.sendMarketingAgreeNotice(customer, senderNo);
				}

				succCnt++;

			} catch (Exception e) {
				log.error(e.getMessage());
				failCnt++;
			}
		}
		result.setInt("succCnt", succCnt);
		result.setInt("failCnt", failCnt);
		return result;
	}

	/**
	 * 회원등급 변경
	 *
	 * @param custGrade - 정보
	 * @return GagaMap - 성공/실패 카운트
	 * @author jsshin
	 * @since 2021. 05. 06
	 */
	@Transactional("shopTxnManager")
	public GagaMap saveCustomerGrade(CustGrade custGrade) {
		GagaMap result = new GagaMap();
		Collection<CustGrade> custGradeCollection = customerDao.getCustGradePolicyList(custGrade);
		int custGradeHstCnt = 0;

		// 1. 등급산정
		for (CustGrade grade : custGradeCollection) {
			grade.setSiteCd(custGrade.getSiteCd());
			grade.setRegNo(custGrade.getRegNo());
			custGradeHstCnt += customerDao.createChangeCustGradeHst(grade);
		}

		// 2. 회원이력 생성
		int custHstCnt = customerDao.createChangeCustomerHst(custGrade);

		// 3. 회원등급 변경
		int custGradeCnt = customerDao.updateCustomerGrade(custGrade);

		result.setInt("custGradeHstCnt", custGradeHstCnt);
		result.setInt("custHstCnt", custHstCnt);
		result.setInt("custGradeCnt", custGradeCnt);

		return result;
	}

	/**
	 * 회원등급 변경 건수
	 *
	 * @param customerSearch - 정보
	 * @return int 건수
	 * @author jsshin
	 * @since 2021. 07. 27
	 */
	public int getCustGradeChangeTargetListCount(CustomerSearch customerSearch) {
		return customerDao.getCustGradeChangeTargetListCount(customerSearch);
	}

	/**
	 * 회원등급 변경
	 *
	 * @param customerSearch - 정보
	 * @return Collection<Customer>
	 * @author jsshin
	 * @since 2021. 05. 06
	 */
	public Collection<Customer> getCustGradeChangeTargetList(CustomerSearch customerSearch) {
		return customerDao.getCustGradeChangeTargetList(customerSearch);
	}

	/**
	 * 회원등급 변경 대상 알림톡
	 *
	 * @param customerCollection - 알림톡 발송대상
	 * @return Collection<Customer>
	 * @author jsshin
	 * @since 2021. 07. 27
	 */
	@Transactional("shopTxnManager")
	public GagaMap sendCustGradeChangeTarget(Collection<Customer> customerCollection) {
		GagaMap result = new GagaMap();
		int succCnt = 0;
		int failCnt = 0;
		Integer senderNo = TsbConstants.REG_NO;
		for (Customer customer : customerCollection) {
			try {
				if (StringUtils.isNotBlank(customer.getCellPhnno()) && "Y".equals(customer.getSmsAgreeYn())) {
					kakaotalkService.sendGradeNotice(customer, senderNo);
				}
				succCnt++;
			} catch (Exception e) {
				failCnt++;
			}
		}
		result.setInt("succCnt", succCnt);
		result.setInt("failCnt", failCnt);
		return result;
	}

	/**
	 * 넷퍼시메일솔루션으로 회원정보 생성
	 * @author gagamel
	 * @since 2021. 5. 10
	 */
	@Transactional("shopTxnManager")
	public void createNetpathyMailCustomer() {
		int result = 0;

		String active = env.getProperty("spring.profiles.active");
		if ("style".equals(active) || "brdg".equals(active)) {
			customerDao.truncateNetpathyMailCustomer();
			result = customerDao.createNetpathyMailCustomer();
		} else {
			customerDao.truncateNetpathyMailCustomerDev();
			result = customerDao.createNetpathyMailCustomerDev();
		}

		log.info("1.넷퍼시메일솔루션으로 회원정보 생성 ({}건) : OK", result);
	}

	/**
	 * 넷퍼시메일솔루션으로 탈퇴/휴면회원정보 생성
	 * @author gagamel
	 * @since 2021. 5. 10
	 */
	@Transactional("shopTxnManager")
	public void createNetpathyMailWithdrawalCustomer() {
		int result = 0;

		String active = env.getProperty("spring.profiles.active");
		if ("style".equals(active) || "brdg".equals(active)) {
			customerDao.truncateNetpathyMailWithdrawalCustomer();
			result = customerDao.createNetpathyMailWithdrawalCustomer();
		} else {
			customerDao.truncateNetpathyMailWithdrawalCustomerDev();
			result = customerDao.createNetpathyMailWithdrawalCustomerDev();
		}

		log.info("2.넷퍼시메일솔루션으로 탈퇴/휴면회원정보 생성 ({}건) : OK", result);
	}

	/**
	 * 넷퍼시메일솔루션으로 회원포인트정보 생성
	 * 		전일자 변경된 포인트가 있는 회원에 대해서만
	 * @author gagamel
	 * @since 2021. 5. 10
	 */
	@Transactional("shopTxnManager")
	public void createNetpathyMailCustomerPoint() {
		int result = 0;

		String active = env.getProperty("spring.profiles.active");
		if ("style".equals(active) || "brdg".equals(active)) {
			customerDao.truncateNetpathyMailCustomerPoint();
			result = customerDao.createNetpathyMailCustomerPoint();
		} else {
			customerDao.truncateNetpathyMailCustomerPointDev();
			result = customerDao.createNetpathyMailCustomerPointDev();
		}

		log.info("3.넷퍼시메일솔루션으로 회원포인트정보 생성 ({}건) : OK", result);
	}

	/**
	 * 생일쿠폰안내대상 목록 총 건수
	 *
	 * @param customerSearch - 사이트 코드
	 * @return int - 총 건수
	 * @author jsshin
	 * @since  2021. 07. 27
	 */
	public int getBirthDayTargetListCount(CustomerSearch customerSearch) {
		return customerDao.getBirthDayTargetListCount(customerSearch);
	}

	/**
	 * 생일쿠폰안내대상 목록
	 *
	 * @param customerSearch - 사이트 코드
	 * @return Collection<Customer> - 목록
	 * @author jsshin
	 * @since  2021. 05. 10
	 */
	public Collection<CustCoupon> getBirthDayTargetList(CustomerSearch customerSearch) {
		return customerDao.getBirthDayTargetList(customerSearch);
	}

	/**
	 * 생일쿠폰 안내
	 *
	 * @param couponCollection - 쿠폰정보, 고객정보
	 * @return GagaMap - 결과
	 * @author jsshin
	 * @since  2021. 05. 10
	 */
	@Transactional("shopTxnManager")
	public GagaMap sendBirthDayCouponNotice(Collection<CustCoupon> couponCollection) {
		GagaMap result = new GagaMap();
		int succCnt = 0;
		int failCnt = 0;
		Integer senderNo = TsbConstants.REG_NO;
		for (CustCoupon custCoupon : couponCollection) {
			try {
				custCoupon.setRegNo(senderNo);
				custCoupon.setUpdNo(senderNo);
				// 이메일 발송
				if (StringUtils.isNotBlank(custCoupon.getEmail()) && "Y".equals(custCoupon.getEmailAgreeYn())) {
					mailService.sendBirthDayCouponNotice(custCoupon, senderNo);
				}
				// 카카오 알림톡
				if (StringUtils.isNotBlank(custCoupon.getCellPhnno()) && "Y".equals(custCoupon.getSmsAgreeYn())) {
					kakaotalkService.sendBirthDayCouponNotice(custCoupon, senderNo);
				}
				succCnt++;
			} catch (Exception e) {
				log.error(e.getMessage());
				failCnt++;
			}

		}
		result.setInt("succCnt", succCnt);
		result.setInt("failCnt", failCnt);
		return result;

	}

	/**
	 * 휴면/미접속자 대상 총 건수
	 *
	 * @param customerSearch - 사이트 코드
	 * @return int - 총 건수
	 * @author jsshin
	 * @since  2021. 07. 27
	 */
	public int getComBackNotiTarketListCount(CustomerSearch customerSearch) {
		return customerDao.getComBackNotiTarketListCount(customerSearch);
	}

	/**
	 * 휴면/미접속자 대상 목록
	 *
	 * @param customerSearch - 사이트 코드
	 * @return Collection<Customer> - 목록
	 * @author jsshin
	 * @since  2021. 05. 10
	 */
	public Collection<Customer> getComBackNotiTarketList(CustomerSearch customerSearch) {
		return customerDao.getComBackNotiTarketList(customerSearch);
	}

}
