station.js 1.9 KB

1
  1. "use strict";Object.defineProperty(exports,"__esModule",{value:!0});const egg_1=require("egg"),moment=require("moment");class StionService extends egg_1.Service{async isIdExisted(d){const{app:a}=this,b=await a.mysql.query("SELECT * FROM station WHERE id = ?",[d]);return Array.isArray(b)&&0<b.length}async isStationEnable(d){const{app:a}=this,b=await a.mysql.query("SELECT * FROM station WHERE id = ? AND disable = 0",[d]);return Array.isArray(b)&&0<b.length}async getStationById(d){const{app:a}=this,b=await a.mysql.query("SELECT name AS stationName, last_update AS updateTime FROM station WHERE id = ?",[d]);return Array.isArray(b)&&0!==b.length?Object.assign({},b[0],{stationId:d}):null}async checkUpdated(h){const{mysql:a}=this.app,b=moment().subtract(h,"minute").format("YYYY-MM-DD HH:mm:ss"),[c,d]=await Promise.all([a.query("SELECT COUNT(*) AS sum FROM station WHERE disable = 0"),a.query("SELECT COUNT(*) AS alive FROM station WHERE disable = 0 AND active >= ?",[b])]),e=Array.isArray(c)&&0<c.length?c[0].sum:0,f=Array.isArray(d)&&0<d.length?d[0].alive:0;return[e,f]}async searchAll(f,a){const{mysql:g}=this.app;let c=`SELECT id, name, disable, active FROM station WHERE disable = ${a}`;if(f){const a=g.escape(`%${f}%`);c+=` AND ( name LIKE ${a} OR id LIKE ${a} )`}const b=await g.query(c);return b}async add(e,a){const{app:b}=this,c=await b.mysql.query("INSERT INTO station (id, name) VALUES (?, ?)",[a,e]);return 1===c.affectedRows}async updateName(e,a){const{app:b}=this,c=await b.mysql.query("UPDATE station SET name = ? WHERE id = ?",[a,e]);return 1===c.changedRows}async updateTime(d,a){const{app:b}=this;b.mysql.query("UPDATE station SET active = ? WHERE id = ?",[a,d])}async disable(d){const{mysql:a}=this.app,b=await a.query("UPDATE station SET disable = ? WHERE id = ?",[1,d]);return b}async enable(d){const{mysql:a}=this.app,b=await a.query("UPDATE station SET disable = ? WHERE id = ?",[0,d]);return b}}exports.default=StionService;