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 findAllRecord(pobj) {
        let pageSize = Number(pobj.pageInfo.pageSize || 20);
        if (pageSize > 50) {
            pageSize = 50;
        }
        var pageIndex = Number(pobj.pageInfo.pageNo || 1);
        var from = pageIndex == 1 ? 0 : Number((pageIndex - 1) * pageSize);
        let sql = `select record.created_at,info.user_name name,info.notes,record.record_status_name from c_form_submit_record record left join  c_form_info info on info.id = record.form_id where info.deleted_at is NULL`;
        let countSql = `select count(*) count from c_form_submit_record record left join  c_form_info info on info.id = record.form_id where info.deleted_at is NULL`;
        if(pobj.record_status_name){
            sql += ` AND record.record_status_name = '${pobj.record_status_name}'`
            countSql += ` AND record.record_status_name = '${pobj.record_status_name}'`
        }
        sql += ` order by info.id desc LIMIT ${pageSize} OFFSET ${from}`;
        countSql += ` order by info.id desc LIMIT ${pageSize} OFFSET ${from}`;
        let result = await this.customQuery(sql);
        let countResult = await this.customQuery(countSql);
        let resData ={
            results:{
                count:countResult[0].count,
                rows:result
            }
        }
        return system.getResult(resData);
    }
}

module.exports = ForminfoService;