/*********************************************
 *           Code generated by the           *
 * Altibase Precompiler2(APRE) Ver.1(7.1.0.1.2)*
 *********************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ulpLibInterface.h>

/* The variable _esqlopts specifies command-line options
 * {-n, -unsafe_null, stmt caching, ...}
 */
static short _esqlopts[10] = {0,0,1,0,0,0,0,0,0,0};

/*
*========================================================================
*
*   SYSTEM   : AZN_PLAZA_LANE BACKGROUND
*   FILE     : send_tagbl.sc
*   Progrmer : JBY (baarong@neighbor21.co.kr)
*   Date     : 2018.09.05
*   Modified :
*   Remark   : Background Transfer Module
 *
 *========================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>

#include "file_no.h"
#include "lo_azn_packet_format.h"
#include "lo_ctrl.h"
#include "snd_rcv_knd.h"
#include "func_list.h"
#include "lb_func_list.h"
#include "bl_tag_chg.h"
#include "bl_tag_mst.h"
#include "pl_tag_mst.h"
#include "pl_tag_chg.h"

extern    int    send_rtag_bl();
extern    int    send_ptag_pl();
extern    int    send_tcrd_pl();

struct    file_tbl {
    int    file_id;                                   /* File id 			*/
    int    (*func)();                                 /* Transger Function 	*/
} file_tbl[] = {                                      /* File Table 		*/
    { FNO_RTAGBLMST         , send_rtag_bl      },    /* TAG B/L			*/
    { FNO_PTAGPLMST         , send_ptag_pl      },    /* TAG P/L			*/
    { -1                    , NULL              }     /* End of tables   	*/
};

struct    lo_comm_ctrl    lo_comm_ctrl; 
int isfd;

int send_tagbl(struct tagbl_struc *cs)
{
    int    retc;
    int    ii;

    for (ii = 0; file_tbl[ii].file_id != -1; ii++)
    {
        if (file_tbl[ii].file_id == cs->file_id)
            break;
    }
    if (file_tbl[ii].file_id == -1) 
    {
        return (0);
    }
    retc = (*file_tbl[ii].func)(cs); 
    return (retc);
}

/*
 *========================================================================
 *
 *   Function : send_rtag_bl()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : TAG B/L Master data Background Transfer
 *
 *========================================================================
*/

int send_rtag_bl(struct tagbl_struc *cs)
{
    struct    write_info     wf;
    struct    bl_tag_mst_tbl     bl_rtag_mst;
    struct    hi_rgstr_bl_tbl_h    *bl_rtag_h;
    struct    hi_rgstr_bl_tbl_f    *bl_rtag_f;
    int    pos, isrc,ii, jj,row=0, row2=0;
    int    tmp_ldyb[76];
    int repeat_cnt;
    int ret=0;

    bl_rtag_h = (struct hi_rgstr_bl_tbl_h *)hi_comm_ctrl.snd.buf;
    bl_rtag_f = (struct hi_rgstr_bl_tbl_f *)hi_comm_ctrl.snd.buf;

    memset(tmp_ldyb, '0', sizeof(tmp_ldyb));

	/* Master Header Transfer */
    memset(bl_rtag_h, 0, sizeof(struct hi_rgstr_bl_tbl_h));
    bl_rtag_h->seq_no = cs->seq_no;
    bl_rtag_h->kind = AZN_TBL_DATA;
    bl_rtag_h->tbl_typ = TB_BL_RTAG;
    bl_rtag_h->frame_no = 0;
    bl_rtag_h->frame_cnt = 0;
    memcpy(&bl_rtag_h->rvs_no, &cs->rvs_no, sizeof(bl_rtag_h->rvs_no));

    isfd = lb_fopen(&wf, FNO_RTAGBLMST, FINPUT+FC_MANULOCK);
    
    if (isfd < 0)
    {
        proc_log( "TAG B/L Master open error\n");
    }

    row = lb_record_cnt(isfd,FNO_RTAGBLMST); 

    if (row < 0)
    {
        row = 0;
        proc_log( "TAG B/L MASTER row not exist\n");
    }

    bl_rtag_h->frame_cnt = row / 76;
    if ((bl_rtag_h->frame_cnt == 0 && row != 0) || (row % 76) != 0)
        bl_rtag_h->frame_cnt++;

    hi_comm_ctrl.frame_no = 0;
    bl_rtag_h->div = 0x00;

    lb_close(isfd);
    
    memset(lo_comm_ctrl.pkt.buf, 0, sizeof(lo_comm_ctrl.pkt.buf));

    tagbl_hd_packet_chng(hi_comm_ctrl.snd.buf, lo_comm_ctrl.pkt.buf);
    
    lo_comm_ctrl.pkt.len = sizeof(struct tbl_data);
    lo_comm_ctrl.pkt.buf[lo_comm_ctrl.pkt.len++] = ETX;
    lo_comm_ctrl.pkt.flg = LP_SEND;
    
    memcpy(lo_comm_ctrl.snd.buf, lo_comm_ctrl.pkt.buf, lo_comm_ctrl.pkt.len);

    lo_comm_ctrl.snd.len = lo_comm_ctrl.pkt.len;

    ret = sock_send_data();
    if (ret == 0)
    {
        proc_log( "TAG B/L MASTER Transfer\n");
    }
    else if (ret < 0) return ret;

	/* Master data Transfer */
    repeat_cnt = bl_rtag_h->frame_cnt;
    for(ii = 0; ii < repeat_cnt; ii++)
    {
        lb_close(isfd);
        isfd = lb_fopen(&wf, FNO_RTAGBLMST, FINPUT+FC_MANULOCK);
        if (isfd < 0) return (-1);
        pos = (ii)*76 ;

        row = lb_record_cnt(isfd,FNO_RTAGBLMST);
        if (row < 0) row = 0;

        lseek(isfd, pos * sizeof(struct bl_tag_mst_tbl),SEEK_SET);

        bl_rtag_f->kind = AZN_TBL_DATA;
        bl_rtag_f->tbl_typ = TB_BL_RTAG;

        row2 = 0;

        memset(bl_rtag_f, 0, sizeof(struct hi_rgstr_bl_tbl_f));
        memset(&bl_rtag_mst, 0, sizeof(struct bl_tag_mst_tbl));

        for(jj = 0; jj < 76; jj++)
        {
            isrc = lb_readl(isfd, &bl_rtag_mst, FNEXT, sizeof(struct bl_tag_mst_tbl));
            if (isrc <= 0) break;

            bl_rtag_f->rgstr_bl_data[jj].div = bl_rtag_mst.div;
            memcpy(bl_rtag_f->rgstr_bl_data[jj].tag_id, bl_rtag_mst.tag_no,24);
            tmp_ldyb[jj] = bl_rtag_mst.snd_no; 
            row2++;
        }

        bl_rtag_f->seq_no = cs->seq_no;
        bl_rtag_f->kind = AZN_TBL_DATA;
        bl_rtag_f->tbl_typ = TB_BL_RTAG;
        bl_rtag_f->last_data_seq_no = tmp_ldyb[jj-1];
        bl_rtag_f->bl_item_cnt = row2;

        lb_close(isfd);

        memset(lo_comm_ctrl.pkt.buf, 0, sizeof(struct tbl_data));

        tagbl_fr_packet_chng(hi_comm_ctrl.snd.buf, lo_comm_ctrl.pkt.buf);

        lo_comm_ctrl.pkt.len = sizeof(struct tbl_data);
        lo_comm_ctrl.pkt.buf[lo_comm_ctrl.pkt.len++] = ETX;
        lo_comm_ctrl.pkt.flg = LP_SEND;
        
        memcpy(lo_comm_ctrl.snd.buf, lo_comm_ctrl.pkt.buf, lo_comm_ctrl.pkt.len);

        lo_comm_ctrl.snd.len = lo_comm_ctrl.pkt.len;

        ret = sock_send_data();
        if (ret == 0)
        {
            if((ii%1000==0) || (ii+1==repeat_cnt))
                proc_log( "TAG B/L Master data Transfer Completion(%d/%d)\n", ii+1, repeat_cnt);
        }
        else if(ret < 0) return ret;
    }
    return (0);
}

/*
 *========================================================================
 *
 *   Function : send_ptag_pl()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 기간 TAG P/L TABLE 송신
 *
 *========================================================================
*/

int send_ptag_pl(struct tagbl_struc *cs)
{
    struct    write_info     wf;
    struct  pl_tag_mst_tbl     pl_ptag_mst;
    struct  hi_perd_pl_tbl_h   *pl_ptag_h;
    struct  hi_perd_pl_tbl_f   *pl_ptag_f;
    int    pos, isrc,ii, jj,row=0, row2=0;
    int    tmp_ldyb[76];
    int repeat_cnt;
    int ret=0;

    pl_ptag_h = (struct hi_perd_pl_tbl_h *)hi_comm_ctrl.snd.buf;
    pl_ptag_f = (struct hi_perd_pl_tbl_f *)hi_comm_ctrl.snd.buf;

    memset(tmp_ldyb, '0', sizeof(tmp_ldyb));

/* 마스터 헤더 전송 */
    memset(pl_ptag_h, 0, sizeof(struct hi_perd_pl_tbl_h));
    pl_ptag_h->seq_no = cs->seq_no;
    pl_ptag_h->kind = AZN_TBL_DATA;
    pl_ptag_h->tbl_typ = TB_PL_RTAG;
    pl_ptag_h->frame_no = 0;
    pl_ptag_h->frame_cnt = 0;
    memcpy(&pl_ptag_h->rvs_no, &cs->rvs_no, sizeof(pl_ptag_h->rvs_no));
    
    isfd = lb_fopen(&wf, FNO_PTAGPLMST, FINPUT+FC_MANULOCK);
    
    if (isfd < 0)
    {
        proc_log( "기간 TAG P/L MASTER OPEN ERROR\n");
    }

    row = lb_record_cnt(isfd,FNO_PTAGPLMST);   /* BL총건수구하기 */
    
    if (row < 0)
    {
        row = 0;
        proc_log( "기간 TAG P/L MASTER row 없음\n");
    }
    proc_log("기간 TAG P/L 총 건수 row=[%d]\n", row); 
    pl_ptag_h->frame_cnt = row / 76;
    if ((pl_ptag_h->frame_cnt == 0 && row != 0) || (row % 76) != 0)
         pl_ptag_h->frame_cnt++;

    hi_comm_ctrl.frame_no = 0;
    pl_ptag_h->div = 0x00;
    
    lb_close(isfd);
    
    memset(lo_comm_ctrl.pkt.buf, 0, sizeof(struct tbl_data));
    
    tagpl_hd_packet_chng(hi_comm_ctrl.snd.buf, lo_comm_ctrl.pkt.buf);
    
    lo_comm_ctrl.pkt.len = sizeof(struct tbl_data);
    lo_comm_ctrl.pkt.buf[lo_comm_ctrl.pkt.len++] = ETX;
    lo_comm_ctrl.pkt.flg = LP_SEND;
    
    memcpy(lo_comm_ctrl.snd.buf, lo_comm_ctrl.pkt.buf, lo_comm_ctrl.pkt.len);
    
    lo_comm_ctrl.snd.len = lo_comm_ctrl.pkt.len;
    
    ret = sock_send_data();
    if (ret == 0)
    {
        proc_log( "기간 TAG P/L MASTER 전송\n");
    }
    else if(ret < 0) return ret;
    
/* 마스터 DATA 전송 */
    proc_log("(send_tagbl.pc) pl_ptag_h->frame_cnt = %d\n", pl_ptag_h->frame_cnt);
    repeat_cnt = pl_ptag_h->frame_cnt;
    for(ii = 0; ii < repeat_cnt; ii++)
    {
        isfd = lb_fopen(&wf, FNO_PTAGPLMST, FINPUT+FC_MANULOCK);
        if (isfd < 0) return (-1);
        pos = (ii)*76 ;

        row = lb_record_cnt(isfd,FNO_PTAGPLMST);
        if (row < 0) row = 0;

        lseek(isfd, pos * sizeof(struct pl_tag_mst_tbl),SEEK_SET);

        pl_ptag_f->kind = AZN_TBL_DATA;
        pl_ptag_f->tbl_typ = TB_PL_RTAG;
     
        row2 = 0;
     
        memset(pl_ptag_f, 0, sizeof(struct hi_perd_pl_tbl_f));
        memset(&pl_ptag_mst, 0, sizeof(struct pl_tag_mst_tbl));
     
        for(jj = 0; jj < 76; jj++)
        {
            isrc = read(isfd, &pl_ptag_mst, sizeof(struct pl_tag_mst_tbl));
            if (isrc <= 0) break;

            pl_ptag_f->perd_pl_data[jj].div = pl_ptag_mst.div;
            memcpy(pl_ptag_f->perd_pl_data[jj].tag_id, pl_ptag_mst.tag_no,24);
            
            tmp_ldyb[jj] = pl_ptag_mst.snd_no;       /*최종연번*/
            row2++;
        }
     
        pl_ptag_f->seq_no = cs->seq_no;
        pl_ptag_f->kind = AZN_TBL_DATA;
        pl_ptag_f->tbl_typ = TB_PL_RTAG;
        pl_ptag_f->last_data_seq_no = tmp_ldyb[jj-1];
        pl_ptag_f->pl_item_cnt = row2;
     
        lb_close(isfd);
        
        memset(lo_comm_ctrl.pkt.buf, 0, sizeof(struct tbl_data));

        tagpl_fr_packet_chng(hi_comm_ctrl.snd.buf, lo_comm_ctrl.pkt.buf);
        
        lo_comm_ctrl.pkt.len = sizeof(struct tbl_data);
        lo_comm_ctrl.pkt.buf[lo_comm_ctrl.pkt.len++] = ETX;
        lo_comm_ctrl.pkt.flg = LP_SEND;
        
        memcpy(lo_comm_ctrl.snd.buf, lo_comm_ctrl.pkt.buf, lo_comm_ctrl.pkt.len);
        
        lo_comm_ctrl.snd.len = lo_comm_ctrl.pkt.len;
        
        ret = sock_send_data();
        if (ret == 0)
        {
        if((ii%1000==0) || (ii+1==repeat_cnt))
            proc_log( "기간 TAG P/L MASTER DATA 전송(%d/%d)\n", ii+1, repeat_cnt);
        }
        else if(ret < 0) return ret;

    }
    return (0);
}
