心澄欲遣

不践迹,亦不入于室

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

定义两个model,一个给get的,一个给post的


var Sequelize = require('sequelize');

const DeviceNos = sequelize.define('DeviceDetail', { DeviceNo: { type: Sequelize.INTEGER } }, { tableName: 'DeviceDetail', timestamps: false, freezeTableName: true }) const Device = sequelize.define('DeviceDetail', { DeviceNo: { type: Sequelize.INTEGER }, Tem: { type: Sequelize.FLOAT, get() { return this.getDataValue("Tem").toFixed(2); } }, Hum: { type: Sequelize.FLOAT, get() { return this.getDataValue("Hum").toFixed(2); } }, Lng: { type: Sequelize.FLOAT, get() { return this.getDataValue("Lng").toFixed(2); } }, Lat: { type: Sequelize.FLOAT, get() { return this.getDataValue("Lat").toFixed(2); } }, ServiceTime: { type: Sequelize.DATE, get() { return moment(this.getDataValue('ServiceTime')).format('YYYY-MM-DD HH:mm:ss'); } } }, { tableName: 'DeviceDetail', timestamps: false, freezeTableName: true });

定义运算符

const Op = Sequelize.Op;

定义get/Post方法

router.post('/searchDeviceRecord', async function (ctx, next) {
  let deviceNo = ctx.request.body.deviceNo;
  let st = ctx.request.body.st;
  let et = ctx.request.body.et;
  console.log(st);
  try {
    var data = await Device.findAll({
      attributes: ['DeviceNo', 'Tem', 'Hum', 'Lng', 'Lat', 'ServiceTime'],
      where: {
        deviceNo: deviceNo,
        serviceTime: {
          [Op.lte]: et,
          [Op.gte]: st
        }
      },
      order: [['ServiceTime', 'ASC']]
    })
    ctx.body = JSON.stringify(data);
  } catch (e) {
    console.log(e);
  }
});

router.get('/getDeviceList', async function (ctx, next) {
  try {
    var data = await DeviceNos.findAll({
      attributes: [[sequelize.literal('distinct DeviceNo'), 'DeviceNo']], order: [['DeviceNo', 'ASC']]
    })
    ctx.body = JSON.stringify(data);
  } catch (e) {
    console.log(e);
  }

});

 

posted on 2017-12-18 15:52  心澄欲遣  阅读(1510)  评论(0编辑  收藏  举报
欢迎第myspace graphics个访客