Created
November 9, 2019 01:23
-
-
Save zew13/e29ce97687b1083f4222605b8ddc574d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env -S node -r ./livescript-transform-implicit-async/register | |
| require! <[ | |
| fs | |
| dayjs | |
| axios | |
| ./phone-book.ls | |
| ./db/_knex.ls | |
| ]> | |
| ALI = _knex require(\./config.aliyun.ls) | |
| csv = require \fast-csv | |
| stream = csv.format! | |
| csv-stream = fs.createWriteStream( | |
| "csv/time-year-cost.csv" | |
| ) | |
| csv-stream.write("\ufeff") | |
| stream.pipe csv-stream | |
| do !~> | |
| user-month = await ALI.kv( | |
| """select rec_creator_id,date_format(rec_create_time,"%Y-%m") from gb_order group by rec_creator_id order by rec_create_time""" | |
| ) | |
| mval = {} | |
| for [user_id, date] in await ALI.li( | |
| """select distinct(rec_creator_id),DATE_FORMAT(delivery_date,"%Y-%m-%d") from gorder where final_amount=0 and points>0 and delivery_date<"2018-10-01" group by rec_creator_id order by delivery_date""" | |
| ) | |
| li = await ALI.li( | |
| """select delivery_date, (points-refunded_points)/100+(final_amount-refunded_amount) from gorder where rec_creator_id=? and date(delivery_date)>=? order by delivery_date""" | |
| user_id | |
| date | |
| ) | |
| begin = li[0][0] | |
| t = [] | |
| pre = -1 | |
| for [day,amount] in li | |
| m = 1+parseInt((day - begin)/86400000/30) | |
| if m > 13 | |
| break | |
| if m == pre | |
| t[*-1]+=(amount - 0) | |
| else | |
| t.push(amount - 0) | |
| pre = m | |
| m = user-month[user_id] | |
| li = mval[m] | |
| if not li | |
| mval[m] = li = [0]*13 | |
| for i,pos in t | |
| li[pos]+=i | |
| li = [] | |
| for k,v of mval | |
| li.push [k].concat v.map( | |
| (i)~>Math.round(i) | |
| ) | |
| li.sort (a,b)~> | |
| if a[0]>b[0] | |
| return 1 | |
| return -1 | |
| for i in li | |
| console.log i | |
| stream.write i | |
| stream.on( | |
| \end | |
| !~> | |
| console.log \end | |
| # process.exit! | |
| ) | |
| stream.end! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment