/
work-order.flow.ts
210 lines (180 loc) · 6.54 KB
/
work-order.flow.ts
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { Repository, Transaction, TransactionRepository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { Logger } from '../lib/logger';
import { BaseFlow } from './base.flow';
import { FlowTemplateEnum, FlowOperationsEnum } from '../aspects/enum';
import { InjectRepository } from '@nestjs/typeorm';
import { FlowTemplate } from '../entities/flow-template.entity';
import { User } from '../entities/user.entity';
import { Flow } from '../entities/flow.entity';
import { WFResult, WFStatus } from '../lib/wf';
import { Service } from '../entities/service.entity';
import { Detail } from '../entities/detail.entity';
@Injectable()
export class WorkOrderFlow extends BaseFlow {
protected readonly name: string = '服务工单';
protected readonly template: FlowTemplateEnum = FlowTemplateEnum.WORK_OR;
protected readonly flowSteps: any = [
{
name: '待申请',
steps: [{ name: '申请', nextState: '待派单', task: this.apply, roles: ['self'] }]
},
{
name: '待派单',
steps: [
{ name: '派单', nextState: '待接单', task: this.allocation, roles: ['admin'], operation: FlowOperationsEnum.ALLOCATION },
{ name: '作废', nextState: '已作废', task: this.cancel, roles: ['self', 'admin'], operation: FlowOperationsEnum.REMARKS }
]
},
{
name: '已拒绝',
steps: [
{ name: '重新派单', nextState: '待接单', roles: ['admin'], task: this.allocation, operation: FlowOperationsEnum.ALLOCATION },
{ name: '作废', nextState: '已作废', roles: ['self', 'admin'], task: this.cancel, operation: FlowOperationsEnum.REMARKS }
]
},
{
name: '待接单',
steps: [
{ name: '接单', nextState: '待执行', roles: ['executor'], task: this.receipt },
{ name: '拒绝', nextState: '已拒绝', roles: ['executor'], task: this.refuse, operation: FlowOperationsEnum.REMARKS },
{ name: '作废', nextState: '已作废', task: this.cancel, roles: ['self', 'admin'], operation: FlowOperationsEnum.REMARKS }
]
},
{
name: '待执行',
steps: [{ name: '完成', nextState: '待结单', roles: ['executor'], task: this.complete }]
},
{
name: '待结单',
steps: [
{ name: '结单', nextState: '已结单', roles: ['admin'], task: this.statement },
{ name: '作废', nextState: '已作废', roles: ['admin'], task: this.cancel, operation: FlowOperationsEnum.REMARKS }
]
},
{
name: '已结单',
steps: []
},
{
name: '已作废',
steps: []
}
];
constructor(
@InjectRepository(FlowTemplate)
protected readonly flowTemplateRepository: Repository<FlowTemplate>
) {
super(flowTemplateRepository);
}
@Transaction()
async apply(
step,
flow,
options,
@TransactionRepository(Flow) flowRepos?: Repository<Flow>
) {
flow = Flow.create(flow) as Flow;
flow.wfResult = WFResult.RUNNING;
flow.wfStatus = WFStatus.RUNNING;
flow.state = step.nextState;
await flowRepos.save(flow);
}
@Transaction()
async allocation(
step,
flow,
options,
@TransactionRepository(Flow) flowRepos?: Repository<Flow>
) {
flow = Flow.create(flow) as Flow;
flow.state = step.nextState;
flow.operator = User.create(options.operator) as User;
flow.executor = User.create(options.executor) as User;
await flowRepos.save(flow);
}
@Transaction()
async refuse(
step,
flow,
options,
@TransactionRepository(Flow) flowRepos?: Repository<Flow>
) {
flow = Flow.create(flow) as Flow;
flow.state = step.nextState;
flow.operator = User.create(options.operator) as User;
flow.executor = null;
await flowRepos.save(flow);
}
@Transaction()
async receipt(
step,
flow,
options,
@TransactionRepository(Flow) flowRepos?: Repository<Flow>
) {
flow = Flow.create(flow) as Flow;
flow.state = step.nextState;
flow.operator = User.create(options.operator) as User;
await flowRepos.save(flow);
}
@Transaction()
async complete(
step,
flow,
options,
@TransactionRepository(Flow) flowRepos?: Repository<Flow>
) {
flow = Flow.create(flow) as Flow;
flow.state = step.nextState;
flow.operator = User.create(options.operator) as User;
await flowRepos.save(flow);
}
@Transaction()
async statement(
step,
flow,
options,
@TransactionRepository(Service) serviceRepos?: Repository<Service>,
@TransactionRepository(User) userRepos?: Repository<User>,
@TransactionRepository(Detail) detailRepos?: Repository<Detail>,
@TransactionRepository(Flow) flowRepos?: Repository<Flow>
) {
flow = await flowRepos.findOne({ where: { id: flow.id }, relations: ['user', 'operator', 'executor'] })
flow.state = step.nextState;
flow.wfResult = WFResult.SUCCESS;
flow.wfStatus = WFStatus.OVER;
flow.operator = User.create(options.operator) as User;
await flowRepos.save(flow);
const service = await serviceRepos.findOne(flow.target);
// 给用户增加积分
const user = await userRepos.findOne(flow.executor.id);
user.points += service.points;
await userRepos.save(user);
// 增加积分明细
const detail = new Detail();
detail.title = '完成任务加积分';
detail.value = service.points;
detail.user = user;
await detailRepos.save(detail);
}
@Transaction()
async cancel(
step,
flow,
options,
@TransactionRepository(Flow) flowRepos?: Repository<Flow>
) {
flow = Flow.create(flow) as Flow;
flow.state = step.nextState;
flow.wfResult = WFResult.FAILURE;
flow.wfStatus = WFStatus.CANCELED;
flow.operator = User.create(options.operator) as User;
if (!!options.remarks) {
const remarks = flow.ex_info.remarks || [];
remarks.push(options.remarks);
flow.ex_info.remarks = remarks;
}
await flowRepos.save(flow);
}
}