package com.sifang.modules.sys.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.entity.FormSet;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class FormSetDao extends BaseDao<FormSet> {

    /**
     * 根据条件查询表单设置
     */
    public List<Record> findListByCondition(String stage, String partner) {
        return findListByCondition(stage, partner, null);
    }

    /**
     * 根据条件查询表单设置
     */
    public List<Record> findListByCondition(String stage, String partner, String groupType) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT s.column_key AS columnKey,s.must_fill_flag AS parentMustFillFlag,s.column_type AS columnType,");
        sql.line("ps.limit_flag AS limitFlag,ps.must_fill_flag AS mustFillFlag,ps.status,ps.id");
        sql.line("FROM crm_form_set s");
        sql.line("INNER JOIN crm_form_partner_set ps ON ps.form_set_id = s.id AND ps.partner = ?", partner);
        sql.line("WHERE s.stage = ?", stage);
        if (StringUtil.isNotBlank(groupType)) {
            sql.line("AND s.group_type = ?", groupType);
        }
        sql.line("AND s.is_show = '1'");
        sql.line("ORDER BY s.sort");
        return Db.find(sql.build(), sql.params());
    }

}
