index.js 3.25 KB
Newer Older
Scott Sun's avatar
Scott Sun committed
1 2 3 4
var fs = require('fs');
var parser = require('topsin.parser');
var db = require('topsin.database');

Scott Sun's avatar
Scott Sun committed
5
// 获取csv的数据
Scott Sun's avatar
Scott Sun committed
6 7 8 9 10
function getCsvArr(path){
    var file = fs.openFile(path);   // 获取文件流
    file.setCodec('UTF-8');   // 设置编码
    var csvstr = file.readAll();  // 获取csv字符串
    var csvdata = parser.parseCsvStr(csvstr);   // 转变为数组
Scott Sun's avatar
Scott Sun committed
11
    csvdata.shift();  // 除去第一行
Scott Sun's avatar
Scott Sun committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    return csvdata;
}
var res = getCsvArr('./confirmation-201912270900(1).csv');

//  创建数据库连接
db.addConnection({
    database_type:'pg',
    database_host:'139.196.104.13:5433',
    database_name:'PMO_TRAINING',
    database_user:'toplinker',
    database_pwd:'TopLinker0510'
}, "Scott_PMO_TRAINING");


// 封装数据库的操作
function Query(db,table,connectName){
    this.db = db;
    this.table = table;
Scott Sun's avatar
Scott Sun committed
30
    this.normal = function(callback){  // 自定义
Scott Sun's avatar
Scott Sun committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        return this.db.query(connectName,callback);
    }
    this.getAllData = function(){  // 获取所有数据
        var that = this
        return this.db.query(connectName,function(q){
            return q.selectArrayMap({
                table:that.table,
                field: '*',
                field_format:{"tags":'array', attr_data:'json'}
            });
        })
    }
    this.getKeys = function(){  // 查询所有字段
        return this.normal(function(q){  
            return q.getFieldList('sec_production_order_confirmation_2')
        });
    } 
}
var query = new Query(db,'sec_production_order_confirmation_2','Scott_PMO_TRAINING');

// 获取数据库的字段
var keys = query.getKeys();

// 查看数据list
var list = query.getAllData()

// 是否需要扩展列
function getkeyCount(){
    var res = 0;
    for(var keycount in list[0]){
        if(list[0][keycount] !== undefined){
Scott Sun's avatar
Scott Sun committed
62
            res++;
Scott Sun's avatar
Scott Sun committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        }   
    }
    return res-1
}
if(getkeyCount() !== res[0].length){
    console.log("需要更新前面数据")
    // 要更新的前面数量
    var updateRows = res.length - list.length
    var updatelen = getkeyCount() - res[0].length
    var updateObj = {}
    for(var i = 1;i<=updateRows;i++){
        updatelen.map(function(v){
            updateObj[keys[getkeyCount()+1+v]] = res[i-1][res[0].length+v]
        })
        query.normal(function(q){
            q.updateRow({
                table:'sec_production_order_confirmation_2',
                data:updateObj,
                where:{id: i},
                update_policy:{tags:'array_append', attr_data:'json_merge'}
            })
        })
    }
}
// 是否需要插入多数据
if (list.length !== res.length){
    // 执行插入
    var insertMany = res.splice(list.length)
    insertMany = insertMany.map(function(v){
        var obj = {}
        v.map(function(v2, i2){
            obj[keys[i2+1]] = v2
        })
        return obj
    })
    
    query.normal(function(q){
        q.batchInsert(
            'sec_production_order_confirmation_2',
            keys.splice(1,res[0].length),
            insertMany
        );
    })
}
Scott Sun's avatar
Scott Sun committed
107 108 109 110 111 112 113 114 115
console.log(query.normal(function(q){
    return q.selectMap({
        table:'sec_production_order_confirmation_2',
        field: '*',
        field_format:{tags:'array', attr_data:'json'},
        limit: 1,
        order:'id DESC'
    });
}))
Scott Sun's avatar
Scott Sun committed
116 117 118

// console.log(res[res.length-1])