1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package brand
- import (
- "errors"
- "zhiyuan/models"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/services/form"
- )
- var MatBrand models.MatBrand
- func Count(where map[string]interface{}) (int64, error) {
- return db.Count(MatBrand.TableName(), where)
- }
- func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.MatBrand, error) {
- if page.PageNum > 0 && page.PageSize > 0 {
- where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
- }
- return MatBrand.GetMulti(where, fields, retVal)
- }
- func GetOne(where map[string]interface{}, fields []string, dest interface{}) (*models.MatBrand, error) {
- return MatBrand.GetOne(where, fields, dest)
- }
- func Add(form form.MaterialBrandAdd) (int64, error) {
- if CheckDuplicate(form.BrandName) {
- return 0, errors.New("品牌已存在")
- }
- brandMap := map[string]interface{}{
- "brand_name": form.BrandName,
- }
- if form.Deleted > 0 {
- brandMap["deleted_at"] = form.Deleted
- }
- pkgID, err := db.InsertOne(MatBrand.TableName(), brandMap)
- if err != nil {
- return 0, nil
- }
- return pkgID, nil
- }
- func Edit(form form.MaterialBrandAdd, id int) (int64, error) {
- brandInfo, _ := GetOne(map[string]interface{}{"id": id}, nil, nil)
- if brandInfo.BrandName != form.BrandName && CheckDuplicate(form.BrandName) {
- return 0, errors.New("品牌已存在")
- }
- brandMap := map[string]interface{}{
- "brand_name": form.BrandName,
- }
- return db.Update(MatBrand.TableName(), map[string]interface{}{"id": id}, brandMap)
- }
- func CheckDuplicate(name string) bool {
- brandInfo, err := GetOne(map[string]interface{}{"brand_name": name}, nil, nil)
- return brandInfo != nil && err == nil
- }
|