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  'video_applet_product_controllers:book_id ID is required:'+params;
            }

            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 getAppletProductDataByStatus(params) {
        try {
            // 参数验证
            // if (!params.status||!params?.wait_status) {
            //     throw  'video_applet_product_controllers:book_id ID is required';
            // }

            const filterData = await video_applet_product_model.findOne({ status: params.status,wait_status:params.wait_status,book_platform:params.book_platform });

            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?.id) {
                throw 'ID is required';
            }


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


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

            // 使用乐观锁更新
            const affectedRows = await video_applet_product_model.updateData(
                params.id,
                tableData
            );

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

    async getAppletProductByStatusAndLimit(params,limit=1) {
        try {
            // 参数验证
            if (params.status==undefined||params.status==null) {
                throw  'getAppletProductByStatusAndLimit:status is required';
            }

            const status = params.status;
            const filterData = await video_applet_product_model.findDataByLimit({ status: status,wait_status :params.wait_status},limit);

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

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

            throw error;
        }
    }
}

module.exports =  new video_applet_product_controllers();