"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateSpotCheckData = void 0;
var tslib_1 = require("tslib");
exports.migrateSpotCheckData = {
    rest: {
        method: 'POST',
        fullPath: '/api/pepsico-content/material/migrate-spot-check-data'
    },
    handler: function (ctx) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var user, logger, materials, migrated, skipped, materials_1, materials_1_1, material, existingRecord, department, spaceUser, error_1, e_1_1, error_2;
            var e_1, _a;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        user = ctx.meta.user;
                        ctx.getObject = this.getObject;
                        return [4, this.getLogger()];
                    case 1:
                        logger = _b.sent();
                        ctx.logger = logger;
                        if (!user || !user.is_space_admin) {
                            return [2, {
                                    status: 403,
                                    msg: "只有管理员可以执行数据迁移",
                                    data: {}
                                }];
                        }
                        _b.label = 2;
                    case 2:
                        _b.trys.push([2, 18, , 19]);
                        return [4, ctx.getObject('pepsico_material').find({
                                filters: [
                                    ['spot_checked', '=', true],
                                    ['pepsico_spot_check', '!=', null]
                                ],
                                fields: ['_id', 'pepsico_spot_check', 'spot_check_time', 'pepsico_spot_check_status', 'spot_description', 'space']
                            })];
                    case 3:
                        materials = _b.sent();
                        logger === null || logger === void 0 ? void 0 : logger.info("Found ".concat(materials.length, " materials with spot check data to migrate"));
                        migrated = 0;
                        skipped = 0;
                        _b.label = 4;
                    case 4:
                        _b.trys.push([4, 15, 16, 17]);
                        materials_1 = tslib_1.__values(materials), materials_1_1 = materials_1.next();
                        _b.label = 5;
                    case 5:
                        if (!!materials_1_1.done) return [3, 14];
                        material = materials_1_1.value;
                        return [4, ctx.getObject('pepsico_material_spot_check_record').findOne({
                                filters: [
                                    ['pepsico_material', '=', material._id],
                                    ['spot_checker', '=', material.pepsico_spot_check]
                                ]
                            })];
                    case 6:
                        existingRecord = _b.sent();
                        if (existingRecord) {
                            skipped++;
                            return [3, 13];
                        }
                        department = '';
                        if (!material.pepsico_spot_check) return [3, 10];
                        _b.label = 7;
                    case 7:
                        _b.trys.push([7, 9, , 10]);
                        return [4, ctx.getObject('space_users').findOne({
                                filters: ['user', '=', material.pepsico_spot_check]
                            })];
                    case 8:
                        spaceUser = _b.sent();
                        department = (spaceUser === null || spaceUser === void 0 ? void 0 : spaceUser.organization) || '';
                        return [3, 10];
                    case 9:
                        error_1 = _b.sent();
                        logger === null || logger === void 0 ? void 0 : logger.error("Error getting department for user ".concat(material.pepsico_spot_check), error_1);
                        return [3, 10];
                    case 10: return [4, ctx.getObject('pepsico_material_spot_check_record').insert({
                            pepsico_material: material._id,
                            spot_checker: material.pepsico_spot_check,
                            spot_check_department: department,
                            spot_check_time: material.spot_check_time || new Date(),
                            spot_check_status: material.pepsico_spot_check_status !== undefined ? material.pepsico_spot_check_status : true,
                            spot_check_opinion: material.spot_description || '',
                            space: material.space
                        }, user)];
                    case 11:
                        _b.sent();
                        return [4, ctx.getObject('pepsico_material').directUpdate(material._id, {
                                spot_checkers: [material.pepsico_spot_check],
                                spot_check_departments: department ? [department] : [],
                                spot_check_overall_status: 'checked'
                            })];
                    case 12:
                        _b.sent();
                        migrated++;
                        if (migrated % 100 === 0) {
                            logger === null || logger === void 0 ? void 0 : logger.info("Migrated ".concat(migrated, " records..."));
                        }
                        _b.label = 13;
                    case 13:
                        materials_1_1 = materials_1.next();
                        return [3, 5];
                    case 14: return [3, 17];
                    case 15:
                        e_1_1 = _b.sent();
                        e_1 = { error: e_1_1 };
                        return [3, 17];
                    case 16:
                        try {
                            if (materials_1_1 && !materials_1_1.done && (_a = materials_1.return)) _a.call(materials_1);
                        }
                        finally { if (e_1) throw e_1.error; }
                        return [7];
                    case 17:
                        logger === null || logger === void 0 ? void 0 : logger.info("Migration complete. Migrated: ".concat(migrated, ", Skipped: ").concat(skipped));
                        return [2, {
                                status: 0,
                                msg: "数据迁移完成",
                                data: {
                                    migrated: migrated,
                                    skipped: skipped,
                                    total: materials.length
                                }
                            }];
                    case 18:
                        error_2 = _b.sent();
                        logger === null || logger === void 0 ? void 0 : logger.error('Migration failed', error_2);
                        return [2, {
                                status: 500,
                                msg: "\u6570\u636E\u8FC1\u79FB\u5931\u8D25: ".concat(error_2.message),
                                data: {}
                            }];
                    case 19: return [2];
                }
            });
        });
    }
};
//# sourceMappingURL=migrateSpotCheckData.js.map