
const works_model =  require('../Model/works_model');
class works_controllers {

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

            const product_id = params.product_id;
            const filterData = await works_model.findOne({ cp_work_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 = ['cp_work_id', 'cp_type', 'name']; 
            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 works_model.createWorkData(tableData);


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

module.exports =  new works_controllers();