package db

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

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

func (masterListCfgDb *MasterListCfgDb) Set() { // set方法
	masterListCfgDb.Db = ZhimengDb
}

func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetAll() (*[]model.MasterListCfg, error) {
	var cfgList []model.MasterListCfg
	if err := masterListCfgDb.Db.Cols("k,v,memo").Find(&cfgList); err != nil {
		return nil, logx.Error(err)
	}
	return &cfgList, nil
}
func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetCron() (*[]model.MasterListCfg, error) {
	var cfgList []model.MasterListCfg
	if err := masterListCfgDb.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 (masterListCfgDb *MasterListCfgDb) MasterListCfgGetOneNoDataNoErr(key string) (*model.MasterListCfg, error) {
	var cfgList model.MasterListCfg
	has, err := masterListCfgDb.Db.Where("`k`=?", key).Get(&cfgList)
	if err != nil {
		return nil, logx.Error(err)
	}
	if !has {
		return nil, nil
	}
	return &cfgList, nil
}

func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetOne(uid, key string) (*model.MasterListCfg, error) {
	var cfgList model.MasterListCfg
	has, err := masterListCfgDb.Db.Where("`k`=? and uid=?", key, uid).Get(&cfgList)
	if err != nil {
		return nil, logx.Error(err)
	}
	if has == false {
		cfgList = model.MasterListCfg{Uid: uid, K: key}
		masterListCfgDb.Db.InsertOne(&cfgList)
	}
	return &cfgList, nil
}
func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetOneData(uid, key string) string {
	var cfgList model.MasterListCfg
	has, err := masterListCfgDb.Db.Where("`k`=? and uid=?", key, uid).Get(&cfgList)
	if err != nil {
		return ""
	}
	if has == false {
		cfgList = model.MasterListCfg{Uid: uid, K: key}
		masterListCfgDb.Db.InsertOne(&cfgList)
	}
	return cfgList.V
}

func (masterListCfgDb *MasterListCfgDb) MasterListCfgInsert(uid, key, val, memo string) bool {
	cfg := model.MasterListCfg{Uid: uid, K: key, V: val, Memo: memo}
	_, err := masterListCfgDb.Db.InsertOne(&cfg)
	if err != nil {
		logx.Error(err)
		return false
	}
	return true
}

func (masterListCfgDb *MasterListCfgDb) MasterListCfgUpdate(uid, key, val string) bool {
	masterListCfgDb.MasterListCfgGetOneData(uid, key)
	cfg := model.MasterListCfg{K: key, V: val}
	_, err := masterListCfgDb.Db.Where("`k`=? and uid=?", key, uid).Cols("v").Update(&cfg)
	if err != nil {
		logx.Error(err)
		return false
	}
	masterListCfgDb.MasterListCfgDel(key)
	return true
}

func (masterListCfgDb *MasterListCfgDb) MasterListCfgDel(HKey string) error {
	cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1])
	_, err := cache.HDel(cacheKey, HKey)
	if err != nil {
		return err
	}
	return nil
}

func MasterListCfgGetOneData(uid, key string) string {
	var cfgList model.MasterListCfg
	has, err := ZhimengDb.Where("`k`=? and uid=?", key, uid).Get(&cfgList)
	if err != nil {
		return ""
	}
	if has == false {
		cfgList = model.MasterListCfg{Uid: uid, K: key}
		ZhimengDb.InsertOne(&cfgList)
	}
	return cfgList.V
}
func MasterListCfgGetKeyAll(key string) *[]model.MasterListCfg {
	var cfgList []model.MasterListCfg
	err := ZhimengDb.Where("`k`=? ", key).Find(&cfgList)
	if err != nil {
		return nil
	}
	return &cfgList
}
func MasterListCfgSave(uid, key, val string) {
	var cfgList model.MasterListCfg
	has, err := ZhimengDb.Where("`k`=? and uid=?", key, uid).Get(&cfgList)
	if err != nil || has == false {
		return
	}
	cfgList.V = val
	ZhimengDb.Where("`k`=? and uid=?", key, uid).Update(&cfgList)
	return
}