const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const settings = require("../../../../config/settings");

class ForminfoService extends ServiceBase {
    constructor() {
        super("configmag", ServiceBase.getDaoName(ForminfoService));
    }

    /**
     * 根据company_id 查询
     * @param pobj
     * @returns {Promise<void>}
     */
    async findByCompanyId(pobj) {
        if (pobj.company_id) {
            pobj.search.company_id = pobj.company_id;
        }
        let result = await this.findAndCountAll(pobj);
        return system.getResult(result);
    }

    /**
     * 创建表单
     * @param pobj
     * @returns {Promise<void>}
     */
    async createForm(pobj){
        if(!pobj.name){
            return system.getResult(null,'name can not be empty');
        }
        if(!pobj.form_describe){
            return system.getResult(null,'form_describe can not be empty');
        }
        let items = '';
        // if(!pobj.form_item){
        //     return 'form_item can not be empty';
        // }
        const formItem = this.composeItem(items);
        if(formItem.status != 0){
            return formItem;
        }
        let code = await this.getBusUid("fm")
        let formData = {
            name:pobj.name,
            code:code,
            form_describe:pobj.form_describe,
            form_item:formItem.data,
            user_id:pobj.user_id,
            user_name:pobj.username
        }
        let result = await this.create(formData);
        return system.getResul(result);
    }
    /**
     *
     * @param pobj
     * @returns {Promise<void>}
     */
    async updateForm(pobj) {

        return null;
    }

    //表单项 数据重组
    async composeItem(obj){
        return system.getResultSuccess();
    }
}

module.exports = ForminfoService;