|
@@ -13,18 +13,15 @@ export class IncomeRecordsService {
|
|
|
|
|
|
|
|
async create(data: CreateIncomeRecordBody): Promise<IncomeRecords> {
|
|
async create(data: CreateIncomeRecordBody): Promise<IncomeRecords> {
|
|
|
const incomeRecord = this.incomeRecordsRepository.create({
|
|
const incomeRecord = this.incomeRecordsRepository.create({
|
|
|
- teamId: data.teamId,
|
|
|
|
|
|
|
+ agentId: data.agentId,
|
|
|
userId: data.userId,
|
|
userId: data.userId,
|
|
|
incomeAmount: data.incomeAmount,
|
|
incomeAmount: data.incomeAmount,
|
|
|
- agentName: data.agentName,
|
|
|
|
|
incomeType: data.incomeType,
|
|
incomeType: data.incomeType,
|
|
|
orderType: data.orderType,
|
|
orderType: data.orderType,
|
|
|
- video: data.video,
|
|
|
|
|
- price: data.price,
|
|
|
|
|
- tipOrderId: data.tipOrderId,
|
|
|
|
|
|
|
+ orderPrice: data.orderPrice,
|
|
|
|
|
+ orderNo: data.orderNo,
|
|
|
payChannel: data.payChannel,
|
|
payChannel: data.payChannel,
|
|
|
- payNo: data.payNo,
|
|
|
|
|
- source: data.source
|
|
|
|
|
|
|
+ payNo: data.payNo
|
|
|
})
|
|
})
|
|
|
return this.incomeRecordsRepository.save(incomeRecord)
|
|
return this.incomeRecordsRepository.save(incomeRecord)
|
|
|
}
|
|
}
|
|
@@ -34,22 +31,31 @@ export class IncomeRecordsService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async findAll(query: ListIncomeRecordsQuery): Promise<PaginationResponse<IncomeRecords>> {
|
|
async findAll(query: ListIncomeRecordsQuery): Promise<PaginationResponse<IncomeRecords>> {
|
|
|
- const { page, size, teamId, userId, agentName, incomeType, orderType, payChannel, startDate, endDate } = query
|
|
|
|
|
|
|
+ const {
|
|
|
|
|
+ page,
|
|
|
|
|
+ size,
|
|
|
|
|
+ agentId,
|
|
|
|
|
+ userId,
|
|
|
|
|
+ incomeType,
|
|
|
|
|
+ orderType,
|
|
|
|
|
+ orderNo,
|
|
|
|
|
+ payChannel,
|
|
|
|
|
+ payNo,
|
|
|
|
|
+ status,
|
|
|
|
|
+ startDate,
|
|
|
|
|
+ endDate
|
|
|
|
|
+ } = query
|
|
|
|
|
|
|
|
const where: any = {}
|
|
const where: any = {}
|
|
|
|
|
|
|
|
- if (teamId) {
|
|
|
|
|
- where.teamId = teamId
|
|
|
|
|
|
|
+ if (agentId) {
|
|
|
|
|
+ where.agentId = agentId
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (userId) {
|
|
if (userId) {
|
|
|
where.userId = userId
|
|
where.userId = userId
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (agentName) {
|
|
|
|
|
- where.agentName = Like(`%${agentName}%`)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
if (incomeType) {
|
|
if (incomeType) {
|
|
|
where.incomeType = incomeType
|
|
where.incomeType = incomeType
|
|
|
}
|
|
}
|
|
@@ -58,10 +64,22 @@ export class IncomeRecordsService {
|
|
|
where.orderType = orderType
|
|
where.orderType = orderType
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (orderNo) {
|
|
|
|
|
+ where.orderNo = Like(`%${orderNo}%`)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (payChannel) {
|
|
if (payChannel) {
|
|
|
where.payChannel = Like(`%${payChannel}%`)
|
|
where.payChannel = Like(`%${payChannel}%`)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (payNo) {
|
|
|
|
|
+ where.payNo = Like(`%${payNo}%`)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (status !== undefined) {
|
|
|
|
|
+ where.status = status
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (startDate && endDate) {
|
|
if (startDate && endDate) {
|
|
|
where.createdAt = Between(new Date(startDate), new Date(endDate))
|
|
where.createdAt = Between(new Date(startDate), new Date(endDate))
|
|
|
}
|
|
}
|
|
@@ -83,6 +101,10 @@ export class IncomeRecordsService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ async findByOrderNo(orderNo: string): Promise<IncomeRecords> {
|
|
|
|
|
+ return this.incomeRecordsRepository.findOneOrFail({ where: { orderNo } })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async update(data: UpdateIncomeRecordBody): Promise<IncomeRecords> {
|
|
async update(data: UpdateIncomeRecordBody): Promise<IncomeRecords> {
|
|
|
const { id, ...updateData } = data
|
|
const { id, ...updateData } = data
|
|
|
await this.incomeRecordsRepository.update(id, updateData)
|
|
await this.incomeRecordsRepository.update(id, updateData)
|
|
@@ -100,12 +122,12 @@ export class IncomeRecordsService {
|
|
|
async getStatistics(
|
|
async getStatistics(
|
|
|
startDate?: string,
|
|
startDate?: string,
|
|
|
endDate?: string,
|
|
endDate?: string,
|
|
|
- teamId?: number,
|
|
|
|
|
|
|
+ agentId?: number,
|
|
|
userId?: number
|
|
userId?: number
|
|
|
): Promise<{
|
|
): Promise<{
|
|
|
dates: string[]
|
|
dates: string[]
|
|
|
- teams: Array<{
|
|
|
|
|
- teamId: number
|
|
|
|
|
|
|
+ agents: Array<{
|
|
|
|
|
+ agentId: number
|
|
|
data: number[]
|
|
data: number[]
|
|
|
tip: number[]
|
|
tip: number[]
|
|
|
commission: number[]
|
|
commission: number[]
|
|
@@ -133,10 +155,10 @@ export class IncomeRecordsService {
|
|
|
start.setHours(0, 0, 0, 0)
|
|
start.setHours(0, 0, 0, 0)
|
|
|
end.setHours(23, 59, 59, 999)
|
|
end.setHours(23, 59, 59, 999)
|
|
|
|
|
|
|
|
- const teamStats = await this.incomeRecordsRepository
|
|
|
|
|
|
|
+ const agentStats = await this.incomeRecordsRepository
|
|
|
.createQueryBuilder('record')
|
|
.createQueryBuilder('record')
|
|
|
.select([
|
|
.select([
|
|
|
- 'record.teamId as teamId',
|
|
|
|
|
|
|
+ 'record.agentId as agentId',
|
|
|
"DATE_FORMAT(CONVERT_TZ(record.createdAt, '+00:00', '+08:00'), '%Y-%m-%d') as date",
|
|
"DATE_FORMAT(CONVERT_TZ(record.createdAt, '+00:00', '+08:00'), '%Y-%m-%d') as date",
|
|
|
'CAST(SUM(CASE WHEN record.incomeType = :tipType THEN CAST(record.incomeAmount AS DECIMAL(10,5)) ELSE 0 END) AS DECIMAL(10,5)) as tipAmount',
|
|
'CAST(SUM(CASE WHEN record.incomeType = :tipType THEN CAST(record.incomeAmount AS DECIMAL(10,5)) ELSE 0 END) AS DECIMAL(10,5)) as tipAmount',
|
|
|
'CAST(SUM(CASE WHEN record.incomeType = :commissionType THEN CAST(record.incomeAmount AS DECIMAL(10,5)) ELSE 0 END) AS DECIMAL(10,5)) as commissionAmount',
|
|
'CAST(SUM(CASE WHEN record.incomeType = :commissionType THEN CAST(record.incomeAmount AS DECIMAL(10,5)) ELSE 0 END) AS DECIMAL(10,5)) as commissionAmount',
|
|
@@ -147,30 +169,30 @@ export class IncomeRecordsService {
|
|
|
.andWhere('record.delFlag = :delFlag', { delFlag: false })
|
|
.andWhere('record.delFlag = :delFlag', { delFlag: false })
|
|
|
.setParameter('tipType', IncomeType.TIP)
|
|
.setParameter('tipType', IncomeType.TIP)
|
|
|
.setParameter('commissionType', IncomeType.COMMISSION)
|
|
.setParameter('commissionType', IncomeType.COMMISSION)
|
|
|
- .andWhere(teamId ? 'record.teamId = :teamId' : '1=1', { teamId })
|
|
|
|
|
|
|
+ .andWhere(agentId ? 'record.agentId = :agentId' : '1=1', { agentId })
|
|
|
.andWhere(userId ? 'record.userId = :userId' : '1=1', { userId })
|
|
.andWhere(userId ? 'record.userId = :userId' : '1=1', { userId })
|
|
|
- .groupBy('record.teamId')
|
|
|
|
|
|
|
+ .groupBy('record.agentId')
|
|
|
.addGroupBy("DATE_FORMAT(CONVERT_TZ(record.createdAt, '+00:00', '+08:00'), '%Y-%m-%d')")
|
|
.addGroupBy("DATE_FORMAT(CONVERT_TZ(record.createdAt, '+00:00', '+08:00'), '%Y-%m-%d')")
|
|
|
- .orderBy('record.teamId', 'ASC')
|
|
|
|
|
|
|
+ .orderBy('record.agentId', 'ASC')
|
|
|
.addOrderBy('date', 'ASC')
|
|
.addOrderBy('date', 'ASC')
|
|
|
.getRawMany()
|
|
.getRawMany()
|
|
|
|
|
|
|
|
- // 按团队分组处理数据
|
|
|
|
|
- const teamMap = new Map<
|
|
|
|
|
|
|
+ // 按代理商分组处理数据
|
|
|
|
|
+ const agentMap = new Map<
|
|
|
number,
|
|
number,
|
|
|
{
|
|
{
|
|
|
- teamId: number
|
|
|
|
|
|
|
+ agentId: number
|
|
|
data: number[]
|
|
data: number[]
|
|
|
tip: number[]
|
|
tip: number[]
|
|
|
commission: number[]
|
|
commission: number[]
|
|
|
}
|
|
}
|
|
|
>()
|
|
>()
|
|
|
|
|
|
|
|
- // 初始化每个团队的数据数组
|
|
|
|
|
- teamStats.forEach(stat => {
|
|
|
|
|
- if (!teamMap.has(stat.teamId)) {
|
|
|
|
|
- teamMap.set(stat.teamId, {
|
|
|
|
|
- teamId: stat.teamId,
|
|
|
|
|
|
|
+ // 初始化每个代理商的数据数组
|
|
|
|
|
+ agentStats.forEach(stat => {
|
|
|
|
|
+ if (!agentMap.has(stat.agentId)) {
|
|
|
|
|
+ agentMap.set(stat.agentId, {
|
|
|
|
|
+ agentId: stat.agentId,
|
|
|
data: dates.map(() => 0),
|
|
data: dates.map(() => 0),
|
|
|
tip: dates.map(() => 0),
|
|
tip: dates.map(() => 0),
|
|
|
commission: dates.map(() => 0)
|
|
commission: dates.map(() => 0)
|
|
@@ -179,31 +201,31 @@ export class IncomeRecordsService {
|
|
|
|
|
|
|
|
const dateIndex = dates.indexOf(stat.date)
|
|
const dateIndex = dates.indexOf(stat.date)
|
|
|
if (dateIndex !== -1) {
|
|
if (dateIndex !== -1) {
|
|
|
- const team = teamMap.get(stat.teamId)!
|
|
|
|
|
- team.data[dateIndex] = Number(stat.totalAmount)
|
|
|
|
|
- team.tip[dateIndex] = Number(stat.tipAmount)
|
|
|
|
|
- team.commission[dateIndex] = Number(stat.commissionAmount)
|
|
|
|
|
|
|
+ const agent = agentMap.get(stat.agentId)!
|
|
|
|
|
+ agent.data[dateIndex] = Number(stat.totalAmount)
|
|
|
|
|
+ agent.tip[dateIndex] = Number(stat.tipAmount)
|
|
|
|
|
+ agent.commission[dateIndex] = Number(stat.commissionAmount)
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- const teams = Array.from(teamMap.values())
|
|
|
|
|
|
|
+ const agents = Array.from(agentMap.values())
|
|
|
|
|
|
|
|
// 计算每天的总收入
|
|
// 计算每天的总收入
|
|
|
const total = dates.map((_, index) => {
|
|
const total = dates.map((_, index) => {
|
|
|
- return teams.reduce((sum, team) => sum + team.data[index], 0)
|
|
|
|
|
|
|
+ return agents.reduce((sum, agent) => sum + agent.data[index], 0)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
const totalTip = dates.map((_, index) => {
|
|
const totalTip = dates.map((_, index) => {
|
|
|
- return teams.reduce((sum, team) => sum + team.tip[index], 0)
|
|
|
|
|
|
|
+ return agents.reduce((sum, agent) => sum + agent.tip[index], 0)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
const totalCommission = dates.map((_, index) => {
|
|
const totalCommission = dates.map((_, index) => {
|
|
|
- return teams.reduce((sum, team) => sum + team.commission[index], 0)
|
|
|
|
|
|
|
+ return agents.reduce((sum, agent) => sum + agent.commission[index], 0)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
|
dates,
|
|
dates,
|
|
|
- teams,
|
|
|
|
|
|
|
+ agents,
|
|
|
total,
|
|
total,
|
|
|
totalTip,
|
|
totalTip,
|
|
|
totalCommission
|
|
totalCommission
|