123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- package model
- import (
- "fmt"
- "github.com/jmoiron/sqlx"
- "trading-go/common"
- "trading-go/response"
- "trading-go/util"
- )
- type Goods struct {
- Id uint `db:"id"`
- OwnerId uint `db:"owner_id"`
- Desc string `db:"desc"`
- Title string `db:"title"`
- Price uint `db:"price"`
- Place string `db:"place"`
- State int `db:"state"`
- Pic []string
- }
- type GoodsJson struct {
- OwnerId uint `json:"ownerId"`
- Desc string `json:"desc"`
- Title string `json:"title"`
- Price uint `json:"price"`
- Place string `json:"place"`
- Categories []uint `json:"categories"`
- State int `json:"state"`
- Pic []string `json:"pic"`
- }
- type GoodsSurfaceJson struct {
- Pic string `json:"pic"`
- OwnerId uint `json:"ownerId"`
- Title string `json:"title"`
- Price uint `json:"price"`
- State int `json:"state"`
- }
- type GoodsSurface struct {
- Id uint `db:"id"`
- Pic string `db:"pic"`
- OwnerId uint `db:"owner_id"`
- Title string `db:"title"`
- Price uint `db:"price"`
- State int `db:"state"`
- }
- func (g *GoodsJson) Change() Goods {
- return Goods{
- Pic: g.Pic,
- OwnerId: g.OwnerId,
- Desc: g.Desc,
- Title: g.Title,
- Price: g.Price,
- Place: g.Place,
- State: g.State,
- }
- }
- func (g *Goods) Create() (id uint, err error) {
- db := common.DB
- sqlStr := "INSERT INTO goods(owner_id, `desc`, title, price, place, state) VALUES (?, ?, ?, ?, ?, ?)"
- _, err = db.Exec(sqlStr, g.OwnerId, g.Desc, g.Title, g.Price, g.Place, g.State)
- if err != nil {
- return
- }
- sqlStr = "INSERT INTO goods_surfaces(pic, owner_id, title, price, state) VALUES (?, ?, ?, ?, ?)"
- exec, err := db.Exec(sqlStr, g.Pic[0], g.OwnerId, g.Title, g.Price, g.State)
- if err != nil {
- return
- }
- ids, err := exec.LastInsertId()
- id = uint(ids)
- if err != nil {
- return
- }
- return
- }
- func (g *Goods) Delete() error {
- db := common.DB
- sqlStr := "DELETE FROM goods WHERE id = ?"
- _, err := db.Exec(sqlStr, g.Id)
- if err != nil {
- return err
- }
- sqlStr = "DELETE FROM goods_surfaces WHERE id = ?"
- _, err = db.Exec(sqlStr, g.Id)
- if err != nil {
- return err
- }
- return nil
- }
- func (g *GoodsSurface) GetPaged(page, pageSize int) (rsp response.PageResponse, err error) {
- var goods []GoodsSurface
- db := common.DB
- sqlStr := "SELECT * FROM goods_surfaces LIMIT ?,?"
- rsp.Page = page
- rsp.PageSize = pageSize
- err = db.Select(&goods, sqlStr, (page-1)*pageSize, pageSize)
- rsp.Data = goods
- return
- }
- func (g *GoodsSurface) GetCnt() (cnt int, err error) {
- db := common.DB
- sqlStr := "SELECT COUNT(1) FROM goods_surfaces"
- err = db.Get(&cnt, sqlStr)
- return
- }
- func (g *Goods) Revise() error {
- db := common.DB
- sqlStr := "UPDATE goods SET pic = ?, owner_id = ?, `desc` = ?, title = ?, price = ?, place = ?, state = ? WHERE id = ?"
- _, err := db.Exec(sqlStr, g.Pic, g.OwnerId, g.Desc, g.Title, g.Price, g.Place, g.State, g.Id)
- if err != nil {
- return err
- }
- sqlStr = "UPDATE goods_surfaces SET pic = ?, owner_id = ?, title = ?, price = ?, state = ? WHERE id = ?"
- _, err = db.Exec(sqlStr, g.Pic, g.OwnerId, g.Title, g.Price, g.State, g.Id)
- return err
- }
- func (g *Goods) GetDetail() error {
- db := common.DB
- sqlStr := "SELECT * FROM goods WHERE id = ?"
- err := db.Get(g, sqlStr, g.Id)
- return err
- }
- func (g *Goods) GetUserGoods() (goods []Goods, err error) {
- db := common.DB
- fmt.Println(g.OwnerId)
- sqlStr := "SELECT * FROM goods WHERE owner_id = ?"
- err = db.Select(&goods, sqlStr, g.OwnerId)
- return
- }
- func (g *GoodsSurface) GetCategoryPaged(page, pageSize int, category uint) (rsp response.PageResponse, err error) {
- db := common.DB
- var goods []GoodsSurface
- var goodsIds []uint
- rsp.Page = page
- rsp.PageSize = pageSize
- sqlStr := "SELECT goods_id FROM category_of_goods WHERE category_id = ?"
- err = db.Select(&goodsIds, sqlStr, category)
- fmt.Println(goodsIds)
- if err != nil {
- return
- }
- sqlStr = "SELECT * FROM goods_surfaces WHERE id IN (?) LIMIT ?,?"
- query, args, err := sqlx.In(sqlStr, goodsIds, (page-1)*pageSize, pageSize)
- if err != nil {
- return
- }
- err = db.Select(&goods, query, args...)
- rsp.Data = goods
- return
- }
- func (g *GoodsSurface) GetCategoryCnt(category uint) (cnt int, err error) {
- db := common.DB
- sqlStr := "SELECT COUNT(1) FROM goods_surfaces WHERE id IN (SELECT goods_id FROM category_of_goods WHERE category_id = ?)"
- err = db.Get(&cnt, sqlStr, category)
- return
- }
- func (g *GoodsSurface) SearchPaged(key string, page, pageSize int) (rsp response.PageResponse, err error) {
- var goods []GoodsSurface
- db := common.DB
- ids := util.SearchId(key)
- if len(ids) == 0 {
- return response.PageResponse{}, util.NoSuchGoodsError
- }
- query, args, err := sqlx.In("SELECT * FROM goods_surfaces WHERE id IN (?) LIMIT ?,?", ids, (page-1)*pageSize, pageSize)
- if err != nil {
- return response.PageResponse{}, err
- }
- query = db.Rebind(query)
- err = db.Select(&goods, query, args...)
- if err != nil {
- return response.PageResponse{}, err
- }
- rsp = response.PageResponse{
- PageSize: pageSize,
- Page: page,
- HNext: len(ids) > len(goods),
- Data: goods,
- }
- return
- }
- func (g *GoodsSurface) GetRecommend(id uint, size int) (goods []GoodsSurface, err error) {
- ids, err := util.GetRecommend(id, size)
- db := common.DB
- if err != nil {
- return nil, err
- }
- if len(ids) == 0 {
- return nil, util.NoRecommendError
- }
- query, args, err := sqlx.In("SELECT * FROM goods_surfaces WHERE id IN (?)", ids)
- if err != nil {
- return nil, err
- }
- query = db.Rebind(query)
- err = db.Select(&goods, query, args...)
- return
- }
- func (g *GoodsSurface) GetGoodsList(id []uint) (goods []GoodsSurface, err error) {
- db := common.DB
- sqlStr := "SELECT * FROM goods_surfaces WHERE id IN (?)"
- query, args, err := sqlx.In(sqlStr, id)
- if err != nil {
- return nil, err
- }
- err = db.Select(&goods, query, args...)
- return
- }
|