package services import ( "errors" "github.com/ahmetb/go-linq/v3" "qteam/app/http/domains" "qteam/app/models/productsmodel" "xorm.io/builder" ) func ProductCreate(do domains.Product) (err error) { m := productsmodel.Products{} m.FromDomain(do) count, err := productsmodel.GetInstance().Insert(m) if err != nil { return } if count == 0 { err = errors.New("product create fail") } return } func ProductUpdate(do domains.Product) (err error) { m := productsmodel.Products{} m.FromDomain(do) count, err := productsmodel.GetInstance().Update(m.Id, m) if err != nil { return } if count == 0 { err = errors.New("product update fail") } return } func ProductDelete(do domains.Product) (err error) { m := productsmodel.Products{} m.FromDomain(do) count, err := productsmodel.GetInstance().Delete(m.Id, m) if err != nil { return } if count == 0 { err = errors.New("product create fail") } return } func ProductQuery(filter domains.ProductFilter) (p []domains.Product, count int64, err error) { repo := productsmodel.GetInstance() products := []productsmodel.Products{} conn := builder.NewCond() if len(filter.ProductName) > 0 { conn = conn.And(builder.Like{"ProductName", filter.ProductName}) } session := repo.GetDb().Where(conn) if filter.Page != 0 && filter.PageSize != 0 { session = session.Limit(filter.PageSize, (filter.Page-1)*filter.PageSize) } count, err = session.FindAndCount(&products) if err != nil { return } linq.From(products).SelectT(func(in productsmodel.Products) (d domains.Product) { d = in.ToDomain() return d }).ToSlice(&p) return }