report.js 1.24 KB
const {mysqlPool} = require('./mysql');

module.exports = async(data) => {
  let dataId = 0;
  const {productId, sizeId, price, time} = data;

  try {
    const updateRows = await mysqlPool.update('UPDATE `channel_sku_compare` SET `channel_price` = :channelPrice, `update_time` = :updateTime WHERE `product_id` = :productId AND `size_id` = :sizeId', {
      channelPrice: price,
      updateTime: time,
      productId,
      sizeId
    });

    if (!updateRows) {
      return Promise.reject(new Error(`更新channel_sku_compare表失败 ${JSON.stringify(data)}`));
    }
    const insertId = await mysqlPool.insert('INSERT INTO `channel_sku_compare_record` (`product_id`, `size_id`, `channel_price`, `create_time`) VALUES (:productId, :sizeId, :channelPrice, :createTime)', {
      productId,
      sizeId,
      channelPrice: price,
      createTime: time
    });

    if (!insertId) {
      return Promise.reject(new Error(`插入channel_sku_compare_record表失败 ${JSON.stringify(data)}`));
    } else {

      return Promise.resolve({
        price,
        sizeId,
        productId,
        dataId
      });
    }
  } catch (error) {
    return Promise.reject(new Error(`更新channel_sku_compare表错误: ${JSON.stringify(data)}, ${error.message}`));
  }
};