import { type Page } from '@playwright/test'; import { reportData } from './reportPage'; export class CardBalanceChangeReportPage { page: Page; private readonly _reportIndex: reportData[]; /** * 储值卡卡金变动表 */ constructor(page: Page) { this.page = page; this._reportIndex = [ { name: '期初', content: [ { name: '期初卡金', index: -1, value: 0, lastValue: 0, }, { name: '期初赠金', index: -1, value: 0, lastValue: 0, }, ], }, { name: '消费', content: [ { name: '消费卡金', index: -1, value: 0, lastValue: 0, }, { name: '消费赠金', index: -1, value: 0, lastValue: 0, }, ], }, { name: '开充卡', content: [ { name: '开充卡金', index: -1, value: 0, lastValue: 0, }, { name: '开充赠金', index: -1, value: 0, lastValue: 0, }, ], }, { name: '调整', content: [ { name: '修改卡金', index: -1, value: 0, lastValue: 0, }, { name: '修改赠金', index: -1, value: 0, lastValue: 0, }, ], }, { name: '转移', content: [ { name: '转移卡金', index: -1, value: 0, lastValue: 0, }, { name: '转移赠金', index: -1, value: 0, lastValue: 0, }, ], }, { name: '结余', content: [ { name: '结余卡金', index: -1, value: 0, lastValue: 0, }, { name: '结余赠金', index: -1, value: 0, lastValue: 0, }, ], }, { name: '变动', content: [ { name: '卡金变动', index: -1, value: 0, lastValue: 0, }, { name: '赠金变动', index: -1, value: 0, lastValue: 0, }, ], }, ]; } get reportIndex() { return this._reportIndex; } /** * 更新报表数据索引 */ updateReportDataIndex = async () => { const thLocator = this.page.locator('.m-table__header-wrapper tr').last().locator('th'); await thLocator.first().waitFor(); const headers = await thLocator.allInnerTexts(); for (const category of this._reportIndex) { for (const content of category.content!) { // 第一列是会员卡名称,故+1 content.index = headers.findIndex(headerText => { return headerText === content.name; }) + 1; } } }; /** * 获取指定卡的报表数据 * @param cardName 卡名称 * @param cardData 卡数据 */ getSpecifyCardReportData = async (cardName: string, cardData: reportData[]) => { const tdLocator = this.page .locator('.table_sub-box .m-table__body-wrapper tr', { hasText: cardName }) .first() .locator('td'); await tdLocator.first().waitFor(); const headers = await tdLocator.allInnerTexts(); for (const category of this._reportIndex) { const cardCategoryIndex = cardData.findIndex(card => card.name === category.name); if (cardCategoryIndex === -1) { // 为空时初始化一个对象 const obj = { name: category.name, content: category.content?.map(item => ({ ...item, value: 0, lastValue: 0 })), }; cardData.push(obj); } const cardCategory = cardData.find(card => card.name === category.name); if (!cardCategory || !cardCategory.content) { throw new Error(`cardCategory:${JSON.stringify(cardCategory)}`); } for (const content of cardCategory.content) { content.lastValue = content.value; content.value = Number(headers[content.index!] === '--' ? 0 : headers[content.index!]); } } }; }