package model import ( "fmt" "github.com/jmoiron/sqlx" "trading-go/common" "trading-go/response" "trading-go/util" ) type Goods struct { Id uint `db:"id"` PicId uint `db:"pic_id"` OwnerId uint `db:"owner_id"` Desc string `db:"desc"` Title string `db:"title"` Price uint `db:"price"` Integrity int `db:"integrity"` Place string `db:"place"` State int `db:"state"` } type GoodsJson struct { PicId uint `json:"pic_id"` OwnerId uint `json:"ownerId"` Desc string `json:"desc"` Title string `json:"title"` Price uint `json:"price"` Integrity int `json:"integrity"` Place string `json:"place"` Categories []uint `json:"categories"` State int `json:"state"` } type GoodsSurfaceJson struct { PicId uint `json:"picId"` OwnerId uint `json:"ownerId"` Title string `json:"title"` Price uint `json:"price"` Integrity int `json:"integrity"` State int `json:"state"` } type GoodsSurface struct { Id uint `db:"id"` PicId uint `db:"pic_id"` OwnerId uint `db:"owner_id"` Title string `db:"title"` Price uint `db:"price"` Integrity int `db:"integrity"` State int `db:"state"` } func (g *GoodsJson) Change() Goods { return Goods{ PicId: g.PicId, OwnerId: g.OwnerId, Desc: g.Desc, Title: g.Title, Price: g.Price, Integrity: g.Integrity, Place: g.Place, State: g.State, } } func (g *Goods) Create() (id uint, err error) { db := common.DB sqlStr := "INSERT INTO goods(pic_id, owner_id, `desc`, title, price, integrity, place, state) VALUES (?, ?, ?, ?, ?, ?, ?, ?)" _, err = db.Exec(sqlStr, g.PicId, g.OwnerId, g.Desc, g.Title, g.Price, g.Integrity, g.Place, g.State) if err != nil { return } sqlStr = "INSERT INTO goods_surfaces(pic_id, owner_id, title, price, integrity, state) VALUES (?, ?, ?, ?, ?, ?)" exec, err := db.Exec(sqlStr, g.PicId, g.OwnerId, g.Title, g.Price, g.Integrity, 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_id = ?, owner_id = ?, `desc` = ?, title = ?, price = ?, integrity = ?, place = ?, state = ? WHERE id = ?" _, err := db.Exec(sqlStr, g.PicId, g.OwnerId, g.Desc, g.Title, g.Price, g.Integrity, g.Place, g.State, g.Id) if err != nil { return err } sqlStr = "UPDATE goods_surfaces SET pic_id = ?, owner_id = ?, title = ?, price = ?, integrity = ?, state = ? WHERE id = ?" _, err = db.Exec(sqlStr, g.PicId, g.OwnerId, g.Title, g.Price, g.Integrity, 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) 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 }