Commit 856ddd7e by 马玮

Add latest fetch point

parent cb218239
......@@ -258,60 +258,61 @@ class DealerHelper {
// 保存线索
static async saveClues(params) {
return new Promise(async (resolve, reject) => {
const dealerID = params.dealerID || 0;
const website = params.website || '';
let clues = params.clues || [];
if (!dealerID || !clues.length || !website) {
throw errors.AppError('Params missing');
}
try {
const dealerID = params.dealerID || 0;
const website = params.website || '';
let clues = params.clues || [];
if (!dealerID || !clues.length || !website) {
throw new errors.AppError('Params missing');
}
clues = await this.mapClues(params).catch((e) => {
console.log(e);
});
clues = await this.mapClues(params).catch((e) => {
logger.error(`accountID: ${params.accountID}, 线索信息匹配失败: ${e}}`);
});
const connection = await DI.getConnection('MASTER_CRM');
const valuesSql = [];
const _clue = {
customerID: 0,
customerName: '',
customerPhone: '',
customerArea: '',
licenseArea: '',
intentionDes: '',
intentionID: 0,
salesmanName: '',
salesPhone: '',
createdAt: 0,
};
for (let clue of clues) {
clue = Object.assign(_clue, clue);
try {
clue.customerID = await this.saveCustomer({
phone: clue.customerPhone,
name: clue.customerName,
area: clue.customerAreaID,
createTime: clue.createdAt
});
logger.debug(`accountID: ${params.accountID}, 客户信息保存成功, customerID: ${clue.customerID}`);
} catch (e) {
// 如果没有customerID则跳过这条线索
logger.error(`accountID: ${params.accountID}, 客户信息保存失败: ${e}}`);
continue;
}
const connection = await DI.getConnection('MASTER_CRM');
const valuesSql = [];
const _clue = {
customerID: 0,
customerName: '',
customerPhone: '',
customerArea: '',
licenseArea: '',
intentionDes: '',
intentionID: 0,
salesmanName: '',
salesPhone: '',
createdAt: 0,
};
for (let clue of clues) {
clue = Object.assign(_clue, clue);
try {
clue.customerID = await this.saveCustomer({
phone: clue.customerPhone,
name: clue.customerName,
area: clue.customerAreaID,
createTime: clue.createdAt
});
logger.debug(`accountID: ${params.accountID}, 客户信息保存成功, customerID: ${clue.customerID}`);
} catch (e) {
// 如果没有customerID则跳过这条线索
logger.error(`accountID: ${params.accountID}, 客户信息保存失败: ${e}}`);
continue;
}
let valueSql = '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
// customer_id, shop_name, salesman_id需要映射
let replacements = [
clue.customerID, clue.customerName, clue.customerPhone, clue.intentionDes, clue.intentionID, '', dealerID, clue.salesmanName, 0,
config.get('CLUE_INPUT_TYPE')[params.website], 4, clue.createdAt
];
let valueSql = '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
// customer_id, shop_name, salesman_id需要映射
let replacements = [
clue.customerID, clue.customerName, clue.customerPhone, clue.intentionDes, clue.intentionID, '', dealerID, clue.salesmanName, 0,
config.get('CLUE_INPUT_TYPE')[params.website], 4, clue.createdAt
];
valueSql = connection.format(valueSql, replacements);
valuesSql.push(valueSql);
}
valueSql = connection.format(valueSql, replacements);
valuesSql.push(valueSql);
}
const sql = `INSERT INTO crm_clue (
const sql = `INSERT INTO crm_clue (
customer_id, customer_name, customer_phone, intention_des, intention_id, shop_name, shop_id,
salesman_name, salesman_id, clue_input_type, create_type, create_time
) VALUES ${valuesSql.join(',')} ON DUPLICATE KEY UPDATE
......@@ -321,38 +322,35 @@ class DealerHelper {
intention_des = VALUES(intention_des),
create_type = VALUES(create_type)`;
connection.query(
sql,
async (err, results, fields) => {
connection.release();
connection.query(
sql,
async (err, results, fields) => {
connection.release();
if (err) {
reject(new errors.AppError(err));
}
if (err) {
reject(new errors.AppError(err));
}
if (results.affectedRows) {
// console.log(results.insertId, results);
logger.info(`accountID: ${params.accountID}, 线索信息保存成功, 总计: ${results.affectedRows}`);
if (results.affectedRows) {
// console.log(results.insertId, results);
logger.info(`accountID: ${params.accountID}, 线索信息保存成功, 总计: ${results.affectedRows}`);
try {
await this.saveSummary(params);
logger.info(`accountID: ${params.accountID}, 线索统计保存成功`);
} catch (e) {
logger.info(`accountID: ${params.accountID}, 线索统计保存失败`);
}
try {
await this.updateLatestFetch(params);
logger.info(`accountID: ${params.accountID}, 抓取时间更新成功`);
} catch (e) {
logger.info(`accountID: ${params.accountID}, 抓取时间更新失败`);
}
try {
await this.saveSummary(params);
logger.info(`accountID: ${params.accountID}, 线索统计保存成功`);
} catch (e) {
logger.info(`accountID: ${params.accountID}, 线索统计保存失败, ${e}`);
}
resolve(true);
} else {
reject(new errors.AppError('线索保存失败'));
resolve(true);
} else {
reject(new errors.AppError('线索保存失败'));
}
}
}
);
);
} catch (e) {
reject(e);
}
});
}
......@@ -386,8 +384,9 @@ class DealerHelper {
return new Promise(async (resolve, reject) => {
try {
const sql = `UPDATE crm_dealer_thirdparty_account SET
latestClueAt = (SELECT MAX(create_time) FROM crm_clue WHERE clue_input_type = ? AND dealerID = ?),
latestFetchAt = UNIX_TIMESTAMP() WHERE id = ?`;
const replacements = [params.accountID];
const replacements = [config.get('CLUE_INPUT_TYPE')[params.website], params.dealerID, params.accountID];
const connection = await DI.getConnection('MASTER_CRM');
connection.query(
sql,
......
......@@ -49,6 +49,16 @@ class Base {
throw new errors.LoginError('登录失败');
}
}
// 更新抓取时间
async updateLatestFetch() {
try {
await DealerHelper.updateLatestFetch(this.params);
logger.info(`accountID: ${this.params.accountID}, 抓取时间更新成功`);
} catch (e) {
logger.error(`accountID: ${this.params.accountID}, 抓取时间更新失败, ${e}`);
}
}
}
module.exports = Base;
\ No newline at end of file
......@@ -198,11 +198,11 @@ class Autohome extends Base {
let startTime;
let customerIndex = 0;
// 读取上次抓取时间点
if (this.params.latestFetchAt) {
startTime = moment.unix(this.params.latestFetchAt).format('YYYY.MM.DD');
// 最新线索时间点作为抓取开始时间
if (this.params.latestClueAt) {
startTime = moment.unix(this.params.latestClueAt).format('YYYY.MM.DD');
} else {
startTime = moment().subtract(30, 'days').format('YYYY.MM.DD');
startTime = moment().subtract(3, 'days').format('YYYY.MM.DD');
}
const params = {};
......@@ -276,17 +276,19 @@ class Autohome extends Base {
// logger.debug('线索类型:', clue.orderType);
// logger.debug('线索状态:', clue.orderState);
logger.debug(`accountID: ${this.params.accountID}, 线索: ${JSON.stringify(clue)}`);
logger.debug('===============================================================================');
logger.info(`accountID: ${this.params.accountID}, 线索: ${JSON.stringify(clue)}`);
logger.info('===============================================================================');
}
if (this.params.dealerID) {
if (clues.length > 0) {
await DealerHelper.saveClues({
clues: clues,
dealerID: this.params.dealerID,
website: this.params.website,
accountID: this.params.accountID,
});
} else {
logger.info(`accountID: ${this.params.accountID}, 时间段(${params.startTime} - ${params.endtime})没有线索`);
}
// 判断是否抓取完全
......@@ -301,8 +303,9 @@ class Autohome extends Base {
});
}
await fetchByPage();
await this.updateLatestFetch();
} catch (e) {
logger.error(e);
logger.error(`accountID: ${this.params.accountID}, ${e}`);
}
}
}
......
......@@ -16,6 +16,8 @@ CREATE TABLE `crm_dealer_thirdparty_account` (
`cookies` text COMMENT '第三方账号cookies',
`error` text COMMENT '爬取第三方过程中产生的错误',
`status` tinyint(255) DEFAULT '0' COMMENT '第三方账号状态: -1: 同步失败, 0: 未同步, 1: 同步成功',
`latestClueAt` bigint(255) DEFAULT NULL COMMENT '最新线索时间',
`latestFetchAt` bigint(255) DEFAULT NULL COMMENT '最近抓取时间',
`createdAt` bigint(20) DEFAULT NULL COMMENT '第三方账号创建时间',
`updatedAt` bigint(20) DEFAULT NULL COMMENT '第三方账号更新时间',
PRIMARY KEY (`id`),
......
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 sign in to comment