package com.cn.config;

import com.cn.model.*;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import org.apache.commons.lang3.StringUtils;
import org.jim.common.utils.Prop;
import org.jim.common.utils.PropUtil;

/**
 * @author Darren
 * @date 2020/1/30 19:30
 */
public class PropertyDataBaseConfigBuilder {

    private static Prop prop;

    private PropertyDataBaseConfigBuilder() {
    }

    public static void init(String file){
        if(StringUtils.isEmpty(file)){
            throw new RuntimeException("file is Empty!");
        }
        prop = PropUtil.use(file);
        //创建 Druid 数据库连接池 (jfinal)
        DruidPlugin druidPlugin = new DruidPlugin(prop.get("jdbcUrl"), prop.get("user"), prop.get("password"));
        //设置连接池相关数据
        druidPlugin.set(prop.getInt("initialSize"),prop.getInt("minIdle"),prop.getInt("maxActive"));
        druidPlugin.setConnectionInitSql("set names utf8mb4");

        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        //设置实体 表 关系映射
        arp.addMapping("sw_conversation","id", Conversation.class);
        arp.addMapping("sw_conversation_record","id", ConversationRecord.class);
        arp.addMapping("sw_service_account","id", ServiceAccount.class);
        arp.addMapping("sw_role_department_middle","id", RoleDepartmentMiddle.class);
        arp.addMapping(
                "sw_service_account_role_department_middle"
                ,"id"
                , ServiceAccountRoleDepartmentMiddle.class
        );
        arp.addMapping("sw_group","group_id", ChatGroup.class);


        // 与 jfinal web 环境唯一的不同是要手动调用一次相关插件的start()方法
        druidPlugin.start();
        arp.start();

    }



}
