package appApi import ( "MeterService/core/config" "MeterService/core/db" "strconv" ) type DbAppApi struct { Db *db.MyDB } func (m *DbAppApi) GetList() ([]interface{}, error) { array := make([]interface{}, 0) arr, ok := m.Db.Query("SELECT api.AppId, api.Type, api.Url,app.Host FROM AppInfo AS app LEFT JOIN AppApiInfo AS api ON app.AppId = api.AppId WHERE app.IsDelete = 0 AND api.AppId IS NOT NULL") if ok && arr != nil && len(arr) > 0 { for _, v := range arr { appId, _ := strconv.Atoi(v["AppId"]) value := &AppApi{ AppId: appId, Type: v["Type"], Host: v["Host"], Url: v["Url"], } array = append(array, value) } } return array, nil } func (m *DbAppApi) AddOrUpdate(v interface{}) error { d := v.(*App) if d.AppId == 0 { if _, err := m.Db.Exec("INSERT INTO AppInfo(appSecret,appName, host,isDelete) VALUES(?,?,?,?)", d.AppSecret, d.AppName, d.Host, 0); err != nil { return err } } else { if _, err := m.Db.Exec("UPDATE AppInfo SET appSecret=?,appName=?,host=?,isDelete=? WHERE AppId=?", d.AppSecret, d.AppName, d.Host, d.GetDelete(), d.AppId); err != nil { return err } if _, err := m.Db.Exec("DELETE FROM AppApiInfo WHERE appId=?", d.AppId); err != nil { return err } } if len(d.Apis) > 0 { for _, v := range d.Apis { _, err := m.Db.Exec("INSERT INTO AppApiInfo(appId, url, type) VALUES(?,?,?)", d.AppId, v.Url, v.Type) if err != nil { return err } } } return nil } func (m *DbAppApi) Delete(v interface{}) error { d := v.(*App) _, err := m.Db.Exec("UPDATE AppInfo SET isDelete=? WHERE AppId=?", 1, d.AppId) return err } func (m *DbAppApi) CheckSecret(d *App) bool { if config.C.Vber.Mode == "debug" && d.AppSecret == "" { return true } appId := d.AppId if arr, ok := m.Db.Query("SELECT AppId, AppSecret FROM AppInfo WHERE appId=? AND IsDelete = 0", appId); !ok { return false } else { if len(arr) != 1 { return false } for _, v := range arr { if v["AppSecret"] == d.AppSecret { return true } } return false } } func NewAppApiDb() *DbAppApi { return &DbAppApi{ Db: db.GetDb(), } }