微信小程序 云函数复杂点的数据库操作(数组字段)

记录,备用

后台数据实例.JPG

业务说明:
1,每个商户一条记录,mbi.oid存储的是商户openId
2,商户记录中的cpi数组字段用于存储会员积分信息,每个会员一条数据,例如cpi[0].cid是会员主键
3,新会员,则需要再cpi数组中增加一条会员积分信息,理论上每个会员仅有一条积分数据
4,老会员,在发放积分时则会更新会员信息,即从cpi数组中通过cid找到对应的积分数据并更新
5,查询积分信息时也是通过cpi数组中的cid找到对应积分数据

// v3 是否新会员(理论上必会返回一条记录list.length==1,主要通过list[0].cpi.length判断是否是新会员)
async function is_new_customer(event, wxContext) {
  try {
    return await db.collection('merchant').aggregate()
      .match({
        _id: event.data.merchant_id,          // merchant primary key
        mbi: { oid: event.userInfo.openId, }, // merchant openId
      })
      .project({
        cpi: $.filter({
          input: '$cpi',
          as: 'item',
          cond: $.eq(['$$item.cid', event.data.customer_id])
        })                                    // customer points info
      })
      .end()
  } catch (e) {
    console.error(e)
  }
}

// v3 新会员初始化积分信息(通过业务逻辑控制:只有检查到是新会员时才会初始化积分信息)
async function init_customer_points_info(event, wxContext) {  
  const now_date = new Date()
  var points = 10000 // 新会员首次赠送积分
  var comment = '新会员首次赠送积分'
  try {         
    return await db.collection('merchant').where({
      _id: event.data.merchant_id,          // merchant primary key
      mbi: { oid: event.userInfo.openId, }, // merchant openId
    }).update({
      data: {
        cpi: _.push({
          each: [{ 
            cid: event.data.customer_id,    // customer primary key
            ta: 0,                          // total amount
            tp: points,                     // total points
            d: [{
              a:0,
              p:points,
              c:comment,
              t:now_date.getTime()
            }]                              // details 积分明细
          }],                               // customer points info
        })
      }
    })
  } catch (e) {
    console.error(e)
  }
}

// v3 老会员发放积分(通过业务逻辑控制给老会员发放积分,即表示能在cpi数组字段中找到会员主键cid)
async function send_points(event, wxContext) {
  const now_date = new Date()
  var details = event.data.points
  details.t = now_date.getTime()
  try {         
    return await db.collection('merchant').where({
      _id: event.data.merchant_id,          // merchant primary key
      mbi: { oid: event.userInfo.openId, }, // merchant openId
      cpi: _.elemMatch({ 
        cid: _.eq(event.data.customer_id), 
      })                                    // 要求数组中包含至少一个满足 elemMatch 给定的所有条件的元素
    }).update({
      data: {
        "cpi.$.ta": _.inc(details.a),       // increase total amount
        "cpi.$.tp": _.inc(details.p),       // increase total point
        "cpi.$.d": _.push({
          each: [details], 
          slice: -100,                      // 只保留最后N条details数据
        })                                  // details
      }
    })
  } catch (e) {
    console.error(e)
  }
}

// v3 查询会员积分明细(使用聚合操作aggregate,通过project指定输出字段-符合条件的内容)
async function customer_points_details(event, wxContext) {
  try {
    return await db.collection('merchant').aggregate()
      .match({
        _id: event.data.merchant_id,          // merchant primary key
        mbi: { oid: event.userInfo.openId, }, // merchant openId
      })
      .project({
        cpi: $.filter({
          input: '$cpi',
          as: 'item',
          cond: $.eq(['$$item.cid', event.data.customer_id])
        })                                    // customer points info
      })
      .end()
  } catch (e) {
    console.error(e)
  }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容