friend.js
4.69 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Created by Tommy Huang on 18/04/10.
*/
const config = require('config-lite')({
config_basedir: __dirname,
config_dir: 'config'
})
const Helper = require('./helper')
const _ = require('lodash')
const moment = require('moment')
const Op = require('sequelize').Op
const User = require('../models').User
const BorrowList = require('../models').BorrowList
const Friendship = require('../models').Friendship
const OverdueRecord = require('../models').OverdueRecord
exports.getList = async function (req, res) {
try {
const idFromToken = req.user.userId
const userId = req.query.userId
const search = req.query.search || ''
if (!userId) throw new Error('参数错误')
if (idFromToken !== userId) throw new Error('token check fail')
let friendships = await Friendship.findAll({where: {userId: userId}})
let friendIds = _.map(friendships, 'friend_id')
let list = await User.findAll({
where: {
id: friendIds,
name: {
[Op.like]: `%${search}%`
}
},
attributes: ['id', 'name', 'headimgurl'],
})
const count = list.length
res.json({
success: 1,
count: count,
list: list
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `获取好友失败:${e.message}`
})
}
}
exports.getFriendInfo = async function (req, res) {
try {
const idFromToken = req.user.userId
const userId = req.query.userId
const id = req.query.id
if (!userId) throw new Error('参数错误')
if (idFromToken !== userId) throw new Error('token check fail')
const [friendship, friend] = await Promise.all([
Friendship.findOne({where: {userId: userId, friendId: id}}),
User.findOne({
where: {id: id},
attributes: ['id', 'name', 'headimgurl', 'phone'],
})
])
if (!friendship || !friend) throw new Error('好友不存在')
const [borrow, credit, allOverDue] = await Promise.all([
BorrowList.findAll({where:{
borrowerId: friend.id,
deleted: false,
status: ['已生效', '已逾期', '已还清']
}}),
BorrowList.findAll({where:{
creditorId: friend.id,
deleted: false,
status: ['已生效', '已逾期', '已还清']
}}),
OverdueRecord.findAll({
where: {
userId: friend.id,
status: {
[Op.not]: '已展期'
}
},
include: [{
model: BorrowList,
required: true
}]
})
])
const borrowInfo = buildBorrowRecordStatistics(borrow, 'borrow')
const creditInfo = buildBorrowRecordStatistics(credit, 'credit')
const overdueInfo = buildOverdueRecordStatistics(allOverDue)
res.json({
success: 1,
user: friend,
borrowInfo: borrowInfo,
creditInfo: creditInfo,
overdueInfo: overdueInfo
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `获取好友信息失败:${e.message}`
})
}
}
/**
*
* @param {Array} list 历史借条列表
* @param {String} type 借款:'borrow', 出借: ’credit‘
*/
function buildBorrowRecordStatistics(list, type) {
let totalAmount = 0
let times = list.length
let peoples = 0
let inOneDay = 0
let effectiveAmount = 0
let targetIds = []
list.forEach((item) => {
totalAmount += item.amount
if (item.status === '已生效' || item.status === '已逾期') {effectiveAmount += item.amount}
if (item.startDate === item.endDate) {inOneDay++}
let targetId = type === 'borrow' ? item.creditorId : item.borrowerId
targetIds.push(targetId)
})
targetIds = new Set(targetIds)
peoples = targetIds.size
const inOneDayRate = times === 0 ? 0 : (Math.floor(inOneDay/times * 10000) / 100)
return {
totalAmount: totalAmount,
times: times,
effectiveAmount: effectiveAmount,
inOneDayRate: inOneDayRate,
peoples: peoples
}
}
/**
*
* @param {Array} list 历史逾期记录列表
*/
function buildOverdueRecordStatistics(list) {
let totalTime = list.length
let totalAmount = 0
let sevenDayTime = 0
let sevenDayAmount = 0
let nowTime = 0
let nowAmount = 0
list.forEach(item => {
totalAmount += item.borrow_list.amount
if (item.status === '未还清') {
nowTime++
nowAmount+= item.borrow_list.amount
if (moment().diff(moment(item.startDate, 'days')) >= 7) {
sevenDayTime++
sevenDayAmount+= item.borrow_list.amount
}
} else {
if (moment(item.endDate).diff(moment(item.startDate, 'days')) >= 7) {
sevenDayTime++
sevenDayAmount+= item.borrow_list.amount
}
}
})
return {
totalTime,
totalAmount,
sevenDayTime,
sevenDayAmount,
nowTime,
nowAmount,
}
}