const cloud = require("@alipay/faas-server-sdk");

exports.main = async (event, context) => {
  const {name, number, address,type,person } = event; // 从事件参数中获取传入的值

  // 获取当前时间，并格式化成字符串

  const currentTime = new Date();

  const createTime = formatTime(currentTime); // formatTime 为自定义函数，见下面

  const db = cloud.database();

  // 通过 add 在 example 中添加文档

  return await db.collection('mysql2').add({
    data: {
    
      name: name,

      number: number,
      address:address,
      type:type,
      person:person,
      createTime: createTime, // 将 createTime 加入文档

    },

  });

};

// 自定义函数，用于将时间对象格式化成字符串

function formatTime(date) {
  const year = date.getFullYear();

  const month = padZero(date.getMonth() + 1);

  const day = padZero(date.getDate());

  const hour = padZero(date.getHours());

  const minute = padZero(date.getMinutes());

  const second = padZero(date.getSeconds());

  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;

}

// 自定义函数，用于补齐数字前面的零

function padZero(num) {
  return num < 10 ? '0' + num : num;

}
