package com.madeu.batch;

import com.madeu.dao.web.websms.WebSmsSqlMapDAO;
import com.popbill.api.MessageService;
import com.popbill.api.message.SentMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class LmsPopbillStateUpdateBatch {

    private static String corpNum;
    private static String userId;

    @Value("${popbill.corp-num}")
    public void setCorpNum(String value){
        corpNum = value;
    }

    @Value("${popbill.user-id}")
    public void setUserId(String value){
        userId = value;
    }

    @Autowired
    private WebSmsSqlMapDAO webSmsSqlMapDAO;
    
    @Autowired
    private MessageService messageService;

    //https://blog.naver.com/lovemema/140200056062
//    @Scheduled(cron = "30 0/5 * * * *") // 5분마다 실행 (30초에 실행)
    public void go() {
        log.debug("******************LmsPopbillStateUpdateBatch START **********************************");

		try{
			log.debug("POPBILL_STATE, POPBILL_RESULT UPDATE START");

			// SMS 환경설정 정보 호출
			// 팝빌회원 사업자번호
			// 팝빌회원 아이디

			log.debug("corpNum : "+corpNum);
			log.debug("userId  : "+userId);

			// 접수번호 대상자 조회
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("type", "L");
			paramMap.put("popbillState", 0);

			List<Map<String, Object>> smsListMap = webSmsSqlMapDAO.selectListPopbillState(paramMap);
			int smsListMapSize = smsListMap.size();

			log.debug("접수번호 갯수 : "+smsListMapSize);

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

				// 접수번호
				String receiptNumber = String.valueOf(smsListMap.get(i).get("receiptNumber"));

				SentMessage[] sentMessages = messageService.getMessages(corpNum, receiptNumber, userId);
				int sentMessagesLength = sentMessages.length;

				for(int j=0; j<sentMessagesLength; j++) {

					String receiveNumber = sentMessages[j].getReceiveNum();
					String receiveName	 = sentMessages[j].getReceiveName();
					String subject		 = sentMessages[j].getSubject();
					String content		 = sentMessages[j].getContent();
					String state		 = String.valueOf(sentMessages[j].getState());
					String result		 = String.valueOf(sentMessages[j].getResult());
					String type			 = String.valueOf(sentMessages[j].getMessageType());
					type = type.substring(0, 1);

					if(("3").equals(state)) {

						HashMap<String, Object> smsParamMap = new HashMap<String, Object>();

						smsParamMap.put("receiptNumber", receiptNumber);
						smsParamMap.put("receiveNumber", receiveNumber);
						smsParamMap.put("receiveName", receiveName);
						smsParamMap.put("popbillState", 0);
						smsParamMap.put("subject", subject);
						smsParamMap.put("content", content);
						smsParamMap.put("state", state);
						smsParamMap.put("result", result);
						smsParamMap.put("type", type);
						smsParamMap.put("modId", "BatchSystem");


						webSmsSqlMapDAO.updateSmsPopbillState(smsParamMap);

					}else {

						/*log.debug("receiptNumber                : "+receiptNumber);
						log.debug("sentMessages["+j+"] receiveNum   : "+receiveNumber);
						log.debug("sentMessages["+j+"] receiveName  : "+receiveName);
						log.debug("sentMessages["+j+"] subject      : "+subject);
						log.debug("sentMessages["+j+"] content      : "+content);
						log.debug("sentMessages["+j+"] state        : "+state);
						log.debug("sentMessages["+j+"] result       : "+result);
						log.debug("sentMessages["+j+"] type         : "+type);*/
					}
				}
			}


			log.debug("POPBILL_STATE, POPBILL_RESULT UPDATE END");

		}catch(Exception e){
			log.debug("Exception LmsPopbillStateUpdateBatch - run" + e.getMessage());
		}

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