index.js
2.39 KB
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
72
const EventEmitter = require('events')
const PDFGenerator = require('./pdfGenerator')
const moment = require('moment')
const BorrowList = require('../models').BorrowList
const User = require('../models').User
class AgreementCenter {
constructor() {
this.listener = new EventEmitter()
this.init()
}
init() {
this.listener.on('new', async (item) => {
try {
const listId = item.id
if (!listId)throw new Error('need borrow list id')
const list = await BorrowList.findOne({
where: {id: listId, status: '已生效'},
attributes: ['id', 'listNo', 'amount', 'startDate', 'endDate', 'rate', 'usage', 'status'],
include: [{
model: User,
as: 'Borrower',
attributes: ['id', 'name', 'phone', 'idNo']
}, {
model: User,
as: 'Creditor',
attributes: ['id', 'name', 'phone', 'idNo']
}]
})
const data = this.buildLoanData(list)
const loanGenerator = new PDFGenerator('loan', data)
const serviceGenerator = new PDFGenerator('service', data)
const [loanFile, serviceFile] = await Promise.all([
loanGenerator.generate(),
serviceGenerator.generate()
])
await list.update({agreementSaveStatus: '处理中'})
console.log(loanFile)
console.log(serviceFile)
} catch (e) {
console.log(e)
}
})
}
buildLoanData(list) {
return {
listId: list.listNo,
creditName: list.Creditor.name,
creditId: list.Creditor.idNo,
creditPhone: list.Creditor.phone,
borrowName: list.Borrower.name,
borrowId: list.Borrower.idNo,
borrowPhone: list.Borrower.phone,
listAmount: list.amount,
listStartDay: moment(list.startDate).format('YYYY-MM-DD'),
listEndDay: moment(list.endDate).format('YYYY-MM-DD'),
listRate: Math.floor(list.rate * 100),
listUsage: list.usage,
listTotal: Math.floor((list.amount + (list.amount * list.rate) * moment(list.endDate).diff(moment(list.startDate), 'days') / 365) * 100) / 100,
listDate: moment().format('YYYY-MM-DD')
}
}
emit(eventName, ...args) {
this.listener.emit(eventName, ...args)
}
newList(id) {
this.emit('new', {id: id})
}
}
const center = new AgreementCenter()
// center.emit('new', {id:'1b20d0ca-2864-4356-8534-2c5de47e0522'})
module.exports = center