You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
3.9 KiB
91 lines
3.9 KiB
'use strict';
|
|
const superagent = require('superagent');
|
|
|
|
module.exports = {
|
|
conf: {
|
|
interval: '0 0 1 * * *',
|
|
/*
|
|
* * * * * *
|
|
┬ ┬ ┬ ┬ ┬ ┬
|
|
│ │ │ │ │ │
|
|
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
|
|
│ │ │ │ └───── month (1 - 12)
|
|
│ │ │ └────────── day of month (1 - 31)
|
|
│ │ └─────────────── hour (0 - 23)
|
|
│ └──────────────────── minute (0 - 59)
|
|
└───────────────────────── second (0 - 59, OPTIONAL)
|
|
*/
|
|
immediate: false, // 启动是否立即执行
|
|
env: ['dev', 'prod'],
|
|
disabled: false, // 是否禁用该定时任务
|
|
},
|
|
callback: async function (app, conf) {
|
|
try {
|
|
const { emisApi, username, password } = conf.pep;
|
|
const models = app.fs.dc.models;
|
|
const Op = app.fs.dc.ORM.Op;
|
|
|
|
const res = await fetch(`${emisApi}/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
username, password
|
|
}),
|
|
});
|
|
const userInfo = await res.json();
|
|
const token = userInfo.token;
|
|
|
|
const productLibraryRes = await superagent
|
|
.get(`${emisApi}/pricing/product/list`)
|
|
.query({ token });
|
|
const productLibraryData = productLibraryRes.body;
|
|
|
|
const materials = await models.Materials.findAll({
|
|
where: { productLibraryId: { [Op.ne]: null } },
|
|
raw: true
|
|
});
|
|
|
|
let count = 0;
|
|
for (const material of materials) {
|
|
for (const item of productLibraryData) {
|
|
const plCode = item.erp || item.serialNumber;
|
|
if (material.productLibraryId === item.id) {
|
|
if (material.code !== plCode) {
|
|
console.log(`update material id: ${material.id}, code: ${material.code} => ${plCode}`);
|
|
await models.Materials.update(
|
|
{ code: plCode },
|
|
{ where: { id: material.id } }
|
|
);
|
|
count++;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log('update materials code count:', count);
|
|
|
|
// const data = pmData.filter(i => i.del === false);
|
|
// let sql = '';
|
|
// for (const material of materials) {
|
|
// for (const item of data) {
|
|
// if (material.code === item.erp || material.code === item.serialNumber) {
|
|
// console.log('update material:', material.name, material.model, material.code, 'id:', item.id);
|
|
// sql += `UPDATE public.materials SET product_library_id=${item.id} WHERE code='${material.code}';\n`;
|
|
// break;
|
|
// } else if ('0' + material.code === item.erp || material.code.trim() === item.erp) {
|
|
// sql += `UPDATE public.materials SET product_library_id=${item.id}, code='${item.erp}' WHERE code='${material.code}';\n`;
|
|
// } else if ('0' + material.code === item.serialNumber || material.code.trim() === item.serialNumber) {
|
|
// sql += `UPDATE public.materials SET product_library_id=${item.id}, code='${item.serialNumber}' WHERE code='${material.code}';\n`;
|
|
// }
|
|
// }
|
|
// }
|
|
// const file = path.join(__dirname, '../static/temp/update_materials_code.sql');
|
|
// fs.writeFileSync(file, sql);
|
|
|
|
console.log('sync-materials-code schedule success', Date.now());
|
|
} catch (error) {
|
|
console.error('sync-materials-code schedule error:', error);
|
|
}
|
|
},
|
|
};
|
|
|