package app import ( "errors" "github.com/go-chi/chi/v5" "gorm.io/gorm" "net/http" "strconv" "strings" ) func userInfoFromRequest(r *Request) (*User, error) { var user User if name, ok := r.Get("name"); ok && len(name) > 0 { user.Name = name } else { return nil, NewError(1, "缺少用户名称") } if phoneNumber, ok := r.Get("phone_number"); ok && len(phoneNumber) > 0 { if len(phoneNumber) != 11 { return nil, NewError(2, "手机号码格式错误") } user.PhoneNumber = phoneNumber } else { return nil, NewError(2, "缺少手机号码") } if password, ok := r.Get("password"); ok && len(password) > 0 { if len(password) < 6 { return nil, NewError(2, "密码太短") } user.Password = password } else { return nil, NewError(2, "缺少密码") } return &user, nil } // CreateUser 创建用户 func CreateUser(w *ResponseWriter, r *Request) { user, err := userInfoFromRequest(r) if err != nil { w.Error(err) return } var count int64 if err = DB.Model(&User{}).Where("phone_number = ?", user.PhoneNumber).Count(&count).Error; err != nil { w.Error(err) return } if count > 0 { w.Error(NewError(2, "手机号码已经被使用了")) return } if err = DB.Create(&user).Error; err != nil { w.Error(err) } else { w.Ok(user, "创建用户成功") } } // UpdateUser 更新用户 func UpdateUser(w *ResponseWriter, r *Request) { id := chi.URLParam(r.Request, "id") if len(id) == 0 { w.Error(NewError(1, "缺少用户ID")) } user, err := userInfoFromRequest(r) if err != nil { w.Error(err) return } } // DeleteUser 删除用户 func DeleteUser(w *ResponseWriter, r *Request) { } // ListUser 用户列表 func ListUser(w *ResponseWriter, r *Request) { } // Login 用户登录 func Login(w *ResponseWriter, r *Request) { } // CreateGoods 创建商品 func CreateGoods(w *ResponseWriter, r *Request) { name := r.Value("name") price := r.Float32("price", 0) if len(name) == 0 { w.Fail(http.StatusBadRequest, 1, "商品名称错误") return } if price <= 0 { w.Fail(http.StatusBadRequest, 2, "商品价格错误") return } var goods Goods err := DB.First(&goods, "name = ?", name).Error if errors.Is(err, gorm.ErrRecordNotFound) { goods = Goods{ Name: name, Price: price, } err = DB.Create(&goods).Error if err != nil { w.Fail(http.StatusBadRequest, 3, "创建商品失败") } else { w.Ok(goods) } } else if err != nil { LogError(err) w.Fail(http.StatusBadRequest, 4, "商品价格错误") } else { w.Fail(http.StatusBadRequest, 5, "商品已经存在") } } // UpdateGoods 更新产品信息 func UpdateGoods(w *ResponseWriter, r *Request) { name := r.Value("name") price := r.Float32("price", 0) id := uint(r.Uint64("id", 0)) if len(name) == 0 { w.Fail(http.StatusBadRequest, 1, "商品名称错误") return } if price <= 0 { w.Fail(http.StatusBadRequest, 2, "商品价格错误") return } var goods Goods err := DB.First(&goods, "id = ?", id).Error if errors.Is(err, gorm.ErrRecordNotFound) { w.Fail(http.StatusBadRequest, 2, "商品不存在") } else if err != nil { LogError(err) w.Fail(http.StatusBadRequest, 3, err.Error()) } else { // 商品名称不能重复 err = DB.Where("id <> ?", id).First(&Goods{}, "name = ?", name).Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { LogError(err) w.Fail(http.StatusBadRequest, 4, err.Error()) return } if goods.Name != name || goods.Price != price { goods.Name = name goods.Price = price err = DB.Save(&goods).Error if err != nil { LogError(err) w.Fail(http.StatusBadRequest, 5, err.Error()) return } } w.Ok(goods, "修改成功") } } // GetGoodsList 查询商品列表 func GetGoodsList(w *ResponseWriter, r *Request) { search := func(db *gorm.DB) *gorm.DB { return db. Model(&Goods{}). Scopes(TimeRange(r, "created_at")). Scopes(Paginate(r)). Scopes(Search(r, "name", "name LIKE ?")) } var goodsList []Goods var total int64 var err error if err = DB.Scopes(search).Count(&total).Error; err == nil { err = DB.Scopes(search).Find(&goodsList).Error } if err != nil { w.Fail(http.StatusInternalServerError, 1, err.Error()) } else { w.Ok(map[string]any{ "list": goodsList, "total": total, }) } } func GetGoodsPrices(w *ResponseWriter, r *Request) { id := uint(r.Uint64("id", 0)) var goods Goods if err := DB.Scopes(func(db *gorm.DB) *gorm.DB { var queries []string var args []any if val, ok := r.Get("start_time"); ok { queries = append(queries, "created_at >= ?") args = append(args, val) } if val, ok := r.Get("end_time"); ok { queries = append(queries, "created_at <= ?") args = append(args, val) } if len(queries) == 0 { return db.Preload("Prices") } args = append([]any{strings.Join(queries, " AND ")}, args...) return db.Preload("Prices", args...) }).First(&goods, "id = ?", id).Error; err != nil { w.Fail(http.StatusInternalServerError, 1, err.Error()) } else { w.Ok(goods) } }