package models

import (
	"github.com/beego/beego/v2/client/orm"
	_ "github.com/go-sql-driver/mysql"
	"hongze/hongze_mobile_admin/models/roadshow"
	"hongze/hongze_mobile_admin/models/tables/admin"
	"hongze/hongze_mobile_admin/models/tables/admin_record"
	"hongze/hongze_mobile_admin/models/tables/approval_flow"
	"hongze/hongze_mobile_admin/models/tables/approval_flow_node"
	"hongze/hongze_mobile_admin/models/tables/community_question"
	"hongze/hongze_mobile_admin/models/tables/company"
	"hongze/hongze_mobile_admin/models/tables/company_approval"
	"hongze/hongze_mobile_admin/models/tables/company_approval_message"
	"hongze/hongze_mobile_admin/models/tables/company_contract"
	"hongze/hongze_mobile_admin/models/tables/company_delay_permission"
	"hongze/hongze_mobile_admin/models/tables/company_log"
	"hongze/hongze_mobile_admin/models/tables/company_operation_record"
	"hongze/hongze_mobile_admin/models/tables/company_permission_log"
	"hongze/hongze_mobile_admin/models/tables/company_product"
	"hongze/hongze_mobile_admin/models/tables/company_product_log"
	"hongze/hongze_mobile_admin/models/tables/company_product_update_log"
	"hongze/hongze_mobile_admin/models/tables/company_report_permission"
	"hongze/hongze_mobile_admin/models/tables/contract"
	"hongze/hongze_mobile_admin/models/tables/contract_approval"
	"hongze/hongze_mobile_admin/models/tables/contract_approval_record"
	"hongze/hongze_mobile_admin/models/tables/contract_operation_record"
	"hongze/hongze_mobile_admin/models/tables/contract_service_detail"
	"hongze/hongze_mobile_admin/models/tables/contract_service_template"
	"hongze/hongze_mobile_admin/models/tables/contract_template"
	"hongze/hongze_mobile_admin/models/tables/h5_admin_session"
	"hongze/hongze_mobile_admin/models/tables/report"
	"hongze/hongze_mobile_admin/models/tables/research_variety_tag_relation"
	"hongze/hongze_mobile_admin/models/tables/resource"
	"hongze/hongze_mobile_admin/models/tables/seal"
	"hongze/hongze_mobile_admin/models/tables/sys_role"
	"hongze/hongze_mobile_admin/models/tables/sys_role_admin"
	"hongze/hongze_mobile_admin/models/tables/user_template_record"
	"hongze/hongze_mobile_admin/models/tables/variety_classify"
	"hongze/hongze_mobile_admin/models/tables/variety_tag"
	"hongze/hongze_mobile_admin/models/tables/wx_token"
	"hongze/hongze_mobile_admin/models/tables/wx_user"
	"hongze/hongze_mobile_admin/models/tables/yb_price_driven_tag"
	"hongze/hongze_mobile_admin/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.Debug = true
	orm.DebugLog = orm.NewLog(utils.Binlog)

	//注册对象
	orm.RegisterModel(
		new(admin.Admin),
		new(admin_record.AdminRecord),
		new(sys_role.SysRole),
		new(sys_role_admin.SysRoleAdmin),
		new(approval_flow.ApprovalFlow),
		new(approval_flow_node.ApprovalFlowNode),
		new(company.Company),
		new(company_approval.CompanyApproval),
		new(company_approval_message.CompanyApprovalMessage),
		new(company_contract.CompanyContract),
		new(company_delay_permission.CompanyDelayPermission),
		new(company_log.CompanyLog),
		new(company_operation_record.CompanyOperationRecord),
		new(company_permission_log.CompanyPermissionLog),
		new(company_product.CompanyProduct),
		new(company_product_log.CompanyProductLog),
		new(company_report_permission.CompanyReportPermission),
		new(contract.Contract),
		new(contract_approval.ContractApproval),
		new(contract_approval_record.ContractApprovalRecord),
		new(contract_operation_record.ContractOperationRecord),
		new(contract_service_detail.ContractServiceDetail),
		new(contract_service_template.ContractServiceTemplate),
		new(contract_template.ContractTemplate),
		new(h5_admin_session.H5AdminSession),
		new(report.Report),
		new(resource.Resource),
		new(wx_token.WxToken),
		new(wx_user.WxUser),
		new(seal.Seal),                //用印表
		new(seal.SealOperationRecord), // 用印操作记录表
		new(roadshow.RsCalendar),
		new(roadshow.RsCalendarResearcher),
		new(roadshow.RsMatters),
		new(roadshow.RsCalendarRelation),
		new(company_product_update_log.CompanyProductUpdateLog), //客户产品状态变更记录表
		new(user_template_record.UserTemplateRecord),            //模版消息记录表
	)

	// 社区问答相关
	initCommunity()

	// 品种标签相关
	initVariety()

	// 价格驱动相关
	initPriceDriven()
}

// initCommunity 社区问答相关
func initCommunity() {
	orm.RegisterModel(
		new(community_question.CommunityQuestion), //社区问答
	)
}

// initVariety 品种标签相关
func initVariety() {
	orm.RegisterModel(
		new(variety_classify.VarietyClassify),                         //品种标签分类
		new(variety_tag.VarietyTag),                                   //品种标签
		new(research_variety_tag_relation.ResearchVarietyTagRelation), //品种标签关系
	)
}

// initPriceDriven 价格驱动相关
func initPriceDriven() {
	orm.RegisterModel(
		new(yb_price_driven_tag.PriceDrivenTag), //价格驱动标签
	)
}

func InitDb() {}