1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package shop
- import (
- "errors"
- "zhiyuan/models"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/redis"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/form"
- )
- var Shop models.Shop
- const cacheKey = "shop"
- func GetList(where map[string]interface{}, fields []string, retVal interface{}) ([]*models.Shop, error) {
- return Shop.GetMulti(where, fields, retVal)
- }
- func GetListByCache() ([]*models.Shop, error) {
- res, err := redis.Get(cacheKey)
- var shopList []*models.Shop
- if err != nil {
- shopList, err = GetList(nil, nil, nil)
- if err != nil {
- return nil, err
- }
- redis.Set(cacheKey, shopList, 3600)
- } else {
- utils.JsonDecode(res).To(&shopList)
- }
- return shopList, nil
- }
- func Add(form form.ShopAdd) (int64, error) {
- if CheckDuplicate(form.ShopName) {
- return 0, errors.New("门店已存在")
- }
- shopMap := map[string]interface{}{
- "shop_name": form.ShopName,
- "collect_info": form.CollectInfo,
- }
- shopID, err := db.InsertOne(Shop.TableName(), shopMap)
- if err != nil {
- return 0, nil
- }
- redis.Del(cacheKey)
- return shopID, nil
- }
- func Edit(form form.ShopAdd, id int) (int64, error) {
- shopInfo, _ := GetOne(map[string]interface{}{"id": id}, nil, nil)
- if shopInfo.ShopName != form.ShopName && CheckDuplicate(form.ShopName) {
- return 0, errors.New("门店已存在")
- }
- shopMap := map[string]interface{}{
- "shop_name": form.ShopName,
- "collect_info": form.CollectInfo,
- }
- redis.Del(cacheKey)
- return db.Update(Shop.TableName(), map[string]interface{}{"id": id}, shopMap)
- }
- func GetOne(where map[string]interface{}, fields []string, dest interface{}) (*models.Shop, error) {
- return Shop.GetOne(where, fields, dest)
- }
- func CheckDuplicate(name string) bool {
- shopInfo, err := GetOne(map[string]interface{}{"shop_name": name}, nil, nil)
- return shopInfo != nil && err == nil
- }
|