
const other_book_model =  require('../Model/other_book_model');
class other_book_controllers {

    async getData(params) {
        try {
            // 参数验证
            if (!params?.product_id) {
                throw  'other_book_controllers:book_id ID is required';
            }

            const product_id = params.product_id;
            const filterData = await other_book_model.findOne({ product_id: product_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 = ['product_id', 'product_name', 'tg_platform_id']; 
            for (const field of requiredFields) {
                if (tableData[field]==null||tableData[field]==undefined) {
                    throw `Missing required field: ${field}${JSON.stringify(tableData)}`;
                }
            }

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

            const id = await other_book_model.createData(tableData);


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

module.exports =  new other_book_controllers();