/*
*========================================================================
*
*   SYSTEM   : NS_PLAZA_LANE APP
*   FILE     : lane_main.c
*   Progrmer : JBY (baarong@neighbor21.co.kr)
*   Date     : 2018.08.24
*   Modified :
*   Remark   : Type Change Low To High Trace
*
*========================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/msg.h>
#include <unistd.h>
#include <sys/utsname.h>

#include "dir_path.h"
#include "path_def.h"
#include "file_lb.h"
#include "func_list.h"
#include "snd_rcv_knd.h"
#include "lb_func_list.h"

FILE    *debug;

/*
 *========================================================================
 *
 *   Function : main()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.24
 *   Modified :
 *   Remark   : 하위차로 제어 프로그램 MAIN PROCEDURE
 *
 *========================================================================
*/
int main(int argc, char *argv[])
{
    extern    int    optind,  opterr, optopt;
    extern    char    *optarg;
    struct    tm     *tm;
    struct rcv_lane_data  rd;
    int  options;
    int  rdflg, nResult;
	int  chk_tm1, chk_tm2, q_tm = 0;
	unsigned int proc_kind = 0;
	char buff[4096];
	char plz_id[2+1];
    char rcv_date[8+1];
	char date[8+1];
	char file_name[30+1];
	char queue_d[128];
	char queue_f[128];
	char que_tm[6+1];
	char que_kind[2+1];
	char que_data[4000];
	char rtn_data[4000];
	char res_date[4000];

	time_t now;
    char *p;
	char *ptr;
    FILE *pack_f = NULL;
	FILE *que_f = NULL;

    p = argv[0];

    memset(buff, 0x0, sizeof(buff));
	memset(plz_id, 0x0, sizeof(plz_id));
    memset(rcv_date, 0x0, sizeof(rcv_date));
	memset(date, 0x0, sizeof(date));
	memset(queue_d, 0x00, sizeof(queue_d));
	memset(queue_f, 0x00, sizeof(queue_f));
	memset(que_tm, 0x00, sizeof(que_tm));
	memset(que_kind, 0x00, sizeof(que_kind));
	memset(que_data, 0x00, sizeof(que_data));
	memset(rtn_data, 0x00, sizeof(rtn_data));
	memset(res_date, 0x00, sizeof(res_date));

    opterr = 0;
    while((options = getopt(argc, argv, "d:f:s:e:")) != EOF)
    {
        switch (options)
        {
             case 'd': /* 날짜 */
                  sprintf(rcv_date, "%.8s", optarg);
                  break;
             case 'f': /* 파일이름 */
                  sprintf(file_name, "%.30s", optarg);
                  break;
             case 's': /* 시작시간 */
                  chk_tm1 = atoi(optarg);
                  break;
             case 'e': /* 종료시간 */
                  chk_tm2 = atoi(optarg);
                  break;
             default:  break;
        }
    }
que_log("DATE[%.8s] FILE[%.30s]\n", rcv_date, file_name);

    lb_user_chg();        /* 사용자ID, GROUP ID를 */
                         /* TCS ID로 FIX         */
    now = time(0);
    tm = localtime(&now);
	
	sprintf(queue_d, "%s/%.8s%c", LOG_DIR, rcv_date, '\0');

    sprintf(date, "%04d%02d%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
    log_open(date, pack_f);
    que_log( "=============== Queue 처리 프로세스 START [%d/%d/%d %d:%d:%d] ===============\n", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);

    nResult = InitMessageSenderConfig(p);

//처리 로직(파일열어서 읽고 큐에 송신)
    sprintf(queue_f, "%s/%s", queue_d, file_name);
    que_f = fopen(queue_f, "r");
	
	if(que_f != NULL) {
		while (!feof(que_f)){
			fgets(buff, 4096, que_f);
			snprintf(que_tm, 6+1, buff);
			q_tm = lb_data2atoi(que_tm, 6);
			if(q_tm >= chk_tm1 && q_tm <= chk_tm2)
			{
				snprintf(que_kind, 2+1, "%s", buff+7);
				proc_kind = strtoul(que_kind, &ptr, 16);
				snprintf(que_data, 4000+1, "%s", buff+7);
                //que_log("QUE[%06d][%02x][%s]\n", q_tm, proc_kind, que_data);

                switch(proc_kind){
					case NS_CLOSE_PASS:
					//야간계수
					nResult = proc_close_pass(que_data, rtn_data);
					break;
					case NS_HND_DATA:
					//처리데이터
					nResult = proc_hnd_data(que_data, rtn_data);
					break;
					case NS_CRD_HND_DATA:
					//카드처리데이터
					nResult = proc_card_data(que_data, rtn_data);
					break;
					case NS_WRK_END:
					//근무종료
					nResult = proc_wrk_end(que_data, rtn_data);
					break;
					case NS_REFUND_DATA:
					//환불처리데이터
					nResult = proc_refund(que_data, rtn_data);
					break;
				}
				//que_log("QUE[%06d][%02x][%s]\n", q_tm, proc_kind, rtn_data);
				
				nResult = MessageSender(proc_kind, (char *)&rtn_data, p, res_date);
                if(nResult < 0)
                {
                    que_log("수동 Queue 처리[%s][%s][%.2s] 처리실패\n", rcv_date, file_name, que_data);
                    return(-1);
                }
				
				sleep(2);

			}else if(q_tm > chk_tm2)
			{
				que_log("===== 파일 Queue 처리 범위 초과 ======\n");
				break;
			}
		}
	}
	
// 프로세스 종료
    RemoveTerminatedThread();
    log_close();
    exit(0);
}
