zhangjq
2026-01-26 a235fc6fa66bb1905e28e531729be2c80858e1a8
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
(function (vc) {
    vc.extends({
        data: {
            importMeterWaterFeeInfo: {
                communityId: vc.getCurrentCommunity().communityId,
                excelTemplate: '',
                configId: '',
                feeConfigs: [],
                secondaryFeeTypeCds: [],
                secondaryFeeTypeCd: '',
                feeTypeCd: '',
                meterTypes: [],
                meterType: ''
            }
        },
        _initMethod: function () {
            vc.getDict('pay_fee_config', "fee_type_cd", function (_data) {
                $that.importMeterWaterFeeInfo.feeTypeCds = _data;
            });
        },
        _initEvent: function () {
            vc.on('importMeterWaterFee', 'openImportMeterWaterFeeModal',
                function (_room) {
                    $that._listImportMeterTypes();
                    $('#importMeterWaterFeeModel').modal('show');
                }
            );
        },
        methods: {
            importMeterWaterFeeValidate() {
                return vc.validate.validate({
                    importMeterWaterFeeInfo: $that.importMeterWaterFeeInfo
                }, {
                    'importMeterWaterFeeInfo.communityId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "数据异常还没有入驻小区"
                        }
                    ],
                    'importMeterWaterFeeInfo.excelTemplate': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "文件不能为空"
                        }
                    ]
                });
            },
            _importMeterWaterData: function () {
                if (!$that.importMeterWaterFeeValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                // 导入数据
                if (!$that.checkFileType($that.importMeterWaterFeeInfo.excelTemplate.name.split('.')[1])) {
                    vc.toast('不是有效的Excel格式');
                    return;
                }
                // 移除文件大小限制检查
                // if (!$that.checkFileSize($that.importMeterWaterFeeInfo.excelTemplate.size)) {
                //     vc.toast('Excel文件大小不能超过20M');
                //     return;
                // }
                let param = new FormData();
                param.append("uploadFile", $that.importMeterWaterFeeInfo.excelTemplate);
                param.append('communityId', $that.importMeterWaterFeeInfo.communityId);
                param.append('feeTypeCd', $that.importMeterWaterFeeInfo.feeTypeCd);
                param.append('configId', $that.importMeterWaterFeeInfo.configId);
                param.append('meterType', $that.importMeterWaterFeeInfo.meterType);
                param.append('userId', '-1'); // 添加userId参数
                param.append('USER-ID', '-1'); // 添加USER-ID参数(大写形式)
                // 根据费用类型选择对应的适配器
if ($that.importMeterWaterFeeInfo.feeTypeCd === '630000017') {
    // 水费使用 importMeterWaterFeeV2
    param.append('importAdapt', "importMeterWaterFeeV2");
} else if ($that.importMeterWaterFeeInfo.feeTypeCd === '630000007') {
    // 电费使用 importElectricityFeeV2
    param.append('importAdapt', "importElectricityFeeV2");
} else {
    // 其他费用使用原有适配器
    param.append('importAdapt', "importMeterWaterFee");
}
                vc.http.upload(
                    'assetImport',
                    'importData',
                    param, {
                        // 移除emulateJSON,避免与Content-Type冲突
                        headers: {
                            "Content-Type": "multipart/form-data"
                        }
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.toast("处理成功");
                            $('#importMeterWaterFeeModel').modal('hide');
                            vc.jumpToPage('/#/pages/property/assetImportLogDetail?logId=' + _json.data.logId + '&logType=importMeterWaterFee');
                            return;
                        }
                        vc.toast(_json.msg, 10000);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo, 10000);
                    });
            },
            _exportMeterWaterFeeTemplate: function () {
                let _feeTypeCd = $that.importMeterWaterFeeInfo.feeTypeCd;
                
                if (!vc.notNull(_feeTypeCd)) {
                    vc.toast('请选择费用类型');
                    return;
                }
                
                // 根据费用类型选择对应的模板
                let templateUrl = '';
                if (_feeTypeCd === '630000017') {
                    // 水费模板(对应 sheet:水费流水模板)
                    templateUrl = '/import/Z4水费流水模版.xlsx';
                } else if (_feeTypeCd === '630000007') {
                    // 电费模板(对应 sheet:电费流水模版)
                    templateUrl = '/import/Z4电费流水模版.xlsx'; // 使用正确的文件名,与实际文件名匹配
                } else {
                    // 其他费用使用默认模板
                    templateUrl = '/import/抄表导入模板.xlsx';
                }
                
                // 直接下载模板文件
                window.open(templateUrl, '_blank');
            },
            _listImportMeterTypes: function (_page, _rows) {
                var param = {
                    params: {
                        page: 1,
                        row: 50,
                        communityId: vc.getCurrentCommunity().communityId
                    }
                };
                //发送get请求
                vc.http.apiGet('meterType.listMeterType',
                    param,
                    function (json, res) {
                        var _meterTypeManageInfo = JSON.parse(json);
                        $that.importMeterWaterFeeInfo.meterTypes = _meterTypeManageInfo.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            clearAddFeeConfigInfo: function () {
                $that.importMeterWaterFeeInfo = {
                    communityId: vc.getCurrentCommunity().communityId,
                    excelTemplate: '',
                    secondaryFeeTypeCd: '',
                    configId: '',
                    feeConfigs: [],
                    feeTypeCd: '',
                    meterTypes: [],
                    meterType: ''
                };
            },
            getExcelTemplate: function (e) {
                // 清除旧的文件对象,确保获取最新的文件
                $that.importMeterWaterFeeInfo.excelTemplate = null;
                
                // 获取用户选择的文件
                let file = e.target.files[0];
                if (!file) {
                    vc.toast('请选择文件');
                    return;
                }
                
                // 显示文件信息,方便调试
                console.log('选择的文件:', file.name, '大小:', file.size, '最后修改时间:', file.lastModified);
                
                // 保存文件对象到组件数据中
                $that.importMeterWaterFeeInfo.excelTemplate = file;
                
                // 重置input[type=file]的value,确保用户修改文件后再次选择同一文件时能触发change事件
                // 注意:这个重置必须在获取文件对象之后执行
                setTimeout(function() {
                    e.target.value = '';
                }, 100);
            },
            checkFileType: function (fileType) {
                const acceptTypes = ['xlsx'];
                for (var i = 0; i < acceptTypes.length; i++) {
                    if (fileType === acceptTypes[i]) {
                        return true;
                    }
                }
                return false;
            },
            checkFileSize: function (fileSize) {
                //2M
                const MAX_SIZE = 20 * 1024 * 1024;
                if (fileSize > MAX_SIZE) {
                    return false;
                }
                return true;
            },
            _changeImportMeterWaterFeeTypeCd:  function (_feeTypeCd){ 
                console.log('开始处理费用类型变更,feeTypeCd:', _feeTypeCd);
                
                // 立即清空当前选择,避免用户看到旧数据
                $that.importMeterWaterFeeInfo.secondaryFeeTypeCd = '';
                $that.importMeterWaterFeeInfo.configId = '';
                
                // 清空费用子类型数组,避免显示旧数据
                $that.$set($that.importMeterWaterFeeInfo, 'secondaryFeeTypeCds', []);
                
                // 添加加载状态提示
                vc.toast('正在获取费用子类型...', { duration: 500 });
                
                let param = {
                    params: {
                        feeTypeCd: _feeTypeCd,
                        communityId: vc.getCurrentCommunity().communityId
                    }
                };
                
                // 立即调用收费项目查询,传入空的费用子类型
                // 确保收费项目列表也能及时更新
                $that._changeSecondaryFeeTypeCdx(_feeTypeCd, '');
                
                vc.http.apiGet('/secondaryFeeTypeCdGl.list.show', param,
                    function (json, res) {
                        console.log('费用子类型API返回原始数据:', json);
                        
                        try {
                            let result = JSON.parse(json);
                            
                            // 处理数据,支持多种返回格式
                            let dataArray = [];
                            if (result && result.code == 0 && Array.isArray(result.data)) {
                                // 标准组件API返回格式
                                dataArray = result.data;
                            } else if (Array.isArray(result)) {
                                // 直接数组格式
                                dataArray = result;
                            } else if (result && typeof result === 'object') {
                                // 单对象格式
                                dataArray = [result];
                            }
                            
                            console.log('费用子类型原始数据数组长度:', dataArray.length);
                            
                            // 去重处理,确保费用子类型不重复
                            let secondaryFeeTypeCds = [];
                            let seenSecondaryFeeTypeCds = new Set();
                            
                            for (let i = 0; i < dataArray.length; i++) {
                                let item = dataArray[i];
                                console.log('处理费用子类型项:', i, item);
                                
                                // 严格验证数据项
                                if (item && typeof item === 'object' && item.secondaryFeeTypeCd) {
                                    // 确保secondaryFeeTypeName存在
                                    item.secondaryFeeTypeName = item.secondaryFeeTypeName || item.secondaryFeeTypeCd;
                                    
                                    if (!seenSecondaryFeeTypeCds.has(item.secondaryFeeTypeCd)) {
                                        seenSecondaryFeeTypeCds.add(item.secondaryFeeTypeCd);
                                        secondaryFeeTypeCds.push(item);
                                        console.log('添加费用子类型:', item.secondaryFeeTypeCd, item.secondaryFeeTypeName);
                                    } else {
                                        console.log('跳过重复费用子类型:', item.secondaryFeeTypeCd);
                                    }
                                } else {
                                    console.log('跳过无效费用子类型项:', item);
                                }
                            }
                            
                            console.log('费用子类型数据去重后,共', secondaryFeeTypeCds.length, '条数据');
                            
                            // 确保组件状态更新可靠
                            if (secondaryFeeTypeCds.length > 0) {
                                // 使用去重后的数据,确保数据稳定
                                $that.$set($that.importMeterWaterFeeInfo, 'secondaryFeeTypeCds', secondaryFeeTypeCds);
                                
                                // 自动选择第一个费用子类型
                                $that.importMeterWaterFeeInfo.secondaryFeeTypeCd = secondaryFeeTypeCds[0].secondaryFeeTypeCd;
                                
                                console.log('费用子类型数据处理完成,当前选择:', $that.importMeterWaterFeeInfo.secondaryFeeTypeCd);
                                
                                // 重新调用收费项目查询,传入新选择的费用子类型
                                // 确保收费项目列表与当前选择的费用子类型匹配
                                $that._changeSecondaryFeeTypeCdx(_feeTypeCd, $that.importMeterWaterFeeInfo.secondaryFeeTypeCd);
                            } else {
                                // 没有可用的费用子类型数据,显示友好提示
                                let noDataOption = {
                                    secondaryFeeTypeCd: "",
                                    secondaryFeeTypeName: "无关联子类型,请直接选择收费项目"
                                };
                                $that.$set($that.importMeterWaterFeeInfo, 'secondaryFeeTypeCds', [noDataOption]);
                                $that.importMeterWaterFeeInfo.secondaryFeeTypeCd = '';
                                
                                console.log('没有可用的费用子类型数据');
                            }
                        } catch (e) {
                            // 解析失败,显示友好错误信息
                            console.error('解析费用子类型失败:', e, json);
                            vc.toast('获取费用子类型失败: 数据格式错误');
                            
                            // 恢复到初始状态
                            $that.$set($that.importMeterWaterFeeInfo, 'secondaryFeeTypeCds', [{
                                secondaryFeeTypeCd: "",
                                secondaryFeeTypeName: "获取费用子类型失败,请直接选择收费项目"
                            }]);
                            $that.importMeterWaterFeeInfo.secondaryFeeTypeCd = '';
                        }
                    },
                    function (errInfo, error) {
                        console.error('获取费用子类型失败:', errInfo, error);
                        
                        // 显示友好错误信息
                        vc.toast('获取费用子类型失败: 网络异常');
                        
                        // 恢复到初始状态
                        $that.$set($that.importMeterWaterFeeInfo, 'secondaryFeeTypeCds', [{
                            secondaryFeeTypeCd: "",
                            secondaryFeeTypeName: "获取费用子类型失败,请直接选择收费项目"
                        }]);
                        $that.importMeterWaterFeeInfo.secondaryFeeTypeCd = '';
                    }
                );
            },
            _changeSecondaryFeeTypeCdx: function(_feeTypeCd,_secondaryFeeTypeCd) {
                $that.importMeterWaterFeeInfo.configId = '';
                // 使用当前选择的费用子类型,而不是传入的参数
                var currentSecondaryFeeTypeCd = $that.importMeterWaterFeeInfo.secondaryFeeTypeCd;
                var param = {
                    params: {
                        page: 1,
                        row: 20,
                        communityId: vc.getCurrentCommunity().communityId,
                        feeTypeCd: _feeTypeCd,
                        secondaryFeeTypeCd: currentSecondaryFeeTypeCd,
                        isDefault: 'F',
                        valid: '1'
                    }
                };
                //发送get请求
                vc.http.apiGet('/feeConfig.listFeeConfigs', param,
                    function (json, res) {
                        var _feeConfigManageInfo = JSON.parse(json);
                        $that.importMeterWaterFeeInfo.feeConfigs = _feeConfigManageInfo.feeConfigs;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
        }
    });
})(window.vc);