快乐的昕的电脑
2025-11-24 6fa86a03230c06b2bc5cfbf4023f5232cd89a1ca
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
// 设备点检接口辅助方法,预留后台接入并提供本地缓存降级逻辑
const STORAGE_PREFIX = 'equipment_inspection_cache_'; // 本地缓存前缀,便于调试阶段存储
const DAY_COUNT = 31;
const DAILY_ITEM_COUNT = 6;  // 日常点检项目数量
const MONTHLY_ITEM_COUNT = 2;  // 月度点检项目数量
 
function buildDefaultRecord() {
    return {
        dailyChecks: Array(DAILY_ITEM_COUNT).fill(null).map(() => Array(DAY_COUNT).fill(false)),
        monthlyChecks: Array(MONTHLY_ITEM_COUNT).fill(null).map(() => Array(DAY_COUNT).fill(false))
    };
}
 
function buildStorageKey(machineNo, year) {
    return `${STORAGE_PREFIX}${machineNo || 'unknown'}_${year}`;
}
 
function normalizeResponse(payload) {
    if (!payload) {
        return buildDefaultRecord();
    }
 
    // 验证并规范化日常点检数据(6项×31天)
    let dailyChecks = [];
    if (Array.isArray(payload.dailyChecks)) {
        dailyChecks = payload.dailyChecks.slice(0, DAILY_ITEM_COUNT).map(item => {
            if (Array.isArray(item)) {
                // 确保每项都是31天的数组
                const normalized = item.slice(0, DAY_COUNT);
                while (normalized.length < DAY_COUNT) {
                    normalized.push(false);
                }
                return normalized;
            }
            // 如果不是数组,返回全false的31天数组
            return Array(DAY_COUNT).fill(false);
        });
    }
    // 补齐到6项
    while (dailyChecks.length < DAILY_ITEM_COUNT) {
        dailyChecks.push(Array(DAY_COUNT).fill(false));
    }
 
    // 验证并规范化月度点检数据(2项×31天)
    let monthlyChecks = [];
    if (Array.isArray(payload.monthlyChecks)) {
        monthlyChecks = payload.monthlyChecks.slice(0, MONTHLY_ITEM_COUNT).map(item => {
            if (Array.isArray(item)) {
                // 确保每项都是31天的数组
                const normalized = item.slice(0, DAY_COUNT);
                while (normalized.length < DAY_COUNT) {
                    normalized.push(false);
                }
                return normalized;
            }
            // 如果不是数组,返回全false的31天数组
            return Array(DAY_COUNT).fill(false);
        });
    }
    // 补齐到2项
    while (monthlyChecks.length < MONTHLY_ITEM_COUNT) {
        monthlyChecks.push(Array(DAY_COUNT).fill(false));
    }
 
    return {
        dailyChecks,
        monthlyChecks
    };
}
 
export async function queryEquipmentInspection(vueCtx, { machineNo, date }, options = {}) {
    const { mock = true, showLoading = false } = options;
    const params = {
        url: '/EquipmentInspection/Query',
        data: { machineNo, date },
        showLoading
    };
 
    if (!vueCtx || typeof vueCtx.$post !== 'function') {
        throw new Error('queryEquipmentInspection 需要传入 Vue 实例以便调用 $post');
    }
 
    try {
        // 预留后台 POST 接口,请求成功后直接返回服务端数据
        const response = await vueCtx.$post(params);
 
        // 处理标准的 MES API 响应格式 { status: 0, data: {...} }
        if (response && response.status === 0 && response.data) {
            return normalizeResponse(response.data);
        }
 
        // 处理 { success: true, data: {...} } 格式
        if (response && response.success && response.data) {
            return normalizeResponse(response.data);
        }
 
        // 处理直接返回数据的情况
        if (response && (response.dailyChecks || response.monthlyChecks)) {
            return normalizeResponse(response);
        }
    } catch (error) {
        // 后台尚未接入时降级使用本地缓存
        if (!mock) {
            throw error;
        }
    }
 
    if (mock) {
        // 从 date (yyyy-MM) 提取年份用于缓存key
        const year = date ? date.split('-')[0] : new Date().getFullYear();
        const cacheKey = buildStorageKey(machineNo, year);
        const cache = uni.getStorageSync(cacheKey);
        if (cache) {
            try {
                return normalizeResponse(JSON.parse(cache));
            } catch (err) {
                console.error('解析设备点检缓存失败', err);
            }
        }
        return buildDefaultRecord();
    }
 
    return buildDefaultRecord();
}
 
export async function saveEquipmentInspection(vueCtx, { machineNo, date, dailyChecks, monthlyChecks }, options = {}) {
    const { mock = true, showLoading = true } = options;
    const payload = {
        machineNo,
        date,
        dailyChecks,
        monthlyChecks
    };
 
    if (!vueCtx || typeof vueCtx.$post !== 'function') {
        throw new Error('saveEquipmentInspection 需要传入 Vue 实例以便调用 $post');
    }
 
    try {
        // 预留后台保存接口,接口应返回 { success: true } 结构
        const response = await vueCtx.$post({
            url: '/EquipmentInspection/Save',
            data: payload,
            showLoading
        });
        if (response && response.success) {
            return response;
        }
        if (!mock) {
            return response;
        }
    } catch (error) {
        if (!mock) {
            throw error;
        }
    }
 
    if (mock) {
        // 本地缓存模拟保存,便于前端演示与联调
        // 从 date (yyyy-MM) 提取年份用于缓存key
        const year = date ? date.split('-')[0] : new Date().getFullYear();
        const cacheKey = buildStorageKey(machineNo, year);
        uni.setStorageSync(cacheKey, JSON.stringify(payload));
        return { success: true, message: '已保存至本地缓存' };
    }
 
    return { success: false };
}