/*
 *========================================================================
 *
 *   시스템명 : 요금징수 시스템
 *   파 일 명 : make_minap.pc
 *   작 성 자 : 오효섭 (catacath@neighbor21.co.kr)
 *   작 성 일 : 2012.10.11
 *   수 정 일 :
 *   설    명 :
 *
 *========================================================================
*/

#include <stdio.h>
#include <sqlca.h>
#include <oraca.h>

#include "path_def.h"
#include "def_set.h"

/*
 *========================================================================
 *
 *   함 수 명 : make_minap
 *   작 성 자 : 오효섭 (catacath@neighbor21.co.kr)
 *   작 성 일 : 2014.05.14
 *   수 정 일 :
 *   설    명 :
 *
 *========================================================================
*/

int make_minap(flag)
int    flag;
{
    FILE *fp;
    char s_buf[1024];
    char f_nm[128];
    char file_nm[64];
    char save_dir[128];
    char n_date[16];
    char rec[1024];
    int  row, col;
    char lst_dt[14+1];
    char plz_id[4];

    get_current_time(n_date);
    row=0;
/*
    sprintf(save_dir, "%s/sdsystem", DATA_APP_DATA);

    proc_log("make_minap flag=%d", flag);
    sprintf(plz_id    ,  "%.3s",  PLZ_CODE2);

    if(flag==0)
        sprintf(file_nm, "MTBM%.12s", n_date);
    else if(flag==1)
        sprintf(file_nm, "MTBC%.12s", n_date);
    else
    {
        proc_log("flag error [%d]\n", flag);
        return 0;
    }

    sprintf(f_nm,"%s/%s",save_dir, file_nm);

    proc_log("MakeMinap FILE NAME = [%s]", f_nm);

    memset(lst_dt, 0x0, sizeof(lst_dt));

    EXEC SQL SELECT LST_HND_TM INTO :lst_dt FROM PUBL_UNPAYEDINFO WHERE PLZ_ID = :plz_id;

    if(flag==0)
    {
        EXEC SQL DECLARE minap_cursor1 CURSOR FOR
        SELECT
            REC,
            LENGTHB(REC)
        FROM
        (SELECT
            TRIM(CAR_NO)                                            || '|' ||
            SUM(COL_PASS_FARE-RCT_PASS_FARE)        || '|' ||
            COUNT(*)                                                        || '|' ||
            '00'                                                            || '|' ||
            '1' AS REC
        FROM PUBL_UNPAYEDCAR
        WHERE  PLZ_ID = :plz_id
        AND    HND_STAT IN ('10', '20')
        GROUP BY TRIM(CAR_NO)
        HAVING TRIM(CAR_NO) IS NOT NULL
        AND    TRIM(CAR_NO) NOT IN ('영상없음', '인식실패'));

        fp = fopen(f_nm, "wb");
        if( fp == NULL )
        {
            proc_log("%s File Create fail", f_nm);

            return 5013;
        }

        EXEC SQL OPEN minap_cursor1;

        while(1)
        {
            memset(rec, 0x00, 1024);

            EXEC SQL FETCH minap_cursor1 INTO :rec, :col;

            if(sqlca.sqlcode != 0 && sqlca.sqlcode != -1405) break;

            rec[col]=0x0;
            fprintf(fp, "%s%c%c", rec, 0x0d, 0x0a);
            printf("[%s]\n", rec);
            row++;
        }
        EXEC SQL CLOSE minap_cursor1;

        fclose(fp);

        EXEC SQL UPDATE PUBL_UNPAYEDINFO SET LST_HND_TM = :n_date WHERE PLZ_ID = :plz_id;

        EXEC SQL COMMIT WORK;
    }
    else if(flag==1)
    {
        EXEC SQL DECLARE minap_cursor2 CURSOR FOR
        SELECT
            REC,
            LENGTHB(REC)
        FROM
        (SELECT
            TRIM(A.CAR_NO)                                          || '|' ||
            SUM(COL_PASS_FARE-RCT_PASS_FARE)        || '|' ||
            COUNT(*)                                                        || '|' ||
            '00'                                                            || '|' ||
            DECODE(SUM(COL_PASS_FARE-RCT_PASS_FARE), 0, 3, 2) AS REC
        FROM PUBL_UNPAYEDCAR A,
        (SELECT CAR_NO, PLZ_ID FROM PUBL_UNPAYEDCAR
        WHERE PLZ_ID = :plz_id
        AND   LST_MODF_DT > :lst_dt
        AND   TRIM(CAR_NO) IS NOT NULL
        AND   TRIM(CAR_NO) NOT IN ('영상없음', '인식실패')) B
        WHERE A.PLZ_ID = :plz_id
        AND   A.PLZ_ID = B.PLZ_ID
        AND   TRIM(A.CAR_NO) = TRIM(B.CAR_NO)
        GROUP BY TRIM(A.CAR_NO));

        fp = fopen(f_nm, "wb");
        if( fp == NULL )
        {
            proc_log("%s File Create fail", f_nm);

            return 5013;
        }

        EXEC SQL OPEN minap_cursor2;

        while(1)
        {
            memset(rec, 0x00, 1024);

            EXEC SQL FETCH minap_cursor2 INTO :rec, :col;
            if(sqlca.sqlcode != 0 && sqlca.sqlcode != -1405) break;

            rec[col]=0x0;
            fprintf(fp, "%s%c%c", rec, 0x0d, 0x0a);
            printf("[%s]\n", rec);
            row++;
        }
        EXEC SQL CLOSE minap_cursor2;

        fclose(fp);

        if(row < 1)
        {
            proc_log("row=%d f_nm = [%s]", row, f_nm);
            unlink(f_nm);
        }

        EXEC SQL UPDATE PUBL_UNPAYEDINFO SET LST_HND_TM = :n_date WHERE PLZ_ID = :plz_id;

        EXEC SQL COMMIT WORK;
    }
*/
    return 0;
}
