package com.madeu.batch;

import com.madeu.dao.crm.crmmemberpay.CrmMemberPaySqlMapDAO;
import com.madeu.dao.web.webmembership.WebMembershipSqlMapDAO;
import com.madeu.dao.web.webpayment.WebPaymentSqlMapDAO;
import com.madeu.dao.web.webuser.WebUserSqlMapDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class InsertPaymentBatch {

	@Autowired
	private WebUserSqlMapDAO webUserSqlMapDAO;

	@Autowired
	private WebPaymentSqlMapDAO webPaymentSqlMapDAO;

	@Autowired
	private WebMembershipSqlMapDAO webMembershipSqlMapDAO;

	@Autowired
	private CrmMemberPaySqlMapDAO crmMemberPaySqlMapDAO;

//    @Scheduled(fixedDelay = 33000000) // 5분30초마다 실행
	public void go() {
		log.debug("****************** InsertPaymentBatch START ******************");

		try{
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			String tId = String.valueOf(System.currentTimeMillis());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar c1 = Calendar.getInstance();
			String tDate = sdf.format(c1.getTime());

			paramMap.put("tId", tId);
			paramMap.put("tDate", tDate);


			// 대상자 조회 (PID 있는)
			HashMap<String, Object> selectMap = new HashMap<String, Object>();
			List<Map<String, Object>> listMap = webUserSqlMapDAO.selectListUserPid(selectMap);
			int listMapSize = listMap.size();

			if(0<listMapSize){
				for(int i=0; i<listMapSize; i++){
					log.debug("muUserId     : "+listMap.get(i).get("muUserId"));
					log.debug("crmMemberPid : "+listMap.get(i).get("crmMemberPid"));
					paramMap.put("muUserId", listMap.get(i).get("muUserId"));
					paramMap.put("crmMemberPid", listMap.get(i).get("crmMemberPid"));

					List<Map<String, Object>> paymentListMap = crmMemberPaySqlMapDAO.selectListMemberPay(paramMap);
					int paymentListMapSize = paymentListMap.size();
					if(0<paymentListMapSize){
						for(int j=0; j<paymentListMapSize; j++){
							log.debug("crmMemberPayPid : "+paymentListMap.get(j).get("crmMemberPayPid"));
							paramMap.put("crmMemberPayPid", paymentListMap.get(j).get("crmMemberPayPid"));
							List<Map<String, Object>> checkListMap = webPaymentSqlMapDAO.selectListPaymentCheck(paramMap);

							if(0==checkListMap.size()){
								String mDivi1Name = String.valueOf(paymentListMap.get(j).get("mDivi1Name"));
								String mDivi2Name = String.valueOf(paymentListMap.get(j).get("mDivi2Name"));
								int payMembership = Integer.parseInt(String.valueOf(paymentListMap.get(j).get("payMembership")));

								log.debug("mDivi1Name    : "+mDivi1Name);
								log.debug("mDivi2Name    : "+mDivi2Name);
								log.debug("payMembership : "+payMembership);

								if(!("양도").equals(mDivi2Name) && !("양도받음").equals(mDivi2Name)){
									String muPaymentId = ("PMT").concat(String.valueOf(System.currentTimeMillis()));

									paramMap.put("muPaymentId", muPaymentId);
									webPaymentSqlMapDAO.insertPaymentBatch(paramMap);
									paramMap.put("muPaymentId", paramMap.get("id"));


									// detail 등록
									String muPaymentDetailId = ("PMD").concat(String.valueOf(System.currentTimeMillis()));

									paramMap.put("muPaymentDetailId", muPaymentDetailId);
									webPaymentSqlMapDAO.insertPaymentDetailBatch(paramMap);
								}

								if(("멤버쉽").equals(mDivi1Name) || 0>payMembership){
									// 구분이 멤버쉽이거나 멤버쉽을 사용했으면
									// 멤버십 등록
									String muMembershipHistoryId = ("M").concat(String.valueOf(System.currentTimeMillis()));

									paramMap.put("muMembershipHistoryId", muMembershipHistoryId);
									webMembershipSqlMapDAO.insertMembershipHistoryBatch(paramMap);
								}
							}
						}


						// 최종 멤버십 업데이트
						// 멤버십 고객 있는지 조회
						List<Map<String, Object>> membershipListMap = webMembershipSqlMapDAO.selectMembershipCheckBatch(paramMap);
						int membershipListMapSize = membershipListMap.size();
						// 멤버십 이력 있는지 조회
						List<Map<String, Object>> membershipHistoryListMap = webMembershipSqlMapDAO.selectMembershipHistoryCheckBatch(paramMap);
						int membershipHistoryCount = Integer.parseInt(String.valueOf(membershipHistoryListMap.get(0).get("totalCount")));

						if(0<membershipListMapSize){
							paramMap.put("muMembershipId", membershipListMap.get(0).get("muMembershipId"));

							webMembershipSqlMapDAO.updateMembershipBatch(paramMap);
						}else{
							if(0<membershipHistoryCount){
								// 멤버십 이력이 있으면

								HashMap<String, Object> insertMap = new HashMap<String, Object>();
								// 고객 멤버십 여부 업데이트
								webUserSqlMapDAO.updateMembershipYnBatch(paramMap);

								// 등록
								String muMembershipId = ("M").concat(String.valueOf(System.currentTimeMillis()));
								insertMap.put("muMembershipId",muMembershipId);
								insertMap.put("muUserId",paramMap.get("muUserId"));
//								insertMap.put("regId",paramMap.get("regId"));
//								insertMap.put("modId",paramMap.get("modId"));
								insertMap.put("tId",paramMap.get("tId"));
								insertMap.put("tDate",paramMap.get("tDate"));
								webMembershipSqlMapDAO.insertMembershipBatch(insertMap);
							}
						}
					}
				}
			}
		}catch(Exception e){
			log.debug("Exception InsertPaymentBatch - run : " + e.getMessage());
		}

		log.debug("****************** InsertPaymentBatch  END  ******************");
	}
}
