package models

import (
	"github.com/beego/beego/v2/client/orm"
	_ "github.com/go-sql-driver/mysql"
	"hongze/hongze_open_api/models/tables/admin"
	"hongze/hongze_open_api/models/tables/article"
	"hongze/hongze_open_api/models/tables/company"
	"hongze/hongze_open_api/models/tables/company_product"
	"hongze/hongze_open_api/models/tables/cygx_activity"
	"hongze/hongze_open_api/models/tables/day_new"
	"hongze/hongze_open_api/models/tables/open_api_user"
	"hongze/hongze_open_api/models/tables/qs_event"
	"hongze/hongze_open_api/models/tables/qs_event_user"
	"hongze/hongze_open_api/models/tables/wx_user"
	"hongze/hongze_open_api/models/tables/yb_activity"
	"hongze/hongze_open_api/utils"
	"time"
)

func init() {
	_ = orm.RegisterDataBase("default", "mysql", utils.MYSQL_URL)
	orm.SetMaxIdleConns("default", 50)
	orm.SetMaxOpenConns("default", 100)

	db, _ := orm.GetDB("default")
	db.SetConnMaxLifetime(10 * time.Minute)

	//rddp数据库
	_ = orm.RegisterDataBase("rddp", "mysql", utils.MYSQL_URL_RDDP)
	orm.SetMaxIdleConns("rddp", 50)
	orm.SetMaxOpenConns("rddp", 100)

	report_db, _ := orm.GetDB("rddp")
	report_db.SetConnMaxLifetime(10 * time.Minute)

	//注册对象
	orm.RegisterModel(
		new(open_api_user.OpenApiUser),                     //开放API用户表
		new(admin.Admin),                                   //系统用户表
		new(article.CygxArticleCeluePush),                  //策略平台推送过来更新的文章
		new(cygx_activity.CygxActivitySignup),              //易董报名审核
		new(cygx_activity.CygxYidongActivitySignupLog),     //易董报名记录日志
		new(cygx_activity.CygxYidongActivityExamineStatus), //易董审核记录日志
		new(cygx_activity.CygxMySchedule),                  //易董审核通过之后添加我的日程
	)
	orm.Debug = true
	orm.DebugLog = orm.NewLog(utils.Binlog)

	// 注册研报相关的表
	initYb()

	// 注册客户相关的表
	initCompany()
}

// initYb 研报活动相关
func initYb() {
	orm.RegisterModel(
		new(yb_activity.Activity),      //研报活动
		new(qs_event.QsEvent),          //全时与ficc研报活动的关系表
		new(qs_event_user.QsEventUser), //全时参会信息
		new(day_new.WeworkMsg),               // 企业微信会话存档表
		new(day_new.WeworkMsgLog),         // 企业微信群消息拉取日志表
		new(day_new.WeworkUser),              // 企业微信用户信息表
	)
}

// initCompany 注册客户相关的
func initCompany() {
	orm.RegisterModel(
		new(company.Company),                //company客户表
		new(company_product.CompanyProduct), //company_product客户产品表
		new(wx_user.WxUser),                 //微信用户表
	)
}
