-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreport.ls
73 lines (65 loc) · 1.81 KB
/
report.ls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require! <[ fs express http mongodb q moment ]>
_ = require 'lodash'
uri = require \./database
start_ts = +new Date!
client = mongodb.MongoClient uri, {
connectTimeoutMS: 10000,
serverSelectionTimeoutMS: 120000,
useUnifiedTopology: true
}
err <- client.connect
db = client.db('pcc')
console.log 'connected', +new Date! - start_ts
deferred = q.defer!
db.collection 'unit' .find {} .toArray (err, units) ->
units = units.reduce (res, unit, key) ->
res[unit.name] = unit
res[unit._id] = unit
return res
, {}
#deferred.resolve units
start = moment process.argv[3], "YYYY-MM-DD" .utcOffset '+0800'
end = moment start .add {months: 1}
pcc = db.collection('pcc')
pcc.aggregate [
{$match: {publish: {$gte: start.toDate!, $lte: end.toDate!}}},
{$group: {_id: {unit_id: "$unit_id", job_number: "$job_number"}, price: {$max: "$price"}}},
{$group: {_id: "$_id.unit_id", price: {$sum: "$price"}, count: {$sum: 1}}}
] .toArray (err, result) ->
if err
console.log err
findParent = (id, unit)->
if units[id] && units[id].parent == null
return units[id].name
else if units[id] && units[id].parent != null
return findParent units[id].parent, units[id]
else if unit
return unit.name
else
return ''
result = result.map (row) ->
#name = units[row._id.replace(/\s+/, '')].name
if units[row._id.replace(/\s+/, '')]
name = units[row._id.replace(/\s+/, '')].name
else
name = row._id.replace(/\s+/, '')
unit = findParent name
return {
#ids: row.value.ids
#repeat: row.value.repeat
parent: unit
unit: name
count: row.count
price: row.price
}
db.collection \report .updateOne {
_id: process.argv[3]
}, {
$set: {
updated_at: new Date(),
res: result
}
}, {upsert: true}, (err, res) ->
console.log err
#console.log res
process.exit!