const video_applet_product_model =  require('../Model/video_applet_product_model');
class video_applet_product_controllers {
    async getAppletProductData(params) {
        try {
            // 参数验证
            if (!params?.book_id||!params?.main_id) {
                throw  'book_id ID is required';
            }

            const book_id = params.book_id;
            const filterData = await video_applet_product_model.findOne({ product_id: book_id,main_id:params.main_id });

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

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

            throw error;
        }
    }

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

            // 检查必要字段
            const requiredFields = ['book_platform', 'product_name', 'product_id', 'dy_small_program_start', 'dy_small_program_start_data', 
                'dy_small_applet_app_id', 'main_id', 'promotion_id']; // 替换为实际必要字段
            for (const field of requiredFields) {
                if (tableData[field]==null||tableData[field]==undefined) {
                    throw `Missing required field: ${field}`;
                }
            }

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

            const id = await video_applet_product_model.createData(tableData);


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


    async updateData(params, tableData) {
        try {
            // 参数验证
            if (!params?.book_id) {
                throw 'ID is required';
            }

            if (!params?.main_id) {
                throw 'ID is required';
            }

            if (!tableData || Object.keys(tableData).length === 0) {
                throw 'No data to update';
            }

            const book_id = params.book_id;

            const main_id = params.main_id;

            // 检查数据是否存在
            const existingData = await origin_data_model.findOne({ product_id:book_id,main_id:main_id });
            if (!existingData) {
                throw 'Filter data not found';
            }

            // 添加更新时间戳
            // const dataToUpdtableDataate = {
            //     ...,
            // };

            // 使用乐观锁更新
            const affectedRows = await origin_data_model.updateFilterData(
                id,
                dataToUpdate,
                existingData.version // 假设使用版本控制
            );

            if (affectedRows === 0) {
                throw 'Data was modified by another process';
            }
            return {
                success: true,
                message: 'Filter data updated successfully'
            };
        } catch (error) {
            throw error;
        }
    }
}

module.exports =  new video_applet_product_controllers();