package db

import (
	"applet/app/db/model"
	"applet/app/utils"
	"xorm.io/xorm"
)

func GetStoreGoods(eg *xorm.Engine, req map[string]string) (*[]model.CommunityTeamGoods, int64) {
	var data []model.CommunityTeamGoods
	limit := utils.StrToInt(req["size"])
	start := (utils.StrToInt(req["p"]) - 1) * limit
	sess := eg.Where("uid=?", req["uid"]).OrderBy("sort desc,id desc").Limit(limit, start)
	if req["title"] != "" {
		sess.And("title like ?", "%"+req["title"]+"%")
	}
	if req["cid"] != "" {
		sess.And("cid=?", req["cid"])
	}
	if req["store_type"] != "" {
		sess.And("store_type=?", req["store_type"])
	}
	if req["is_up"] != "" {
		sess.And("state=?", 0)
	}
	if req["start_time"] != "" {
		sess.And("create_at>=?", req["start_time"])
	}
	if req["end_time"] != "" {
		sess.And("create_at<=?", req["end_time"])
	}
	count, err := sess.FindAndCount(&data)
	if err != nil {
		return nil, count
	}
	return &data, count
}

func GetGoodsById(eg *xorm.Engine, id string) *model.CommunityTeamGoods {
	var data model.CommunityTeamGoods
	get, err := eg.Where("id=?", id).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
func GetGoodsByIdSess(sess *xorm.Session, id string) *model.CommunityTeamGoods {
	var data model.CommunityTeamGoods
	get, err := sess.Where("id=?", id).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
