feat: 重构勘察业务为明细模式并新增定时任务人性化配置
- 重构勘察业务:移除SOP相关表,新增勘察明细表支持建筑/楼层/房间/照片管理 - 新增定时任务人性化频率配置,支持分钟/小时/天/周/月/自定义六种模式 - 新增频率预览接口,支持配置转换为cron表达式与执行时间预览 - 更新系统品牌名称为鑫潼智电,修改全局标题与登录页文案
This commit is contained in:
1 parent
f72644c24f
commit
22d27a940d
29 files changed
+1908
-576
No files matched your search
@@ -6,8 +6,7 @@ import { Context } from "@midwayjs/koa";
|
||||
import SurveyOrder from "../entity/survey_order.entity";
|
||||
import Customer from "../entity/customer.entity";
|
||||
import Worker from "../entity/worker.entity";
|
||||
import SurveySopItem from "../entity/survey_sop_item.entity";
|
||||
import SurveySopResult from "../entity/survey_sop_result.entity";
|
||||
import SurveyDetail from "../entity/survey_detail.entity";
|
||||
import DispatchPrice from "../entity/dispatch_price.entity";
|
||||
import SysUser from "../entity/sysUser.entity";
|
||||
import SysUserRole from "../entity/sysUserRole.entity";
|
||||
@@ -18,8 +17,6 @@ import { NoticeRuleService } from "./notice_rule.service";
|
||||
|
||||
// 勘察工单状态:0待指派 1现场勘察中 2待技术复核 3已归档 4预留 5已取消
|
||||
const STATUS = { PENDING_ASSIGN: 0, SURVEYING: 1, PENDING_REVIEW: 2, ARCHIVED: 3, RESERVED: 4, CANCELED: 5 };
|
||||
// 默认 SOP 模板编码
|
||||
const DEFAULT_TEMPLATE = "standard_dist";
|
||||
// 默认到场时限(分钟)与缺省报价(dispatch_price 缺失时兜底,不写库)
|
||||
const DEFAULT_EXPECT_MINUTES = 45;
|
||||
const DEFAULT_BASE_FEE = 350;
|
||||
@@ -38,11 +35,8 @@ export class SurveyOrderService {
|
||||
@InjectRepository(Worker)
|
||||
workerModel!: Repository<Worker>;
|
||||
|
||||
@InjectRepository(SurveySopItem)
|
||||
sopItemModel!: Repository<SurveySopItem>;
|
||||
|
||||
@InjectRepository(SurveySopResult)
|
||||
sopResultModel!: Repository<SurveySopResult>;
|
||||
@InjectRepository(SurveyDetail)
|
||||
detailModel!: Repository<SurveyDetail>;
|
||||
|
||||
@InjectRepository(DispatchPrice)
|
||||
priceModel!: Repository<DispatchPrice>;
|
||||
@@ -257,13 +251,50 @@ export class SurveyOrderService {
|
||||
return { baseFee, loopFee, loopCount: count, total };
|
||||
}
|
||||
|
||||
// 按模板批量生成回执记录(checked=false)
|
||||
private async createSopResults(orderId: number, templateCode: string): Promise<void> {
|
||||
const items = await this.sopItemModel.findAll({ where: { templateCode: templateCode || DEFAULT_TEMPLATE } });
|
||||
if (items.length === 0) return;
|
||||
await this.sopResultModel.bulkCreate(
|
||||
items.map((item) => ({ surveyOrderId: orderId, sopItemId: item.id, checked: false })) as any
|
||||
);
|
||||
// 明细入参清洗:photos 数组转 JSON 字符串,建筑/楼层缺失的行丢弃
|
||||
private normalizeDetails(input: any): any[] {
|
||||
if (!Array.isArray(input)) return [];
|
||||
return input
|
||||
.map((row) => {
|
||||
const photos = this.parsePhotos(row?.photos);
|
||||
const buildingId = Number(row?.buildingId) || null;
|
||||
const floorNo = row?.floorNo === undefined || row?.floorNo === null ? "" : String(row.floorNo);
|
||||
const roomId = Number(row?.roomId) || null;
|
||||
return {
|
||||
buildingId,
|
||||
buildingName: row?.buildingName ? String(row.buildingName).slice(0, 128) : null,
|
||||
floorNo: floorNo.slice(0, 16),
|
||||
roomId,
|
||||
roomName: row?.roomName ? String(row.roomName).slice(0, 128) : null,
|
||||
photos: photos.length ? JSON.stringify(photos) : null,
|
||||
remark: row?.remark ? String(row.remark).slice(0, 255) : null
|
||||
};
|
||||
})
|
||||
// 建筑与楼层是明细的定位要素,缺失的行直接丢弃
|
||||
.filter((row) => row.buildingId && row.floorNo);
|
||||
}
|
||||
|
||||
// 全量替换工单明细(管理端与电工端共用;调用方负责权限与状态校验)
|
||||
async replaceDetails(orderId: number, details: any): Promise<void> {
|
||||
const rows = this.normalizeDetails(details);
|
||||
await this.detailModel.destroy({ where: { surveyOrderId: orderId } });
|
||||
if (rows.length === 0) return;
|
||||
await this.detailModel.bulkCreate(rows.map((row) => ({ ...row, surveyOrderId: orderId })) as any);
|
||||
}
|
||||
|
||||
// 工单明细 + 照片预签名访问 URL(管理端详情与电工端订单详情共用)
|
||||
async getDetails(orderId: number): Promise<any[]> {
|
||||
const results = await this.detailModel.findAll({ where: { surveyOrderId: orderId }, order: [["id", "ASC"]] });
|
||||
if (results.length === 0) return [];
|
||||
|
||||
const rows = results.map((r) => {
|
||||
const json: any = r.toJSON();
|
||||
return { ...json, photos: this.parsePhotos(json.photos) };
|
||||
});
|
||||
|
||||
const keys = [...new Set<string>(rows.flatMap((r) => r.photos))];
|
||||
const urlMap = keys.length ? await this.uploadService.getPresignedUrlMap(keys) : {};
|
||||
return rows.map((row) => ({ ...row, photoUrls: row.photos.map((k: string) => urlMap[k] || "") }));
|
||||
}
|
||||
|
||||
async list(query: any, ctx: Context): Promise<any> {
|
||||
@@ -342,13 +373,6 @@ export class SurveyOrderService {
|
||||
};
|
||||
}
|
||||
|
||||
// SOP 模板检查项
|
||||
async listSopItems(templateCode?: string): Promise<any> {
|
||||
const code = templateCode || DEFAULT_TEMPLATE;
|
||||
const rows = await this.sopItemModel.findAll({ where: { templateCode: code }, order: [["sort_order", "ASC"]] });
|
||||
return { code: 200, message: "获取勘察SOP检查项成功", data: rows.map((r) => r.toJSON()) };
|
||||
}
|
||||
|
||||
// 报酬试算
|
||||
async quote(loopCount?: string): Promise<any> {
|
||||
const data = await this.calcFee(Number(loopCount) || 0);
|
||||
@@ -362,7 +386,7 @@ export class SurveyOrderService {
|
||||
|
||||
const vo = this.toVO(record);
|
||||
await this.decorate([vo]);
|
||||
vo.sopResults = await this.getSopResults(id);
|
||||
vo.details = await this.getDetails(id);
|
||||
// 现场取证照片:把对象 key 批量换取预签名访问 URL,供详情只读视图直接渲染
|
||||
const photoKeys: string[] = vo.photos || [];
|
||||
const urlMap = photoKeys.length ? await this.uploadService.getPresignedUrlMap(photoKeys) : {};
|
||||
@@ -370,84 +394,17 @@ export class SurveyOrderService {
|
||||
return { code: 200, message: "获取勘察派单详情成功", data: vo };
|
||||
}
|
||||
|
||||
// 工单回执 + 检查项信息(手动 join,避免额外关联);receiptValue 以 key 数组出参,并补预签名访问 URL
|
||||
private async getSopResults(orderId: number): Promise<any[]> {
|
||||
const results = await this.sopResultModel.findAll({ where: { surveyOrderId: orderId }, order: [["id", "ASC"]] });
|
||||
if (results.length === 0) return [];
|
||||
const itemIds = [...new Set(results.map((r) => r.sopItemId).filter(Boolean))];
|
||||
const items = await this.sopItemModel.findAll({ where: { id: { [Op.in]: itemIds } } });
|
||||
const itemMap = new Map(items.map((i) => [i.id, i]));
|
||||
|
||||
const rows = results.map((r) => {
|
||||
const json: any = r.toJSON();
|
||||
const item: any = itemMap.get(json.sopItemId);
|
||||
return {
|
||||
...json,
|
||||
receiptValue: this.parsePhotos(json.receiptValue),
|
||||
itemName: item?.itemName || "",
|
||||
itemDesc: item?.itemDesc || "",
|
||||
receiptType: item?.receiptType || "",
|
||||
receiptRequire: item?.receiptRequire || "",
|
||||
required: item?.required ?? false
|
||||
};
|
||||
});
|
||||
|
||||
// photo/meter 类回执是照片 key,批量换取预签名 URL 供前端直接渲染;form 类是文本值,不参与换取
|
||||
const keys = [...new Set<string>(rows.filter((r) => r.receiptType !== "form").flatMap((r) => r.receiptValue))];
|
||||
const urlMap = keys.length ? await this.uploadService.getPresignedUrlMap(keys) : {};
|
||||
return rows.map((row) => ({
|
||||
...row,
|
||||
receiptUrls: row.receiptType === "form" ? [] : row.receiptValue.map((k: string) => urlMap[k] || "")
|
||||
}));
|
||||
}
|
||||
|
||||
// 保存 SOP 工单回执(增量:只更新传入的检查项,未传入项保持原值)
|
||||
async saveSopResults(id: number, dto: any, ctx: Context): Promise<any> {
|
||||
// 保存工单明细(勘察明细整体替换:新增/编辑/删除均由前端以全量数组提交)
|
||||
async saveDetails(id: number, dto: any, ctx: Context): Promise<any> {
|
||||
const record = await this.surveyModel.findByPk(id);
|
||||
if (!record) return { code: 404, message: "勘察派单不存在", data: null };
|
||||
if (!(await this.isOrderVisible(ctx.state.user.userId, record))) return { code: 403, message: "无权操作该工单", data: null };
|
||||
if (record.status !== STATUS.SURVEYING) return { code: 400, message: "仅现场勘察中的工单可录入回执", data: null };
|
||||
|
||||
const incoming: any[] = Array.isArray(dto?.results) ? dto.results : [];
|
||||
if (incoming.length === 0) return { code: 400, message: "回执内容不能为空", data: null };
|
||||
|
||||
// 仅允许更新本工单已生成的回执记录,防止跨工单/跨检查项写入
|
||||
const existing = await this.sopResultModel.findAll({ where: { surveyOrderId: id } });
|
||||
const resultMap = new Map(existing.map((r) => [Number(r.sopItemId), r]));
|
||||
|
||||
let updated = 0;
|
||||
const skipped: number[] = [];
|
||||
for (const item of incoming) {
|
||||
const sopItemId = Number(item?.sopItemId);
|
||||
const target = resultMap.get(sopItemId);
|
||||
if (!target) {
|
||||
skipped.push(sopItemId);
|
||||
continue;
|
||||
}
|
||||
|
||||
const patch: any = {};
|
||||
if (item.checked !== undefined) {
|
||||
const checked = !!item.checked;
|
||||
patch.checked = checked;
|
||||
// 首次标记完成时记录回执时间,回退未完成则清空
|
||||
patch.checkedAt = checked ? target.checkedAt || new Date() : null;
|
||||
}
|
||||
if (item.receiptValue !== undefined) {
|
||||
const keys = this.parsePhotos(item.receiptValue);
|
||||
patch.receiptValue = keys.length ? JSON.stringify(keys) : null;
|
||||
}
|
||||
if (item.remark !== undefined) patch.remark = item.remark || null;
|
||||
if (Object.keys(patch).length === 0) continue;
|
||||
|
||||
await target.update(patch);
|
||||
updated++;
|
||||
if (record.status !== STATUS.PENDING_ASSIGN && record.status !== STATUS.SURVEYING) {
|
||||
return { code: 400, message: "仅待指派或现场勘察中的工单可维护勘察明细", data: null };
|
||||
}
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: "保存勘察回执成功",
|
||||
data: { updated, skipped, sopResults: await this.getSopResults(id) }
|
||||
};
|
||||
await this.replaceDetails(id, dto?.details);
|
||||
return { code: 200, message: "保存勘察明细成功", data: await this.getDetails(id) };
|
||||
}
|
||||
|
||||
async create(data: any, ctx: Context): Promise<any> {
|
||||
@@ -456,9 +413,8 @@ export class SurveyOrderService {
|
||||
if (!(await this.isCustomerOwner(userId, data.customerId))) return { code: 403, message: "无权限为该客户创建勘察派单", data: null };
|
||||
if (!isAdmin) data.salesmanId = userId;
|
||||
|
||||
const templateCode = data.templateCode || DEFAULT_TEMPLATE;
|
||||
const payload = this.normalizePayload(data);
|
||||
delete payload.templateCode;
|
||||
delete payload.details;
|
||||
|
||||
payload.surveyNo = await this.generateSurveyNo();
|
||||
if (payload.progressTotal === undefined || payload.progressTotal === null) payload.progressTotal = payload.loopCount ?? 0;
|
||||
@@ -484,9 +440,13 @@ export class SurveyOrderService {
|
||||
}
|
||||
}
|
||||
|
||||
await this.createSopResults(created.id, templateCode);
|
||||
// 创建时若携带勘察明细则一并写入
|
||||
if (Array.isArray(data.details) && data.details.length) {
|
||||
await this.replaceDetails(created.id, data.details);
|
||||
}
|
||||
const vo = this.toVO(created);
|
||||
await this.decorate([vo]);
|
||||
vo.details = await this.getDetails(created.id);
|
||||
await this.logService.logAction(ctx, "勘察派单", "创建", created.surveyNo, `创建勘察派单 ${created.surveyNo}`);
|
||||
return { code: 200, message: "创建勘察派单成功", data: vo };
|
||||
}
|
||||
@@ -503,13 +463,19 @@ export class SurveyOrderService {
|
||||
delete data.salesmanId;
|
||||
}
|
||||
delete data.surveyNo;
|
||||
delete data.templateCode;
|
||||
|
||||
const details = data.details;
|
||||
delete data.details;
|
||||
const payload = this.normalizePayload(data);
|
||||
await record.update(payload);
|
||||
// 仅当显式传入 details 时才整体替换明细,避免只改主表字段时误清空
|
||||
if (Array.isArray(details)) {
|
||||
await this.replaceDetails(id, details);
|
||||
}
|
||||
const fresh = await this.surveyModel.findByPk(id);
|
||||
const vo = this.toVO(fresh!);
|
||||
await this.decorate([vo]);
|
||||
vo.details = await this.getDetails(id);
|
||||
await this.logService.logAction(ctx, "勘察派单", "更新", record.surveyNo, `更新勘察派单 ${record.surveyNo}`);
|
||||
return { code: 200, message: "更新勘察派单成功", data: vo };
|
||||
}
|
||||
@@ -643,8 +609,8 @@ export class SurveyOrderService {
|
||||
if (!record) return { code: 404, message: "勘察派单不存在", data: null };
|
||||
if (!(await this.isCustomerOwner(ctx.state.user.userId, record.customerId))) return { code: 403, message: "无权限删除该勘察派单", data: null };
|
||||
const surveyNo = record.surveyNo;
|
||||
// 级联清理回执后删除工单
|
||||
await this.sopResultModel.destroy({ where: { surveyOrderId: id } });
|
||||
// 级联清理明细后删除工单
|
||||
await this.detailModel.destroy({ where: { surveyOrderId: id } });
|
||||
await record.destroy();
|
||||
await this.logService.logAction(ctx, "勘察派单", "删除", surveyNo, `删除勘察派单 ${surveyNo}`);
|
||||
return { code: 200, message: "删除勘察派单成功", data: null };
|
||||
|
||||
Reference in new issue
Block a user