
var db = require('topsin.database');
var _ = require("lodash")
var fs = require("fs");
db.addConnection({
    databse_type:'pg',
    database_host:'10.90.2.100:5432',
    database_name:'TOPSTQ_ATS_V6',
    database_user:'toplinker',
    database_pwd:'TopLinker0510' 
}, "MES_DB");

var result = db.query("MES_DB", function(query) {
    return  query.selectArrayMap({
        table:'pdm_aimdfm_task',
        field:['id','job_id','user_name',"current_process_id",'progress',"msg","task_title"],
        order:'id DESC',
    })
});
var jobid = {}
var okData = []
result.forEach(function(item){
    // 过滤条件 jobid里面没有这个id  job_id查询表pdm_job发现是正常料号// 顺便获取时间 task_title是topcam或者分析
    if(!jobid[item.job_id]){
        var job_info = db.query("MES_DB", function(query) {
            return  query.selectMap({
                table:'pdm_job',
                field:['customer_code','del_flag','action_data'],
                where:{id:item.job_id}
            });
        });
        if(job_info && job_info.del_flag!=1){
            item.job_info = job_info;
            var task_workflow = db.query("MES_DB", function(query) {
                return  query.selectMap({
                    table:'pdm_aimdfm_task_workprocess',
                    field:['start_time','end_time','action_data'],
                    where:{id:item.current_process_id}
                });
            });
            item.task_workflow = task_workflow
            okData.push(item)
            jobid[item.job_id] = true;
        }
    }
})
console.log(result.length);
console.log(okData.length);
fs.writeFile("./test.js", _.toString(okData))