|
- package db
-
- import (
- "applet/app/e"
- "applet/app/utils"
- "applet/app/utils/logx"
- "applet/supply/db/model"
- "errors"
- "fmt"
- "reflect"
- "xorm.io/xorm"
- )
-
- // MerchantFindByParams 通过传入的参数查询数据(多条)
- func MerchantFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.Merchant, error) {
- var m []model.Merchant
- if params["key"] == nil {
- //查询全部数据
- err := Db.Find(&m)
- if err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- } else {
- if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
- //指定In查询
- if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- } else {
- var query = fmt.Sprintf("%s =?", params["key"])
- err := Db.Where(query, params["value"]).Find(&m)
- if err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- }
- }
-
- // MerchantProfileFindByParams 通过传入的参数查询数据(多条)
- func MerchantProfileFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.MerchantProfile, error) {
- var m []model.MerchantProfile
- if params["key"] == nil {
- //查询全部数据
- err := Db.Find(&m)
- if err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- } else {
- if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
- //指定In查询
- if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- } else {
- var query = fmt.Sprintf("%s =?", params["key"])
- err := Db.Where(query, params["value"]).Find(&m)
- if err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- }
- }
-
- func GetMerchantById(eg *xorm.Engine, id int) (*model.Merchant, error) {
- var m model.Merchant
- get, err := eg.ID(id).Get(&m)
- if err != nil {
- return nil, err
- }
- if !get {
- return nil, errors.New("账号不存在")
- }
- return &m, nil
- }
-
- func GetSystemMerchant(eg *xorm.Engine) (bool, *model.Merchant, error) {
- var m model.Merchant
- get, err := eg.Where("is_system = 1").Get(&m)
- if err != nil {
- return get, nil, err
- }
- return get, &m, nil
- }
-
- func GetMerchantByIdWithSession(session *xorm.Session, id int) (*model.Merchant, error) {
- var m model.Merchant
- get, err := session.ID(id).Get(&m)
- if err != nil {
- return nil, err
- }
- if !get {
- return nil, errors.New("账号不存在")
- }
- return &m, nil
- }
-
- func GetMerchantByPhone(eg *xorm.Engine, phone string) (*model.Merchant, error) {
- var m model.Merchant
- get, err := eg.Where("phone=?", phone).Get(&m)
- if err != nil {
- return nil, err
- }
- if !get {
- return nil, e.NewErr(e.ERR_ITEM_NOT_FOUND, "手机号不存在")
- }
- return &m, nil
- }
-
- func CheckMerchantPhoneExists(eg *xorm.Engine, phone string) (bool, error) {
- exist, err := eg.Where("phone=?", phone).Exist(&model.Merchant{})
- if err != nil {
- return false, err
- }
-
- return exist, nil
- }
-
- func GetMerchantApplyById(eg *xorm.Engine, id interface{}) (*model.MerchantProfile, error) {
- var m model.MerchantProfile
- get, err := eg.ID(id).Get(&m)
- if err != nil {
- return nil, err
- }
- if !get {
- return nil, e.NewErr(e.ERR_ITEM_NOT_FOUND, "该申请不存在")
- }
-
- return &m, nil
- }
|