var fs = require("fs");
var db = require('topsin.database');
var error = require('topsin.error');
var user_name = "Sys";
try {
    db.addConnection({
        databse_type: 'pg',
        database_host: '10.90.2.100:5432',
        database_name: 'TOPSTQ_ATS_V6',
        database_user: 'toplinker',
        database_pwd: 'TopLinker0510'
    }, "ATS_DFM_DB");
    var query_1 = db.query('ATS_DFM_DB');
    // 1.获取客户名称
    var customers_1 = query_1.selectArrayValue({
        table: 'pub_customer',
        field: "code"
    });
    if (query_1.lastError().isValid()){
        throw query_1.lastError().text()};
    // 获取远程文件夹
    var samba = fs.listDir("\\\\10.90.2.100\\samba", 1);

    var sambaDirs_1 = [];
    var sambaFiles_1 = [];
    samba.forEach(function (v) {
        if (v.isDir) {
            sambaDirs_1.push(v);
        }
        else {
            sambaFiles_1.push(v);
        }
    });
    sambaDirs_1.forEach(function (v) {
        if (customers_1.indexOf(v.name) >= 0) {
            // 获取该客户下所有料号
            var jobs = fs.listDir("\\\\10.90.2.100\\samba\\" + v.name, 0).filter(function (v) { return v.isDir; });
            // 获取dfm种已经存在的料号名
            var dfm_jobs_1 = query_1.selectArrayValue({
                table: 'pdm_job',
                field: "jobname",
                where: { customer_code: v.name },
                order: 'id DESC'
            });
            // 循环判断新料号
            jobs.forEach(function (v2) {
                if (/_/.test(v2.name) && dfm_jobs_1.indexOf(v2.name) < 0) {
                    // 为该新料号创建报价
                    var jobId = query_1.insertRow({
                        table: 'pdm_job',
                        data: {
                            jobname: v2.name,
                            version: '',
                            customer_code: v.name,
                            job_type: "normal",
                            spec_list: "{" + v.name + ",__GENERAL__}",
                            job_status: "active",
                            job_attrs: { "vc_number": v2.name.split("_")[0], "vc_position": v2.name.split("_")[1] },
                            action_data: { "create_time": query_1.getNow(), "create_user_id": 1, "create_user_name": user_name },
                            sys_data: { "sys_data_version": 0 },
                            spec_mode: 0
                        },
                        return_field: 'id'
                    });
                    if (query_1.lastError().isValid())
                        throw query_1.lastError().text();
                    var flowInfo = query_1.selectMap({
                        table: 'pdm_aimdfm_workflow',
                        where: {
                            name: "topcam"
                        }
                    });
                    var flowId = flowInfo["id"];
                    var flowTitle = flowInfo["title"];
                    // 创建料号任务
                    query_1.insertRow({
                        table: 'pdm_aimdfm_task',
                        data: {
                            job_id: jobId,
                            flow_id: flowId,
                            status: 'waiting',
                            user_id: 1,
                            user_name: user_name,
                            priority: 0,
                            task_title: v2.name + ":" + flowTitle
                        }
                    });
                    if (query_1.lastError().isValid())
                        throw query_1.lastError().text();
                }
            });
        }
    });
}
catch (error) {
    console.log("err");
    console.log(error);
}
