package model

import (
	"database/sql"
	"fmt"

	"git.jiaxianghudong.com/go/logs"
	"git.jiaxianghudong.com/go/xlog"
	"git.jiaxianghudong.com/webs/pkg/dbx"
)

//AppsCache app的配置
var AppsCache = make(map[string]string, 0)

//Apps app所有信息
type Apps struct {
	Appid     int    `json:"app_id" sql:"app_id"`         //AppId
	Channelid int    `json:"channel_id" sql:"channel_id"` //ChannelID
	WxID      string `json:"wechat_syncid" sql:"wechat_syncid"`
}

//InitAppsCache 获取配置
func InitAppsCache() map[string]string {

	rows, err := dbx.MySQLExt.Query("select app_id, channel_id, wechat_syncid from `main`.`apps` where channel_id = 818")
	defer rows.Close()
	if err != nil && err != sql.ErrNoRows {
		logs.Errorf("InitAppsCache err:%v", err)
		return AppsCache
	}

	g := Apps{}
	for rows.Next() {
		err = rows.Scan(&g.Appid, &g.Channelid, &g.WxID)
		if err != nil {
			logs.Errorf("InitAppsCache err:%v", err)
			return AppsCache
		}

		AppsCache[fmt.Sprintf("%d_%d", g.Appid, g.Channelid)] = g.WxID
	}

	xlog.Infof("初始化配置 %+v", AppsCache)
	return AppsCache
}
