package db

import (
	"applet/app/db/model"
	"applet/app/md"
	"applet/app/utils/cache"
	"applet/app/utils/logx"
	"fmt"
	"xorm.io/xorm"
)

type SysCfgDb struct {
	Db *xorm.Engine `json:"db"`
}

func (sysCfgDb *SysCfgDb) Set() { // set方法
	sysCfgDb.Db = ZhimengDb
}

func (sysCfgDb *SysCfgDb) SysCfgGetAll() (*[]model.SysCfg, error) {
	var cfgList []model.SysCfg
	if err := sysCfgDb.Db.Cols("k,v,memo").Find(&cfgList); err != nil {
		return nil, logx.Error(err)
	}
	return &cfgList, nil
}
func (sysCfgDb *SysCfgDb) SysCfgGetCron() (*[]model.SysCfg, error) {
	var cfgList []model.SysCfg
	if err := sysCfgDb.Db.Where("`k` LIKE 'zhimeng_cron\\_%' AND v != ''").Cols("k,v,memo").Find(&cfgList); err != nil {
		return nil, logx.Error(err)
	}
	return &cfgList, nil
}
func (sysCfgDb *SysCfgDb) SysCfgGetOneNoDataNoErr(key string) (*model.SysCfg, error) {
	var cfgList model.SysCfg
	has, err := sysCfgDb.Db.Where("`k`=?", key).Get(&cfgList)
	if err != nil {
		return nil, logx.Error(err)
	}
	if !has {
		return nil, nil
	}
	return &cfgList, nil
}

func (sysCfgDb *SysCfgDb) SysCfgGetOne(key string) (*model.SysCfg, error) {
	var cfgList model.SysCfg
	if has, err := sysCfgDb.Db.Where("`k`=?", key).Get(&cfgList); err != nil || has == false {
		return nil, logx.Error(err)
	}
	return &cfgList, nil
}
func (sysCfgDb *SysCfgDb) SysCfgGetOneData(key string) string {
	var cfgList model.SysCfg
	if has, err := sysCfgDb.Db.Where("`k`=?", key).Get(&cfgList); err != nil || has == false {
		return ""
	}
	return cfgList.V
}

func (sysCfgDb *SysCfgDb) SysCfgInsert(key, val, memo string) bool {
	cfg := model.SysCfg{K: key, V: val, Memo: memo}
	_, err := sysCfgDb.Db.InsertOne(&cfg)
	if err != nil {
		logx.Error(err)
		return false
	}
	return true
}

func (sysCfgDb *SysCfgDb) SysCfgUpdate(key, val string) bool {
	cfg := model.SysCfg{K: key, V: val}
	_, err := Db.Where("`key`=?", key).Cols("val").Update(&cfg)
	if err != nil {
		logx.Error(err)
		return false
	}
	sysCfgDb.SysCfgDel(key)
	return true
}

func (sysCfgDb *SysCfgDb) SysCfgGetWithDb(HKey string) string {
	cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1])
	get, err := cache.HGetString(cacheKey, HKey)
	if err != nil || get == "" {
		cfg, err := sysCfgDb.SysCfgGetOne(HKey)
		if err != nil || cfg == nil {
			_ = logx.Error(err)
			return ""
		}

		// key是否存在
		cacheKeyExist := false
		if cache.Exists(cacheKey) {
			cacheKeyExist = true
		}

		// 设置缓存
		_, err = cache.HSet(cacheKey, HKey, cfg.V)
		if err != nil {
			_ = logx.Error(err)
			return ""
		}
		if !cacheKeyExist { // 如果是首次设置 设置过期时间
			_, err := cache.Expire(cacheKey, md.CfgCacheTime)
			if err != nil {
				_ = logx.Error(err)
				return ""
			}
		}
		return cfg.V
	}
	return get
}

func (sysCfgDb *SysCfgDb) SysCfgDel(HKey string) error {
	cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1])
	_, err := cache.HDel(cacheKey, HKey)
	if err != nil {
		return err
	}
	return nil
}
func ZhimengSysCfgGetOneData(key string) string {
	var cfgList model.SysCfg
	if has, err := ZhimengDb.Where("`key`=?", key).Get(&cfgList); err != nil || has == false {
		return ""
	}
	return cfgList.V
}
func (sysCfgDb *SysCfgDb) SysCfgFindWithDb(keys ...string) map[string]string {
	res := map[string]string{}
	for _, v := range keys {
		val := sysCfgDb.SysCfgGetWithDb(v)
		res[v] = val
	}
	return res
}