"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initUrgentQuota = void 0;
var tslib_1 = require("tslib");
exports.initUrgentQuota = {
    rest: {
        method: 'POST',
        fullPath: '/api/pepsico-content/urgent-quota/init'
    },
    handler: function (ctx) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var user, space, _a, year, h1_used, h2_used, h1, h2, spaceUsers, ownerIds, created, updated, ownerIds_1, ownerIds_1_1, userId, existing, e_1_1;
            var e_1, _b;
            return tslib_1.__generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        ctx.getObject = this.getObject;
                        user = ctx.meta.user;
                        space = user === null || user === void 0 ? void 0 : user.spaceId;
                        if (!space) {
                            throw new Error('无法获取当前工作区');
                        }
                        _a = ctx.params, year = _a.year, h1_used = _a.h1_used, h2_used = _a.h2_used;
                        if (!year || typeof year !== 'number') {
                            throw new Error('请指定有效的年份');
                        }
                        h1 = typeof h1_used === 'number' ? h1_used : 0;
                        h2 = typeof h2_used === 'number' ? h2_used : 0;
                        return [4, ctx.getObject('space_users').directFind({
                                filters: [['space', '=', space], ['user_accepted', '=', true], ['is_collaborator', '!=', true]],
                                fields: ['user']
                            })];
                    case 1:
                        spaceUsers = _c.sent();
                        ownerIds = tslib_1.__spreadArray([], tslib_1.__read(new Set(spaceUsers.map(function (u) { return u.user; }).filter(Boolean))), false);
                        if (ownerIds.length === 0) {
                            return [2, { message: '未找到任何内部用户', created: 0, updated: 0 }];
                        }
                        created = 0;
                        updated = 0;
                        _c.label = 2;
                    case 2:
                        _c.trys.push([2, 10, 11, 12]);
                        ownerIds_1 = tslib_1.__values(ownerIds), ownerIds_1_1 = ownerIds_1.next();
                        _c.label = 3;
                    case 3:
                        if (!!ownerIds_1_1.done) return [3, 9];
                        userId = ownerIds_1_1.value;
                        return [4, ctx.getObject('pepsico_urgent_quota').findOne({
                                filters: [['user', '=', userId], ['space', '=', space], ['year', '=', year]]
                            })];
                    case 4:
                        existing = _c.sent();
                        if (!existing) return [3, 6];
                        return [4, ctx.getObject('pepsico_urgent_quota').directUpdate(existing._id, {
                                h1_used: h1,
                                h2_used: h2
                            })];
                    case 5:
                        _c.sent();
                        updated++;
                        return [3, 8];
                    case 6: return [4, ctx.getObject('pepsico_urgent_quota').insert({
                            user: userId,
                            space: space,
                            year: year,
                            h1_used: h1,
                            h2_used: h2
                        })];
                    case 7:
                        _c.sent();
                        created++;
                        _c.label = 8;
                    case 8:
                        ownerIds_1_1 = ownerIds_1.next();
                        return [3, 3];
                    case 9: return [3, 12];
                    case 10:
                        e_1_1 = _c.sent();
                        e_1 = { error: e_1_1 };
                        return [3, 12];
                    case 11:
                        try {
                            if (ownerIds_1_1 && !ownerIds_1_1.done && (_b = ownerIds_1.return)) _b.call(ownerIds_1);
                        }
                        finally { if (e_1) throw e_1.error; }
                        return [7];
                    case 12: return [2, {
                            message: "\u521D\u59CB\u5316\u5B8C\u6210\uFF1A".concat(created, " \u6761\u65B0\u5EFA\uFF0C").concat(updated, " \u6761\u66F4\u65B0"),
                            created: created,
                            updated: updated,
                            total: ownerIds.length
                        }];
                }
            });
        });
    }
};
//# sourceMappingURL=initUrgentQuota.js.map