
const spreads_model =  require('../Model/spreads_model');
class spreads_controllers {

    async getData(params) {
        try {
            // 参数验证
            if (!params?.cp_spread_id) {
                throw  'get_fq_book_tab_controllers:book_id ID is required';
            }
            const filterData = await spreads_model.findOne({ cp_spread_id: params?.cp_spread_id,cp_type: params?.cp_type,product_id: params?.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', 'name', 'cp_type', 'cp_spread_id', 'work_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 spreads_model.createData(tableData);


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

module.exports =  new spreads_controllers();