
const get_fq_book_tab_model =  require('../Model/get_fq_book_tab_model');
class get_fq_book_tab_controllers {
    async getData(params) {
        try {
            // 参数验证
            if (!params?.book_id) {
                throw  'get_fq_book_tab_controllers:book_id ID is required';
            }

            const book_id = params.book_id;
            const filterData = await get_fq_book_tab_model.findOne({ book_key: book_id });

            if (!filterData) {
                return {
                    success: false,
                    data: null
                };
            }

            return {
                success: true,
                data: filterData
            };
        } catch (error) {

            throw error;
        }
    }

    async createData(tableData) {
        try {
            // 数据验证
            if (!tableData || Object.keys(tableData).length === 0) {
                throw 'Invalid table data';
            }

            // 检查必要字段
            const requiredFields = ['book_key', 'book_id', 'info']; // 替换为实际必要字段
            for (const field of requiredFields) {
                if (tableData[field]==null||tableData[field]==undefined) {
                    throw `Missing required field: ${field}${tableData}`;
                }
            }

            // 添加创建时间戳
            // const dataToCreate = {
            //     ...tableData
            //     // created_at: new Date(),
            //     // updated_at: new Date()
            // };

            const id = await get_fq_book_tab_model.createData(tableData);


            return {
                success: true,
                data: { id }
            };
        } catch (error) {
            throw error;
        }
    }
}

module.exports =  new get_fq_book_tab_controllers();