Commit 0b111932 authored by Scott Sun's avatar Scott Sun

scott

parent 4a7b4724
module.exports = {
connect:{
"database_type": "pg", // 数据库类型
"database_host": "139.196.104.13:5433", // 地址
"database_name": "PMO_TRAINING", // 数据库名
"database_user": "toplinker", // 用户名
"database_pwd": "TopLinker0510" // 密码
},
tableName: "sec_production_order_confirmation_2", // 表名
connectName: "Scott_PMO_TRAINING" // 连接名
}
\ No newline at end of file
module.exports = {
A : {
dbKey: "order_no", // 数据库的字段名
attr: false, // 是否是新增的额外字段 默认是false
format: false, // 获取数据的格式 默认是false
},
B : {
dbKey: "confirmation",
attr: false,
format: false,
},
C : {
dbKey: "plant",
attr: false,
format: false,
},
D : {
dbKey: "material",
attr: false,
format: false,
},
E : {
dbKey: "material_description",
attr: false,
format: false,
},
F : {
dbKey: "posting_date",
attr: false,
format: formatDate,
},
G : {
dbKey: "confirmation_entry_time",
attr: false,
format: formatTime,
},
H : {
dbKey: "activity",
attr: false,
format: false,
},
I : {
dbKey: "work_center",
attr: false,
format: false,
},
J : {
dbKey: "confirmed_yield",
attr: false,
format: false,
},
K : {
dbKey: "base_unit",
attr: false,
format: false,
},
L : {
dbKey: "confirmed_scrap",
attr: false,
format: false,
},
M : {
dbKey: "reason_for_variance",
attr: false,
format: false,
},
N : {
dbKey: "rework",
attr: false,
format: false,
},
O : {
dbKey: "confirmation_text",
attr: false,
format: false,
},
P : {
dbKey: "entered_by_user",
attr: false,
format: false,
},
Q : {
dbKey: "ind_final_confirmation",
attr: false,
format: false,
},
R : {
dbKey: "milestone_confirmed",
attr: false,
format: false,
},
S : {
dbKey: "ind_delete_doc",
attr: false,
format: false,
},
T : {
dbKey: "hh",
attr: true,
format: false,
}
}
function formatDate (numb) {
var time = new Date((numb - 1) * 24 * 3600000 + 1)
time.setYear(time.getFullYear() - 70)
var year = time.getFullYear() + ''
var month = time.getMonth() + 1 + ''
var date = time.getDate() + ''
return year + "-" + (month<10?"0"+month:month) + "-" + (date<10?"0"+date:date)
}
function formatTime(numb) {
var hourTmp = numb * 24;
var hour = Math.floor(hourTmp);
var minuteTmp = hourTmp - hour;
var minute = Math.floor(minuteTmp * 60);
var secondTmp = (minuteTmp*60) - minute;
var second = Math.round(secondTmp*60);
if(second >= 60){
second = 0
minute += 1
}
if(minute >= 60) {
minute = minute - 60
hour += 1
}
if(hour >= 24) {
hour = 0
}
return (hour<10? "0" + hour : hour) + ":" + (minute<10? "0" + minute : minute) + ":" + (second<10? "0" + second : second)
}
\ No newline at end of file
var fs = require("fs");
var book = require('topsin.excelxs');
var db = require('topsin.database');
var error = require('topsin.error');
function ExlDb (props) {
function ExlDb(props) {
this.props = props
}
ExlDb.prototype.read = function (path, suffix) { // 读取方法 接收参数1 string 文件夹路径 参数2 array 读取对应后缀的文件
var suffix = suffix || "XLSX"
var files = fs.listDir(path).filter(function(v){
var files = fs.listDir(path).filter(function (v) {
return Array.isArray(suffix) ? suffix.indexOf(v.completeSuffix) >= 0 :
v.completeSuffix === suffix
v.completeSuffix === suffix
})
if(files.length === 0){ return }
files = files.map(function(v){return (v.dir + '/' + v.name)})
if (files.length === 0) { return }
files = files.map(function (v) { return (v.dir + '/' + v.name) })
return files;
}
ExlDb.prototype.readExl = function (path, config) { // 读取列表 根据配置获取数据
if (book.open(path)) {
var sheet = book.getActiveSheet() // 获取当前表
var lastRow = sheet.getLastRow() // 获取有内容的最后一行
var keysCount = sheet.getLastCol() // 字段数量
var res = [] // 存放结果
var KeyConfig = [] // 存放配置信息
this.dbkeys = [] // 存放数据库字段名
for (var i = 0; i < keysCount; i++) {
KeyConfig[i] = config[getColKey(i + 1)]
if(!KeyConfig[i]["attr"]){
this.dbkeys.push(KeyConfig[i]["dbKey"])
}
}
this.dbkeys.push("attr_data")
for (var i = 1; i < lastRow; i++) { // 双重循环 根据配置信息获取数据
res[i-1] = {}
res[i-1]["attr_data"] = {}
for(var j = 0; j < keysCount; j++){
if(KeyConfig[j]["attr"]){
res[i-1]["attr_data"][KeyConfig[j]["dbKey"]] = KeyConfig[j]["format"] ?
KeyConfig[j]["format"](sheet.getCellValue(i, j)) : sheet.getCellValue(i, j)
} else {
res[i-1][KeyConfig[j]["dbKey"]] = KeyConfig[j]["format"] ?
KeyConfig[j]["format"](sheet.getCellValue(i, j)) : sheet.getCellValue(i, j)
}
}
}
book.close()
return res
}
}
ExlDb.prototype.openDb = function(dbConfig){
db.addConnection(dbConfig.connect, dbConfig.connectName); // 连接数据库
this.tablename = dbConfig.tableName // 设置数据库表名
this.connectName = dbConfig.connectName
this.query = db.query(dbConfig.connectName); // 获取操作对象
return this
}
ExlDb.prototype.closeDb = function(){ // 关闭数据库连接
db.removeConnection(this.connectName);
}
ExlDb.prototype.dbGetAll = function(){ // 获取数据库所有数据
this.query.begin();
var data;
try {
data = this.query.selectArrayMap({
table: this.tablename,
field: '*',
field_format:{"tags":'array', attr_data:'json'}
});
if (this.query.lastError().isValid()){
throw this.query.lastError().text();
}
this.query.commit();
} catch (error) {
print("获取数据库数据错误", error);
this.query.rollback();
}
return data
}
ExlDb.prototype.dbDeleteAll = function(){ // 删除所有数据
return this.query.deleteRow({
table:this.tablename,
where: ["id >= 0"]
})
}
ExlDb.prototype.insertAny = function(datas){ // 批量插入数据
console.log(this.dbkeys);
this.query.begin()
try{
this.query.batchInsert(
this.tablename,
this.dbkeys,
datas
);
if (this.query.lastError().isValid()){
throw this.query.lastError().text();
}
this.query.commit();
}
catch(err) {
print("插入数据错误", err)
this.query.rollback();
}
}
ExlDb.prototype.UpdateKeys = function(data, id){ // 更新数据
this.query.begin()
try {
this.query.updateRow({
table:this.tablename,
data:data,
where:{id:id},
update_policy:{tags:'array_append', attr_data:'json_merge'}
})
if (this.query.lastError().isValid()){
throw this.query.lastError().text();
}
this.query.commit();
} catch (error) {
print("更新错误", err)
this.query.rollback();
}
}
module.exports = function (props) {
return new ExlDb(props);
}
module.exports = function(props){
return new ExlDb(props);
// 数字转字母
function getColKey(num) {
var stringArray = [];
function numToString(numm) {
stringArray.length = 0;
var numToStringAction = function (nnum) {
var num = nnum - 1;
var a = parseInt(num / 26);
var b = num % 26;
stringArray.push(String.fromCharCode(64 + parseInt(b + 1)));
if (a > 0) {
numToStringAction(a);
}
}
numToStringAction(numm);
return stringArray.reverse().join("");
}
return numToString(num)
}
\ No newline at end of file
var fs = require('fs');
var process = require('process')
var _ = require('lodash')
// 程序启动时 判断是否存在 .
var begin = true;
// 程序启动时 判断是否运行中
if(!fs.dirExists("./"+process.evalFileName() + "temp")){
fs.mkdir("./"+process.evalFileName() + "temp")
} else {
console.log("程序运行中,退出");
begin = false;
}
var exl = require("./index.js")({}); // 引入并初始化
if(begin){
var exlConfig = require("./config/exlConfig.js"); // 引入excel字段配置
var dbConfig = require('./config/dbConfig.js'); // 引入数据库配置
var exl = require("./index.js")({}); // 引入并初始化
var files = exl.read("../excel"); // 读取文件夹获取文件列表 默认是只读取XLSX后缀文件
var files = exl.read("../excel"); // 读取文件夹获取文件列表 默认是只读取XLSX后缀文件
var datalist = []; // 存放excel的数据
console.log(files);
_.forEach(files, function(value){
var res = exl.readExl(value, exlConfig); // 读取excel文件 根据exl配置返回结果
datalist = _.concat(datalist, res); // 将获取到的数据拼接到datalist
// undo 将读取过的文件 移动到别处
fs.rename(value,"../remove/"+value.split("/").pop(),true)
})
if(datalist.length !== 0){
// 数据库操作
var dbDatas = exl.openDb(dbConfig).dbGetAll() // 连接数据库 链式调用获取所有数据
console.log("excel的数据数量:" + datalist.length)
console.log("数据库的数据数量:" + dbDatas.length)
// Contrast begin 数据比较 将数据库数据 与 excel数据比较 旧数据判断是否要更新 新数据插入
var newData = contrastData(datalist,dbDatas,exl)
if(newData.length !== 0){
// 执行插入操作
exl.insertAny(newData)
}
// Contrast over
exl.closeDb() // 关闭数据库
// 删除文件夹
}
fs.rmdir("./"+process.evalFileName() + "temp")
}
function contrastData(data, dbData, exl){
dbI = dbData.map(function(v){ // 获取数据库唯一键值对
var tempStr = ""
tempStr += v["confirmation"] + v["plant"] + v["material"] + v["posting_date"] + v["confirmation_entry_time"] + v["activity"]
return tempStr
})
var oldData = [],
newData = [];
for(var i = 0,len=data.length;i<len;i++){
var v = data[i]
var dataI = v["confirmation"] + v["plant"] + v["material"] + v["posting_date"] + v["confirmation_entry_time"] + v["activity"]
if(dbI.indexOf(dataI) >= 0){
var updateData = {}
// 判断老数据是否更新了
for(var key in v){
if(key==="attr_data"){
var tempflag = false
for(var tempdbkey in v["attr_data"]){
if(!dbData[dbI.indexOf(dataI)][key][tempdbkey] && dbData[dbI.indexOf(dataI)][key][tempdbkey] != v[key][tempdbkey]){
tempflag = true
}
}
if(tempflag){
updateData["attr_data"] = v["attr_data"]
}
} else {
if(v[key] != dbData[dbI.indexOf(dataI)][key]) {
updateData[key] = v[key]
}
}
}
if(JSON.stringify(updateData) !== "{}"){ // 有要更新的情况
console.log("更新...")
exl.UpdateKeys(updateData, dbData[dbI.indexOf(dataI)]["id"])
}
oldData.push(v)
} else {
// 新数据
newData.push(v)
}
}
console.log("新数据:"+newData.length);
console.log("老数据:"+oldData.length);
return newData
}
\ No newline at end of file
var cl = require('console');
var _ = require('lodash');
var reg = new RegExp('^[a-zA-Z]+$');
function stringZip(iStr){
if((typeof iStr == 'string') && iStr != '' && reg.test(iStr)){
var count = 1; //计数
var str = '';
var index = 1; //比较字符的索引
var nextChar = iStr[0];
if(iStr.length == index){
return index+iStr;
};
while(index < iStr.length){
if(iStr[index] != nextChar){
str += (count + nextChar);
count = 1;
nextChar = iStr[index];
}else{
count += 1;
};
index += 1;
};
str += (count + nextChar);
return str;
}else{
cl.log('不是字符串 或 字符串为空 或 不是纯英文字母');
};
};
function stringUnzip(iStr){
var str1 = '';
for(var i=0; i<iStr.length; i+=2){
str2 = ''
var n = _.toNumber(iStr[i]);
for(var j=0; j<n; j++){
str1 += iStr[i+1]
};
str2 += str1;
};
return str1;
};
a = 'aaaaaanbbnnnbbbbbbbbbbsssssss';
cl.log(stringZip(a));
b = '3a5b4c7d9f';
cl.log(stringUnzip(b));
//扩展:效率问题
\ No newline at end of file
......@@ -115,8 +115,6 @@ Main.prototype.paytionType = function(props){ // 处理数据
this.tablename = 'sec_production_order_confirmation_2' // 设置数据库表名
this.query = db.query('Scott_PMO_TRAINING'); // 获取操作对象
var dbDatas = getDbData(this.query,this.tablename) // 获取数据库的所有数据
if(!dbDatas){return}
console.log("数据库的数据数量:" + dbDatas.length)
if(this.newkeys.length === 0){ // 不存在新的字段 只要新增就行
// console.log(this.query.deleteRow({
// table:'sec_production_order_confirmation_2',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment