Showing
3 changed files
with
127 additions
and
1 deletions
server/modules/UniqueId.js
0 → 100644
| 1 | +/** | ||
| 2 | + * UniqueId | ||
| 3 | + * Created by lintry on 2018/8/14. | ||
| 4 | + */ | ||
| 5 | + | ||
| 6 | +const crypto_utils = require('kml-crypto-utils'); | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * 唯一键值生成器 | ||
| 10 | + */ | ||
| 11 | +class UniqueId { | ||
| 12 | + constructor (join_char) { | ||
| 13 | + this.join_char = join_char || '-'; | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + /** | ||
| 17 | + * 根据参数合成唯一ID | ||
| 18 | + * @param args | ||
| 19 | + * @returns {string} | ||
| 20 | + */ | ||
| 21 | + hash (...args) { | ||
| 22 | + if (!args || !args.length) { | ||
| 23 | + throw new Error('参数不能为空'); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + let keys = []; | ||
| 27 | + args.forEach(arg => { | ||
| 28 | + if (Array.isArray(arg)) { | ||
| 29 | + keys.concat(arg.sort()); | ||
| 30 | + } else { | ||
| 31 | + keys.push(arg); | ||
| 32 | + } | ||
| 33 | + }); | ||
| 34 | + | ||
| 35 | + return crypto_utils.MD5(args.join(this.join_char)); | ||
| 36 | + } | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +module.exports = UniqueId; | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
server/routers/c/info_action.js
0 → 100644
| 1 | +/** | ||
| 2 | + * info_action | ||
| 3 | + * Created by lintry on 2020/1/28. | ||
| 4 | + */ | ||
| 5 | + | ||
| 6 | +"use strict"; | ||
| 7 | + | ||
| 8 | + | ||
| 9 | +module.exports = function (dbo) { | ||
| 10 | + | ||
| 11 | + //api公共模块 | ||
| 12 | + const _ = require('lodash'), | ||
| 13 | + po = global.po, | ||
| 14 | + Result = require('kml-express-stage-lib').Result, | ||
| 15 | + logger = global.loggers.system, | ||
| 16 | + redisDb = require('../../init/redis-promisify'), | ||
| 17 | + moment = require('moment'); | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * 按日期统计 | ||
| 22 | + * @param req | ||
| 23 | + * @returns {Promise<Result>} | ||
| 24 | + */ | ||
| 25 | + this.statGet = async (req) => { | ||
| 26 | + try { | ||
| 27 | + // 获取数据 | ||
| 28 | + let params = req.query; | ||
| 29 | + let {province_name} = params; | ||
| 30 | + | ||
| 31 | + province_name = province_name || ''; | ||
| 32 | + | ||
| 33 | + // 代码逻辑区域 | ||
| 34 | + let result = await dbo.query(` | ||
| 35 | + with days as ( | ||
| 36 | + select generate_series('2020-01-24'::date, CURRENT_DATE, '1 days')::date today | ||
| 37 | + ) | ||
| 38 | + select province_short_name, d.today, max(confirmed_count) confirmed_count | ||
| 39 | + from days d | ||
| 40 | + left join tb_mf_timeline_area ta on ta.update_time::date = d.today | ||
| 41 | + where 1=1 | ||
| 42 | + and province_name ~ :province_name | ||
| 43 | + group by 1,2 | ||
| 44 | + order by 2, 1 | ||
| 45 | + `, {replacements: {province_name}, type: dbo.QueryTypes.SELECT}); | ||
| 46 | + | ||
| 47 | + return Result.Ok('成功!', result); | ||
| 48 | + } catch (e) { | ||
| 49 | + logger.error('失败!', e); | ||
| 50 | + return Result.Error('失败!', e.message); | ||
| 51 | + } | ||
| 52 | + }; | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * 最新情况 | ||
| 56 | + * @param req | ||
| 57 | + * @returns {Promise<Result>} | ||
| 58 | + */ | ||
| 59 | + this.latestGet = async (req) => { | ||
| 60 | + try { | ||
| 61 | + // 获取数据 | ||
| 62 | + let params = req.query; | ||
| 63 | + let {date} = params; | ||
| 64 | + date = date || moment().format('YYYY-MM-DD'); | ||
| 65 | + | ||
| 66 | + // 代码逻辑区域 | ||
| 67 | + let result = await dbo.query(` | ||
| 68 | + with t as ( | ||
| 69 | + select update_time, province_short_name, confirmed_count, suspected_count, cured_count, dead_count, "comment", cities, country, row_number() over(partition by province_short_name order by update_time desc) rn | ||
| 70 | + from tb_mf_timeline_area | ||
| 71 | + where update_time::date = :date | ||
| 72 | + ) | ||
| 73 | + select update_time, province_short_name, confirmed_count, suspected_count, cured_count, dead_count, "comment", cities, country | ||
| 74 | + from t | ||
| 75 | + where rn = 1 | ||
| 76 | + order by confirmed_count desc | ||
| 77 | + `, {replacements: {date}, type: dbo.QueryTypes.SELECT}); | ||
| 78 | + | ||
| 79 | + return Result.Ok('成功!', result); | ||
| 80 | + } catch (e) { | ||
| 81 | + logger.error('失败!', e); | ||
| 82 | + return Result.Error('失败!', e.message); | ||
| 83 | + } | ||
| 84 | + }; | ||
| 85 | + | ||
| 86 | +}; |
| ... | @@ -7,6 +7,7 @@ const _ = require('lodash'), | ... | @@ -7,6 +7,7 @@ const _ = require('lodash'), |
| 7 | logger = global.loggers.system; | 7 | logger = global.loggers.system; |
| 8 | const id_worker = require('../modules/id_worker'); | 8 | const id_worker = require('../modules/id_worker'); |
| 9 | const api_sdk = new (require('kml-api-request'))(); | 9 | const api_sdk = new (require('kml-api-request'))(); |
| 10 | +const uid = new (require('../modules/UniqueId'))(); | ||
| 10 | 11 | ||
| 11 | class DataImporter { | 12 | class DataImporter { |
| 12 | constructor (dbo) { | 13 | constructor (dbo) { |
| ... | @@ -37,7 +38,7 @@ class DataImporter { | ... | @@ -37,7 +38,7 @@ class DataImporter { |
| 37 | country} = row; | 38 | country} = row; |
| 38 | 39 | ||
| 39 | areas.push({ | 40 | areas.push({ |
| 40 | - id: id_worker.nextId(), | 41 | + id: uid.hash(updateTime, provinceName), |
| 41 | province_name: provinceName, | 42 | province_name: provinceName, |
| 42 | province_short_name: provinceShortName, | 43 | province_short_name: provinceShortName, |
| 43 | confirmed_count: confirmedCount, | 44 | confirmed_count: confirmedCount, | ... | ... |
-
Please register or login to post a comment