
const checks_products_model =  require('../Model/checks_products_model');
class checks_products_controllers {

    async  queryArrayValue(key,searchValue) {
        try {
            // 参数验证
            const filterData = await checks_products_model.queryArrayValue(key,searchValue);

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

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

            throw error;
        }

    }
    async getData(params) {
        try {
            // 参数验证
            if (!params?.main_id) {
                throw  'checks_products_controllers:main_id ID is required';
            }
            const filterData = await checks_products_model.findOne({ main_id: params.main_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 checks_products_model.createData(tableData);


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

module.exports =  new checks_products_controllers();