|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 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
- }
|